Skip to content

Commit

Permalink
Fix: Make template token objects adhere to token object structure (#343)
Browse files Browse the repository at this point in the history
While fixing an issue in `eslint-plugin-react`
(jsx-eslint/eslint-plugin-react#1061),
I noticed that template tokens differ from other tokens. A typical token
looks 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 ] }
```

A template token looks like this:

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

I've not be able to figure out why templates are plain JavaScript
objects and not of the class type `Token` (aside from the fact that the
template tokens are constructed differently in the token translator.

This fix copies the `range` values into `start` and `end` properties on
the template tokens to make them adhere to the same structure as other
token objects.

Fixes #319
  • Loading branch information
iancmyers authored and not-an-aardvark committed Jul 10, 2017
1 parent 9ac671a commit d4bdcb6
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/token-translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function convertTemplatePart(tokens, code) {
}

if (firstToken.range) {
token.range = [firstToken.range[0], lastTemplateToken.range[1]];
token.start = firstToken.range[0];
token.end = lastTemplateToken.range[1];
token.range = [token.start, token.end];
}

return token;
Expand Down

0 comments on commit d4bdcb6

Please sign in to comment.