Skip to content

Commit

Permalink
fix loc.end for JSXEmptyExpression
Browse files Browse the repository at this point in the history
ensure that it correctly handles line terminators, whether part of the
empty expression, or block comment inside it.

this is particularly important for recast or other tools that care about
comments inside jsx expressions. recast in particular will error out if
there is a loc mismatch between the comment and its corresponding node.
see benjamn/recast#175 for more context.

the AST for the newly added tests differs only in its JSXEmptyExpression
loc.end data when run against the old code:

```
1058c1058
<                             end: { line: 1, column: 6 }
---
>                             end: { line: 2, column: 1 }
1194c1194
<                             end: { line: 1, column: 38 }
---
>                             end: { line: 5, column: 11 }
```
  • Loading branch information
jenseng committed Apr 1, 2015
1 parent 7fff45a commit 1e7e673
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 4 deletions.
16 changes: 13 additions & 3 deletions esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -7008,9 +7008,19 @@
}

function parseJSXEmptyExpression() {
var marker = markerCreatePreserveWhitespace();
while (source.charAt(index) !== '}') {
index++;
var ch, marker = markerCreatePreserveWhitespace();
while (index < length) {
ch = source.charCodeAt(index);
if (ch === 125) {
break;
} else if (isLineTerminator(ch)) {
if (ch === 13 && source.charCodeAt(index + 1) === 10) {
++index;
}
++lineNumber;
lineStart = index;
}
++index;
}
return markerApply(marker, delegate.createJSXEmptyExpression());
}
Expand Down
2 changes: 2 additions & 0 deletions test/fbtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ module.exports = {
'<AbC-def\n test="&#x0026;&#38;">\nbar\nbaz\r\n</AbC-def>',
'<a b={x ? <c /> : <d />} />',
'<a>{}</a>',
'<a>{\r\n}</a>',
'<a>{/* this is a comment */}</a>',
'<a>{/* this\nis\na\nmulti-line\ncomment */}</a>',
'<div>@test content</div>',
'<div><br />7x invalid-js-identifier</div>',
'<LeftRight left=<a /> right=<b>monkeys</b> />',
Expand Down
138 changes: 137 additions & 1 deletion test/fbtest.rec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* tests/fbtest.js and run tools/generate-fbtest.js
*/

var numTests = 221
var numTests = 223
var testFixture;

var fbTestFixture = {
Expand Down Expand Up @@ -1008,6 +1008,74 @@ var fbTestFixture = {
end: { line: 1, column: 9 }
}
},
'<a>{\r\n}</a>': {
type: 'ExpressionStatement',
expression: {
type: 'JSXElement',
openingElement: {
type: 'JSXOpeningElement',
name: {
type: 'JSXIdentifier',
name: 'a',
range: [1, 2],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 2 }
}
},
selfClosing: false,
attributes: [],
range: [0, 3],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 3 }
}
},
closingElement: {
type: 'JSXClosingElement',
name: {
type: 'JSXIdentifier',
name: 'a',
range: [9, 10],
loc: {
start: { line: 2, column: 3 },
end: { line: 2, column: 4 }
}
},
range: [7, 11],
loc: {
start: { line: 2, column: 1 },
end: { line: 2, column: 5 }
}
},
children: [{
type: 'JSXExpressionContainer',
expression: {
type: 'JSXEmptyExpression',
range: [4, 6],
loc: {
start: { line: 1, column: 4 },
end: { line: 2, column: 1 }
}
},
range: [3, 7],
loc: {
start: { line: 1, column: 3 },
end: { line: 2, column: 1 }
}
}],
range: [0, 11],
loc: {
start: { line: 1, column: 0 },
end: { line: 2, column: 5 }
}
},
range: [0, 11],
loc: {
start: { line: 1, column: 0 },
end: { line: 2, column: 5 }
}
},
'<a>{/* this is a comment */}</a>': {
type: 'ExpressionStatement',
expression: {
Expand Down Expand Up @@ -1076,6 +1144,74 @@ var fbTestFixture = {
end: { line: 1, column: 32 }
}
},
'<a>{/* this\nis\na\nmulti-line\ncomment */}</a>': {
type: 'ExpressionStatement',
expression: {
type: 'JSXElement',
openingElement: {
type: 'JSXOpeningElement',
name: {
type: 'JSXIdentifier',
name: 'a',
range: [1, 2],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 2 }
}
},
selfClosing: false,
attributes: [],
range: [0, 3],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 3 }
}
},
closingElement: {
type: 'JSXClosingElement',
name: {
type: 'JSXIdentifier',
name: 'a',
range: [41, 42],
loc: {
start: { line: 5, column: 13 },
end: { line: 5, column: 14 }
}
},
range: [39, 43],
loc: {
start: { line: 5, column: 11 },
end: { line: 5, column: 15 }
}
},
children: [{
type: 'JSXExpressionContainer',
expression: {
type: 'JSXEmptyExpression',
range: [4, 38],
loc: {
start: { line: 1, column: 4 },
end: { line: 5, column: 11 }
}
},
range: [3, 39],
loc: {
start: { line: 1, column: 3 },
end: { line: 5, column: 11 }
}
}],
range: [0, 43],
loc: {
start: { line: 1, column: 0 },
end: { line: 5, column: 15 }
}
},
range: [0, 43],
loc: {
start: { line: 1, column: 0 },
end: { line: 5, column: 15 }
}
},
'<div>@test content</div>': {
type: 'ExpressionStatement',
expression: {
Expand Down

0 comments on commit 1e7e673

Please sign in to comment.