diff --git a/lib/rules/utils/ast-utils.js b/lib/rules/utils/ast-utils.js index ecde099fa02..21742366ce4 100644 --- a/lib/rules/utils/ast-utils.js +++ b/lib/rules/utils/ast-utils.js @@ -32,6 +32,7 @@ const thisTagPattern = /^[\s*]*@this/mu; const COMMENTS_IGNORE_PATTERN = /^\s*(?:eslint|jshint\s+|jslint\s+|istanbul\s+|globals?\s+|exported\s+|jscs)/u; +const ESLINT_DIRECTIVE_PATTERN = /^(?:eslint[- ]|(?:globals?|exported) )/u; const LINEBREAKS = new Set(["\r\n", "\r", "\n", "\u2028", "\u2029"]); // A set of node types that can contain a list of statements @@ -908,12 +909,8 @@ module.exports = { const comment = node.value.trim(); return ( - node.type === "Line" && comment.indexOf("eslint-") === 0 || - node.type === "Block" && ( - comment.indexOf("global ") === 0 || - comment.indexOf("eslint ") === 0 || - comment.indexOf("eslint-") === 0 - ) + node.type === "Line" && comment.startsWith("eslint-") || + node.type === "Block" && ESLINT_DIRECTIVE_PATTERN.test(comment) ); }, diff --git a/tests/lib/rules/no-inline-comments.js b/tests/lib/rules/no-inline-comments.js index 463c86eea66..7eb0cac730f 100644 --- a/tests/lib/rules/no-inline-comments.js +++ b/tests/lib/rules/no-inline-comments.js @@ -39,6 +39,9 @@ ruleTester.run("no-inline-comments", rule, { "// A solitary comment", "var a = 1; // eslint-disable-line no-debugger", "var a = 1; /* eslint-disable-line no-debugger */", + "foo(); /* global foo */", + "foo(); /* globals foo */", + "var foo; /* exported foo */", // JSX exception `var a = ( diff --git a/tests/lib/rules/utils/ast-utils.js b/tests/lib/rules/utils/ast-utils.js index 7789de7742c..1cd8727b46b 100644 --- a/tests/lib/rules/utils/ast-utils.js +++ b/tests/lib/rules/utils/ast-utils.js @@ -204,7 +204,10 @@ describe("ast-utils", () => { "// lalala I'm a normal comment", "// trying to confuse eslint ", "//trying to confuse eslint-directive-detection", - "//eslint is awesome" + "//eslint is awesome", + "//global line comment is not a directive", + "//globals line comment is not a directive", + "//exported line comment is not a directive" ].join("\n"); const ast = espree.parse(code, ESPREE_CONFIG); const sourceCode = new SourceCode(code, ast); @@ -247,7 +250,10 @@ describe("ast-utils", () => { "/*eslint-enable no-undef*/", "/* eslint-env {\"es6\": true} */", "/* eslint foo */", - "/*eslint bar*/" + "/*eslint bar*/", + "/*global foo*/", + "/*globals foo*/", + "/*exported foo*/" ].join("\n"); const ast = espree.parse(code, ESPREE_CONFIG); const sourceCode = new SourceCode(code, ast);