Skip to content

Commit

Permalink
Update: show the original identifier place (refs eslint#13646)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-mangoe committed Nov 21, 2020
1 parent 4255f36 commit f235f99
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 51 deletions.
29 changes: 28 additions & 1 deletion lib/rules/no-shadow.js
Expand Up @@ -44,7 +44,7 @@ module.exports = {
],

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

Expand Down Expand Up @@ -117,6 +117,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|undefined} The declared line and column of the variable.
*/
function getDeclaredLocation(variable) {
const identifier = variable.identifiers[0];
let obj;

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

/**
* Checks if a variable is in TDZ of scopeVar.
* @param {Object} variable The variable to check.
Expand Down Expand Up @@ -165,6 +188,10 @@ module.exports = {
!isOnInitializer(variable, shadowed) &&
!(options.hoist !== "all" && isInTdz(variable, shadowed))
) {
const location = getDeclaredLocation(shadowed);

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

0 comments on commit f235f99

Please sign in to comment.