-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Selective ignore using comments (feature request) #384
Comments
This is probably best achieved by parsing the AST, �à la #200, although not strictly blocked by that feature. We welcome pull requests. |
I would love to see that too. |
+1 |
The alternative would be that you could omit a file from beautification using a comment at the top, is this currently possible or would it be part of this feature request? |
That would be part of this feature. |
@bitwiseman thanks for the clarification 👍 |
Why not: ? |
I've implemented a first version of this feature, which can be tested here: Any of the following comments will work:
The feature is implemented rather naively, but it seems to work quite well (although I have not tested extensively):
The comments themselves get beautified, and if that results in them being indented, then all the lines between the start and end comments get indented as well. |
Wow! Nice start. For clarity and simplicty, let's pick one style and stick to it.
Do you consider the indenting a feature or a bug? Can I suggest a simpler solution? So, if you want to you could add code right here: https://github.com/strille/js-beautify/blob/master/js/lib/beautify.js#L1756 That could would check the contents of the comment (which you already do), and if it is an ignore section, just naively add raw lines to the current comment token until it finds the the close section. This would result in less overhead and extremely consistent behavior for this feature. It would also mean that unmatched braces/parens would cause problems, but that is a bug that could be addressed later. Make sense? function() {
var a = {
/* beautify ignore:start*/
b: 0
};
/* beautify ignore:end*/
// Uh-oh! Beautifier doesn't know that the object literal was closed!
} |
I agree that picking one comment style is probably best for simplicity, and I have no problem with the jshint style. The indentation is actually a feature. I felt that the "whole" ignore block (start comment, contents and end comment) should indent as a result of the start comment changing indentation (so that the whole "block" stays intact). But it kind of goes against the feature a bit and could cause confusion, so I'm not 100% sure it's a good idea. In fact, if you define a string literal over multiple lines by using a backslash (which isn't recommended, but that's another story), you definitely don't want to have whitespace added to the beginning of your lines since that would change the string value:
Your suggested implementation definitely feels more like the correct way of doing it rather than beautifying the whole thing and then restoring certain sections. Like you pointed out, it will "throw off" the beautifier a bit though since you could start and end the ignore blocks at any time, just like in your example. |
So... Would you like it implement my suggestion? If not, that's okay too. |
I might give that a crack, to see how that solution turns out. |
There's some trouble with React's JSX. The code gets indented weirdly. Some option to ignore parts of the code would be nice. |
@alectic - Agreed, there's a lot of places where this would be good. |
A bit of an update: I haven't had time to look at the alternative implementation suggested by @bitwiseman, and I'm not sure I will in the hear future :-/ If anyone wants to give it a crack, feel free to go ahead. |
// Text below will remain unchanged /* beautify preserve:start */ {asdklgh;y;;{}dd2d} /* beautify preserve:end */ NOTE: this is not quiet ignore - still tracks tokens. Non-javascript may not work. Related #384
It would be nice to be able to have sections of code which are ignored by js-beautify (similar to the control comments available in jslint).
For instance, it would be helpful in cases where you have definitions of static data which you want to format in a specific way for increased readability and compactness.
Something along the lines of:
Thoughts?
The text was updated successfully, but these errors were encountered: