Skip to content

Commit 4c1f17d

Browse files
authored
Chore: Refactor TemplateElement range fix (#489)
* Chore: Refactor `TemplateElement` range fix * Chore: Move code inline * Chore: Restore unrelated change * Chore: Rename variable
1 parent 6ffd604 commit 4c1f17d

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

lib/espree.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,21 @@ export default () => Parser => {
195195
* values without affect any other part of the process.
196196
*/
197197
this[STATE].templateElements.forEach(templateElement => {
198-
const terminalDollarBraceL = this.input.slice(templateElement.end, templateElement.end + 2) === "${";
198+
const startOffset = -1;
199+
const endOffset = templateElement.tail ? 1 : 2;
199200

200-
templateElement.start--;
201-
templateElement.end += (terminalDollarBraceL ? 2 : 1);
201+
templateElement.start += startOffset;
202+
templateElement.end += endOffset;
203+
204+
if (templateElement.range) {
205+
templateElement.range[0] += startOffset;
206+
templateElement.range[1] += endOffset;
207+
}
208+
209+
if (templateElement.loc) {
210+
templateElement.loc.start.column += startOffset;
211+
templateElement.loc.end.column += endOffset;
212+
}
202213
});
203214

204215
return program;
@@ -295,19 +306,6 @@ export default () => Parser => {
295306
// so we have to adjust ranges/locations appropriately.
296307
if (result.type === "TemplateElement") {
297308

298-
// additional adjustment needed if ${ is the last token
299-
const terminalDollarBraceL = this.input.slice(result.end, result.end + 2) === "${";
300-
301-
if (result.range) {
302-
result.range[0]--;
303-
result.range[1] += (terminalDollarBraceL ? 2 : 1);
304-
}
305-
306-
if (result.loc) {
307-
result.loc.start.column--;
308-
result.loc.end.column += (terminalDollarBraceL ? 2 : 1);
309-
}
310-
311309
// save template element references to fix start/end later
312310
this[STATE].templateElements.push(result);
313311
}

0 commit comments

Comments
 (0)