Skip to content
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

Merged
5 commits merged into from
May 8, 2018

Conversation

ghost
Copy link

@ghost ghost commented May 1, 2018

Fixes one of the issues from #22330. Similar to #22386.

@ghost ghost requested review from mhegazy, sheetalkamat and amcasey May 1, 2018 18:52
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;
Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate.

Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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..

Copy link
Author

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.

Copy link
Contributor

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

Copy link
Author

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 {
Copy link
Contributor

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) ;

@ghost ghost force-pushed the codeFixUnusedIdentifier_destructure branch from 0afb217 to 18effd2 Compare May 8, 2018 18:29
@ghost ghost merged commit 5725428 into master May 8, 2018
@ghost ghost deleted the codeFixUnusedIdentifier_destructure branch May 8, 2018 20:33
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant