-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
fixUnusedIdentifier: Handle destructure with all bindings unused #23805
Conversation
const message = isTypeDeclaration(declaration) ? Diagnostics._0_is_declared_but_never_used : Diagnostics._0_is_declared_but_its_value_is_never_read; | ||
addDiagnostic(UnusedKind.Local, createDiagnosticForNodeSpan(getSourceFileOfNode(declaration), declaration, node, message, name)); | ||
} | ||
const message = isTypeDeclaration(declaration) ? Diagnostics._0_is_declared_but_never_used : Diagnostics._0_is_declared_but_its_value_is_never_read; |
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:
declare var o: any;
var { x, y } = o;
y;
var { a, b } = o;
a;
still convert to:
declare var o: any;
var {, y } = o;
y;
var { a, } = o;
a;
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()
if foo
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 function isSideEffectFree
, 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.
for instance var { x } = foo() if foo had side effects, then deleting it would be wrong..
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.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
add a test for for(var {} of o) ;
0afb217
to
18effd2
Compare
Fixes one of the issues from #22330. Similar to #22386.