Skip to content

Commit

Permalink
Chore: Refactor TemplateElement range fix (#489)
Browse files Browse the repository at this point in the history
* Chore: Refactor `TemplateElement` range fix

* Chore: Move code inline

* Chore: Restore unrelated change

* Chore: Rename variable
  • Loading branch information
fisker committed May 3, 2021
1 parent 6ffd604 commit 4c1f17d
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions lib/espree.js
Expand Up @@ -195,10 +195,21 @@ export default () => Parser => {
* values without affect any other part of the process.
*/
this[STATE].templateElements.forEach(templateElement => {
const terminalDollarBraceL = this.input.slice(templateElement.end, templateElement.end + 2) === "${";
const startOffset = -1;
const endOffset = templateElement.tail ? 1 : 2;

templateElement.start--;
templateElement.end += (terminalDollarBraceL ? 2 : 1);
templateElement.start += startOffset;
templateElement.end += endOffset;

if (templateElement.range) {
templateElement.range[0] += startOffset;
templateElement.range[1] += endOffset;
}

if (templateElement.loc) {
templateElement.loc.start.column += startOffset;
templateElement.loc.end.column += endOffset;
}
});

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

// additional adjustment needed if ${ is the last token
const terminalDollarBraceL = this.input.slice(result.end, result.end + 2) === "${";

if (result.range) {
result.range[0]--;
result.range[1] += (terminalDollarBraceL ? 2 : 1);
}

if (result.loc) {
result.loc.start.column--;
result.loc.end.column += (terminalDollarBraceL ? 2 : 1);
}

// save template element references to fix start/end later
this[STATE].templateElements.push(result);
}
Expand Down

0 comments on commit 4c1f17d

Please sign in to comment.