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

[Bug]: error parsing typescript assertion signature #13761

Closed
1 task
JonathanMEdwards opened this issue Sep 15, 2021 · 4 comments · Fixed by #13771
Closed
1 task

[Bug]: error parsing typescript assertion signature #13761

JonathanMEdwards opened this issue Sep 15, 2021 · 4 comments · Fixed by #13771
Assignees
Labels
area: typescript good first issue i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser

Comments

@JonathanMEdwards
Copy link

💻

  • Would you like to work on a fix?

How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

Input code

this doesn't parse

but this does:

function assert(condition: any): asserts condition {}

Configuration file name

No response

Configuration

No response

Current and expected behavior

parsing should match typescript

Environment

babel repl

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @JonathanMEdwards! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@fedeci
Copy link
Member

fedeci commented Sep 15, 2021

This bug comes from the tsParseTypePredicateAsserts function where we check that the asserts keyword does not have any line break before it. We should change the behaviour of that function to accept line breaks.

tsParseTypePredicateAsserts(): boolean {
if (
!this.match(tt.name) ||
this.state.value !== "asserts" ||
this.hasPrecedingLineBreak()
) {
return false;
}
const containsEsc = this.state.containsEsc;
this.next();
if (!this.match(tt.name) && !this.match(tt._this)) {
return false;
}
if (containsEsc) {
this.raise(
this.state.lastTokStart,
Errors.InvalidEscapedReservedWord,
"asserts",
);
}
return true;
}


If anyone wants to help fixing this bug and it is the first time that you contribute to Babel, follow these steps: (you need to have make and yarn available on your machine)

  1. Write a comment here to let other possible contributors know that you are working on this bug.
  2. Fork the repo
  3. Run git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
  4. Run yarn && make bootstrap
  5. Wait ⏳
  6. Run make watch (or make build whenever you change a file)
  7. Add a test (only input.ts; output.json will be automatically generated) in https://github.com/babel/babel/tree/main/packages/babel-parser/test/fixtures/typescript/assert-predicate
    • We need a test for the valid case (the example in the bug report) and for the invalid case [function].
  8. Update the code!
  9. yarn jest parser to run the tests
    • If some test outputs don't match but the new results are correct, you can delete the bad output.js files and run the tests again
    • If you prefer, you can run OVERWRITE=true yarn jest parser and they will be automatically updated.
    • New parser tests sometimes fail: don't worry, run yarn jest parser again after that
  10. If it is working, run make test to run all the tests
  11. Run git push and open a PR!

@JuniorTour
Copy link
Contributor

Can i take this?

@fedeci
Copy link
Member

fedeci commented Sep 15, 2021

Sure!

JuniorTour added a commit to JuniorTour/babel that referenced this issue Sep 16, 2021
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 17, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript good first issue i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants