Skip to content

Fix start position of intersection/union node#31017

Closed
dhcmrlchtdj wants to merge 1 commit into
microsoft:masterfrom
dhcmrlchtdj:fix-union-position
Closed

Fix start position of intersection/union node#31017
dhcmrlchtdj wants to merge 1 commit into
microsoft:masterfrom
dhcmrlchtdj:fix-union-position

Conversation

@dhcmrlchtdj
Copy link
Copy Markdown

Fixes #30995

  • I am not sure how to write a baseline test for startPos, do we have docs or examples?
  • parseOptionalToken use scanner.getStartPos() as start position. What about use scanner.getTokenPos() instead?

Comment thread src/compiler/parser.ts
parseOptional(operator);
const leadingToken = parseOptionalToken(operator);
let type = parseConstituentType();
const startPos = leadingToken ? leadingToken.pos : type.pos;
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.

Can you add a test for this output. It seems like this change would result in missing comments:

type A = 
  /*leading0*/
  | /*leading1*/ 1 /*trailing1*/ 
  | /*leading2*/ 2 /*trailing2*/;

When we emit this declaration today, we include the leading and trailing comments for each type in the union, but do not emit /*leading0*/. I would like to see the test verify that we still preserve the comments around each type and whether this will now also include the leading comment before the first |.

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.

Also, I would move this line inside of the if statement that follows it? There is no need to determine startPos if we end up not using it.

Comment thread src/compiler/parser.ts
}
const node = <UnionOrIntersectionTypeNode>createNode(kind, type.pos);
const node = <UnionOrIntersectionTypeNode>createNode(kind, startPos);
node.types = createNodeArray(types, type.pos);
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.

should the NodeArray also include the leading operator? currently only the Union/IntersectionType contains the operator

@ajafff
Copy link
Copy Markdown
Contributor

ajafff commented May 7, 2019

  • I am not sure how to write a baseline test for startPos, do we have docs or examples?

Have a look at my attempt: #31265

For the test @rbuckton requested:

  • add a file to tests/cases/compiler with a comment // @declaration: true at the top and the desired content after that.
  • run gulp runtests-parallel (will fail)
  • run gulp baseline-accept (and verify the diff) now all tests should pass
  • parseOptionalToken use scanner.getStartPos() as start position. What about use scanner.getTokenPos() instead?

In my PR (linked above) I used scanner.getStartPos() to avoid creating a Token object that is never really used.

@mysticatea
Copy link
Copy Markdown

Hi. There is a discussion about how this should be fixed on the original issue #30995. Especially, this PR leaves the leading operator as floating if the second operator didn't exist.

@dhcmrlchtdj
Copy link
Copy Markdown
Author

#31265 by @ajafff is a better solution.
So I will close this PR.

Thanks for your review. @rbuckton @ajafff

@dhcmrlchtdj dhcmrlchtdj closed this May 8, 2019
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
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.

Leading |/& is not included in the intersection/union node

4 participants