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 error caused by templates in ConditionalExpressions (jsx-indent) #1077

Merged

Conversation

iancmyers
Copy link
Contributor

Template strings are constructed differently from other tokens in espree. Other tokens are created by the Token class and are thus Token class objects. They look like this:

Token {
  type: 'String',
  value: '"bar"',
  start: 44,
  end: 49,
  loc:
   SourceLocation {
     start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }

Template tokens, however, are constructed differently and end up as plain JavaScript objects, which look like this:

{ type: 'Template',
  value: '`bar`',
  loc:
   { start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }

See: espree's token-translator.js

As a result of this different construction, the start and end properties are not present on the template token object. To correct this I've changed to using the range property, which I infer to be more proper given the method it is being passed into is called getNodeByRangeIndex. I also think we can safely rely on range being present on all token types.

Fixes #1061

Template strings are constructed differently from other tokens in
espree. Other tokens are created by the Token class and are thus
Token class objects. They look like this:

```
Token {
  type: 'String',
  value: '"bar"',
  start: 44,
  end: 49,
  loc:
   SourceLocation {
     start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }
```

Template tokens, however, are constructed differently and end up as
plain JavaScript objects, which look like this:

```
{ type: 'Template',
  value: '`bar`',
  loc:
   { start: Position { line: 4, column: 11 },
     end: Position { line: 4, column: 16 } },
  range: [ 44, 49 ] }
```

See: [espree's token-translator.js](https://github.com/eslint/espree/blob/58f75be6b89d8904b6366ed368045cb02c4a4e33/lib/token-translator.js#L43)

As a result of this different construction, the `start` and `end`
properties are not present on the template token object. To correct this
I've changed to using the `range` property, which I infer to be more
proper given the method it is being passed into is called
`getNodeByRangeIndex`. I also think we can safely rely on `range` being
present on all token types.

Fixes jsx-eslint#1061
@ljharb ljharb added the bug label Feb 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants