error on unclosed comments and blocks with only whitespace

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

@ -112,14 +112,14 @@ export class Parser {
throw new ParseError(message, this.template, index, this.filename); 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)) { if (this.match(str)) {
this.index += str.length; this.index += str.length;
return true; return true;
} }
if (required) { if (required) {
this.error(`Expected ${str}`); this.error(message || `Expected ${str}`);
} }
return false; return false;

@ -5,6 +5,13 @@ import reservedNames from '../../utils/reservedNames';
import { Parser } from '../index'; import { Parser } from '../index';
import { Node } from '../../interfaces'; 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) { function trimWhitespace(block: Node, trimBefore: boolean, trimAfter: boolean) {
if (!block.children) return; // AwaitBlock if (!block.children) return; // AwaitBlock
@ -74,7 +81,7 @@ export default function mustache(parser: Parser) {
} }
// strip leading/trailing whitespace as necessary // 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 charBefore = parser.template[block.start - 1];
const charAfter = parser.template[parser.index]; const charAfter = parser.template[parser.index];

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

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

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

Loading…
Cancel
Save