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

fix: do not report global references in id-match rule #15420

Merged
merged 6 commits into from Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
18 changes: 17 additions & 1 deletion lib/rules/id-match.js
Expand Up @@ -5,6 +5,13 @@

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("./utils/ast-utils");
const globals = require("globals");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -76,14 +83,23 @@ module.exports = {
const ALLOWED_PARENT_TYPES = new Set(["CallExpression", "NewExpression"]);
const DECLARATION_TYPES = new Set(["FunctionDeclaration", "VariableDeclarator"]);
const IMPORT_TYPES = new Set(["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"]);
const BUILT_IN_REFERENCES = new Set(Object.keys(globals.builtin));

/**
* Checks if a string matches the provided pattern
* Checks if a string matches the provided pattern and is not a global built-in reference
* @param {string} name The string to check.
* @returns {boolean} if the string is a match
* @private
*/
function isInvalid(name) {
const scope = context.getScope();
const variable = astUtils.getVariableByName(scope, name);

// Do not report global built-in references
if (variable && variable.scope.type === "global" && BUILT_IN_REFERENCES.has(name)) {
return false;
}

mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
return !regexp.test(name);
}

Expand Down
68 changes: 68 additions & 0 deletions tests/lib/rules/id-match.js
Expand Up @@ -185,6 +185,17 @@ ruleTester.run("id-match", rule, {
}]
},

// Should not report for global references that are native objects - https://github.com/eslint/eslint/issues/15395
{
code: `
var foo = Object.keys(bar);
var a = Array.from(b);
`,
options: ["^\\$?[a-z]+([A-Z0-9][a-z0-9]+)*$", {
properties: true
}]
},

// Class Methods
{
code: "class x { foo() {} }",
Expand Down Expand Up @@ -641,6 +652,63 @@ ruleTester.run("id-match", rule, {
]
},

// https://github.com/eslint/eslint/issues/15395
{
code: `
const foo_variable = 1;
class MyClass {
}
let a = new MyClass();
let b = {id: 1};
let c = Object.keys(b);
let d = Array.from(b);
let e = (Object) => Object.keys(obj, prop); // not global Object
let f = (Array) => Array.from(obj, prop); // not global Array
`,
options: ["^\\$?[a-z]+([A-Z0-9][a-z0-9]+)*$", {
properties: true
}],
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "Identifier 'foo_variable' does not match the pattern '^\\$?[a-z]+([A-Z0-9][a-z0-9]+)*$'.",
type: "Identifier",
line: 2,
column: 19
},
{
message: "Identifier 'MyClass' does not match the pattern '^\\$?[a-z]+([A-Z0-9][a-z0-9]+)*$'.",
type: "Identifier",
line: 3,
column: 19
},
{
message: "Identifier 'Object' does not match the pattern '^\\$?[a-z]+([A-Z0-9][a-z0-9]+)*$'.",
type: "Identifier",
line: 9,
column: 22
},
{
message: "Identifier 'Object' does not match the pattern '^\\$?[a-z]+([A-Z0-9][a-z0-9]+)*$'.",
type: "Identifier",
line: 9,
column: 33
},
{
message: "Identifier 'Array' does not match the pattern '^\\$?[a-z]+([A-Z0-9][a-z0-9]+)*$'.",
type: "Identifier",
line: 10,
column: 22
},
{
message: "Identifier 'Array' does not match the pattern '^\\$?[a-z]+([A-Z0-9][a-z0-9]+)*$'.",
type: "Identifier",
line: 10,
column: 32
}
]
},

// Class Methods
{
code: "class x { _foo() {} }",
Expand Down