Skip to content

Commit

Permalink
fix when bad templates
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Nov 15, 2023
1 parent 90d1fc4 commit 72e3889
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/parsers/gjs-gts-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ function convertAst(result, preprocessedResult, visitorKeys) {
if (
node.type === 'ExpressionStatement' ||
node.type === 'StaticBlock' ||
node.type === 'TemplateLiteral' ||
node.type === 'TaggedTemplateExpression' ||
node.type === 'ExportDefaultDeclaration'
) {
let range = node.range;
Expand All @@ -397,7 +397,9 @@ function convertAst(result, preprocessedResult, visitorKeys) {
}

const template = templateInfos.find(
(t) => t.templateRange[0] === range[0] && t.templateRange[1] === range[1]
(t) =>
t.templateRange[0] === range[0] &&
(t.templateRange[1] === range[1] || t.templateRange[1] === range[1] + 1)
);
if (!template) {
return null;
Expand Down Expand Up @@ -501,9 +503,9 @@ function transformForLint(code) {
jsCode = replaceRange(jsCode, tplInfo.range.start, tplInfo.range.end, replacementCode);
} else {
const tplLength = tplInfo.range.end - tplInfo.range.start;
const spaces = tplLength - '`'.length - '`'.length - lineBreaks;
const spaces = tplLength - '""`'.length - '`'.length - lineBreaks;
const total = ' '.repeat(spaces) + '\n'.repeat(lineBreaks);
const replacementCode = `\`${total}\``;
const replacementCode = `""\`${total}\``;
jsCode = replaceRange(jsCode, tplInfo.range.start, tplInfo.range.end, replacementCode);
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules-preprocessor/gjs-gts-parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ const valid = [
<template>
<div {{on 'click' noop}} />
</template>
<template>
<div {{on 'click' noop}} />
</template>
`,
},
{
Expand Down

0 comments on commit 72e3889

Please sign in to comment.