Skip to content
Permalink
Browse files
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 ce7f06121d9eb9cc2b3da24b4456b4d382e1413b
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 59 deletions.
@@ -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."
}
},

@@ -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.
@@ -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
});
}
}

0 comments on commit ce7f061

Please sign in to comment.