From 3805aeaaf974667dc493919c9e940865d7916e83 Mon Sep 17 00:00:00 2001 From: Ian Christian Myers Date: Sun, 9 Jul 2017 18:31:55 -0700 Subject: [PATCH] Make template token objects adhere token object structure (#343) While fixing an issue in `eslint-plugin-react` (https://github.com/yannickcr/eslint-plugin-react/issues/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 --- lib/token-translator.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/token-translator.js b/lib/token-translator.js index 3921ac78..f7e3dbf1 100644 --- a/lib/token-translator.js +++ b/lib/token-translator.js @@ -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;