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

fix parsing issue #76

Merged
merged 1 commit into from
Mar 12, 2024
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
11 changes: 6 additions & 5 deletions src/parser/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,20 +566,21 @@ module.exports.transformForLint = function transformForLint(code) {
*/
const result = processor.parse(code);
for (const tplInfo of result.reverse()) {
const backticks = [...tplInfo.contents].reduce(
(prev, curr) => (prev + (curr.codePointAt(0) === '`') ? 1 : 0),
const specialChars = [...tplInfo.contents].reduce(
(prev, curr) => (prev + ['`', '$'].includes(curr.codePointAt(0)) ? 1 : 0),
0
);
const content = tplInfo.contents.replace(/`/g, '\\`');
const content = tplInfo.contents.replace(/`/g, '\\`').replace(/\$/g, '\\$');
if (tplInfo.type === 'class-member') {
const tplLength = tplInfo.range.end - tplInfo.range.start;
const spaces = tplLength - byteLength(content) - 'static{`'.length - '`}'.length - backticks;
const spaces =
tplLength - byteLength(content) - 'static{`'.length - '`}'.length - specialChars;
const total = content + ' '.repeat(spaces);
const replacementCode = `static{\`${total}\`}`;
jsCode = replaceRange(jsCode, tplInfo.range.start, tplInfo.range.end, replacementCode);
} else {
const tplLength = tplInfo.range.end - tplInfo.range.start;
const spaces = tplLength - byteLength(content) - '`'.length - '`'.length - backticks;
const spaces = tplLength - byteLength(content) - '`'.length - '`'.length - specialChars;
const total = content + ' '.repeat(spaces);
const replacementCode = `\`${total}\``;
jsCode = replaceRange(jsCode, tplInfo.range.start, tplInfo.range.end, replacementCode);
Expand Down
220 changes: 160 additions & 60 deletions tests/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const NotFound = <template>

<br /><br /><br>
<BackToStart test=" {{foo}} {{bar}}" />
\${{this.price}}
\`
</template>`;

result = parseForESLint(text, {
Expand Down Expand Up @@ -115,6 +117,8 @@ export const NotFound = <template>

<br /><br /><br>
<BackToStart test=" {{foo}} {{bar}}" />
\${{this.price}}
\`
</template>",
"type": "Program",
}
Expand All @@ -132,6 +136,8 @@ export const NotFound = <template>

<br /><br /><br>
<BackToStart test=" {{foo}} {{bar}}" />
\${{this.price}}
\`
</template>",
"type": "ExportNamedDeclaration",
}
Expand All @@ -149,6 +155,8 @@ export const NotFound = <template>

<br /><br /><br>
<BackToStart test=" {{foo}} {{bar}}" />
\${{this.price}}
\`
</template>",
"type": "VariableDeclaration",
}
Expand All @@ -166,6 +174,8 @@ export const NotFound = <template>

<br /><br /><br>
<BackToStart test=" {{foo}} {{bar}}" />
\${{this.price}}
\`
</template>",
"type": "VariableDeclarator",
}
Expand All @@ -183,6 +193,8 @@ export const NotFound = <template>

<br /><br /><br>
<BackToStart test=" {{foo}} {{bar}}" />
\${{this.price}}
\`
</template>",
"type": "GlimmerTemplate",
}
Expand All @@ -200,6 +212,8 @@ export const NotFound = <template>

<br /><br /><br>
<BackToStart test=" {{foo}} {{bar}}" />
\${{this.price}}
\`
</template>",
"type": "GlimmerElementNode",
}
Expand All @@ -210,6 +224,33 @@ export const NotFound = <template>
"type": "GlimmerElementNodePart",
}
`);
expect({ type: nodes[i].type, slice: text.slice(...nodes[i++].range) }).toMatchInlineSnapshot(`
{
"slice": "
\`
",
"type": "GlimmerTextNode",
}
`);
expect({ type: nodes[i].type, slice: text.slice(...nodes[i++].range) }).toMatchInlineSnapshot(`
{
"slice": "{{this.price}}",
"type": "GlimmerMustacheStatement",
}
`);
expect({ type: nodes[i].type, slice: text.slice(...nodes[i++].range) }).toMatchInlineSnapshot(`
{
"slice": "this.price",
"type": "GlimmerPathExpression",
}
`);
expect({ type: nodes[i].type, slice: text.slice(...nodes[i++].range) }).toMatchInlineSnapshot(`
{
"slice": "
$",
"type": "GlimmerTextNode",
}
`);
expect({ type: nodes[i].type, slice: text.slice(...nodes[i++].range) }).toMatchInlineSnapshot(`
{
"slice": "<BackToStart test=" {{foo}} {{bar}}" />",
Expand Down Expand Up @@ -720,30 +761,6 @@ export const NotFound = <template>
"type": "Identifier",
}
`);
expect({ type: nodes[i].type, slice: text.slice(...nodes[i++].range) }).toMatchInlineSnapshot(`
{
"slice": "Link",
"type": "Identifier",
}
`);
expect({ type: nodes[i].type, slice: text.slice(...nodes[i++].range) }).toMatchInlineSnapshot(`
{
"slice": "ExternalLink",
"type": "ImportSpecifier",
}
`);
expect({ type: nodes[i].type, slice: text.slice(...nodes[i++].range) }).toMatchInlineSnapshot(`
{
"slice": "ExternalLink",
"type": "Identifier",
}
`);
expect({ type: nodes[i].type, slice: text.slice(...nodes[i++].range) }).toMatchInlineSnapshot(`
{
"slice": "ExternalLink",
"type": "Identifier",
}
`);
});

it('node tokens are correct', () => {
Expand Down Expand Up @@ -933,6 +950,18 @@ export const NotFound = <template>
""",
"/",
">",
"
$",
"{",
"{",
"this",
".",
"price",
"}",
"}",
"
\`
",
"<",
"/",
"template",
Expand Down Expand Up @@ -1005,6 +1034,18 @@ export const NotFound = <template>
""",
"/",
">",
"
$",
"{",
"{",
"this",
".",
"price",
"}",
"}",
"
\`
",
"<",
"/",
"template",
Expand Down Expand Up @@ -1076,6 +1117,18 @@ export const NotFound = <template>
""",
"/",
">",
"
$",
"{",
"{",
"this",
".",
"price",
"}",
"}",
"
\`
",
"<",
"/",
"template",
Expand Down Expand Up @@ -1146,6 +1199,18 @@ export const NotFound = <template>
""",
"/",
">",
"
$",
"{",
"{",
"this",
".",
"price",
"}",
"}",
"
\`
",
"<",
"/",
"template",
Expand Down Expand Up @@ -1214,6 +1279,18 @@ export const NotFound = <template>
""",
"/",
">",
"
$",
"{",
"{",
"this",
".",
"price",
"}",
"}",
"
\`
",
"<",
"/",
"template",
Expand Down Expand Up @@ -1282,6 +1359,18 @@ export const NotFound = <template>
""",
"/",
">",
"
$",
"{",
"{",
"this",
".",
"price",
"}",
"}",
"
\`
",
"<",
"/",
"template",
Expand All @@ -1299,6 +1388,53 @@ export const NotFound = <template>
"type": "GlimmerElementNodePart",
}
`);
expect({ type: nodes[i].type, tokens: source.getTokens(nodes[i++]).map((t) => t.value) })
.toMatchInlineSnapshot(`
{
"tokens": [
"
\`
",
],
"type": "GlimmerTextNode",
}
`);
expect({ type: nodes[i].type, tokens: source.getTokens(nodes[i++]).map((t) => t.value) })
.toMatchInlineSnapshot(`
{
"tokens": [
"{",
"{",
"this",
".",
"price",
"}",
"}",
],
"type": "GlimmerMustacheStatement",
}
`);
expect({ type: nodes[i].type, tokens: source.getTokens(nodes[i++]).map((t) => t.value) })
.toMatchInlineSnapshot(`
{
"tokens": [
"this",
".",
"price",
],
"type": "GlimmerPathExpression",
}
`);
expect({ type: nodes[i].type, tokens: source.getTokens(nodes[i++]).map((t) => t.value) })
.toMatchInlineSnapshot(`
{
"tokens": [
"
$",
],
"type": "GlimmerTextNode",
}
`);
expect({ type: nodes[i].type, tokens: source.getTokens(nodes[i++]).map((t) => t.value) })
.toMatchInlineSnapshot(`
{
Expand Down Expand Up @@ -2540,41 +2676,5 @@ export const NotFound = <template>
"type": "Identifier",
}
`);
expect({ type: nodes[i].type, tokens: source.getTokens(nodes[i++]).map((t) => t.value) })
.toMatchInlineSnapshot(`
{
"tokens": [
"Link",
],
"type": "Identifier",
}
`);
expect({ type: nodes[i].type, tokens: source.getTokens(nodes[i++]).map((t) => t.value) })
.toMatchInlineSnapshot(`
{
"tokens": [
"ExternalLink",
],
"type": "ImportSpecifier",
}
`);
expect({ type: nodes[i].type, tokens: source.getTokens(nodes[i++]).map((t) => t.value) })
.toMatchInlineSnapshot(`
{
"tokens": [
"ExternalLink",
],
"type": "Identifier",
}
`);
expect({ type: nodes[i].type, tokens: source.getTokens(nodes[i++]).map((t) => t.value) })
.toMatchInlineSnapshot(`
{
"tokens": [
"ExternalLink",
],
"type": "Identifier",
}
`);
});
});
Loading