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

@babel/parser does not parse non-null members named 'readonly' in TypeScript #9571

Closed
elevatebart opened this issue Feb 24, 2019 · 4 comments · Fixed by #9572
Closed

@babel/parser does not parse non-null members named 'readonly' in TypeScript #9571

elevatebart opened this issue Feb 24, 2019 · 4 comments · Fixed by #9572
Labels
Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser

Comments

@elevatebart
Copy link
Contributor

elevatebart commented Feb 24, 2019

Bug Report

Current Behavior
When I use @pabel/parser with the following code:

class MyComp {
          readonly!: boolean;
}

I get SyntaxError: Unexpected Token

Expected behavior/code
I expect readonly to be the name of my property

Babel Configuration (.babelrc, package.json, cli command)

import { parse } from '@babel/parser'
const src = 'code above'

parse(src, {
  sourceType: 'module',
  strictMode: false,
  tokens: true,
  plugins:['typescript', 'classProperties']
})

Environment

  • Babel version(s): v7.3.3
  • Node/npm version: Node 10/npm 6
  • OS: OSX 10.13.4
  • Monorepo: no
  • How you are using Babel: none, I am using only the parser

Additional context/Screenshots
This was found investgating this bug.
vue-styleguidist/vue-styleguidist#281

@babel-bot
Copy link
Collaborator

Hey @elevatebart! 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.

@elevatebart
Copy link
Contributor Author

I forgot to add the call stack

at Object.raise (node_modules/@babel/parser/lib/index.js:3831:17)
      at Object.unexpected (node_modules/@babel/parser/lib/index.js:5143:16)
      at Object.parseIdentifierName (node_modules/@babel/parser/lib/index.js:6970:18)
      at Object.parseIdentifier (node_modules/@babel/parser/lib/index.js:6948:23)
      at Object.parseMaybePrivateName (node_modules/@babel/parser/lib/index.js:6312:19)
      at Object.parsePropertyName (node_modules/@babel/parser/lib/index.js:6768:98)
      at Object.parseClassPropertyName (node_modules/@babel/parser/lib/index.js:8173:22)
      at Object.parseClassMemberWithIsStatic (node_modules/@babel/parser/lib/index.js:10175:12)
      at Object.parseClassMember (node_modules/@babel/parser/lib/index.js:8067:10)
      at Object.parseClassMember (node_modules/@babel/parser/lib/index.js:10138:11)
      at withTopicForbiddingContext (node_modules/@babel/parser/lib/index.js:8022:14)
      at Object.withTopicForbiddingContext (node_modules/@babel/parser/lib/index.js:7150:14)
      at Object.parseClassBody (node_modules/@babel/parser/lib/index.js:7999:10)
      at Object.parseClass (node_modules/@babel/parser/lib/index.js:7973:10)
      at Object.parseExportDefaultExpression (node_modules/@babel/parser/lib/index.js:8394:19)
      at Object.parseExportDefaultExpression (node_modules/@babel/parser/lib/index.js:10113:18)
      at Object.parseExport (node_modules/@babel/parser/lib/index.js:8299:31)
      at Object.parseExport (node_modules/@babel/parser/lib/index.js:10091:20)
      at Object.parseStatementContent (node_modules/@babel/parser/lib/index.js:7347:27)
      at Object.parseStatementContent (node_modules/@babel/parser/lib/index.js:10128:18)
      at Object.parseStatement (node_modules/@babel/parser/lib/index.js:7243:17)
      at Object.parseBlockOrModuleBlockBody (node_modules/@babel/parser/lib/index.js:7810:25)
      at Object.parseBlockBody (node_modules/@babel/parser/lib/index.js:7797:10)
      at Object.parseTopLevel (node_modules/@babel/parser/lib/index.js:7181:10)
      at Object.parse (node_modules/@babel/parser/lib/index.js:8658:17)
      at Object.parse (node_modules/@babel/parser/lib/index.js:10658:38)

I hope it helps

@elevatebart
Copy link
Contributor Author

I managed to track the bug back to this function parseClassMemberWithIsStatic

parseClassMemberWithIsStatic(

It parses out the modifier before having a token to modify.
Should I try preventing this in a PR ?

@elevatebart
Copy link
Contributor Author

Actually, I would say that the bang should be added to this list

tsNextTokenCanFollowModifier() {
// Note: TypeScript's implementation is much more complicated because
// more things are considered modifiers there.
// This implementation only handles modifiers not handled by @babel/parser itself. And "static".
// TODO: Would be nice to avoid lookahead. Want a hasLineBreakUpNext() method...
this.next();
return (
!this.hasPrecedingLineBreak() &&
!this.match(tt.parenL) &&
!this.match(tt.parenR) &&
!this.match(tt.colon) &&
!this.match(tt.eq) &&
!this.match(tt.question)
);
}

@elevatebart elevatebart changed the title @babel/parser does not parse members named 'readonly' in TypeScript @babel/parser does not parse unitialized members named 'readonly!' in TypeScript Feb 24, 2019
@elevatebart elevatebart changed the title @babel/parser does not parse unitialized members named 'readonly!' in TypeScript @babel/parser does not parse no-null members named 'readonly' in TypeScript Feb 24, 2019
@elevatebart elevatebart changed the title @babel/parser does not parse no-null members named 'readonly' in TypeScript @babel/parser does not parse non-null members named 'readonly' in TypeScript Feb 24, 2019
nicolo-ribaudo pushed a commit that referenced this issue Feb 24, 2019
* Fix TypeScript parsers missing token check (#9571)

* fix unit test
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 26, 2019
@lock lock bot locked as resolved and limited conversation to collaborators May 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Has PR 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.

3 participants