From 4423821065dcef310b0d451bc83a203f0734c77e Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Fri, 16 Sep 2022 12:42:17 +0900 Subject: [PATCH 1/3] fix: parsing error when ` + + + diff --git a/tests/fixtures/parser/ast/script-tag-output.json b/tests/fixtures/parser/ast/script-tag-output.json new file mode 100644 index 00000000..26ce94cb --- /dev/null +++ b/tests/fixtures/parser/ast/script-tag-output.json @@ -0,0 +1,1627 @@ +{ + "type": "Program", + "body": [ + { + "type": "SvelteElement", + "kind": "html", + "name": { + "type": "SvelteName", + "name": "head", + "range": [ + 1, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [], + "selfClosing": false, + "range": [ + 0, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + "children": [ + { + "type": "SvelteText", + "value": "\n ", + "range": [ + 6, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 2, + "column": 4 + } + } + }, + { + "type": "SvelteElement", + "kind": "html", + "name": { + "type": "SvelteName", + "name": "script", + "range": [ + 12, + 18 + ], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [ + { + "type": "SvelteAttribute", + "key": { + "type": "SvelteName", + "name": "type", + "range": [ + 19, + 23 + ], + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 16 + } + } + }, + "boolean": false, + "value": [ + { + "type": "SvelteLiteral", + "value": "text/javascript", + "range": [ + 25, + 40 + ], + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 33 + } + } + } + ], + "range": [ + 19, + 41 + ], + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 34 + } + } + }, + { + "type": "SvelteAttribute", + "key": { + "type": "SvelteName", + "name": "id", + "range": [ + 42, + 44 + ], + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 37 + } + } + }, + "boolean": false, + "value": [], + "range": [ + 42, + 47 + ], + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 40 + } + } + }, + { + "type": "SvelteAttribute", + "key": { + "type": "SvelteName", + "name": "src", + "range": [ + 48, + 51 + ], + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 44 + } + } + }, + "boolean": false, + "value": [ + { + "type": "SvelteLiteral", + "value": "/some-script.js", + "range": [ + 53, + 68 + ], + "loc": { + "start": { + "line": 2, + "column": 46 + }, + "end": { + "line": 2, + "column": 61 + } + } + } + ], + "range": [ + 48, + 69 + ], + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 62 + } + } + } + ], + "selfClosing": false, + "range": [ + 11, + 70 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 63 + } + } + }, + "children": [ + { + "type": "SvelteText", + "value": "", + "range": [ + 70, + 70 + ], + "loc": { + "start": { + "line": 2, + "column": 63 + }, + "end": { + "line": 2, + "column": 63 + } + } + } + ], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 70, + 79 + ], + "loc": { + "start": { + "line": 2, + "column": 63 + }, + "end": { + "line": 2, + "column": 72 + } + } + }, + "range": [ + 11, + 79 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 72 + } + } + }, + { + "type": "SvelteText", + "value": "\n ", + "range": [ + 79, + 84 + ], + "loc": { + "start": { + "line": 2, + "column": 72 + }, + "end": { + "line": 3, + "column": 4 + } + } + }, + { + "type": "SvelteElement", + "kind": "html", + "name": { + "type": "SvelteName", + "name": "link", + "range": [ + 85, + 89 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [ + { + "type": "SvelteAttribute", + "key": { + "type": "SvelteName", + "name": "href", + "range": [ + 90, + 94 + ], + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 14 + } + } + }, + "boolean": false, + "value": [ + { + "type": "SvelteLiteral", + "value": "/style.css", + "range": [ + 96, + 106 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 26 + } + } + } + ], + "range": [ + 90, + 107 + ], + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 27 + } + } + }, + { + "type": "SvelteAttribute", + "key": { + "type": "SvelteName", + "name": "rel", + "range": [ + 108, + 111 + ], + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 31 + } + } + }, + "boolean": false, + "value": [ + { + "type": "SvelteLiteral", + "value": "stylesheet", + "range": [ + 113, + 123 + ], + "loc": { + "start": { + "line": 3, + "column": 33 + }, + "end": { + "line": 3, + "column": 43 + } + } + } + ], + "range": [ + 108, + 124 + ], + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 44 + } + } + } + ], + "selfClosing": false, + "range": [ + 84, + 125 + ], + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 45 + } + } + }, + "children": [], + "endTag": null, + "range": [ + 84, + 125 + ], + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 45 + } + } + }, + { + "type": "SvelteText", + "value": "\n ", + "range": [ + 125, + 130 + ], + "loc": { + "start": { + "line": 3, + "column": 45 + }, + "end": { + "line": 4, + "column": 4 + } + } + }, + { + "type": "SvelteElement", + "kind": "html", + "name": { + "type": "SvelteName", + "name": "script", + "range": [ + 131, + 137 + ], + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 11 + } + } + }, + "startTag": { + "type": "SvelteStartTag", + "attributes": [], + "selfClosing": false, + "range": [ + 130, + 138 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + "children": [ + { + "type": "SvelteText", + "value": "console.log('foo')", + "range": [ + 138, + 156 + ], + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 30 + } + } + } + ], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 156, + 165 + ], + "loc": { + "start": { + "line": 4, + "column": 30 + }, + "end": { + "line": 4, + "column": 39 + } + } + }, + "range": [ + 130, + 165 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 39 + } + } + }, + { + "type": "SvelteText", + "value": "\n", + "range": [ + 165, + 166 + ], + "loc": { + "start": { + "line": 4, + "column": 39 + }, + "end": { + "line": 5, + "column": 0 + } + } + } + ], + "endTag": { + "type": "SvelteEndTag", + "range": [ + 166, + 173 + ], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 7 + } + } + }, + "range": [ + 0, + 173 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 7 + } + } + } + ], + "sourceType": "module", + "comments": [], + "tokens": [ + { + "type": "Punctuator", + "value": "<", + "range": [ + 0, + 1 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "head", + "range": [ + 1, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 5, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + { + "type": "HTMLText", + "value": "\n ", + "range": [ + 6, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 2, + "column": 4 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 11, + 12 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 12, + 18 + ], + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "type", + "range": [ + 19, + 23 + ], + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 16 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + } + } + }, + { + "type": "HTMLText", + "value": "text/javascript", + "range": [ + 25, + 40 + ], + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 33 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 40, + 41 + ], + "loc": { + "start": { + "line": 2, + "column": 33 + }, + "end": { + "line": 2, + "column": 34 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "id", + "range": [ + 42, + 44 + ], + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 37 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 44, + 45 + ], + "loc": { + "start": { + "line": 2, + "column": 37 + }, + "end": { + "line": 2, + "column": 38 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 45, + 46 + ], + "loc": { + "start": { + "line": 2, + "column": 38 + }, + "end": { + "line": 2, + "column": 39 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 46, + 47 + ], + "loc": { + "start": { + "line": 2, + "column": 39 + }, + "end": { + "line": 2, + "column": 40 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "src", + "range": [ + 48, + 51 + ], + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 44 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 51, + 52 + ], + "loc": { + "start": { + "line": 2, + "column": 44 + }, + "end": { + "line": 2, + "column": 45 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 52, + 53 + ], + "loc": { + "start": { + "line": 2, + "column": 45 + }, + "end": { + "line": 2, + "column": 46 + } + } + }, + { + "type": "HTMLText", + "value": "/some-script.js", + "range": [ + 53, + 68 + ], + "loc": { + "start": { + "line": 2, + "column": 46 + }, + "end": { + "line": 2, + "column": 61 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 68, + 69 + ], + "loc": { + "start": { + "line": 2, + "column": 61 + }, + "end": { + "line": 2, + "column": 62 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 69, + 70 + ], + "loc": { + "start": { + "line": 2, + "column": 62 + }, + "end": { + "line": 2, + "column": 63 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 70, + 71 + ], + "loc": { + "start": { + "line": 2, + "column": 63 + }, + "end": { + "line": 2, + "column": 64 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 71, + 72 + ], + "loc": { + "start": { + "line": 2, + "column": 64 + }, + "end": { + "line": 2, + "column": 65 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 72, + 78 + ], + "loc": { + "start": { + "line": 2, + "column": 65 + }, + "end": { + "line": 2, + "column": 71 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 78, + 79 + ], + "loc": { + "start": { + "line": 2, + "column": 71 + }, + "end": { + "line": 2, + "column": 72 + } + } + }, + { + "type": "HTMLText", + "value": "\n ", + "range": [ + 79, + 84 + ], + "loc": { + "start": { + "line": 2, + "column": 72 + }, + "end": { + "line": 3, + "column": 4 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 84, + 85 + ], + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "link", + "range": [ + 85, + 89 + ], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "href", + "range": [ + 90, + 94 + ], + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 94, + 95 + ], + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 15 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 95, + 96 + ], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 16 + } + } + }, + { + "type": "HTMLText", + "value": "/style.css", + "range": [ + 96, + 106 + ], + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 26 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 106, + 107 + ], + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 27 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "rel", + "range": [ + 108, + 111 + ], + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 31 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 111, + 112 + ], + "loc": { + "start": { + "line": 3, + "column": 31 + }, + "end": { + "line": 3, + "column": 32 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 112, + 113 + ], + "loc": { + "start": { + "line": 3, + "column": 32 + }, + "end": { + "line": 3, + "column": 33 + } + } + }, + { + "type": "HTMLText", + "value": "stylesheet", + "range": [ + 113, + 123 + ], + "loc": { + "start": { + "line": 3, + "column": 33 + }, + "end": { + "line": 3, + "column": 43 + } + } + }, + { + "type": "Punctuator", + "value": "\"", + "range": [ + 123, + 124 + ], + "loc": { + "start": { + "line": 3, + "column": 43 + }, + "end": { + "line": 3, + "column": 44 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 124, + 125 + ], + "loc": { + "start": { + "line": 3, + "column": 44 + }, + "end": { + "line": 3, + "column": 45 + } + } + }, + { + "type": "HTMLText", + "value": "\n ", + "range": [ + 125, + 130 + ], + "loc": { + "start": { + "line": 3, + "column": 45 + }, + "end": { + "line": 4, + "column": 4 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 130, + 131 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 131, + 137 + ], + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 11 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 137, + 138 + ], + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + { + "type": "HTMLText", + "value": "console.log('foo')", + "range": [ + 138, + 156 + ], + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 30 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 156, + 157 + ], + "loc": { + "start": { + "line": 4, + "column": 30 + }, + "end": { + "line": 4, + "column": 31 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 157, + 158 + ], + "loc": { + "start": { + "line": 4, + "column": 31 + }, + "end": { + "line": 4, + "column": 32 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "script", + "range": [ + 158, + 164 + ], + "loc": { + "start": { + "line": 4, + "column": 32 + }, + "end": { + "line": 4, + "column": 38 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 164, + 165 + ], + "loc": { + "start": { + "line": 4, + "column": 38 + }, + "end": { + "line": 4, + "column": 39 + } + } + }, + { + "type": "HTMLText", + "value": "\n", + "range": [ + 165, + 166 + ], + "loc": { + "start": { + "line": 4, + "column": 39 + }, + "end": { + "line": 5, + "column": 0 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 166, + 167 + ], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 167, + 168 + ], + "loc": { + "start": { + "line": 5, + "column": 1 + }, + "end": { + "line": 5, + "column": 2 + } + } + }, + { + "type": "HTMLIdentifier", + "value": "head", + "range": [ + 168, + 172 + ], + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 6 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 172, + 173 + ], + "loc": { + "start": { + "line": 5, + "column": 6 + }, + "end": { + "line": 5, + "column": 7 + } + } + } + ], + "range": [ + 0, + 174 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 0 + } + } +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/script-tag-scope-output.json b/tests/fixtures/parser/ast/script-tag-scope-output.json new file mode 100644 index 00000000..d392ca4b --- /dev/null +++ b/tests/fixtures/parser/ast/script-tag-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 diff --git a/tests/src/parser/__snapshots__/html.ts.snap b/tests/src/parser/__snapshots__/html.ts.snap index 40933937..da18ef53 100644 --- a/tests/src/parser/__snapshots__/html.ts.snap +++ b/tests/src/parser/__snapshots__/html.ts.snap @@ -143,3 +143,45 @@ Object { "index": 12, } `; + +exports[`parseAttributes empty="" 1`] = ` +Object { + "attributes": Array [ + Object { + "key": Object { + "end": 5, + "name": "empty", + "start": 0, + }, + "value": Object { + "end": 8, + "quote": "\\"", + "start": 6, + "value": "", + }, + }, + ], + "index": 10, +} +`; + +exports[`parseAttributes empty='' 1`] = ` +Object { + "attributes": Array [ + Object { + "key": Object { + "end": 5, + "name": "empty", + "start": 0, + }, + "value": Object { + "end": 8, + "quote": "'", + "start": 6, + "value": "", + }, + }, + ], + "index": 10, +} +`; diff --git a/tests/src/parser/html.ts b/tests/src/parser/html.ts index 3b30776f..b0964e24 100644 --- a/tests/src/parser/html.ts +++ b/tests/src/parser/html.ts @@ -33,6 +33,12 @@ describe("parseAttributes", () => { { input: "attr ", }, + { + input: `empty="" `, + }, + { + input: `empty='' `, + }, ]; for (const { input, index } of testCases) { it(input, () => { From 2ab32e3495a7c123801a2497b6f66e9552680078 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Fri, 16 Sep 2022 12:43:02 +0900 Subject: [PATCH 2/3] Create sweet-boats-exist.md --- .changeset/sweet-boats-exist.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sweet-boats-exist.md diff --git a/.changeset/sweet-boats-exist.md b/.changeset/sweet-boats-exist.md new file mode 100644 index 00000000..ea37a4b7 --- /dev/null +++ b/.changeset/sweet-boats-exist.md @@ -0,0 +1,5 @@ +--- +"svelte-eslint-parser": patch +--- + +fix: parsing error when `