@@ -12,7 +12,7 @@ import {
12
12
} from '@typescript-eslint/scope-manager' ;
13
13
import * as util from '../util' ;
14
14
15
- type MessageIds = 'noShadow' ;
15
+ type MessageIds = 'noShadow' | 'noShadowGlobal' ;
16
16
type Options = [
17
17
{
18
18
allow ?: string [ ] ;
@@ -64,7 +64,9 @@ export default util.createRule<Options, MessageIds>({
64
64
} ,
65
65
] ,
66
66
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." ,
68
70
} ,
69
71
} ,
70
72
defaultOptions : [
@@ -517,6 +519,28 @@ export default util.createRule<Options, MessageIds>({
517
519
) ;
518
520
}
519
521
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
+
520
544
/**
521
545
* Checks the current context for shadowed variables.
522
546
* @param {Scope } scope Fixme
@@ -595,12 +619,25 @@ export default util.createRule<Options, MessageIds>({
595
619
) &&
596
620
! ( options . hoist !== 'all' && isInTdz ( variable , shadowed ) )
597
621
) {
622
+ const location = getDeclaredLocation ( shadowed ) ;
623
+
598
624
context . report ( {
599
625
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
+ } ) ,
604
641
} ) ;
605
642
}
606
643
}
0 commit comments