From 2b8d4cf5677040fab277954206445844b8cd1da5 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Thu, 15 Jul 2021 23:29:29 +0900 Subject: [PATCH] Fix wrong AST for empty style tag. --- src/parser/converts/root.ts | 2 +- .../parser/ast/empty-elements01-input.svelte | 3 + .../parser/ast/empty-elements01-output.json | 837 ++++++++++++++++++ .../ast/empty-elements01-scope-output.json | 34 + 4 files changed, 875 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/parser/ast/empty-elements01-input.svelte create mode 100644 tests/fixtures/parser/ast/empty-elements01-output.json create mode 100644 tests/fixtures/parser/ast/empty-elements01-scope-output.json diff --git a/src/parser/converts/root.ts b/src/parser/converts/root.ts index fd9e6b87..44a09d0d 100644 --- a/src/parser/converts/root.ts +++ b/src/parser/converts/root.ts @@ -109,7 +109,7 @@ export function convertSvelteRoot( }, }) - if (style.endTag) { + if (style.endTag && style.startTag.range[1] < style.endTag.range[0]) { const contentRange = { start: style.startTag.range[1], end: style.endTag.range[0], diff --git a/tests/fixtures/parser/ast/empty-elements01-input.svelte b/tests/fixtures/parser/ast/empty-elements01-input.svelte new file mode 100644 index 00000000..00604d59 --- /dev/null +++ b/tests/fixtures/parser/ast/empty-elements01-input.svelte @@ -0,0 +1,3 @@ + +
+ diff --git a/tests/fixtures/parser/ast/empty-elements01-output.json b/tests/fixtures/parser/ast/empty-elements01-output.json new file mode 100644 index 00000000..03e8e3cc --- /dev/null +++ b/tests/fixtures/parser/ast/empty-elements01-output.json @@ -0,0 +1,837 @@ +{ + "type": "Program", + "body": [ + { + "type": "SvelteStyleElement", + "name": { + "type": "SvelteName", + "name": "style", + "range": [ + 1, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [ + { + "type": "SvelteAttribute", + "key": { + "type": "SvelteName", + "name": "src", + "range": [ + 7, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + "boolean": false, + "value": [ + { + "type": "SvelteLiteral", + "value": "./style.scss", + "range": [ + 12, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 24 + } + } + } + ], + "range": [ + 7, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 25 + } + } + } + ], + "selfClosing": false, + "range": [ + 0, + 26 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + "children": [], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 26, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 34 + } + } + }, + "range": [ + 0, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 34 + } + } + }, + { + "type": "SvelteText", + "value": "\n", + "range": [ + 34, + 35 + ], + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 2, + "column": 0 + } + } + }, + { + "type": "SvelteElement", + "kind": "html", + "name": { + "type": "SvelteName", + "name": "div", + "range": [ + 36, + 39 + ], + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 4 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [], + "selfClosing": false, + "range": [ + 35, + 40 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 5 + } + } + }, + "children": [], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 40, + 46 + ], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + "range": [ + 35, + 46 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + { + "type": "SvelteText", + "value": "\n", + "range": [ + 46, + 47 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 3, + "column": 0 + } + } + }, + { + "type": "SvelteScriptElement", + "name": { + "type": "SvelteName", + "name": "script", + "range": [ + 48, + 54 + ], + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [], + "selfClosing": false, + "range": [ + 47, + 55 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 8 + } + } + }, + "body": [], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 55, + 64 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 17 + } + } + }, + "range": [ + 47, + 64 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + ], + "sourceType": "module", + "comments": [], + "tokens": [ + { + "type": "Punctuator", + "value": "<", + "range": [ + 0, + 1 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "style", + "range": [ + 1, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "src", + "range": [ + 7, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 11, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "HTMLText", + "value": "./style.scss", + "range": [ + 12, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 25, + 26 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 26, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 27, + 28 + ], + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 28 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "style", + "range": [ + 28, + 33 + ], + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 33 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 33, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + } + } + }, + { + "type": "HTMLText", + "value": "\n", + "range": [ + 34, + 35 + ], + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 2, + "column": 0 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 35, + 36 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "div", + "range": [ + 36, + 39 + ], + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 4 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 39, + 40 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 40, + 41 + ], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 41, + 42 + ], + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "div", + "range": [ + 42, + 45 + ], + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 45, + 46 + ], + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + { + "type": "HTMLText", + "value": "\n", + "range": [ + 46, + 47 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 3, + "column": 0 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 47, + 48 + ], + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 48, + 54 + ], + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 3, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 54, + 55 + ], + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 55, + 56 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 56, + 57 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 10 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 57, + 63 + ], + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 63, + 64 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + ], + "range": [ + 0, + 65 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 0 + } + } +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/empty-elements01-scope-output.json b/tests/fixtures/parser/ast/empty-elements01-scope-output.json new file mode 100644 index 00000000..d392ca4b --- /dev/null +++ b/tests/fixtures/parser/ast/empty-elements01-scope-output.json @@ -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": [] +} \ No newline at end of file