Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: add shadowed variable loc to message in no-shadow (fixes #13646) #13841

Merged
merged 3 commits into from Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 33 additions & 2 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 {{shadowedLine}}:{{shadowedColumn}}.",
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
noShadowGlobal: "{{name}} is already a global variable."
btmills marked this conversation as resolved.
Show resolved Hide resolved
}
},

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|undefined} The declared line and column of the variable.
btmills marked this conversation as resolved.
Show resolved Hide resolved
*/
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,9 +189,16 @@ module.exports = {
!isOnInitializer(variable, shadowed) &&
!(options.hoist !== "all" && isInTdz(variable, shadowed))
) {
const location = getDeclaredLocation(shadowed);
const messageId = location.global ? "noShadowGlobal" : "noShadow";

if (!location.global) {
variable.shadowedLine = location.line;
variable.shadowedColumn = location.column;
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
}
context.report({
node: variable.identifiers[0],
messageId: "noShadow",
messageId,
data: variable
});
}
Expand Down