From 40633327c41ff94343ac33ffdc6ca290a6610767 Mon Sep 17 00:00:00 2001 From: Christopher Pfohl Date: Wed, 7 Feb 2018 05:46:32 -0500 Subject: [PATCH 1/7] Empty Each Blocks should have consistent behavior. Here the `error-each-blocks-empty` behaves as expected. It throws a ParseError. I can't get the whitespace errors to repro in the tests. They're easily seen here: https://svelte.technology/repl?version=1.51.0&gist=e1596f9631619a689e85c80e46506692 If you uncomment each section in sequence it demonstrates the behavior. I can't quite figure out why my examples don't work. --- test/parser/samples/error-comment-unclosed/error.json | 8 ++++++++ test/parser/samples/error-comment-unclosed/input.html | 1 + test/parser/samples/error-each-blocks-empty/error.json | 8 ++++++++ test/parser/samples/error-each-blocks-empty/input.html | 1 + .../samples/error-each-blocks-keyed-whitespace/error.json | 8 ++++++++ .../samples/error-each-blocks-keyed-whitespace/input.html | 8 ++++++++ .../samples/error-each-blocks-whitespace/error.json | 8 ++++++++ .../samples/error-each-blocks-whitespace/input.html | 2 ++ 8 files changed, 44 insertions(+) create mode 100644 test/parser/samples/error-comment-unclosed/error.json create mode 100644 test/parser/samples/error-comment-unclosed/input.html create mode 100644 test/parser/samples/error-each-blocks-empty/error.json create mode 100644 test/parser/samples/error-each-blocks-empty/input.html create mode 100644 test/parser/samples/error-each-blocks-keyed-whitespace/error.json create mode 100644 test/parser/samples/error-each-blocks-keyed-whitespace/input.html create mode 100644 test/parser/samples/error-each-blocks-whitespace/error.json create mode 100644 test/parser/samples/error-each-blocks-whitespace/input.html diff --git a/test/parser/samples/error-comment-unclosed/error.json b/test/parser/samples/error-comment-unclosed/error.json new file mode 100644 index 0000000000..2502a32735 --- /dev/null +++ b/test/parser/samples/error-comment-unclosed/error.json @@ -0,0 +1,8 @@ +{ + "message": "comment was left open", + "loc": { + "line": 1, + "column": 0 + }, + "pos": 0 +} diff --git a/test/parser/samples/error-comment-unclosed/input.html b/test/parser/samples/error-comment-unclosed/input.html new file mode 100644 index 0000000000..725cf9be60 --- /dev/null +++ b/test/parser/samples/error-comment-unclosed/input.html @@ -0,0 +1 @@ +/); - parser.eat('-->'); + parser.eat('-->', true, 'comment was left open, expected -->'); parser.current().children.push({ start, diff --git a/test/parser/samples/error-comment-unclosed/error.json b/test/parser/samples/error-comment-unclosed/error.json index 2502a32735..f392e12fa1 100644 --- a/test/parser/samples/error-comment-unclosed/error.json +++ b/test/parser/samples/error-comment-unclosed/error.json @@ -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 } diff --git a/test/parser/samples/error-each-blocks-keyed-whitespace/input.html b/test/parser/samples/error-each-blocks-keyed-whitespace/input.html index c801984a56..6984d4ae88 100644 --- a/test/parser/samples/error-each-blocks-keyed-whitespace/input.html +++ b/test/parser/samples/error-each-blocks-keyed-whitespace/input.html @@ -1,11 +1,11 @@ {{#each foos as foo @id}} {{/each}} From 8b6a099ced17eb391c19b9fe7d7b9aea2090ff08 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 10 Feb 2018 11:46:23 -0500 Subject: [PATCH 4/7] empty blocks are a dev warning, not an error --- src/parse/state/mustache.ts | 9 --------- src/validate/html/index.ts | 11 +++++++++++ src/validate/index.ts | 5 +++-- .../parser/samples/error-each-blocks-empty/input.html | 1 - .../error-each-blocks-keyed-whitespace/error.json | 8 -------- .../error-each-blocks-keyed-whitespace/input.html | 11 ----------- .../samples/error-each-blocks-whitespace/error.json | 8 -------- .../samples/error-each-blocks-whitespace/input.html | 11 ----------- test/validator/index.js | 6 ++++-- .../validator/samples/empty-block-dev.solo/_config.js | 3 +++ .../validator/samples/empty-block-dev.solo/input.html | 3 +++ .../samples/empty-block-dev.solo/warnings.json} | 4 ++-- test/validator/samples/empty-block-prod/input.html | 3 +++ test/validator/samples/empty-block-prod/warnings.json | 1 + 14 files changed, 30 insertions(+), 54 deletions(-) delete mode 100644 test/parser/samples/error-each-blocks-empty/input.html delete mode 100644 test/parser/samples/error-each-blocks-keyed-whitespace/error.json delete mode 100644 test/parser/samples/error-each-blocks-keyed-whitespace/input.html delete mode 100644 test/parser/samples/error-each-blocks-whitespace/error.json delete mode 100644 test/parser/samples/error-each-blocks-whitespace/input.html create mode 100644 test/validator/samples/empty-block-dev.solo/_config.js create mode 100644 test/validator/samples/empty-block-dev.solo/input.html rename test/{parser/samples/error-each-blocks-empty/error.json => validator/samples/empty-block-dev.solo/warnings.json} (93%) create mode 100644 test/validator/samples/empty-block-prod/input.html create mode 100644 test/validator/samples/empty-block-prod/warnings.json diff --git a/src/parse/state/mustache.ts b/src/parse/state/mustache.ts index 20331368d8..6e44f7c15e 100644 --- a/src/parse/state/mustache.ts +++ b/src/parse/state/mustache.ts @@ -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); diff --git a/src/validate/html/index.ts b/src/validate/html/index.ts index b2a190696f..9ae50baca1 100644 --- a/src/validate/html/index.ts +++ b/src/validate/html/index.ts @@ -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); diff --git a/src/validate/index.ts b/src/validate/index.ts index b57f2b0c2a..da603a437b 100644 --- a/src/validate/index.ts +++ b/src/validate/index.ts @@ -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) { diff --git a/test/parser/samples/error-each-blocks-empty/input.html b/test/parser/samples/error-each-blocks-empty/input.html deleted file mode 100644 index 00b4b07d5a..0000000000 --- a/test/parser/samples/error-each-blocks-empty/input.html +++ /dev/null @@ -1 +0,0 @@ -{{#each foos as foo}}{{/each}} diff --git a/test/parser/samples/error-each-blocks-keyed-whitespace/error.json b/test/parser/samples/error-each-blocks-keyed-whitespace/error.json deleted file mode 100644 index 6ecdc198bd..0000000000 --- a/test/parser/samples/error-each-blocks-keyed-whitespace/error.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "message": "Empty block", - "loc": { - "line": 1, - "column": 0 - }, - "pos": 0 -} diff --git a/test/parser/samples/error-each-blocks-keyed-whitespace/input.html b/test/parser/samples/error-each-blocks-keyed-whitespace/input.html deleted file mode 100644 index 6984d4ae88..0000000000 --- a/test/parser/samples/error-each-blocks-keyed-whitespace/input.html +++ /dev/null @@ -1,11 +0,0 @@ -{{#each foos as foo @id}} -{{/each}} - diff --git a/test/parser/samples/error-each-blocks-whitespace/error.json b/test/parser/samples/error-each-blocks-whitespace/error.json deleted file mode 100644 index 6ecdc198bd..0000000000 --- a/test/parser/samples/error-each-blocks-whitespace/error.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "message": "Empty block", - "loc": { - "line": 1, - "column": 0 - }, - "pos": 0 -} diff --git a/test/parser/samples/error-each-blocks-whitespace/input.html b/test/parser/samples/error-each-blocks-whitespace/input.html deleted file mode 100644 index 2b4af0cac6..0000000000 --- a/test/parser/samples/error-each-blocks-whitespace/input.html +++ /dev/null @@ -1,11 +0,0 @@ -{{#each foos as foo}} -{{/each}} - diff --git a/test/validator/index.js b/test/validator/index.js index 176d060faf..0a132a7c9e 100644 --- a/test/validator/index.js +++ b/test/validator/index.js @@ -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); diff --git a/test/validator/samples/empty-block-dev.solo/_config.js b/test/validator/samples/empty-block-dev.solo/_config.js new file mode 100644 index 0000000000..e26996239d --- /dev/null +++ b/test/validator/samples/empty-block-dev.solo/_config.js @@ -0,0 +1,3 @@ +export default { + dev: true +}; \ No newline at end of file diff --git a/test/validator/samples/empty-block-dev.solo/input.html b/test/validator/samples/empty-block-dev.solo/input.html new file mode 100644 index 0000000000..53e2ca8333 --- /dev/null +++ b/test/validator/samples/empty-block-dev.solo/input.html @@ -0,0 +1,3 @@ +{{#each things as thing}} + +{{/each}} \ No newline at end of file diff --git a/test/parser/samples/error-each-blocks-empty/error.json b/test/validator/samples/empty-block-dev.solo/warnings.json similarity index 93% rename from test/parser/samples/error-each-blocks-empty/error.json rename to test/validator/samples/empty-block-dev.solo/warnings.json index 6ecdc198bd..6cbc567ada 100644 --- a/test/parser/samples/error-each-blocks-empty/error.json +++ b/test/validator/samples/empty-block-dev.solo/warnings.json @@ -1,8 +1,8 @@ -{ +[{ "message": "Empty block", "loc": { "line": 1, "column": 0 }, "pos": 0 -} +}] \ No newline at end of file diff --git a/test/validator/samples/empty-block-prod/input.html b/test/validator/samples/empty-block-prod/input.html new file mode 100644 index 0000000000..53e2ca8333 --- /dev/null +++ b/test/validator/samples/empty-block-prod/input.html @@ -0,0 +1,3 @@ +{{#each things as thing}} + +{{/each}} \ No newline at end of file diff --git a/test/validator/samples/empty-block-prod/warnings.json b/test/validator/samples/empty-block-prod/warnings.json new file mode 100644 index 0000000000..0637a088a0 --- /dev/null +++ b/test/validator/samples/empty-block-prod/warnings.json @@ -0,0 +1 @@ +[] \ No newline at end of file From b4edc7c01e420ced0961f322a91c847bca6e349e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 10 Feb 2018 11:51:24 -0500 Subject: [PATCH 5/7] doh --- .../samples/{empty-block-dev.solo => empty-block-dev}/_config.js | 0 .../samples/{empty-block-dev.solo => empty-block-dev}/input.html | 0 .../{empty-block-dev.solo => empty-block-dev}/warnings.json | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename test/validator/samples/{empty-block-dev.solo => empty-block-dev}/_config.js (100%) rename test/validator/samples/{empty-block-dev.solo => empty-block-dev}/input.html (100%) rename test/validator/samples/{empty-block-dev.solo => empty-block-dev}/warnings.json (100%) diff --git a/test/validator/samples/empty-block-dev.solo/_config.js b/test/validator/samples/empty-block-dev/_config.js similarity index 100% rename from test/validator/samples/empty-block-dev.solo/_config.js rename to test/validator/samples/empty-block-dev/_config.js diff --git a/test/validator/samples/empty-block-dev.solo/input.html b/test/validator/samples/empty-block-dev/input.html similarity index 100% rename from test/validator/samples/empty-block-dev.solo/input.html rename to test/validator/samples/empty-block-dev/input.html diff --git a/test/validator/samples/empty-block-dev.solo/warnings.json b/test/validator/samples/empty-block-dev/warnings.json similarity index 100% rename from test/validator/samples/empty-block-dev.solo/warnings.json rename to test/validator/samples/empty-block-dev/warnings.json From 4c6b3b79da5a901b42cf5c25106d31dd0844040f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 10 Feb 2018 22:08:39 -0500 Subject: [PATCH 6/7] increase test coverage, handle immediately-closed blocks --- src/parse/state/mustache.ts | 2 +- .../samples/empty-block-dev/input.html | 4 +++- .../samples/empty-block-dev/warnings.json | 24 +++++++++++++------ .../samples/empty-block-prod/input.html | 4 +++- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/parse/state/mustache.ts b/src/parse/state/mustache.ts index 6e44f7c15e..0a255b1256 100644 --- a/src/parse/state/mustache.ts +++ b/src/parse/state/mustache.ts @@ -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]; diff --git a/test/validator/samples/empty-block-dev/input.html b/test/validator/samples/empty-block-dev/input.html index 53e2ca8333..32b892d470 100644 --- a/test/validator/samples/empty-block-dev/input.html +++ b/test/validator/samples/empty-block-dev/input.html @@ -1,3 +1,5 @@ {{#each things as thing}} -{{/each}} \ No newline at end of file +{{/each}} + +{{#each things as thing}}{{/each}} \ No newline at end of file diff --git a/test/validator/samples/empty-block-dev/warnings.json b/test/validator/samples/empty-block-dev/warnings.json index 6cbc567ada..54584bf135 100644 --- a/test/validator/samples/empty-block-dev/warnings.json +++ b/test/validator/samples/empty-block-dev/warnings.json @@ -1,8 +1,18 @@ -[{ - "message": "Empty block", - "loc": { - "line": 1, - "column": 0 +[ + { + "message": "Empty block", + "loc": { + "line": 1, + "column": 0 + }, + "pos": 0 }, - "pos": 0 -}] \ No newline at end of file + { + "message": "Empty block", + "loc": { + "line": 5, + "column": 0 + }, + "pos": 38 + } +] \ No newline at end of file diff --git a/test/validator/samples/empty-block-prod/input.html b/test/validator/samples/empty-block-prod/input.html index 53e2ca8333..32b892d470 100644 --- a/test/validator/samples/empty-block-prod/input.html +++ b/test/validator/samples/empty-block-prod/input.html @@ -1,3 +1,5 @@ {{#each things as thing}} -{{/each}} \ No newline at end of file +{{/each}} + +{{#each things as thing}}{{/each}} \ No newline at end of file From 991c9b42aea23ca352c4f0e152bb2706f283d067 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 10 Feb 2018 23:29:47 -0500 Subject: [PATCH 7/7] increase test coverage --- .../samples/each-block-multiple-children/_config.js | 3 +++ .../samples/each-block-multiple-children/input.html | 10 ++++++++++ .../samples/each-block-multiple-children/warnings.json | 1 + 3 files changed, 14 insertions(+) create mode 100644 test/validator/samples/each-block-multiple-children/_config.js create mode 100644 test/validator/samples/each-block-multiple-children/input.html create mode 100644 test/validator/samples/each-block-multiple-children/warnings.json diff --git a/test/validator/samples/each-block-multiple-children/_config.js b/test/validator/samples/each-block-multiple-children/_config.js new file mode 100644 index 0000000000..e26996239d --- /dev/null +++ b/test/validator/samples/each-block-multiple-children/_config.js @@ -0,0 +1,3 @@ +export default { + dev: true +}; \ No newline at end of file diff --git a/test/validator/samples/each-block-multiple-children/input.html b/test/validator/samples/each-block-multiple-children/input.html new file mode 100644 index 0000000000..9d996a3acf --- /dev/null +++ b/test/validator/samples/each-block-multiple-children/input.html @@ -0,0 +1,10 @@ +{{#each things as thing}} + this only exists... + ...to increase test coverage +{{/each}} + +{{#each things as thing}}{{soDoesThis}}{{/each}} + +{{#each things as thing}} + andThis +{{/each}} \ No newline at end of file diff --git a/test/validator/samples/each-block-multiple-children/warnings.json b/test/validator/samples/each-block-multiple-children/warnings.json new file mode 100644 index 0000000000..0637a088a0 --- /dev/null +++ b/test/validator/samples/each-block-multiple-children/warnings.json @@ -0,0 +1 @@ +[] \ No newline at end of file