From ed23d7acb34f1da791082ca27b44d52faac79284 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 2 Dec 2018 17:24:42 -0500 Subject: [PATCH] various tweaks --- src/compile/Component.ts | 1 - src/compile/css/Stylesheet.ts | 6 +++--- src/compile/render-dom/Block.ts | 14 +++++++------- src/compile/render-dom/index.ts | 8 ++++---- src/compile/render-ssr/index.ts | 3 ++- src/parse/state/tag.ts | 12 +++++++----- test/js/index.js | 2 +- test/js/samples/computed-collapsed-if/expected.js | 2 +- test/js/samples/debug-foo/expected.js | 1 - test/js/samples/if-block-no-update/expected.js | 1 - test/js/samples/media-bindings/expected.js | 6 +++--- test/js/samples/ssr-preserve-comments/expected.js | 2 ++ test/js/samples/title/expected.js | 3 --- test/server-side-rendering/index.js | 2 +- 14 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/compile/Component.ts b/src/compile/Component.ts index bb6c5c1c9a..b3a3f80ec1 100644 --- a/src/compile/Component.ts +++ b/src/compile/Component.ts @@ -204,7 +204,6 @@ export default class Component { }); const importedHelpers = Array.from(helpers) - .concat(options.dev ? 'SvelteComponentDev' : 'SvelteComponent') .sort() .map(name => { const alias = this.alias(name); diff --git a/src/compile/css/Stylesheet.ts b/src/compile/css/Stylesheet.ts index 223f865996..dcb3fc5a7f 100644 --- a/src/compile/css/Stylesheet.ts +++ b/src/compile/css/Stylesheet.ts @@ -116,9 +116,9 @@ class Declaration { if (!this.node.property) return; // @apply, and possibly other weird cases? const c = this.node.start + this.node.property.length; - const first = this.node.value.children ? - this.node.value.children[0] : - this.node.value; + const first = this.node.value.children + ? this.node.value.children[0] + : this.node.value; let start = first.start; while (/\s/.test(code.original[start])) start += 1; diff --git a/src/compile/render-dom/Block.ts b/src/compile/render-dom/Block.ts index db8d846232..12b7cca061 100644 --- a/src/compile/render-dom/Block.ts +++ b/src/compile/render-dom/Block.ts @@ -246,7 +246,7 @@ export default class Block { } if (this.builders.create.isEmpty() && this.builders.hydrate.isEmpty()) { - properties.addBlock(`c: @noop,`); + properties.addLine(`c: @noop,`); } else { const hydrate = !this.builders.hydrate.isEmpty() && ( this.renderer.options.hydratable @@ -264,7 +264,7 @@ export default class Block { if (this.renderer.options.hydratable || !this.builders.claim.isEmpty()) { if (this.builders.claim.isEmpty() && this.builders.hydrate.isEmpty()) { - properties.addBlock(`l: @noop,`); + properties.addLine(`l: @noop,`); } else { properties.addBlock(deindent` ${dev ? 'l: function claim' : 'l'}(nodes) { @@ -284,7 +284,7 @@ export default class Block { } if (this.builders.mount.isEmpty()) { - properties.addBlock(`m: @noop,`); + properties.addLine(`m: @noop,`); } else { properties.addBlock(deindent` ${dev ? 'm: function mount' : 'm'}(#target, anchor) { @@ -295,7 +295,7 @@ export default class Block { if (this.hasUpdateMethod || this.maintainContext) { if (this.builders.update.isEmpty() && !this.maintainContext) { - properties.addBlock(`p: @noop,`); + properties.addLine(`p: @noop,`); } else { properties.addBlock(deindent` ${dev ? 'p: function update' : 'p'}(changed, ${this.maintainContext ? 'new_ctx' : 'ctx'}) { @@ -324,7 +324,7 @@ export default class Block { if (this.hasIntroMethod || this.hasOutroMethod) { if (this.builders.mount.isEmpty()) { - properties.addBlock(`i: @noop,`); + properties.addLine(`i: @noop,`); } else { properties.addBlock(deindent` ${dev ? 'i: function intro' : 'i'}(#target, anchor) { @@ -336,7 +336,7 @@ export default class Block { } if (this.builders.outro.isEmpty()) { - properties.addBlock(`o: @run,`); + properties.addLine(`o: @run,`); } else { properties.addBlock(deindent` ${dev ? 'o: function outro' : 'o'}(#outrocallback) { @@ -351,7 +351,7 @@ export default class Block { } if (this.builders.destroy.isEmpty()) { - properties.addBlock(`d: @noop`); + properties.addLine(`d: @noop`); } else { properties.addBlock(deindent` ${dev ? 'd: function destroy' : 'd'}(detach) { diff --git a/src/compile/render-dom/index.ts b/src/compile/render-dom/index.ts index c07806c7d4..d54aab52ff 100644 --- a/src/compile/render-dom/index.ts +++ b/src/compile/render-dom/index.ts @@ -53,8 +53,6 @@ export default function dom( const refs = Array.from(component.refs); - const superclass = component.alias(options.dev ? 'SvelteComponentDev' : 'SvelteComponent'); - if (options.dev && !options.hydratable) { block.builders.claim.addLine( 'throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");' @@ -182,7 +180,7 @@ export default function dom( ${component.fully_hoisted.length > 0 && component.fully_hoisted.join('\n\n')} `); - let has_definition = ( + const has_definition = ( component.javascript || component.props.length > 0 || component.partly_hoisted.length > 0 || @@ -246,8 +244,10 @@ export default function dom( customElements.define("${component.tag}", ${name}); `); } else { + const superclass = options.dev ? 'SvelteComponentDev' : 'SvelteComponent'; + builder.addBlock(deindent` - class ${name} extends ${superclass} { + class ${name} extends @${superclass} { constructor(options) { super(${options.dev && `options`}); ${should_add_css && `if (!document.getElementById("${component.stylesheet.id}-style")) @add_css();`} diff --git a/src/compile/render-ssr/index.ts b/src/compile/render-ssr/index.ts index ffe2f24fb4..f702cb4948 100644 --- a/src/compile/render-ssr/index.ts +++ b/src/compile/render-ssr/index.ts @@ -23,11 +23,12 @@ export default function ssr( // TODO concatenate CSS maps return (deindent` + ${component.javascript && deindent` function #define($$props) { ${component.javascript} return { ${component.declarations.join(', ')} }; - } + }`} var ${name} = {}; diff --git a/src/parse/state/tag.ts b/src/parse/state/tag.ts index be39fb37ad..c34f3d5d2a 100644 --- a/src/parse/state/tag.ts +++ b/src/parse/state/tag.ts @@ -373,16 +373,18 @@ function readAttribute(parser: Parser, uniqueNames: Set) { uniqueNames.add(name); + let end = parser.index; + parser.allowWhitespace(); const colon_index = name.indexOf(':'); const type = colon_index !== 1 && get_directive_type(name.slice(0, colon_index)); - const value = parser.eat('=') - ? readAttributeValue(parser) - : true; - - const end = parser.index; + let value: any[] | true = true; + if (parser.eat('=')) { + value = readAttributeValue(parser); + end = parser.index; + } if (type) { const directive_name = name.slice(colon_index + 1); diff --git a/test/js/index.js b/test/js/index.js index 3ee1a1da2d..71416d8cf3 100644 --- a/test/js/index.js +++ b/test/js/index.js @@ -3,7 +3,7 @@ import * as fs from "fs"; import * as path from "path"; import { loadConfig, svelte } from "../helpers.js"; -describe("js", () => { +describe.only("js", () => { fs.readdirSync("test/js/samples").forEach(dir => { if (dir[0] === ".") return; diff --git a/test/js/samples/computed-collapsed-if/expected.js b/test/js/samples/computed-collapsed-if/expected.js index 3d7fd15dec..86110d6dd2 100644 --- a/test/js/samples/computed-collapsed-if/expected.js +++ b/test/js/samples/computed-collapsed-if/expected.js @@ -19,7 +19,7 @@ function create_fragment(component, ctx) { }; } -function define($$self, $$props, $$make_dirty) { +function define($$self, $$props) { let { x } = $$props; function a() { diff --git a/test/js/samples/debug-foo/expected.js b/test/js/samples/debug-foo/expected.js index 50efda841a..5e79d5280f 100644 --- a/test/js/samples/debug-foo/expected.js +++ b/test/js/samples/debug-foo/expected.js @@ -143,7 +143,6 @@ function create_fragment(component, ctx) { function define($$self, $$props) { let { things, foo } = $$props; - // TODO only what's needed by the template $$self.$$.get = () => ({ things, foo }); $$self.$$.set = $$props => { diff --git a/test/js/samples/if-block-no-update/expected.js b/test/js/samples/if-block-no-update/expected.js index 672d315162..d7521ecf0d 100644 --- a/test/js/samples/if-block-no-update/expected.js +++ b/test/js/samples/if-block-no-update/expected.js @@ -97,7 +97,6 @@ function create_fragment(component, ctx) { function define($$self, $$props) { let { foo } = $$props; - // TODO only what's needed by the template $$self.$$.get = () => ({ foo }); $$self.$$.set = $$props => { diff --git a/test/js/samples/media-bindings/expected.js b/test/js/samples/media-bindings/expected.js index d98e5dbf2d..d8f4f39801 100644 --- a/test/js/samples/media-bindings/expected.js +++ b/test/js/samples/media-bindings/expected.js @@ -29,8 +29,8 @@ function create_fragment(component, ctx) { }, p(changed, ctx) { - if (!audio_updating && !isNaN(ctx.currentTime ) && changed.currentTime) audio.currentTime = ctx.currentTime ; - if (!audio_updating && audio_is_paused !== (audio_is_paused = ctx.paused ) && changed.paused) audio[audio_is_paused ? "pause" : "play"](); + if (!audio_updating && !isNaN(ctx.currentTime) && changed.currentTime) audio.currentTime = ctx.currentTime; + if (!audio_updating && audio_is_paused !== (audio_is_paused = ctx.paused) && changed.paused) audio[audio_is_paused ? "pause" : "play"](); if (!audio_updating && !isNaN(ctx.volume) && changed.volume) audio.volume = ctx.volume; }, @@ -58,7 +58,7 @@ function create_fragment(component, ctx) { }; } -function define($$self, $$make_dirty) { +function define($$self, $$props, $$make_dirty) { // TODO some of these are read-only... let { buffered, seekable, played, currentTime, duration, paused, volume } = $$props; diff --git a/test/js/samples/ssr-preserve-comments/expected.js b/test/js/samples/ssr-preserve-comments/expected.js index 06e84fbf5e..0c688b87c5 100644 --- a/test/js/samples/ssr-preserve-comments/expected.js +++ b/test/js/samples/ssr-preserve-comments/expected.js @@ -1,4 +1,5 @@ /* generated by Svelte vX.Y.Z-alpha1 */ + var SvelteComponent = {}; SvelteComponent.render = function(props = {}, options = {}) { @@ -37,4 +38,5 @@ SvelteComponent.css = { }; var warned = false; + export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/title/expected.js b/test/js/samples/title/expected.js index 4f74ed9745..5ae1a3da4d 100644 --- a/test/js/samples/title/expected.js +++ b/test/js/samples/title/expected.js @@ -8,7 +8,6 @@ function create_fragment(component, ctx) { return { c: noop, - m: noop, p(changed, ctx) { @@ -18,9 +17,7 @@ function create_fragment(component, ctx) { }, i: noop, - o: run, - d: noop }; } diff --git a/test/server-side-rendering/index.js b/test/server-side-rendering/index.js index a8d68aeaca..de57f49090 100644 --- a/test/server-side-rendering/index.js +++ b/test/server-side-rendering/index.js @@ -21,7 +21,7 @@ function tryToReadFile(file) { const sveltePath = process.cwd(); -describe.only("ssr", () => { +describe("ssr", () => { before(() => { require("../../register")({ extensions: ['.svelte', '.html'],