Skip to content

Add a leading space on binary operator token trailing comments#17691

Merged
weswigham merged 2 commits into
microsoft:masterfrom
weswigham:add-space-before-comment
Aug 9, 2017
Merged

Add a leading space on binary operator token trailing comments#17691
weswigham merged 2 commits into
microsoft:masterfrom
weswigham:add-space-before-comment

Conversation

@weswigham
Copy link
Copy Markdown
Member

Looking through our RWC results, I realized that we could be a bit better in how we emitted comments on binary expressions (from #16584), thanks to work done in #17557.

Copy link
Copy Markdown
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

Looks good. The comment you added seems wrong though.

Comment thread src/compiler/emitter.ts Outdated
emitLeadingCommentsOfPosition(node.operatorToken.pos);
writeTokenNode(node.operatorToken);
emitTrailingCommentsOfPosition(node.operatorToken.end);
emitTrailingCommentsOfPosition(node.operatorToken.end, /*prefixSpace*/ true); // Comma should have a space after it
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comma? This is true for all binary operators, right?

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.

Yep, that comment's a bit too specific. I'll correct it.

var c = 'some'
/* comment */
+/*comment1*/
+ /*comment1*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks incorrect.

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.

This output matches the input now.

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.

And no longer has trailing whitespace.

1
// before
>>>// after
>>> // after
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We shouldn't emit a space if the comment has a leading newline

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.

This comment doesn't have a leading newline, though? And the output for this test now exactly matches the input.

@weswigham
Copy link
Copy Markdown
Member Author

weswigham commented Aug 9, 2017

@rbuckton In all cases, both in our test suite and in our RWC suite, just having there be a space before the trailing comment of a binary operator instead of after always matches the original comment spacing-presence. It's likely just because not many people write code like var a=/*comment*/ foo; or (2+/*comment*/2), but we don't preserve whitespace verbatim in many (any?) of these places anyway. This is just a much more common default.

@rbuckton
Copy link
Copy Markdown
Contributor

rbuckton commented Aug 9, 2017

You are correct. GitHub's diff didn't give enough apparent context and the + and >>> looked like custom diff emit from one of our test artifacts. These do look correct. 👍

@weswigham weswigham merged commit a6f37f5 into microsoft:master Aug 9, 2017
uniqueiniquity pushed a commit to uniqueiniquity/TypeScript that referenced this pull request Aug 9, 2017
…soft#17691)

* Add a leading space on token trailing comments

* Demystify comment
@weswigham weswigham deleted the add-space-before-comment branch August 17, 2017 23:05
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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.

4 participants