Skip to content
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

Upgrade: levn@0.4.1 (fixes #9366) #13140

Merged
merged 4 commits into from Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -69,7 +69,7 @@
"is-glob": "^4.0.0",
"js-yaml": "^3.13.1",
"json-stable-stringify-without-jsonify": "^1.0.1",
"levn": "^0.3.0",
"levn": "^0.4.1",
"lodash": "^4.17.14",
"minimatch": "^3.0.4",
"natural-compare": "^1.4.0",
Expand Down
21 changes: 19 additions & 2 deletions tests/lib/linter/linter.js
Expand Up @@ -2733,14 +2733,31 @@ describe("Linter", () => {
});

describe("when evaluating code with comments which have colon in its value", () => {
const code = "/* eslint max-len: [2, 100, 2, {ignoreUrls: true, ignorePattern: \"data:image\\/|\\s*require\\s*\\(|^\\s*loader\\.lazy|-\\*-\"}] */\nalert('test');";
const code = "/* eslint max-len: [2, 100, 2, {ignoreUrls: true, ignorePattern: \"data:image\\/|\\\\s*require\\\\s*\\\\(|^\\\\s*loader\\\\.lazy|-\\\\*-\"}] */\nalert('test');";
kaicataldo marked this conversation as resolved.
Show resolved Hide resolved

it("should not parse errors, should report a violation", () => {
const messages = linter.verify(code, {}, filename);

assert.strictEqual(messages.length, 1);
assert.strictEqual(messages[0].ruleId, "max-len");
assert.strictEqual(messages[0].message, "This line has a length of 122. Maximum allowed is 100.");
assert.strictEqual(messages[0].message, "This line has a length of 128. Maximum allowed is 100.");
assert.include(messages[0].nodeType, "Program");
});
});

describe("when evaluating code with comments that contain escape sequences", () => {
const code = '/* eslint max-len: ["error", 1, { ignoreComments: true, ignorePattern: "console\\.log\\\\(" }] */\nconsole.log("test");\nvar a = "test2";';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the "...console\\.log..." does anything, since this is console\.log, when when passed through levn is just console.log

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you switch to using String.raw this will become more clear

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the issue this PR fixes. Since this string is evaluated at runtime and then used to generate a RegExp, we need to ensure that the . is escaped (so that it's evaluated as a literal . character in the RegExp).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a test case like consoleXlog("test"); and make sure it does not fire if it's anything but a dot.

I'm saying is that what you actually want is an escaped backslash \, so you want \\ (under String.raw conditions). In JS console type "console\.log", you will see that the output is console.log. You get this right later on when you ensure there is an escaped backlash before the ( by doing \\(, so that it is escaped in the regex

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH, I understand now - thanks! Updating now.


it("should validate correctly", () => {
const config = { rules: {} };

const messages = linter.verify(code, config, filename);

assert.strictEqual(messages.length, 1);
assert.strictEqual(messages[0].ruleId, "max-len");
assert.strictEqual(messages[0].message, "This line has a length of 16. Maximum allowed is 1.");
assert.strictEqual(messages[0].line, 3);
assert.strictEqual(messages[0].column, 1);
assert.include(messages[0].nodeType, "Program");
});
});
Expand Down