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

Fix start position of intersection/union node #31017

Closed
wants to merge 1 commit into from

Conversation

dhcmrlchtdj
Copy link

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?

let type = parseConstituentType();
const startPos = leadingToken ? leadingToken.pos : type.pos;
Copy link
Member

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
Member

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.

if (token() === operator) {
const types = [type];
while (parseOptional(operator)) {
types.push(parseConstituentType());
}
const node = <UnionOrIntersectionTypeNode>createNode(kind, type.pos);
const node = <UnionOrIntersectionTypeNode>createNode(kind, startPos);
node.types = createNodeArray(types, type.pos);
Copy link
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
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

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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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