Skip to content

Commit a08f0ab

Browse files
authored
[gjs-gts-parser] fix parsing when there are multiple default <template> blocks (not allowed) (#2005)
1 parent 90d1fc4 commit a08f0ab

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

lib/parsers/gjs-gts-parser.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ function convertAst(result, preprocessedResult, visitorKeys) {
388388
if (
389389
node.type === 'ExpressionStatement' ||
390390
node.type === 'StaticBlock' ||
391-
node.type === 'TemplateLiteral' ||
391+
node.type === 'TaggedTemplateExpression' ||
392392
node.type === 'ExportDefaultDeclaration'
393393
) {
394394
let range = node.range;
@@ -397,7 +397,9 @@ function convertAst(result, preprocessedResult, visitorKeys) {
397397
}
398398

399399
const template = templateInfos.find(
400-
(t) => t.templateRange[0] === range[0] && t.templateRange[1] === range[1]
400+
(t) =>
401+
t.templateRange[0] === range[0] &&
402+
(t.templateRange[1] === range[1] || t.templateRange[1] === range[1] + 1)
401403
);
402404
if (!template) {
403405
return null;
@@ -501,9 +503,9 @@ function transformForLint(code) {
501503
jsCode = replaceRange(jsCode, tplInfo.range.start, tplInfo.range.end, replacementCode);
502504
} else {
503505
const tplLength = tplInfo.range.end - tplInfo.range.start;
504-
const spaces = tplLength - '`'.length - '`'.length - lineBreaks;
506+
const spaces = tplLength - '""`'.length - '`'.length - lineBreaks;
505507
const total = ' '.repeat(spaces) + '\n'.repeat(lineBreaks);
506-
const replacementCode = `\`${total}\``;
508+
const replacementCode = `""\`${total}\``;
507509
jsCode = replaceRange(jsCode, tplInfo.range.start, tplInfo.range.end, replacementCode);
508510
}
509511
}

tests/lib/rules-preprocessor/gjs-gts-parser-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ const valid = [
9191
<template>
9292
<div {{on 'click' noop}} />
9393
</template>
94+
95+
<template>
96+
<div {{on 'click' noop}} />
97+
</template>
9498
`,
9599
},
96100
{

0 commit comments

Comments
 (0)