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

Template string tokens are strange #44

Closed
nzakas opened this issue Jan 31, 2015 · 3 comments
Closed

Template string tokens are strange #44

nzakas opened this issue Jan 31, 2015 · 3 comments

Comments

@nzakas
Copy link
Member

nzakas commented Jan 31, 2015

The way that template string tokens are generated is pretty strange. This code:

var foo = `hi${bar}`;

Is tokenized as:

[
    {
        "type": "Keyword",
        "value": "var"
    },
    {
        "type": "Identifier",
        "value": "foo"
    },
    {
        "type": "Punctuator",
        "value": "="
    },
    {
        "value": "`hi${"
    },
    {
        "type": "Identifier",
        "value": "bar"
    },
    {
        "type": "Punctuator",
        "value": "}"
    },
    {
        "type": "Punctuator",
        "value": ";"
    }
]

So the first part of the template string is represented by a token without a type, and then the rest are just regular tokens, completely losing the end of the template string.

@nzakas
Copy link
Member Author

nzakas commented Jan 31, 2015

Working on this.

@nzakas nzakas closed this as completed in f607433 Feb 1, 2015
nzakas added a commit that referenced this issue Feb 1, 2015
Fix: Tokenization for template strings (fixes #44)
@nzakas nzakas reopened this Feb 7, 2015
@nzakas
Copy link
Member Author

nzakas commented Feb 7, 2015

Not quite fixed. The code var a; console.log(${a}, \"a\"); still outputs this:

[ { type: 'Identifier',
    value: 'console',
    range: [ 7, 14 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: '.',
    range: [ 14, 15 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Identifier',
    value: 'log',
    range: [ 15, 18 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: '(',
    range: [ 18, 19 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Template',
    value: '`${',
    range: [ 19, 22 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Identifier',
    value: 'a',
    range: [ 22, 23 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: '}',
    range: [ 23, 24 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: ',',
    range: [ 25, 26 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'String',
    value: '"a"',
    range: [ 27, 30 ],
    loc: { start: [Object], end: [Object] } },
  { type: 'Punctuator',
    value: ')',
    range: [ 30, 31 ],
    loc: { start: [Object], end: [Object] } } ]

@nzakas
Copy link
Member Author

nzakas commented Feb 7, 2015

Followup: This was actually a problem with using parse(), not when using tokenize(), since they use different logic.

@nzakas nzakas closed this as completed in 4f4e524 Feb 7, 2015
nzakas added a commit that referenced this issue Feb 7, 2015
Fix: Actuall fix tokenization issue with templates (fixes #44)
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

1 participant