handle trailing comments in script tags – closes #64

pull/70/head
Rich Harris 8 years ago
parent 0cee0a7d7e
commit db65f01ee3

@ -11,7 +11,15 @@ export default function readScript ( parser, start, attributes ) {
scriptEnd = parser.index;
if ( parser.eat( '</script>' ) ) break;
if ( parser.eat( '/script>' ) ) {
// this happens with trailing comments!
scriptEnd -= 1;
break;
}
if ( parser.eat( '</script>' ) ) {
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 {

@ -0,0 +1,5 @@
<div></div>
<script>
// TODO write some code
</script>

@ -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
}

@ -0,0 +1,13 @@
<h1>Hello {{name}}!</h1>
<script>
export default {
data: () => ({
name: 'world'
})
};
/*
trailing multiline comment
*/
</script>

@ -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"
}
}
}

@ -0,0 +1,11 @@
<h1>Hello {{name}}!</h1>
<script>
export default {
data: () => ({
name: 'world'
})
};
// trailing line comment
</script>

@ -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"
}
}
}
Loading…
Cancel
Save