From 0467c988a85f60359d8c7e3d3120082943e4313d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Sat, 30 Sep 2017 13:45:06 +0800 Subject: [PATCH] Fix: parsing regex option failing when defined in a comment. (fixes #9366) levn parsing string differently than JSON.parse, e.g. JSON.parse( '{ "foo": "\\n" }') // => { foo: '\n' } levn.parse('Object', '{ "foo": "\\n" }') // => { foo: '\\n' } --- lib/linter.js | 4 ++++ tests/lib/linter.js | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/linter.js b/lib/linter.js index e96e713ffdb..c5d57629fcd 100755 --- a/lib/linter.js +++ b/lib/linter.js @@ -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, "\\"); + items = levn.parse("Object", string) || {}; // Some tests say that it should ignore invalid comments such as `/*eslint no-alert:abc*/`. diff --git a/tests/lib/linter.js b/tests/lib/linter.js index b9d1c5d3ace..cd8bb435fff 100644 --- a/tests/lib/linter.js +++ b/tests/lib/linter.js @@ -1513,6 +1513,15 @@ describe("Linter", () => { assert.include(messages[0].nodeType, "CallExpression"); }); + // https://github.com/eslint/eslint/issues/9366 + it("rules should parse regex option correctly", () => { + const config = {}; + const code = String.raw`/* eslint id-match: [2, "^(([^$\\W]|\\$[a-f\\d]{2})+|[$_]\\w*|[^\\W\\d]\\w*|[A-Z]([A-Z_]*[A-Z])?)$", {properties: true}] */ var is$2dvoid = 0;`; + const messages = linter.verify(code, config, filename, false); + + assert.equal(messages.length, 0); + }); + it("rules should not change initial config", () => { const config = { rules: { strict: 2 } }; const codeA = "/*eslint strict: 0*/ function bar() { return 2; }";