error on unclosed comments and blocks with only whitespace

pull/1158/head
Rich Harris 7 years ago
parent 620badc9d0
commit a4d08c205a

@ -112,14 +112,14 @@ export class Parser {
throw new ParseError(message, this.template, index, this.filename);
}
eat(str: string, required?: boolean) {
eat(str: string, required?: boolean, message?: string) {
if (this.match(str)) {
this.index += str.length;
return true;
}
if (required) {
this.error(`Expected ${str}`);
this.error(message || `Expected ${str}`);
}
return false;

@ -5,6 +5,13 @@ import reservedNames from '../../utils/reservedNames';
import { Parser } from '../index';
import { Node } from '../../interfaces';
function isEmpty(nodes: Node[]) {
if (!nodes || nodes.length > 1) return false;
if (nodes.length === 0) return true;
if (nodes.length > 1) return false;
return nodes[0].type === 'Text' && !/\S/.test(nodes[0].data);
}
function trimWhitespace(block: Node, trimBefore: boolean, trimAfter: boolean) {
if (!block.children) return; // AwaitBlock
@ -74,7 +81,7 @@ export default function mustache(parser: Parser) {
}
// strip leading/trailing whitespace as necessary
if (block.children && !block.children.length) parser.error(`Empty block`, block.start);
if (isEmpty(block.children)) parser.error(`Empty block`, block.start);
const charBefore = parser.template[block.start - 1];
const charAfter = parser.template[parser.index];

@ -71,7 +71,7 @@ export default function tag(parser: Parser) {
if (parser.eat('!--')) {
const data = parser.readUntil(/-->/);
parser.eat('-->');
parser.eat('-->', true, 'comment was left open, expected -->');
parser.current().children.push({
start,

@ -1,8 +1,8 @@
{
"message": "comment was left open",
"message": "comment was left open, expected -->",
"loc": {
"line": 1,
"column": 0
"column": 24
},
"pos": 0
"pos": 24
}

@ -1,11 +1,11 @@
{{#each foos as foo @id}}
{{/each}}
<script>
export default {
data: () => ({
foos: [
{ "id": 5 }
]
})
};
export default {
data: () => ({
foos: [
{ id: 5 }
]
})
};
</script>

Loading…
Cancel
Save