Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gjs-gts-parser] fix parsing when there are multiple default <template> blocks (not allowed) #2005

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}} />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmish this is the bad code -- can't have two default templates

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmish it would have failed to parse it without this change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide some context about the actual fix too? What was wrong before and how does your change fix it?

Copy link
Contributor Author

@patricklx patricklx Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before i would have converted the template parts to template string literals.

`
Tpl1
`
`
Tpl2
`

But that makes it one tagged template literal... Instead of 2 separates.
Now i make them tagged already.

""
`
Tpl1
`
""
`
Tpl2
`

I use string as tag name, as otherwise eslint would complain about missing references to identifiers

</template>
`,
},
{
Expand Down