From db65f01ee3903bdfa7b60d82bd808e1337d8fc75 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 30 Nov 2016 16:01:11 -0500 Subject: [PATCH] =?UTF-8?q?handle=20trailing=20comments=20in=20script=20ta?= =?UTF-8?q?gs=20=E2=80=93=20closes=20#64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compiler/parse/read/script.js | 12 +- test/parser/script-comment-only/input.html | 5 + test/parser/script-comment-only/output.json | 19 +++ .../input.html | 13 ++ .../output.json | 121 ++++++++++++++++++ .../parser/script-comment-trailing/input.html | 11 ++ .../script-comment-trailing/output.json | 121 ++++++++++++++++++ 7 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 test/parser/script-comment-only/input.html create mode 100644 test/parser/script-comment-only/output.json create mode 100644 test/parser/script-comment-trailing-multiline/input.html create mode 100644 test/parser/script-comment-trailing-multiline/output.json create mode 100644 test/parser/script-comment-trailing/input.html create mode 100644 test/parser/script-comment-trailing/output.json diff --git a/compiler/parse/read/script.js b/compiler/parse/read/script.js index 1015c7d249..9ebff7f620 100644 --- a/compiler/parse/read/script.js +++ b/compiler/parse/read/script.js @@ -11,7 +11,15 @@ export default function readScript ( parser, start, attributes ) { scriptEnd = parser.index; - if ( parser.eat( '' ) ) break; + if ( parser.eat( '/script>' ) ) { + // this happens with trailing comments! + scriptEnd -= 1; + break; + } + + if ( parser.eat( '' ) ) { + break; + } } const source = spaces( scriptStart ) + parser.template.slice( scriptStart, scriptEnd ); @@ -27,6 +35,8 @@ export default function readScript ( parser, start, attributes ) { parser.acornError( err ); } + if ( !ast.body.length ) return null; + ast.start = scriptStart; return { diff --git a/test/parser/script-comment-only/input.html b/test/parser/script-comment-only/input.html new file mode 100644 index 0000000000..9dd86391f3 --- /dev/null +++ b/test/parser/script-comment-only/input.html @@ -0,0 +1,5 @@ +
+ + diff --git a/test/parser/script-comment-only/output.json b/test/parser/script-comment-only/output.json new file mode 100644 index 0000000000..0e8e7877df --- /dev/null +++ b/test/parser/script-comment-only/output.json @@ -0,0 +1,19 @@ +{ + "html": { + "start": 0, + "end": 11, + "type": "Fragment", + "children": [ + { + "start": 0, + "end": 11, + "type": "Element", + "name": "div", + "attributes": [], + "children": [] + } + ] + }, + "css": null, + "js": null +} diff --git a/test/parser/script-comment-trailing-multiline/input.html b/test/parser/script-comment-trailing-multiline/input.html new file mode 100644 index 0000000000..901749ce25 --- /dev/null +++ b/test/parser/script-comment-trailing-multiline/input.html @@ -0,0 +1,13 @@ +

Hello {{name}}!

+ + diff --git a/test/parser/script-comment-trailing-multiline/output.json b/test/parser/script-comment-trailing-multiline/output.json new file mode 100644 index 0000000000..f97eb78052 --- /dev/null +++ b/test/parser/script-comment-trailing-multiline/output.json @@ -0,0 +1,121 @@ +{ + "html": { + "start": 0, + "end": 24, + "type": "Fragment", + "children": [ + { + "start": 0, + "end": 24, + "type": "Element", + "name": "h1", + "attributes": [], + "children": [ + { + "start": 4, + "end": 10, + "type": "Text", + "data": "Hello " + }, + { + "start": 10, + "end": 18, + "type": "MustacheTag", + "expression": { + "start": 12, + "end": 16, + "type": "Identifier", + "name": "name" + } + }, + { + "start": 18, + "end": 19, + "type": "Text", + "data": "!" + } + ] + } + ] + }, + "css": null, + "js": { + "start": 26, + "end": 143, + "attributes": [], + "content": { + "type": "Program", + "start": 34, + "end": 134, + "body": [ + { + "declaration": { + "start": 51, + "end": 94, + "type": "ObjectExpression", + "properties": [ + { + "start": 55, + "end": 91, + "type": "Property", + "computed": false, + "key": { + "start": 55, + "end": 59, + "type": "Identifier", + "name": "data" + }, + "kind": "init", + "method": false, + "shorthand": false, + "value": { + "start": 61, + "end": 91, + "type": "ArrowFunctionExpression", + "async": false, + "body": { + "start": 68, + "end": 90, + "type": "ObjectExpression", + "properties": [ + { + "start": 73, + "end": 86, + "type": "Property", + "computed": false, + "key": { + "start": 73, + "end": 77, + "type": "Identifier", + "name": "name" + }, + "kind": "init", + "method": false, + "shorthand": false, + "value": { + "start": 79, + "end": 86, + "type": "Literal", + "raw": "'world'", + "value": "world" + } + } + ] + }, + "expression": true, + "generator": false, + "id": null, + "params": [] + } + } + ] + }, + "end": 95, + "start": 36, + "type": "ExportDefaultDeclaration" + } + ], + "sourceType": "module" + } + } +} diff --git a/test/parser/script-comment-trailing/input.html b/test/parser/script-comment-trailing/input.html new file mode 100644 index 0000000000..76c5391099 --- /dev/null +++ b/test/parser/script-comment-trailing/input.html @@ -0,0 +1,11 @@ +

Hello {{name}}!

+ + diff --git a/test/parser/script-comment-trailing/output.json b/test/parser/script-comment-trailing/output.json new file mode 100644 index 0000000000..e531599fea --- /dev/null +++ b/test/parser/script-comment-trailing/output.json @@ -0,0 +1,121 @@ +{ + "html": { + "start": 0, + "end": 24, + "type": "Fragment", + "children": [ + { + "start": 0, + "end": 24, + "type": "Element", + "name": "h1", + "attributes": [], + "children": [ + { + "start": 4, + "end": 10, + "type": "Text", + "data": "Hello " + }, + { + "start": 10, + "end": 18, + "type": "MustacheTag", + "expression": { + "start": 12, + "end": 16, + "type": "Identifier", + "name": "name" + } + }, + { + "start": 18, + "end": 19, + "type": "Text", + "data": "!" + } + ] + } + ] + }, + "css": null, + "js": { + "start": 26, + "end": 132, + "attributes": [], + "content": { + "type": "Program", + "start": 34, + "end": 123, + "body": [ + { + "declaration": { + "start": 51, + "end": 94, + "type": "ObjectExpression", + "properties": [ + { + "start": 55, + "end": 91, + "type": "Property", + "computed": false, + "key": { + "start": 55, + "end": 59, + "type": "Identifier", + "name": "data" + }, + "kind": "init", + "method": false, + "shorthand": false, + "value": { + "start": 61, + "end": 91, + "type": "ArrowFunctionExpression", + "async": false, + "body": { + "start": 68, + "end": 90, + "type": "ObjectExpression", + "properties": [ + { + "start": 73, + "end": 86, + "type": "Property", + "computed": false, + "key": { + "start": 73, + "end": 77, + "type": "Identifier", + "name": "name" + }, + "kind": "init", + "method": false, + "shorthand": false, + "value": { + "start": 79, + "end": 86, + "type": "Literal", + "raw": "'world'", + "value": "world" + } + } + ] + }, + "expression": true, + "generator": false, + "id": null, + "params": [] + } + } + ] + }, + "end": 95, + "start": 36, + "type": "ExportDefaultDeclaration" + } + ], + "sourceType": "module" + } + } +}