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 all 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
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