-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allowed unused parameters before used ones #12139
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
Closed
JoshuaKGoldberg
wants to merge
6
commits into
microsoft:master
from
JoshuaKGoldberg:unused-parameters-ordered
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
20de26b
Reverted allowing _-prefixed unused parameters
5ab9405
Changed unused param checking to rely on order
63f8884
Added _-prefixed unused parameter allowance back
da90201
Added tests for unused parameters with underscore
1e6d518
Added tests for unused parameter ordering
ac8b814
Stopped created unusedParameterLocals by default
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15958,24 +15958,57 @@ namespace ts { | |
|
||
function checkUnusedLocalsAndParameters(node: Node): void { | ||
if (node.parent.kind !== SyntaxKind.InterfaceDeclaration && noUnusedIdentifiers && !isInAmbientContext(node)) { | ||
for (const key in node.locals) { | ||
const local = node.locals[key]; | ||
if (!local.isReferenced) { | ||
if (local.valueDeclaration && getRootDeclaration(local.valueDeclaration).kind === SyntaxKind.Parameter) { | ||
const parameter = <ParameterDeclaration>getRootDeclaration(local.valueDeclaration); | ||
if (compilerOptions.noUnusedParameters && | ||
!isParameterPropertyDeclaration(parameter) && | ||
!parameterIsThisKeyword(parameter) && | ||
!parameterNameStartsWithUnderscore(local.valueDeclaration.name)) { | ||
error(local.valueDeclaration.name, Diagnostics._0_is_declared_but_never_used, local.name); | ||
} | ||
} | ||
else if (compilerOptions.noUnusedLocals) { | ||
forEach(local.declarations, d => errorUnusedLocal(d.name || d, local.name)); | ||
if (compilerOptions.noUnusedParameters) { | ||
checkUnusedParameterLocals(node.locals); | ||
} | ||
|
||
if (compilerOptions.noUnusedLocals) { | ||
checkUnusedLocals(node.locals); | ||
} | ||
} | ||
} | ||
|
||
function checkUnusedParameterLocals(locals: Map<Symbol>): void { | ||
let unusedParameterLocals: Symbol[] | undefined; | ||
|
||
for (const key in locals) { | ||
const local = locals[key]; | ||
if (!isLocalParameter(local)) { | ||
continue; | ||
} | ||
|
||
const parameter = <ParameterDeclaration>getRootDeclaration(local.valueDeclaration); | ||
if (!isParameterPropertyDeclaration(parameter) && !parameterIsThisKeyword(parameter)) { | ||
if (local.isReferenced || parameterNameStartsWithUnderscore(local.valueDeclaration.name)) { | ||
if (unusedParameterLocals) { | ||
unusedParameterLocals.length = 0; | ||
} | ||
} else if (unusedParameterLocals) { | ||
unusedParameterLocals.push(local); | ||
} else { | ||
unusedParameterLocals = [local]; | ||
} | ||
} | ||
} | ||
|
||
if (unusedParameterLocals) { | ||
forEach(unusedParameterLocals, local => error(local.valueDeclaration.name, Diagnostics._0_is_declared_but_never_used, local.name)); | ||
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. Should |
||
} | ||
} | ||
|
||
function checkUnusedLocals(locals: Map<Symbol>): void { | ||
for (const key in locals) { | ||
const local = locals[key]; | ||
if (local.isReferenced || isLocalParameter(local)) { | ||
continue; | ||
} | ||
|
||
forEach(local.declarations, d => errorUnusedLocal(d.name || d, local.name)); | ||
} | ||
} | ||
|
||
function isLocalParameter(local: Symbol): boolean { | ||
return local.valueDeclaration && getRootDeclaration(local.valueDeclaration).kind === SyntaxKind.Parameter; | ||
} | ||
|
||
function errorUnusedLocal(node: Node, name: string) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From a performance perspective I was hesitant to make a new [] no matter what. Hence the slight added complexity.