Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: add shadowed variable loc to message in no-shadow (fixes #13646
…) (#13841)

* Update: show the original identifier place (refs #13646)

* Update: show message when the variable is global (refs #13646)

* Update: fix code according to review.
  • Loading branch information
t-mangoe committed Jan 30, 2021
1 parent c60e23f commit ce7f061
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 59 deletions.
38 changes: 35 additions & 3 deletions lib/rules/no-shadow.js
Expand Up @@ -44,7 +44,8 @@ module.exports = {
],

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

Expand Down Expand Up @@ -117,6 +118,29 @@ module.exports = {
return def && def.name.range;
}

/**
* Get declared line and column of a variable.
* @param {eslint-scope.Variable} variable The variable to get.
* @returns {Object} The declared line and column of the variable.
*/
function getDeclaredLocation(variable) {
const identifier = variable.identifiers[0];
let obj;

if (identifier) {
obj = {
global: false,
line: identifier.loc.start.line,
column: identifier.loc.start.column + 1
};
} else {
obj = {
global: true
};
}
return obj;
}

/**
* Checks if a variable is in TDZ of scopeVar.
* @param {Object} variable The variable to check.
Expand Down Expand Up @@ -165,10 +189,18 @@ module.exports = {
!isOnInitializer(variable, shadowed) &&
!(options.hoist !== "all" && isInTdz(variable, shadowed))
) {
const location = getDeclaredLocation(shadowed);
const messageId = location.global ? "noShadowGlobal" : "noShadow";
const data = { name: variable.name };

if (!location.global) {
data.shadowedLine = location.line;
data.shadowedColumn = location.column;
}
context.report({
node: variable.identifiers[0],
messageId: "noShadow",
data: variable
messageId,
data
});
}
}
Expand Down

0 comments on commit ce7f061

Please sign in to comment.