-
Notifications
You must be signed in to change notification settings - Fork 7.6k
jslint: remove whitespaces using simple regex #8674
Conversation
Replacing obsolete for loop with a simple regex to remove whitespaces, if a line contains only whitespace.
} | ||
} | ||
text = arr.join("\n"); | ||
text = text.replace(/^[\x20\t\f]+$/gm, ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the \f
token for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\f
stands for form feed. I don't know if it is necessary.
Only the characters "space" (U+0020), "tab" (U+0009), "line feed" (U+000A), "carriage return" (U+000D), and "form feed" (U+000C) can occur in whitespace.
As per http://www.w3.org/TR/css3-selectors/#whitespace
Maybe \f
is catched by $
. I'm checking it later ...
edit A line containing spaces only, but with \f
at the end isn't matched if you omit the regex token for it. Form feed (sometimes called page break character) is kind of a weird character as you basically can't easily enter it directly using Brackets. I'd keep it in the regex though, for the sake of completeness. Should I remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think just using /^[ \t]+$/gm
should be fine. If there are other weird whitespace-like chars in the file, we probably want to let JSLint flag it as an error anyway...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Fwiw, I think the only problem with the original regex in the comment is that it was using \r instead of \n -- all editor text in Brackets is normalized to \n. The new regex is cleaner anyway, though)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just use \s
for whitespace? It covers all of those cases. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-white-space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RegExp should only span lines if you use the m
flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@peterflynn You can use /^[^\S\n]+$/g
to have \s
w/o \n
. See http://regex101.com/r/uN6dC3/1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@redmunds But this one does use the m
flag, by design since it relies on ^
and $
to mark line boundaries.
@marcelgerber's Cool, that seems like a viable option too. So @MartinMa I think either /^[ \t]+$/gm
or /^[^\S\n]+$/gm
is fine -- personally I don't have a strong opinion between the two.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the trick here is to use minimum match length (the default is maximum) by changing +
to +?
like /^\s+?$/gm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@peterflynn redmunds' RegEx works too (I even tested it) and is the shortest here.
@redmunds You don't need that extra stuff.
seems to be cleaner, also other "weird whitespace characters" in an isolated line will be flagged as an error by jslint
@peterflynn @marcelgerber I updated the regex to |
@MartinMa Thank you. |
jslint: remove whitespaces using simple regex
Small speed improvement to the default lintjs extension. Replacing obsolete for loop with a simple regex to remove whitespaces, if a line contains only whitespace.