diff --git a/src/compiler/parse/index.ts b/src/compiler/parse/index.ts index 0603a0f800..27067caf26 100644 --- a/src/compiler/parse/index.ts +++ b/src/compiler/parse/index.ts @@ -177,9 +177,9 @@ export class Parser { return identifier; } - read_until(pattern: RegExp) { + read_until(pattern: RegExp, error_message?: Parameters[0]) { if (this.index >= this.template.length) { - this.error({ + this.error(error_message || { code: 'unexpected-eof', message: 'Unexpected end of input' }); diff --git a/src/compiler/parse/read/script.ts b/src/compiler/parse/read/script.ts index 2855e640e7..db76cb8892 100644 --- a/src/compiler/parse/read/script.ts +++ b/src/compiler/parse/read/script.ts @@ -3,8 +3,6 @@ import { Parser } from '../index'; import { Script } from '../../interfaces'; import { Node, Program } from 'estree'; -const script_closing_tag = ''; - function get_context(parser: Parser, attributes: any[], start: number): string { const context = attributes.find(attribute => attribute.name === 'context'); if (!context) return 'default'; @@ -30,18 +28,17 @@ function get_context(parser: Parser, attributes: any[], start: number): string { export default function read_script(parser: Parser, start: number, attributes: Node[]): Script { const script_start = parser.index; - const script_end = parser.template.indexOf(script_closing_tag, script_start); - - if (script_end === -1) { - parser.error({ - code: 'unclosed-script', - message: ' + +

Hello {name}!

\ No newline at end of file diff --git a/test/parser/samples/whitespace-after-script-tag/output.json b/test/parser/samples/whitespace-after-script-tag/output.json new file mode 100644 index 0000000000..903c392685 --- /dev/null +++ b/test/parser/samples/whitespace-after-script-tag/output.json @@ -0,0 +1,150 @@ +{ + "html": { + "start": 51, + "end": 73, + "type": "Fragment", + "children": [ + { + "start": 49, + "end": 51, + "type": "Text", + "raw": "\n\n", + "data": "\n\n" + }, + { + "start": 51, + "end": 73, + "type": "Element", + "name": "h1", + "attributes": [], + "children": [ + { + "start": 55, + "end": 61, + "type": "Text", + "raw": "Hello ", + "data": "Hello " + }, + { + "start": 61, + "end": 67, + "type": "MustacheTag", + "expression": { + "type": "Identifier", + "start": 62, + "end": 66, + "loc": { + "start": { + "line": 10, + "column": 11 + }, + "end": { + "line": 10, + "column": 15 + } + }, + "name": "name" + } + }, + { + "start": 67, + "end": 68, + "type": "Text", + "raw": "!", + "data": "!" + } + ] + } + ] + }, + "instance": { + "type": "Script", + "start": 0, + "end": 49, + "context": "default", + "content": { + "type": "Program", + "start": 8, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 0 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 10, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 14, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "start": 14, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "name": "name" + }, + "init": { + "type": "Literal", + "start": 21, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "value": "world", + "raw": "'world'" + } + } + ], + "kind": "let" + } + ], + "sourceType": "module" + } + } +} \ No newline at end of file diff --git a/test/parser/samples/whitespace-after-style-tag/input.svelte b/test/parser/samples/whitespace-after-style-tag/input.svelte new file mode 100644 index 0000000000..ae4277b697 --- /dev/null +++ b/test/parser/samples/whitespace-after-style-tag/input.svelte @@ -0,0 +1,12 @@ +
foo
+ + \ No newline at end of file diff --git a/test/parser/samples/whitespace-after-style-tag/output.json b/test/parser/samples/whitespace-after-style-tag/output.json new file mode 100644 index 0000000000..5eee81f034 --- /dev/null +++ b/test/parser/samples/whitespace-after-style-tag/output.json @@ -0,0 +1,97 @@ +{ + "html": { + "start": 0, + "end": 14, + "type": "Fragment", + "children": [ + { + "start": 0, + "end": 14, + "type": "Element", + "name": "div", + "attributes": [], + "children": [ + { + "start": 5, + "end": 8, + "type": "Text", + "raw": "foo", + "data": "foo" + } + ] + }, + { + "start": 14, + "end": 16, + "type": "Text", + "raw": "\n\n", + "data": "\n\n" + } + ] + }, + "css": { + "type": "Style", + "start": 16, + "end": 66, + "attributes": [], + "children": [ + { + "type": "Rule", + "prelude": { + "type": "SelectorList", + "children": [ + { + "type": "Selector", + "children": [ + { + "type": "TypeSelector", + "name": "div", + "start": 25, + "end": 28 + } + ], + "start": 25, + "end": 28 + } + ], + "start": 25, + "end": 28 + }, + "block": { + "type": "Block", + "children": [ + { + "type": "Declaration", + "important": false, + "property": "color", + "value": { + "type": "Value", + "children": [ + { + "type": "Identifier", + "name": "red", + "start": 40, + "end": 43 + } + ], + "start": 40, + "end": 43 + }, + "start": 33, + "end": 43 + } + ], + "start": 29, + "end": 47 + }, + "start": 25, + "end": 47 + } + ], + "content": { + "start": 23, + "end": 48, + "styles": "\n\tdiv {\n\t\tcolor: red;\n\t}\n" + } + } +} \ No newline at end of file