Skip to content

Commit

Permalink
feat: add declaration loc to message in block-scoped-var (#17252)
Browse files Browse the repository at this point in the history
* feat: add declaration loc to message in block-scoped-var

Fixes #17244

* add test with multiple declarations
  • Loading branch information
mdjermanovic committed Jun 11, 2023
1 parent 0e9980c commit e50fac3
Show file tree
Hide file tree
Showing 2 changed files with 314 additions and 22 deletions.
20 changes: 15 additions & 5 deletions lib/rules/block-scoped-var.js
Expand Up @@ -22,7 +22,7 @@ module.exports = {
schema: [],

messages: {
outOfScope: "'{{name}}' used outside of binding context."
outOfScope: "'{{name}}' declared on line {{definitionLine}} column {{definitionColumn}} is used outside of binding context."
}
},

Expand Down Expand Up @@ -50,12 +50,22 @@ module.exports = {
/**
* Reports a given reference.
* @param {eslint-scope.Reference} reference A reference to report.
* @param {eslint-scope.Definition} definition A definition for which to report reference.
* @returns {void}
*/
function report(reference) {
function report(reference, definition) {
const identifier = reference.identifier;

context.report({ node: identifier, messageId: "outOfScope", data: { name: identifier.name } });
const definitionPosition = definition.name.loc.start;

context.report({
node: identifier,
messageId: "outOfScope",
data: {
name: identifier.name,
definitionLine: definitionPosition.line,
definitionColumn: definitionPosition.column + 1
}
});
}

/**
Expand Down Expand Up @@ -92,7 +102,7 @@ module.exports = {
variables[i]
.references
.filter(isOutsideOfScope)
.forEach(report);
.forEach(ref => report(ref, variables[i].defs.find(def => def.parent === node)));
}
}

Expand Down

0 comments on commit e50fac3

Please sign in to comment.