Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ca08e9

Browse files
authored
feat(eslint-plugin): [no-shadow] add shadowed variable location to the error message (typescript-eslint#5183)
1 parent 2600f7f commit 8ca08e9

File tree

4 files changed

+2049
-2032
lines changed

4 files changed

+2049
-2032
lines changed

packages/eslint-plugin/src/rules/no-shadow.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '@typescript-eslint/scope-manager';
1313
import * as util from '../util';
1414

15-
type MessageIds = 'noShadow';
15+
type MessageIds = 'noShadow' | 'noShadowGlobal';
1616
type Options = [
1717
{
1818
allow?: string[];
@@ -64,7 +64,9 @@ export default util.createRule<Options, MessageIds>({
6464
},
6565
],
6666
messages: {
67-
noShadow: "'{{name}}' is already declared in the upper scope.",
67+
noShadow:
68+
"'{{name}}' is already declared in the upper scope on line {{shadowedLine}} column {{shadowedColumn}}.",
69+
noShadowGlobal: "'{{name}}' is already a global variable.",
6870
},
6971
},
7072
defaultOptions: [
@@ -517,6 +519,28 @@ export default util.createRule<Options, MessageIds>({
517519
);
518520
}
519521

522+
/**
523+
* Get declared line and column of a variable.
524+
* @param variable The variable to get.
525+
* @returns The declared line and column of the variable.
526+
*/
527+
function getDeclaredLocation(
528+
variable: TSESLint.Scope.Variable,
529+
): { global: true } | { global: false; line: number; column: number } {
530+
const identifier = variable.identifiers[0];
531+
if (identifier) {
532+
return {
533+
global: false,
534+
line: identifier.loc.start.line,
535+
column: identifier.loc.start.column + 1,
536+
};
537+
} else {
538+
return {
539+
global: true,
540+
};
541+
}
542+
}
543+
520544
/**
521545
* Checks the current context for shadowed variables.
522546
* @param {Scope} scope Fixme
@@ -595,12 +619,25 @@ export default util.createRule<Options, MessageIds>({
595619
) &&
596620
!(options.hoist !== 'all' && isInTdz(variable, shadowed))
597621
) {
622+
const location = getDeclaredLocation(shadowed);
623+
598624
context.report({
599625
node: variable.identifiers[0],
600-
messageId: 'noShadow',
601-
data: {
602-
name: variable.name,
603-
},
626+
...(location.global
627+
? {
628+
messageId: 'noShadowGlobal',
629+
data: {
630+
name: variable.name,
631+
},
632+
}
633+
: {
634+
messageId: 'noShadow',
635+
data: {
636+
name: variable.name,
637+
shadowedLine: location.line,
638+
shadowedColumn: location.column,
639+
},
640+
}),
604641
});
605642
}
606643
}

0 commit comments

Comments
 (0)