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

Tokens before template literals are sometimes missing #293

Closed
not-an-aardvark opened this issue Sep 17, 2016 · 2 comments
Closed

Tokens before template literals are sometimes missing #293

not-an-aardvark opened this issue Sep 17, 2016 · 2 comments

Comments

@not-an-aardvark
Copy link
Member

Example code:

if (foo) { bar }
`template literal`;
// using espree 3.2.0
require('espree').parse('if (foo) { bar }\n`template literal`;', {ecmaVersion: 6, tokens: true}).tokens

// output:
[ Token { type: 'Keyword', value: 'if', start: 0, end: 2 },
  Token { type: 'Punctuator', value: '(', start: 3, end: 4 },
  Token { type: 'Identifier', value: 'foo', start: 4, end: 7 },
  Token { type: 'Punctuator', value: ')', start: 7, end: 8 },
  Token { type: 'Punctuator', value: '{', start: 9, end: 10 },
  Token { type: 'Identifier', value: 'bar', start: 11, end: 14 },
  { type: 'Template', value: '`template literal`' },
  Token { type: 'Punctuator', value: ';', start: 35, end: 36 } ]

Note that the closing } for the if statement is missing in the list of tokens.

This does not appear to be a problem when using acorn directly:

// using acorn 4.0.3
var tokens = [];
require('acorn').parse('if (foo) { bar }\n`template literal`;', {onToken: tokens});
tokens

// output:

[
  // ... other tokens
  Token {
    type:
     TokenType {
       label: '}',
       keyword: undefined,
       beforeExpr: false,
       startsExpr: false,
       isLoop: false,
       isAssign: false,
       prefix: false,
       postfix: false,
       binop: null,
       updateContext: [Function] },
    value: undefined,
    start: 15,
    end: 16 },
  // ... other tokens
]
@nzakas
Copy link
Member

nzakas commented Sep 19, 2016

Yeah, that's our bug. Acorn handles templates very different from Esprima, so we do some magic to get the correct output.

@mysticatea
Copy link
Member

I'm working on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants