Skip to content

Commit c6f4a5e

Browse files
committed
Fix error caused by templates in ConditionalExpressions (jsx-indent)
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
1 parent a4b6a85 commit c6f4a5e

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/rules/jsx-indent.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ module.exports = {
275275
do {
276276
prevToken = sourceCode.getTokenBefore(prevToken);
277277
} while (prevToken.type === 'Punctuator');
278-
prevToken = sourceCode.getNodeByRangeIndex(prevToken.start);
278+
prevToken = sourceCode.getNodeByRangeIndex(prevToken.range[0]);
279+
279280
while (prevToken.parent && prevToken.parent.type !== 'ConditionalExpression') {
280281
prevToken = prevToken.parent;
281282
}

tests/lib/rules/jsx-indent.js

+41
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,47 @@ ruleTester.run('jsx-indent', rule, {
410410
].join('\n'),
411411
options: [4, {indentLogicalExpressions: true}],
412412
parserOptions: parserOptions
413+
}, {
414+
code: [
415+
'<span>',
416+
' {condition ?',
417+
' <Thing',
418+
' foo={`bar`}',
419+
' /> :',
420+
' <Thing/>',
421+
' }',
422+
'</span>'
423+
].join('\n'),
424+
options: [2],
425+
parserOptions: parserOptions
426+
}, {
427+
code: [
428+
'<span>',
429+
' {condition ?',
430+
' <Thing',
431+
' foo={"bar"}',
432+
' /> :',
433+
' <Thing/>',
434+
' }',
435+
'</span>'
436+
].join('\n'),
437+
options: [2],
438+
parserOptions: parserOptions
439+
}, {
440+
code: [
441+
'function foo() {',
442+
' <span>',
443+
' {condition ?',
444+
' <Thing',
445+
' foo={super}',
446+
' /> :',
447+
' <Thing/>',
448+
' }',
449+
' </span>',
450+
'}'
451+
].join('\n'),
452+
options: [2],
453+
parserOptions: parserOptions
413454
}],
414455

415456
invalid: [{

0 commit comments

Comments
 (0)