Skip to content

Commit

Permalink
fix: do not throw error if <script> exists inside {@html}
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama committed Jan 5, 2025
1 parent 73e8892 commit b873fb4
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-guests-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: prevent errors when `<script>` tags are used inside `{@html}`
13 changes: 13 additions & 0 deletions src/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,19 +383,32 @@ type SelfClosingBlock = {
startTagRange: [number, number];
};

function isValidStartTagOpenIndex(
code: string,
startTagOpenIndex: number,
): boolean {
const prev = code.slice(0, startTagOpenIndex);
return />\s*$|^\s*$/m.test(prev);
}

/** Extract <script> blocks */
function* extractBlocks(code: string): IterableIterator<Block> {
const startTagOpenRe = /<!--[\s\S]*?-->|<(script|style|template)([\s>])/giu;
const endScriptTagRe = /<\/script>/giu;
const endStyleTagRe = /<\/style>/giu;
const endTemplateTagRe = /<\/template>/giu;

let startTagOpenMatch;
while ((startTagOpenMatch = startTagOpenRe.exec(code))) {
const [, tag, nextChar] = startTagOpenMatch;
if (!tag) {
continue;
}
const startTagStart = startTagOpenMatch.index;
if (!isValidStartTagOpenIndex(code, startTagStart)) {
continue;
}

let startTagEnd = startTagOpenRe.lastIndex;

const lowerTag = tag.toLowerCase() as "script" | "style" | "template";
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/parser/ast/at-html01-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{@html `<script>var x = ${50}</script>`}
233 changes: 233 additions & 0 deletions tests/fixtures/parser/ast/at-html01-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
{
"type": "Program",
"body": [
{
"type": "SvelteMustacheTag",
"kind": "raw",
"expression": {
"type": "TemplateLiteral",
"expressions": [
{
"type": "Literal",
"raw": "50",
"value": 50,
"range": [
26,
28
],
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 28
}
}
}
],
"quasis": [
{
"type": "TemplateElement",
"tail": false,
"value": {
"cooked": "<script>var x = ",
"raw": "<script>var x = "
},
"range": [
7,
26
],
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 26
}
}
},
{
"type": "TemplateElement",
"tail": true,
"value": {
"cooked": "</script>",
"raw": "</script>"
},
"range": [
28,
39
],
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 39
}
}
}
],
"range": [
7,
39
],
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 39
}
}
},
"range": [
0,
40
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 40
}
}
}
],
"sourceType": "module",
"comments": [],
"tokens": [
{
"type": "Punctuator",
"value": "{",
"range": [
0,
1
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
}
}
},
{
"type": "MustacheKeyword",
"value": "@html",
"range": [
1,
6
],
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 6
}
}
},
{
"type": "Template",
"value": "`<script>var x = ${",
"range": [
7,
26
],
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 26
}
}
},
{
"type": "Numeric",
"value": "50",
"range": [
26,
28
],
"loc": {
"start": {
"line": 1,
"column": 26
},
"end": {
"line": 1,
"column": 28
}
}
},
{
"type": "Template",
"value": "}</script>`",
"range": [
28,
39
],
"loc": {
"start": {
"line": 1,
"column": 28
},
"end": {
"line": 1,
"column": 39
}
}
},
{
"type": "Punctuator",
"value": "}",
"range": [
39,
40
],
"loc": {
"start": {
"line": 1,
"column": 39
},
"end": {
"line": 1,
"column": 40
}
}
}
],
"range": [
0,
41
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 0
}
}
}
34 changes: 34 additions & 0 deletions tests/fixtures/parser/ast/at-html01-scope-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"type": "global",
"variables": [
{
"name": "$$slots",
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$$props",
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$$restProps",
"identifiers": [],
"defs": [],
"references": []
}
],
"references": [],
"childScopes": [
{
"type": "module",
"variables": [],
"references": [],
"childScopes": [],
"through": []
}
],
"through": []
}

0 comments on commit b873fb4

Please sign in to comment.