-
-
Notifications
You must be signed in to change notification settings - Fork 4.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
Chore: Enable no-useless-escape
#7403
Conversation
LGTM |
@vitorbal, thanks for your PR! By analyzing the history of the files in this pull request, we identified @Kyle-Mendes, @azhang496 and @kaicataldo to be potential reviewers. |
Looks like there's a linting error |
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.
Thanks! This mostly looks good, I just have a few questions about the changes.
@@ -111,7 +111,7 @@ ruleTester.run("no-useless-escape", rule, { | |||
{ code: "var foo = `\\'`;", parserOptions: {ecmaVersion: 6}, errors: [{ line: 1, column: 11, message: "Unnecessary escape character: \\'.", type: "TemplateElement"}] }, | |||
{ code: "var foo = `\\#`;", parserOptions: {ecmaVersion: 6}, errors: [{ line: 1, column: 11, message: "Unnecessary escape character: \\#.", type: "TemplateElement"}] }, | |||
{ | |||
code: "var foo = '\\\`foo\\\`';", | |||
code: "var foo = '\\\`foo\\\`';", // eslint-disable-line no-useless-escape |
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.
Could this line be replaced with:
code: "var foo = '\\`foo\\`';",
I'm not sure I understand why disabling the rule on this line is necessary.
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.
Thanks, you're totally right! Think I thought the inner one was a single-quote too. Should use my slightly larger glasses next time :)
@@ -271,7 +271,7 @@ ruleTester.run("comma-dangle", rule, { | |||
options: ["always-multiline"], | |||
}, | |||
{ | |||
code: "foo(\na,\mb\m)", |
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.
Is this supposed to be something different? Removing only the useless escapes would result in
code: "foo(\na,mbm)",
But I'm not sure what the intention of the test is.
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'm guessing typo (i.e., should be "foo(\na,\nb\n)"
.
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.
Yeah, I was not sure about this one, as the test above this one is also testing "foo(\na,\nb\n)"
. I might as well just delete this one?
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.
Actually, might as well leave the new version, as there's no other test that tests foo(\na,b)
(as in, newline after opening parenthesis, no newline after first argument)
@@ -126,7 +126,7 @@ ruleTester.run("no-useless-escape", rule, { | |||
] | |||
}, | |||
{ | |||
code: "var foo = `\\\'${foo}\\\'`;", | |||
code: "var foo = `\\\'${foo}\\\'`;", // eslint-disable-line no-useless-escape |
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.
Same thing here
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'm fairly sure all of the inline disables can be resolved, as long as we can understand what the original intent of each test was. Happy hunting! 😄
@@ -62,10 +62,12 @@ ruleTester.run("new-cap", rule, { | |||
|
|||
{ code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Bar"] }] }, | |||
{ code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptions: ["Foo.Bar"] }] }, | |||
{ code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptionPattern: "^Foo\.." }] }, | |||
|
|||
{ code: "var x = Foo.Bar(42);", options: [{ capIsNewExceptionPattern: "^Foo\.." }] }, // eslint-disable-line no-useless-escape |
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.
Pretty sure this should be capIsNewExceptionPattern: "^Foo\\.."
(value is a string that we want interpreted as a regex, so we need to escape the backslash here).
This test is probably passing by accident because \.
is generously interpreted as .
, so the real pattern is Foo..
, and "Foo.B"
does match that pattern.
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.
You are totally right, good catch! Thank you :)
{ code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["bar"] }] }, | ||
{ code: "var x = new foo.bar(42);", options: [{ newIsCapExceptions: ["foo.bar"] }] }, | ||
{ code: "var x = new foo.bar(42);", options: [{ newIsCapExceptionPattern: "^foo\.." }] }, | ||
|
||
{ code: "var x = new foo.bar(42);", options: [{ newIsCapExceptionPattern: "^foo\.." }] }, // eslint-disable-line no-useless-escape |
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.
Same here.
@@ -143,7 +145,8 @@ ruleTester.run("new-cap", rule, { | |||
}, | |||
{ | |||
code: "var x = Bar.Foo(42);", | |||
options: [{capIsNewExceptionPattern: "^Foo\.."}], | |||
|
|||
options: [{capIsNewExceptionPattern: "^Foo\.."}], // eslint-disable-line no-useless-escape |
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.
Same here, although in this case it matters less since the test fails anyway.
@@ -153,7 +156,8 @@ ruleTester.run("new-cap", rule, { | |||
}, | |||
{ | |||
code: "var x = new bar.foo(42);", | |||
options: [{newIsCapExceptionPattern: "^foo\.."}], | |||
|
|||
options: [{newIsCapExceptionPattern: "^foo\.."}], // eslint-disable-line no-useless-escape |
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.
And here as well.
@@ -26,11 +26,11 @@ ruleTester.run("no-invalid-regexp", rule, { | |||
{ code: "new RegExp('.', 'y')", options: [{ allowConstructorFlags: ["y"] }]}, | |||
{ code: "new RegExp('.', 'u')", options: [{ allowConstructorFlags: ["U"] }]}, | |||
{ code: "new RegExp('.', 'yu')", options: [{ allowConstructorFlags: ["y", "u"] }]}, | |||
{ code: "new RegExp('\/', 'yu')", options: [{ allowConstructorFlags: ["y", "u"] }]}, | |||
{ code: "new RegExp('\/', 'yu')", options: [{ allowConstructorFlags: ["y", "u"] }]}, // eslint-disable-line no-useless-escape |
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 we need to figure out the intent of this test. Either the regex is /\\\//
(matching backslash followed by slash), in which case this string should say '\\/'
; or the regex is /\//
(matching slash), in which case the string should say '/'
.
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 honestly cannot tell why is this test different than the one on the line above it. Here's the original commit: c0a7be6#diff-9f7fae656233bad860f2ba8d46986835R26
Those tests were added to test the new y
and u
flags, as it seems.
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.
Both of them resolve to the same regexp literal in the end, so i'll change this to '/'
.
{ code: "new RegExp('.', 'y')", parserOptions: { ecmaVersion: 6 }}, | ||
{ code: "new RegExp('.', 'u')", parserOptions: { ecmaVersion: 6 }}, | ||
{ code: "new RegExp('.', 'yu')", parserOptions: { ecmaVersion: 6 }}, | ||
{ code: "new RegExp('\/', 'yu')", parserOptions: { ecmaVersion: 6 }} | ||
{ code: "new RegExp('\/', 'yu')", parserOptions: { ecmaVersion: 6 }} // eslint-disable-line no-useless-escape |
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.
Same here.
@@ -72,8 +72,8 @@ ruleTester.run("no-useless-escape", rule, { | |||
{code: "var foo = `${foo}\\f`;", parserOptions: {ecmaVersion: 6}}, | |||
{code: "var foo = `${foo}\\\n`;", parserOptions: {ecmaVersion: 6}}, | |||
{code: "var foo = `${foo}\\\r\n`;", parserOptions: {ecmaVersion: 6}}, | |||
{code: "var foo = `\\\``", parserOptions: {ecmaVersion: 6}}, | |||
{code: "var foo = `\\\`${foo}\\\``", parserOptions: {ecmaVersion: 6}}, | |||
{code: "var foo = `\\\``", parserOptions: {ecmaVersion: 6}}, // eslint-disable-line no-useless-escape |
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.
Not sure what the intent of this test is either, but again we should be able to resolve this as either two backslashes total or four.
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.
4 backslashes would produce a parsing error in this case, so I will resolve this one to two backslashes.
{code: "var foo = `\\\``", parserOptions: {ecmaVersion: 6}}, | ||
{code: "var foo = `\\\`${foo}\\\``", parserOptions: {ecmaVersion: 6}}, | ||
{code: "var foo = `\\\``", parserOptions: {ecmaVersion: 6}}, // eslint-disable-line no-useless-escape | ||
{code: "var foo = `\\\`${foo}\\\``", parserOptions: {ecmaVersion: 6}}, // eslint-disable-line no-useless-escape |
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.
And probably here as well (albeit with more total backslashes).
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.
Similar here, I'll resolve this one to two backslashes. Thanks for all the feedback!
All this confusion on the intended behavior is making me think we should add |
LGTM |
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.
LGTM, thanks!
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[x] Other, please explain:
Chore, see overview section below.
What changes did you make? (Give an overview)
As per the discussion here, I've turned the
no-useless-escape
rule on and fixed all the pertinent errors in our codebase.Is there anything you'd like reviewers to focus on?
Nothing in specific, I guess just some extra eyes to make sure there were not any false-positives.