0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-30 00:46:29 +01:00

empty blocks are a dev warning, not an error

This commit is contained in:
Rich Harris 2018-02-10 11:46:23 -05:00
parent a4d08c205a
commit ff67b137c4
14 changed files with 30 additions and 54 deletions

View File

@ -5,13 +5,6 @@ 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
@ -81,8 +74,6 @@ export default function mustache(parser: Parser) {
}
// strip leading/trailing whitespace as necessary
if (isEmpty(block.children)) 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);

View File

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

View File

@ -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) {

View File

@ -1 +0,0 @@
{{#each foos as foo}}{{/each}}

View File

@ -1,8 +0,0 @@
{
"message": "Empty block",
"loc": {
"line": 1,
"column": 0
},
"pos": 0
}

View File

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

View File

@ -1,8 +0,0 @@
{
"message": "Empty block",
"loc": {
"line": 1,
"column": 0
},
"pos": 0
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1 @@
[]