8000 fix: Ensure module scope is checked for references in `consistent-thi… · eslint/eslint@db1b9a6 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit db1b9a6

Browse files
authored
fix: Ensure module scope is checked for references in consistent-this (#19383)
fixes #19244
1 parent 8bcd820 commit db1b9a6

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)
0