diff --git a/src/strategies/typescript.ts b/src/strategies/typescript.ts index af77722..f0aa357 100644 --- a/src/strategies/typescript.ts +++ b/src/strategies/typescript.ts @@ -61,11 +61,15 @@ export default { }; }, 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 }; } diff --git a/test/parse-tests.ts b/test/parse-tests.ts index fec4d3c..926c991 100644 --- a/test/parse-tests.ts +++ b/test/parse-tests.ts @@ -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 + } + ] + } + ]); + }); }