Skip to content

Commit a8f5c1e

Browse files
committed
fix loc.end info for multi-line JSXAttributeValue
on the old code, the updated test would fail since `loc.end` would contain `{ line: 1, column: 62 }` also pull new-line handling code out of jsx_readToken for easy reuse. note that we don't do any normalization of `<CR><LF>` in a JSXAttributeValue, so as to be consistent with esprima-fb's behavior.
1 parent a996c0c commit a8f5c1e

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

inject.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,7 @@ module.exports = function(acorn) {
6161
default:
6262
if (acorn.isNewLine(ch)) {
6363
out += this.input.slice(chunkStart, this.pos);
64-
++this.pos;
65-
if (ch === 13 && this.input.charCodeAt(this.pos) === 10) {
66-
++this.pos;
67-
out += '\n';
68-
} else {
69-
out += String.fromCharCode(ch);
70-
}
71-
if (this.options.locations) {
72-
++this.curLine;
73-
this.lineStart = this.pos;
74-
}
64+
out += this.jsx_readNewLine(true);
7565
chunkStart = this.pos;
7666
} else {
7767
++this.pos;
@@ -80,6 +70,24 @@ module.exports = function(acorn) {
8070
}
8171
};
8272

73+
pp.jsx_readNewLine = function(normalizeCRLF) {
74+
var ch = this.input.charCodeAt(this.pos);
75+
var out;
76+
++this.pos;
77+
if (ch === 13 && this.input.charCodeAt(this.pos) === 10) {
78+
++this.pos;
79+
out = normalizeCRLF ? '\n' : '\r\n';
80+
} else {
81+
out = String.fromCharCode(ch);
82+
}
83+
if (this.options.locations) {
84+
++this.curLine;
85+
this.lineStart = this.pos;
86+
}
87+
88+
return out;
89+
};
90+
8391
pp.jsx_readString = function(quote) {
8492
var out = '', chunkStart = ++this.pos;
8593
for (;;) {
@@ -91,6 +99,10 @@ module.exports = function(acorn) {
9199
out += this.input.slice(chunkStart, this.pos);
92100
out += this.jsx_readEntity();
93101
chunkStart = this.pos;
102+
} else if (acorn.isNewLine(ch)) {
103+
out += this.input.slice(chunkStart, this.pos);
104+
out += this.jsx_readNewLine(false);
105+
chunkStart = this.pos;
94106
} else {
95107
++this.pos;
96108
}

test/tests-jsx.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3607,6 +3607,10 @@ var fbTestFixture = {
36073607
},
36083608
value: {
36093609
type: "Literal",
3610+
loc: {
3611+
start: { line: 1, column: 8 },
3612+
end: { line: 3, column: 15 }
3613+
},
36103614
range: [8, 62],
36113615
value: "M230 80\n\t\tA 45 45, 0, 1, 0, 275 125 \r\n L 275 80 Z",
36123616
raw: "\"M230 80\n\t\tA 45 45, 0, 1, 0, 275 125 \r\n L 275 80 Z\""

0 commit comments

Comments
 (0)