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
Fix: id-match mysteriously fails when defined in a comment. (fixes #9… #9372
Conversation
Thanks for the PR! Replying to #9366 (comment):
I think you were correct originally -- this seems like a bug in JSON.parse( '{ "foo": "\\n" }') // => { foo: '\n' }
levn.parse('Object', '{ "foo": "\\n" }') // => { foo: '\\n' } To fix this issue, I think we need to handle all escape characters, not just We could either:
|
thanks for quick response. I think I got your point, would it be better to specify an option to not sure it works, but worth a try, thoughts? |
tests/lib/linter.js
Outdated
const code = [ | ||
|
||
// "\\\\W" equals to "\\W" in source code. | ||
"/* eslint id-match: [2, \"^(([^$\\\\W]|\\\\$[a-f\\\\d]{2})+|[$_]\\\\w*|[^\\\\W\\\\d]\\\\w*|[A-Z]([A-Z_]*[A-Z])?)$\", {properties: true}] */", |
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 realize this is WIP, but this might be more readable if you use String.raw
.
String.raw`\n` // => \n (backslash + newline)
(fixes eslint#9366) levn parsing string differently than JSON.parse, e.g. JSON.parse( '{ "foo": "\\n" }') // => { foo: '\n' } levn.parse('Object', '{ "foo": "\\n" }') // => { foo: '\\n' }
00b8888
to
0467c98
Compare
LGTM |
@@ -94,6 +94,10 @@ function parseJsonConfig(string, location) { | |||
|
|||
// Parses a JSON-like comment by the same way as parsing CLI option. | |||
try { | |||
|
|||
// https://github.com/eslint/eslint/issues/9366 | |||
string = string.replace(/\\\\/g, "\\"); |
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-an-aardvark it can fix the issue. but has a side effect:
// `\` is optional, so either is fine.
/* eslint id-match: [2, "^(([^$\\W]|\\$[a-f\\d]{2})+|[$_]\\w*|[^\\W\\d]\\w*|[A-Z]([A-Z_]*[A-Z])?)$"]*/
/* eslint id-match: [2, "^(([^$\W]|\$[a-f\d]{2})+|[$_]\w*|[^\W\d]\w*|[A-Z]([A-Z_]*[A-Z])?)$"]*/
@not-an-aardvark @aladdin-add What's the resolution on this PR? Is this something we want to fix, or should this be fixed in levn? |
When the option value is a regex, we use levn.parse('\\\\n'); => '\\n'
new Regex('\\n'); => /\n/ But since I'm not sure this is ideal. Hope we can figure out a better way. (can be discussed in TSC meeting?) |
…366)
What is the purpose of this pull request? (put an "X" next to item)
[x] Bug fix (template)
What changes did you make? (Give an overview)
fixes #9366
When the option value is a regex, we use
levn.parse()
andnew Regex()
to get it. It should be unescaped twice if working correctly:But since
levn
don't support escape\
, the parsed result would be'\\\\n'
-- this is not right. this PR simply replace'\\\\'
to'\\'
, so the result can be right.Is there anything you'd like reviewers to focus on?