Skip to content

Commit 8819aa1

Browse files
committed
fix: handle class/function identifier names inside template literal
Fixes sveltejs/svelte#13188
1 parent f184d66 commit 8819aa1

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

.changeset/stupid-cats-care.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/acorn-typescript': patch
3+
---
4+
5+
fix: handle class/function identifier names inside template literal

__test__/__snapshot__/expression/variables.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5336,6 +5336,118 @@ const VariablesTypeSnapshot = {
53365336
}
53375337
],
53385338
sourceType: 'module'
5339+
},
5340+
TemplateLiteralClass: {
5341+
type: 'Program',
5342+
start: 0,
5343+
end: 28,
5344+
loc: { start: { line: 1, column: 0, index: 0 }, end: { line: 1, column: 28, index: 28 } },
5345+
body: [
5346+
{
5347+
type: 'VariableDeclaration',
5348+
start: 0,
5349+
end: 28,
5350+
loc: { start: { line: 1, column: 0, index: 0 }, end: { line: 1, column: 28, index: 28 } },
5351+
declarations: [
5352+
{
5353+
type: 'VariableDeclarator',
5354+
start: 6,
5355+
end: 27,
5356+
loc: {
5357+
start: { line: 1, column: 6, index: 6 },
5358+
end: { line: 1, column: 27, index: 27 }
5359+
},
5360+
id: {
5361+
type: 'Identifier',
5362+
start: 6,
5363+
end: 9,
5364+
loc: {
5365+
start: { line: 1, column: 6, index: 6 },
5366+
end: { line: 1, column: 9, index: 9 }
5367+
},
5368+
name: 'str'
5369+
},
5370+
init: {
5371+
type: 'TemplateLiteral',
5372+
start: 12,
5373+
end: 27,
5374+
loc: {
5375+
start: { line: 1, column: 12, index: 12 },
5376+
end: { line: 1, column: 27, index: 27 }
5377+
},
5378+
expressions: [
5379+
{
5380+
type: 'ChainExpression',
5381+
start: 15,
5382+
end: 25,
5383+
loc: {
5384+
start: { line: 1, column: 15, index: 15 },
5385+
end: { line: 1, column: 25, index: 25 }
5386+
},
5387+
expression: {
5388+
type: 'MemberExpression',
5389+
start: 15,
5390+
end: 25,
5391+
loc: {
5392+
start: { line: 1, column: 15, index: 15 },
5393+
end: { line: 1, column: 25, index: 25 }
5394+
},
5395+
object: {
5396+
type: 'Identifier',
5397+
start: 15,
5398+
end: 18,
5399+
loc: {
5400+
start: { line: 1, column: 15, index: 15 },
5401+
end: { line: 1, column: 18, index: 18 }
5402+
},
5403+
name: 'obj'
5404+
},
5405+
property: {
5406+
type: 'Identifier',
5407+
start: 20,
5408+
end: 25,
5409+
loc: {
5410+
start: { line: 1, column: 20, index: 20 },
5411+
end: { line: 1, column: 25, index: 25 }
5412+
},
5413+
name: 'class'
5414+
},
5415+
computed: false,
5416+
optional: true
5417+
}
5418+
}
5419+
],
5420+
quasis: [
5421+
{
5422+
type: 'TemplateElement',
5423+
start: 13,
5424+
end: 13,
5425+
loc: {
5426+
start: { line: 1, column: 13, index: 13 },
5427+
end: { line: 1, column: 13, index: 13 }
5428+
},
5429+
value: { raw: '', cooked: '' },
5430+
tail: false
5431+
},
5432+
{
5433+
type: 'TemplateElement',
5434+
start: 26,
5435+
end: 26,
5436+
loc: {
5437+
start: { line: 1, column: 26, index: 26 },
5438+
end: { line: 1, column: 26, index: 26 }
5439+
},
5440+
value: { raw: '', cooked: '' },
5441+
tail: true
5442+
}
5443+
]
5444+
}
5445+
}
5446+
],
5447+
kind: 'const'
5448+
}
5449+
],
5450+
sourceType: 'module'
53395451
}
53405452
};
53415453

__test__/expression/variables.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,10 @@ describe('variables declaration', () => {
195195

196196
equalNode(node, VariablesTypeSnapshot.NonNullAssignment);
197197
});
198+
199+
it('identifier that could be keyword inside template literal', () => {
200+
const node = parseSource(generateSource(['const str = `${obj?.class}`;']));
201+
202+
equalNode(node, VariablesTypeSnapshot.TemplateLiteralClass);
203+
});
198204
});

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3337,7 +3337,15 @@ export function tsPlugin(options?: {
33373337

33383338
parseIdentNode() {
33393339
let node = this.startNode();
3340-
if (tokenIsKeywordOrIdentifier(this.type)) {
3340+
if (
3341+
tokenIsKeywordOrIdentifier(this.type) &&
3342+
// Taken from super-class method
3343+
!(
3344+
(this.type.keyword === 'class' || this.type.keyword === 'function') &&
3345+
(this.lastTokEnd !== this.lastTokStart + 1 ||
3346+
this.input.charCodeAt(this.lastTokStart) !== 46)
3347+
)
3348+
) {
33413349
node.name = this.value;
33423350
} else {
33433351
return super.parseIdentNode();

0 commit comments

Comments
 (0)