Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

jslint: remove whitespaces using simple regex #8674

Merged
merged 2 commits into from
Aug 7, 2014

Conversation

MartinMa
Copy link
Contributor

@MartinMa MartinMa commented Aug 6, 2014

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.

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, "");
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Member

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...

Copy link
Member

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)

Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor

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

Copy link
Member

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.

Copy link
Contributor

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

Copy link
Contributor

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
@MartinMa
Copy link
Contributor Author

MartinMa commented Aug 7, 2014

@peterflynn @marcelgerber I updated the regex to /^[ \t]+$/gm because I think it is more straightforward and easier to read than the other variant. Also, I like the idea that other weird whitespace-like chars in an isolated line will be flagged as an error by jslint.

@ingorichter
Copy link
Contributor

@MartinMa Thank you.

ingorichter added a commit that referenced this pull request Aug 7, 2014
jslint: remove whitespaces using simple regex
@ingorichter ingorichter merged commit f30febb into adobe:master Aug 7, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants