Skip to content

Commit

Permalink
fix: use the global scope
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Dec 14, 2021
1 parent 978db48 commit 5db9860
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
11 changes: 4 additions & 7 deletions lib/rules/id-match.js
Expand Up @@ -67,6 +67,8 @@ module.exports = {
onlyDeclarations = !!options.onlyDeclarations,
ignoreDestructuring = !!options.ignoreDestructuring;

const globalScope = context.getScope();

//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
Expand All @@ -84,15 +86,14 @@ module.exports = {
* @returns {boolean} `true` if the node is a reference to a global variable.
*/
function isReferenceToGlobalVariable(node) {
const scope = context.getScope();
const variable = scope.set.get(node.name);
const variable = globalScope.set.get(node.name);

return variable && variable.defs.length === 0 &&
variable.references.some(ref => ref.identifier === node);
}

/**
* Checks if a string matches the provided pattern and is not a global reference
* Checks if a string matches the provided pattern
* @param {string} name The string to check.
* @returns {boolean} if the string is a match
* @private
Expand Down Expand Up @@ -267,10 +268,6 @@ module.exports = {

const isClassField = node.parent.type === "PropertyDefinition";

if (isReferenceToGlobalVariable(node)) {
return;
}

if (isClassField && !checkClassFields) {
return;
}
Expand Down
8 changes: 5 additions & 3 deletions tests/lib/rules/id-match.js
Expand Up @@ -188,12 +188,14 @@ ruleTester.run("id-match", rule, {
// Should not report for global references - https://github.com/eslint/eslint/issues/15395
{
code: `
var foo = Object.keys(bar);
var a = Array.from(b);
const foo = Object.keys(bar);
const a = Array.from(b);
const bar = () => Array;
`,
options: ["^\\$?[a-z]+([A-Z0-9][a-z0-9]+)*$", {
properties: true
}]
}],
parserOptions: { ecmaVersion: 2022 }
},

// Class Methods
Expand Down

0 comments on commit 5db9860

Please sign in to comment.