Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [no-shadow] add shadowed variable location to the error message#5183
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -12,7 +12,7 @@ import { | ||
} from '@typescript-eslint/scope-manager'; | ||
import * as util from '../util'; | ||
type MessageIds = 'noShadow' | 'noShadowGlobal'; | ||
type Options = [ | ||
{ | ||
allow?: string[]; | ||
@@ -64,7 +64,9 @@ export default util.createRule<Options, MessageIds>({ | ||
}, | ||
], | ||
messages: { | ||
noShadow: | ||
"'{{name}}' is already declared in the upper scope on line {{shadowedLine}} column {{shadowedColumn}}.", | ||
noShadowGlobal: "'{{name}}' is already a global variable.", | ||
}, | ||
}, | ||
defaultOptions: [ | ||
@@ -517,6 +519,28 @@ export default util.createRule<Options, MessageIds>({ | ||
); | ||
} | ||
/** | ||
* Get declared line and column of a variable. | ||
* @param variable The variable to get. | ||
* @returns The declared line and column of the variable. | ||
*/ | ||
function getDeclaredLocation( | ||
variable: TSESLint.Scope.Variable, | ||
): { global: true } | { global: false; line: number; column: number } { | ||
const identifier = variable.identifiers[0]; | ||
if (identifier) { | ||
return { | ||
global: false, | ||
line: identifier.loc.start.line, | ||
column: identifier.loc.start.column + 1, | ||
}; | ||
} else { | ||
return { | ||
global: true, | ||
}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Nitty nit: could be simplified to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. you really do love your ternaries 😄 | ||
} | ||
/** | ||
* Checks the current context for shadowed variables. | ||
* @param {Scope} scope Fixme | ||
@@ -595,12 +619,25 @@ export default util.createRule<Options, MessageIds>({ | ||
) && | ||
!(options.hoist !== 'all' && isInTdz(variable, shadowed)) | ||
) { | ||
const location = getDeclaredLocation(shadowed); | ||
context.report({ | ||
node: variable.identifiers[0], | ||
...(location.global | ||
? { | ||
messageId: 'noShadowGlobal', | ||
data: { | ||
name: variable.name, | ||
}, | ||
} | ||
: { | ||
messageId: 'noShadow', | ||
data: { | ||
name: variable.name, | ||
shadowedLine: location.line, | ||
shadowedColumn: location.column, | ||
}, | ||
}), | ||
}); | ||
} | ||
} | ||
Uh oh!
There was an error while loading.Please reload this page.