Skip to content

Special case arrow functions with only parameter unused - #21144

Merged
Andrew Casey (amcasey) merged 4 commits into
microsoft:masterfrom
amcasey:GH18274
Jan 17, 2018
Merged

Andrew Casey (amcasey) merged 4 commits into
microsoft:masterfrom
amcasey:GH18274

Conversation

@amcasey

Copy link
Copy Markdown
Member

Fixes #18274

I've made some notes in the regression tests where the trivia handling isn't perfect, but none of the issues seemed impactful enough to justify deeper changes.

oldFunction.equalsGreaterThanToken,
oldFunction.body);

suppressLeadingAndTrailingTrivia(newFunction);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you add a comment why this is done?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I see the comment above newFunction, but I was talking about the line suppressLeadingAndTrailingTrivia.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added


suppressLeadingAndTrailingTrivia(newFunction);

changes.replaceRange(sourceFile, { pos: oldFunction.getStart(), end: oldFunction.end }, newFunction);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could this use replaceNode?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Possibly we need to add another method to ChangeTracker, because I don't believe replaceNode is suitable for (nearly?) any code fix or refactoring - it clobbers the leading trivia of the replaced node.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hm, replaceNode starts with node.getStart() but then adjusts it -- maybe there's a bug in getAdjustedStartPosition, because it should be replacing a range that doesn't include leading trivia.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I understood the adjusted start position was an attempt to establish the boundary between the trailing trivia of the preceding token and the leading trivia of the current token.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Well, that's a problem if we want to preserve the leading trivia of the current token -- maybe all the uses of replaceNode have this bug and replaceNode should be fixed to just use getStart().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, every caller of replaceNode probably needs to be reviewed. Few of the existing tests include interesting trivia.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

OK, I've created an issue for this at #21246. This PR should be good to go for now.


// @noUnusedLocals: true
// @noUnusedParameters: true
//// function f1() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is the outer function needed? Could we reproduce this with just x => {}?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I just copied the existing test, but I can remove it, if you prefer.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yeah, would be better to simplify the other test too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

@amcasey
Andrew Casey (amcasey) merged commit 095aa77 into microsoft:master Jan 17, 2018
@amcasey
Andrew Casey (amcasey) deleted the GH18274 branch January 17, 2018 23:45
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jul 3, 2018
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