-
Notifications
You must be signed in to change notification settings - Fork 12.8k
fixUnusedIdentifier: Handle destructure with all bindings unused #23805
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6bda0cd
fixUnusedIdentifier: Handle destructure with all bindings unused
540998f
Add parameters test
18effd2
Merge branch 'master' into codeFixUnusedIdentifier_destructure
10df13e
Add test for 'for' loop
36237ca
Merge branch 'master' into codeFixUnusedIdentifier_destructure
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ namespace ts.codefix { | |
Diagnostics._0_is_declared_but_never_used.code, | ||
Diagnostics.Property_0_is_declared_but_its_value_is_never_read.code, | ||
Diagnostics.All_imports_in_import_declaration_are_unused.code, | ||
Diagnostics.All_destructured_elements_are_unused.code, | ||
]; | ||
registerCodeFix({ | ||
errorCodes, | ||
|
@@ -18,6 +19,10 @@ namespace ts.codefix { | |
const changes = textChanges.ChangeTracker.with(context, t => t.deleteNode(sourceFile, importDecl)); | ||
return [createCodeFixAction(fixName, changes, [Diagnostics.Remove_import_from_0, showModuleSpecifier(importDecl)], fixIdDelete, Diagnostics.Delete_all_unused_declarations)]; | ||
} | ||
const delDestructure = textChanges.ChangeTracker.with(context, t => tryDeleteFullDestructure(t, sourceFile, context.span.start)); | ||
if (delDestructure.length) { | ||
return [createCodeFixAction(fixName, delDestructure, Diagnostics.Remove_destructuring, fixIdDelete, Diagnostics.Delete_all_unused_declarations)]; | ||
} | ||
|
||
const token = getToken(sourceFile, textSpanEnd(context.span)); | ||
const result: CodeFixAction[] = []; | ||
|
@@ -50,7 +55,9 @@ namespace ts.codefix { | |
changes.deleteNode(sourceFile, importDecl); | ||
} | ||
else { | ||
tryDeleteDeclaration(changes, sourceFile, token); | ||
if (!tryDeleteFullDestructure(changes, sourceFile, diag.start!)) { | ||
tryDeleteDeclaration(changes, sourceFile, token); | ||
} | ||
} | ||
break; | ||
default: | ||
|
@@ -65,6 +72,26 @@ namespace ts.codefix { | |
return startToken.kind === SyntaxKind.ImportKeyword ? tryCast(startToken.parent, isImportDeclaration) : undefined; | ||
} | ||
|
||
function tryDeleteFullDestructure(changes: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number): boolean { | ||
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. add a test for |
||
const startToken = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false); | ||
if (startToken.kind !== SyntaxKind.OpenBraceToken || !isObjectBindingPattern(startToken.parent)) return false; | ||
const decl = startToken.parent.parent; | ||
switch (decl.kind) { | ||
case SyntaxKind.VariableDeclaration: | ||
tryDeleteVariableDeclaration(changes, sourceFile, decl); | ||
break; | ||
case SyntaxKind.Parameter: | ||
changes.deleteNodeInList(sourceFile, decl); | ||
break; | ||
case SyntaxKind.BindingElement: | ||
changes.deleteNode(sourceFile, decl); | ||
break; | ||
default: | ||
return Debug.assertNever(decl); | ||
} | ||
return true; | ||
} | ||
|
||
function getToken(sourceFile: SourceFile, pos: number): Node { | ||
const token = findPrecedingToken(pos, sourceFile); | ||
// this handles var ["computed"] = 12; | ||
|
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
tests/cases/compiler/unusedDestructuring.ts(3,7): error TS6198: All destructured elements are unused. | ||
tests/cases/compiler/unusedDestructuring.ts(4,9): error TS6133: 'c' is declared but its value is never read. | ||
tests/cases/compiler/unusedDestructuring.ts(6,7): error TS6133: 'e' is declared but its value is never read. | ||
tests/cases/compiler/unusedDestructuring.ts(8,1): error TS6133: 'f' is declared but its value is never read. | ||
tests/cases/compiler/unusedDestructuring.ts(8,12): error TS6198: All destructured elements are unused. | ||
tests/cases/compiler/unusedDestructuring.ts(8,24): error TS6133: 'c' is declared but its value is never read. | ||
tests/cases/compiler/unusedDestructuring.ts(8,32): error TS6133: 'e' is declared but its value is never read. | ||
|
||
|
||
==== tests/cases/compiler/unusedDestructuring.ts (7 errors) ==== | ||
export {}; | ||
declare const o: any; | ||
const { a, b } = o; | ||
~~~~~~~~ | ||
!!! error TS6198: All destructured elements are unused. | ||
const { c, d } = o; | ||
~ | ||
!!! error TS6133: 'c' is declared but its value is never read. | ||
d; | ||
const { e } = o; | ||
~~~~~ | ||
!!! error TS6133: 'e' is declared but its value is never read. | ||
|
||
function f({ a, b }, { c, d }, { e }) { | ||
~~~~~~~~~~ | ||
!!! error TS6133: 'f' is declared but its value is never read. | ||
~~~~~~~~ | ||
!!! error TS6198: All destructured elements are unused. | ||
~ | ||
!!! error TS6133: 'c' is declared but its value is never read. | ||
~~~~~ | ||
!!! error TS6133: 'e' is declared but its value is never read. | ||
d; | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//// [unusedDestructuring.ts] | ||
export {}; | ||
declare const o: any; | ||
const { a, b } = o; | ||
const { c, d } = o; | ||
d; | ||
const { e } = o; | ||
|
||
function f({ a, b }, { c, d }, { e }) { | ||
d; | ||
} | ||
|
||
|
||
//// [unusedDestructuring.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
var a = o.a, b = o.b; | ||
var c = o.c, d = o.d; | ||
d; | ||
var e = o.e; | ||
function f(_a, _b, _c) { | ||
var a = _a.a, b = _a.b; | ||
var c = _b.c, d = _b.d; | ||
var e = _c.e; | ||
d; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
=== tests/cases/compiler/unusedDestructuring.ts === | ||
export {}; | ||
declare const o: any; | ||
>o : Symbol(o, Decl(unusedDestructuring.ts, 1, 13)) | ||
|
||
const { a, b } = o; | ||
>a : Symbol(a, Decl(unusedDestructuring.ts, 2, 7)) | ||
>b : Symbol(b, Decl(unusedDestructuring.ts, 2, 10)) | ||
>o : Symbol(o, Decl(unusedDestructuring.ts, 1, 13)) | ||
|
||
const { c, d } = o; | ||
>c : Symbol(c, Decl(unusedDestructuring.ts, 3, 7)) | ||
>d : Symbol(d, Decl(unusedDestructuring.ts, 3, 10)) | ||
>o : Symbol(o, Decl(unusedDestructuring.ts, 1, 13)) | ||
|
||
d; | ||
>d : Symbol(d, Decl(unusedDestructuring.ts, 3, 10)) | ||
|
||
const { e } = o; | ||
>e : Symbol(e, Decl(unusedDestructuring.ts, 5, 7)) | ||
>o : Symbol(o, Decl(unusedDestructuring.ts, 1, 13)) | ||
|
||
function f({ a, b }, { c, d }, { e }) { | ||
>f : Symbol(f, Decl(unusedDestructuring.ts, 5, 16)) | ||
>a : Symbol(a, Decl(unusedDestructuring.ts, 7, 12)) | ||
>b : Symbol(b, Decl(unusedDestructuring.ts, 7, 15)) | ||
>c : Symbol(c, Decl(unusedDestructuring.ts, 7, 22)) | ||
>d : Symbol(d, Decl(unusedDestructuring.ts, 7, 25)) | ||
>e : Symbol(e, Decl(unusedDestructuring.ts, 7, 32)) | ||
|
||
d; | ||
>d : Symbol(d, Decl(unusedDestructuring.ts, 7, 25)) | ||
} | ||
|
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.
but even with this, we still have a comma probelm.. like:
still convert to:
The first one is a syntax error. the second is not what a human would have written either.
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.
so were you intending this change to address that, or this will be in a separate change?
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.
Separate.
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.
and if this is handled with a different change, then do we still need this one?
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.
I think so, even if we correctly delete every element from the list, that's different from deleting the entire list.
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.
that is another issue.. deleting the entire list may not be correct in the general case.. for instance
var { x } = foo()
iffoo
had side effects, then deleting it would be wrong..One option is to convert it to
var {} = foo()
which is what i was thinking about when i asked about commas earlier. but i do agree that this solution would be ugly.Alternatively, we gonna just make it
foo()
, and if the expression was a single identifier or a property access we can remove it. We have in the checker a functionisSideEffectFree
, we could leverage this to decide whether to delete the whole statement or not.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.
Another question, how does this apply to functions. I see in this change you are only handling variable statements..
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.
But that's exactly what we do for
var x = foo()
... I think it's better to assume that any side effects are unwanted, since the function was being called for a result before and not merely for a side effect -- the situation is similar to #17624.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.
mind filing a bug for removing nodes with possible side-effects
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.
#23970