Skip to content

Commit

Permalink
fix: parse errors with JS comments at end of expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
asyncLiz committed Feb 12, 2021
1 parent 355c41d commit 3bf9053
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/strategies/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ export default <TypescriptStrategy>{
};
},
getMiddleTailTemplatePart(node: ts.TemplateMiddle | ts.TemplateTail) {
const fullText = node.getFullText(currentRoot);
// Use text, not fullText, to avoid prefix comments, which are part of the
// expression.
const text = node.getText(currentRoot);
const endOffset = ts.isTemplateMiddle(node) ? 2 : 1;
return {
text: fullText.slice(1, fullText.length - endOffset),
start: node.pos + 1,
text: text.slice(1, text.length - endOffset),
// Use getStart() and not node.pos, which may include prefix comments,
// which are part of the expression
start: node.getStart(currentRoot) + 1,
end: node.end - endOffset
};
}
Expand Down
25 changes: 25 additions & 0 deletions test/parse-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,29 @@ export default function createParseTests(options: ParseTestsOptions = {}) {
}
]);
});

it('should parse literals with comments in template tail', () => {
expect(
parseLiterals(
options.codePrefix +
'`head${true/* tail comment */}tail`' +
options.codeSuffix
)
).to.deep.equal([
{
parts: [
{
text: 'head',
start: 1 + offset,
end: 5 + offset
},
{
text: 'tail',
start: 30 + offset,
end: 34 + offset
}
]
}
]);
});
}

0 comments on commit 3bf9053

Please sign in to comment.