Merge pull request #1158 from sveltejs/gh-1156

[WIP] Error on unclosed comments and blocks with no content
pull/1182/head
Rich Harris 7 years ago committed by GitHub
commit b3049a6fa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -6,7 +6,7 @@ import { Parser } from '../index';
import { Node } from '../../interfaces';
function trimWhitespace(block: Node, trimBefore: boolean, trimAfter: boolean) {
if (!block.children) return; // AwaitBlock
if (!block.children || block.children.length === 0) return; // AwaitBlock
const firstChild = block.children[0];
const lastChild = block.children[block.children.length - 1];
@ -74,8 +74,6 @@ export default function mustache(parser: Parser) {
}
// strip leading/trailing whitespace as necessary
if (block.children && !block.children.length) parser.error(`Empty block`, block.start);
const charBefore = parser.template[block.start - 1];
const charAfter = parser.template[parser.index];
const trimBefore = !charBefore || whitespace.test(charBefore);

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

@ -12,6 +12,13 @@ const meta = new Map([
[':Head', validateHead]
]);
function isEmptyBlock(node: Node) {
if (!/Block$/.test(node.type) || !node.children) return false;
if (node.children.length > 1) return false;
const child = node.children[0];
return !child || (child.type === 'Text' && !/\S/.test(child.data));
}
export default function validateHtml(validator: Validator, html: Node) {
const refs = new Map();
const refCallees: Node[] = [];
@ -58,6 +65,10 @@ export default function validateHtml(validator: Validator, html: Node) {
}
}
if (validator.options.dev && isEmptyBlock(node)) {
validator.warn('Empty block', node.start);
}
if (node.children) {
if (node.type === 'Element') elementStack.push(node);
stack.push(node);

@ -93,7 +93,7 @@ export default function validate(
stylesheet: Stylesheet,
options: CompileOptions
) {
const { onwarn, onerror, name, filename, store } = options;
const { onwarn, onerror, name, filename, store, dev } = options;
try {
if (name && !/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test(name)) {
@ -114,7 +114,8 @@ export default function validate(
onwarn,
name,
filename,
store
store,
dev
});
if (parsed.js) {

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

@ -1,6 +1,6 @@
import * as fs from "fs";
import assert from "assert";
import { svelte, tryToLoadJson } from "../helpers.js";
import { svelte, loadConfig, tryToLoadJson } from "../helpers.js";
describe("validate", () => {
fs.readdirSync("test/validator/samples").forEach(dir => {
@ -15,6 +15,7 @@ describe("validate", () => {
}
(solo ? it.only : skip ? it.skip : it)(dir, () => {
const config = loadConfig(`./validator/samples/${dir}/_config.js`);
const filename = `test/validator/samples/${dir}/input.html`;
const input = fs.readFileSync(filename, "utf-8").replace(/\s+$/, "");
@ -32,7 +33,8 @@ describe("validate", () => {
pos: warning.pos,
loc: warning.loc
});
}
},
dev: config.dev
});
assert.deepEqual(warnings, expectedWarnings);

@ -0,0 +1,10 @@
{{#each things as thing}}
<span>this only exists...</span>
<span>...to increase test coverage</span>
{{/each}}
{{#each things as thing}}{{soDoesThis}}{{/each}}
{{#each things as thing}}
andThis
{{/each}}

@ -0,0 +1,3 @@
export default {
dev: true
};

@ -0,0 +1,5 @@
{{#each things as thing}}
{{/each}}
{{#each things as thing}}{{/each}}

@ -0,0 +1,18 @@
[
{
"message": "Empty block",
"loc": {
"line": 1,
"column": 0
},
"pos": 0
},
{
"message": "Empty block",
"loc": {
"line": 5,
"column": 0
},
"pos": 38
}
]

@ -0,0 +1,5 @@
{{#each things as thing}}
{{/each}}
{{#each things as thing}}{{/each}}
Loading…
Cancel
Save