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 7885db3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 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
2 changes: 2 additions & 0 deletions tests/lib/rules/id-match.js
Expand Up @@ -665,6 +665,8 @@ ruleTester.run("id-match", rule, {
let e = (Object) => Object.keys(obj, prop); // not global Object
let f = (Array) => Array.from(obj, prop); // not global Array
foo.Array = 5; // not global Array
foo = () => Array; // global Array
`,
options: ["^\\$?[a-z]+([A-Z0-9][a-z0-9]+)*$", {
properties: true
Expand Down

0 comments on commit 7885db3

Please sign in to comment.