Skip to content

Commit

Permalink
Update: show message when the variable is global (refs eslint#13646)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-mangoe committed Jan 16, 2021
1 parent fdd6444 commit 6c67621
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
16 changes: 10 additions & 6 deletions lib/rules/no-shadow.js
Expand Up @@ -44,7 +44,8 @@ module.exports = {
],

messages: {
noShadow: "'{{name}}' is already declared in the upper scope {{shadowedLine}}:{{shadowedColumn}}."
noShadow: "'{{name}}' is already declared in the upper scope {{shadowedLine}}:{{shadowedColumn}}.",
noShadowGlobal: "{{name}} is already a global variable."
}
},

Expand Down Expand Up @@ -128,13 +129,13 @@ module.exports = {

if (identifier) {
obj = {
global: false,
line: identifier.loc.start.line,
column: identifier.loc.start.column + 1
};
} else {
obj = {
line: 0,
column: 0
global: true
};
}
return obj;
Expand Down Expand Up @@ -189,12 +190,15 @@ module.exports = {
!(options.hoist !== "all" && isInTdz(variable, shadowed))
) {
const location = getDeclaredLocation(shadowed);
const messageId = location.global ? "noShadowGlobal" : "noShadow";

variable.shadowedLine = location.line;
variable.shadowedColumn = location.column;
if (!location.global) {
variable.shadowedLine = location.line;
variable.shadowedColumn = location.column;
}
context.report({
node: variable.identifiers[0],
messageId: "noShadow",
messageId,
data: variable
});
}
Expand Down
36 changes: 12 additions & 24 deletions tests/lib/rules/no-shadow.js
Expand Up @@ -633,11 +633,9 @@ ruleTester.run("no-shadow", rule, {
code: "function foo() { var Object = 0; }",
options: [{ builtinGlobals: true }],
errors: [{
messageId: "noShadow",
messageId: "noShadowGlobal",
data: {
name: "Object",
shadowedLine: 0,
shadowedColumn: 0
name: "Object"
},
type: "Identifier"
}]
Expand All @@ -647,11 +645,9 @@ ruleTester.run("no-shadow", rule, {
options: [{ builtinGlobals: true }],
env: { browser: true },
errors: [{
messageId: "noShadow",
messageId: "noShadowGlobal",
data: {
name: "top",
shadowedLine: 0,
shadowedColumn: 0
name: "top"
},
type: "Identifier"
}]
Expand All @@ -661,11 +657,9 @@ ruleTester.run("no-shadow", rule, {
options: [{ builtinGlobals: true }],
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [{
messageId: "noShadow",
messageId: "noShadowGlobal",
data: {
name: "Object",
shadowedLine: 0,
shadowedColumn: 0
name: "Object"
},
type: "Identifier"
}]
Expand All @@ -676,11 +670,9 @@ ruleTester.run("no-shadow", rule, {
parserOptions: { ecmaVersion: 6, sourceType: "module" },
env: { browser: true },
errors: [{
messageId: "noShadow",
messageId: "noShadowGlobal",
data: {
name: "top",
shadowedLine: 0,
shadowedColumn: 0
name: "top"
},
type: "Identifier"
}]
Expand All @@ -690,11 +682,9 @@ ruleTester.run("no-shadow", rule, {
options: [{ builtinGlobals: true }],
parserOptions: { ecmaFeatures: { globalReturn: true } },
errors: [{
messageId: "noShadow",
messageId: "noShadowGlobal",
data: {
name: "Object",
shadowedLine: 0,
shadowedColumn: 0
name: "Object"
},
type: "Identifier"
}]
Expand All @@ -705,11 +695,9 @@ ruleTester.run("no-shadow", rule, {
parserOptions: { ecmaFeatures: { globalReturn: true } },
env: { browser: true },
errors: [{
messageId: "noShadow",
messageId: "noShadowGlobal",
data: {
name: "top",
shadowedLine: 0,
shadowedColumn: 0
name: "top"
},
type: "Identifier"
}]
Expand Down

0 comments on commit 6c67621

Please sign in to comment.