Skip to content

Commit aa8e286

Browse files
committed
fix: Ensure module scope is checked for references
fixes #19244
1 parent 8bcd820 commit aa8e286

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/rules/consistent-this.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,17 @@ module.exports = {
119119
function ensureWasAssigned(node) {
120120
const scope = sourceCode.getScope(node);
121121

122+
// if this is program scope we also need to check module scope
123+
const extraScope = node.type === "Program" && node.sourceType === "module"
124+
? scope.childScopes[0]
125+
: null;
126+
122127
aliases.forEach(alias => {
123128
checkWasAssigned(alias, scope);
129+
130+
if (extraScope) {
131+
checkWasAssigned(alias, extraScope);
132+
}
124133
});
125134
}
126135

tests/lib/rules/consistent-this.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ ruleTester.run("consistent-this", rule, {
6868
{ code: "that = this", options: ["self"], errors: [{ messageId: "unexpectedAlias", data: { name: "that" }, type: "AssignmentExpression" }] },
6969
{ code: "self = this", options: ["that"], errors: [{ messageId: "unexpectedAlias", data: { name: "self" }, type: "AssignmentExpression" }] },
7070
{ code: "self += this", options: ["self"], errors: [{ messageId: "aliasNotAssignedToThis", data: { name: "self" }, type: "AssignmentExpression" }] },
71-
{ code: "var self; (function() { self = this; }())", options: ["self"], errors: [{ messageId: "aliasNotAssignedToThis", data: { name: "self" }, type: "VariableDeclarator" }] }
71+
{ code: "var self; (function() { self = this; }())", options: ["self"], errors: [{ messageId: "aliasNotAssignedToThis", data: { name: "self" }, type: "VariableDeclarator" }] },
72+
{ code: "var self; (function() { self = this; }())", options: ["self"], languageOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [{ messageId: "aliasNotAssignedToThis", data: { name: "self" }, type: "VariableDeclarator" }] }
7273
]
7374
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy