From d8269b3e7fd46b001f73d4b7a5570b2bd79793c5 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 29 Aug 2017 08:37:27 -0400 Subject: [PATCH 1/3] attach options to component (#550) --- src/generators/dom/index.ts | 4 ++-- test/runtime/index.js | 25 +++++++++---------------- test/runtime/samples/options/_config.js | 12 ++++++++++++ test/runtime/samples/options/main.html | 11 +++++++++++ 4 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 test/runtime/samples/options/_config.js create mode 100644 test/runtime/samples/options/main.html diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index f606e61d8cd4..6003c57c8fb6 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -151,9 +151,9 @@ export default function dom( // TODO deprecate component.teardown() builder.addBlock(deindent` function ${name} ( options ) { - options = options || {}; ${options.dev && - `if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );`} + `if ( !options || (!options.target && !options._root) ) throw new Error( "'target' is a required option" );`} + this.options = options; ${generator.usesRefs && `this.refs = {};`} this._state = ${templateProperties.data ? `@assign( @template.data(), options.data )` diff --git a/test/runtime/index.js b/test/runtime/index.js index 5b109dcd324c..78415ac42881 100644 --- a/test/runtime/index.js +++ b/test/runtime/index.js @@ -81,6 +81,12 @@ describe("runtime", () => { code.slice(0, startIndex).split('\n').map(x => spaces(x.length)).join('\n') + code.slice(startIndex).replace(/export default .+/, ""); acorn.parse(es5, { ecmaVersion: 5 }); + + if (/Object\.assign/.test(es5)) { + throw new Error( + "cannot use Object.assign in generated code, as it is not supported everywhere" + ); + } } catch (err) { failed.add(dir); showOutput(cwd, { shared }); // eslint-disable-line no-console @@ -133,11 +139,6 @@ describe("runtime", () => { throw err; } - let usedObjectAssign = false; - Object.assign = () => { - usedObjectAssign = true; - }; - global.window = window; // Put the constructor on window for testing @@ -151,13 +152,13 @@ describe("runtime", () => { warnings.push(warning); }; - const component = new SvelteComponent({ + const options = Object.assign({}, { target, hydrate, data: config.data - }); + }, config.options || {}); - Object.assign = Object_assign; + const component = new SvelteComponent(options); console.warn = warn; @@ -183,15 +184,7 @@ describe("runtime", () => { component.destroy(); assert.equal(target.innerHTML, ""); } - - if (usedObjectAssign) { - throw new Error( - "cannot use Object.assign in generated code, as it is not supported everywhere" - ); - } } catch (err) { - Object.assign = Object_assign; - if (config.error && !unintendedError) { config.error(assert, err); } else { diff --git a/test/runtime/samples/options/_config.js b/test/runtime/samples/options/_config.js new file mode 100644 index 000000000000..6f321d7a6f98 --- /dev/null +++ b/test/runtime/samples/options/_config.js @@ -0,0 +1,12 @@ +export default { + 'skip-ssr': true, + html: `

from this.options

`, + + options: { + text: 'from this.options' + }, + + test(assert, component) { + assert.equal(component.options.text, 'from this.options'); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/options/main.html b/test/runtime/samples/options/main.html new file mode 100644 index 000000000000..2cd633923535 --- /dev/null +++ b/test/runtime/samples/options/main.html @@ -0,0 +1,11 @@ +

{{text}}

+ + \ No newline at end of file From 5f5f09b94e3c06e58393f002c6bc4e497800cdb8 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 29 Aug 2017 08:43:11 -0400 Subject: [PATCH 2/3] update snapshot tests --- .../samples/collapses-text-around-comments/expected-bundle.js | 2 +- test/js/samples/collapses-text-around-comments/expected.js | 2 +- test/js/samples/computed-collapsed-if/expected-bundle.js | 2 +- test/js/samples/computed-collapsed-if/expected.js | 2 +- test/js/samples/css-media-query/expected-bundle.js | 2 +- test/js/samples/css-media-query/expected.js | 2 +- test/js/samples/each-block-changed-check/expected-bundle.js | 2 +- test/js/samples/each-block-changed-check/expected.js | 2 +- test/js/samples/event-handlers-custom/expected-bundle.js | 2 +- test/js/samples/event-handlers-custom/expected.js | 2 +- test/js/samples/if-block-no-update/expected-bundle.js | 2 +- test/js/samples/if-block-no-update/expected.js | 2 +- test/js/samples/if-block-simple/expected-bundle.js | 2 +- test/js/samples/if-block-simple/expected.js | 2 +- test/js/samples/legacy-input-type/expected-bundle.js | 2 +- test/js/samples/legacy-input-type/expected.js | 2 +- test/js/samples/non-imported-component/expected-bundle.js | 2 +- test/js/samples/non-imported-component/expected.js | 2 +- .../js/samples/onrender-onteardown-rewritten/expected-bundle.js | 2 +- test/js/samples/onrender-onteardown-rewritten/expected.js | 2 +- test/js/samples/setup-method/expected-bundle.js | 2 +- test/js/samples/setup-method/expected.js | 2 +- test/js/samples/use-elements-as-anchors/expected-bundle.js | 2 +- test/js/samples/use-elements-as-anchors/expected.js | 2 +- 24 files changed, 24 insertions(+), 24 deletions(-) diff --git a/test/js/samples/collapses-text-around-comments/expected-bundle.js b/test/js/samples/collapses-text-around-comments/expected-bundle.js index 49416ae54cc2..9d9d6a7f3d3e 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle.js @@ -217,7 +217,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = assign( template.data(), options.data ); this._observers = { diff --git a/test/js/samples/collapses-text-around-comments/expected.js b/test/js/samples/collapses-text-around-comments/expected.js index dec0b39b8eb3..88dd6ab34f95 100644 --- a/test/js/samples/collapses-text-around-comments/expected.js +++ b/test/js/samples/collapses-text-around-comments/expected.js @@ -53,7 +53,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = assign( template.data(), options.data ); this._observers = { diff --git a/test/js/samples/computed-collapsed-if/expected-bundle.js b/test/js/samples/computed-collapsed-if/expected-bundle.js index cce5a11ef6fb..36b0c20402da 100644 --- a/test/js/samples/computed-collapsed-if/expected-bundle.js +++ b/test/js/samples/computed-collapsed-if/expected-bundle.js @@ -165,7 +165,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._recompute( {}, this._state, {}, true ); diff --git a/test/js/samples/computed-collapsed-if/expected.js b/test/js/samples/computed-collapsed-if/expected.js index 91e45d405753..340622cd28ae 100644 --- a/test/js/samples/computed-collapsed-if/expected.js +++ b/test/js/samples/computed-collapsed-if/expected.js @@ -25,7 +25,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._recompute( {}, this._state, {}, true ); diff --git a/test/js/samples/css-media-query/expected-bundle.js b/test/js/samples/css-media-query/expected-bundle.js index 721ea74084ea..a9b624b079dc 100644 --- a/test/js/samples/css-media-query/expected-bundle.js +++ b/test/js/samples/css-media-query/expected-bundle.js @@ -199,7 +199,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/css-media-query/expected.js b/test/js/samples/css-media-query/expected.js index 1431442a3d45..86167afeb1bb 100644 --- a/test/js/samples/css-media-query/expected.js +++ b/test/js/samples/css-media-query/expected.js @@ -39,7 +39,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index 51a29b34f356..a09dc68e4803 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -313,7 +313,7 @@ function create_each_block ( state, each_block_value, comment, i, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index f7ad013def9b..b81c64ec93e4 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -140,7 +140,7 @@ function create_each_block ( state, each_block_value, comment, i, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/event-handlers-custom/expected-bundle.js b/test/js/samples/event-handlers-custom/expected-bundle.js index 004eeb61325f..ba38124b0a1f 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -210,7 +210,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/event-handlers-custom/expected.js b/test/js/samples/event-handlers-custom/expected.js index e64db2d07450..e713056e73b8 100644 --- a/test/js/samples/event-handlers-custom/expected.js +++ b/test/js/samples/event-handlers-custom/expected.js @@ -50,7 +50,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/if-block-no-update/expected-bundle.js b/test/js/samples/if-block-no-update/expected-bundle.js index cefae921f5d6..4e9a0bcf69b0 100644 --- a/test/js/samples/if-block-no-update/expected-bundle.js +++ b/test/js/samples/if-block-no-update/expected-bundle.js @@ -252,7 +252,7 @@ function select_block_type ( state ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/if-block-no-update/expected.js b/test/js/samples/if-block-no-update/expected.js index 0f4dcf49e785..ca082058f4be 100644 --- a/test/js/samples/if-block-no-update/expected.js +++ b/test/js/samples/if-block-no-update/expected.js @@ -88,7 +88,7 @@ function select_block_type ( state ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/if-block-simple/expected-bundle.js b/test/js/samples/if-block-simple/expected-bundle.js index d34df63b58ae..230b781c42b4 100644 --- a/test/js/samples/if-block-simple/expected-bundle.js +++ b/test/js/samples/if-block-simple/expected-bundle.js @@ -228,7 +228,7 @@ function create_if_block ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/if-block-simple/expected.js b/test/js/samples/if-block-simple/expected.js index 51c9064f95e3..1eedecd1477a 100644 --- a/test/js/samples/if-block-simple/expected.js +++ b/test/js/samples/if-block-simple/expected.js @@ -64,7 +64,7 @@ function create_if_block ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/legacy-input-type/expected-bundle.js b/test/js/samples/legacy-input-type/expected-bundle.js index 2dd18e4215d0..d636262a364b 100644 --- a/test/js/samples/legacy-input-type/expected-bundle.js +++ b/test/js/samples/legacy-input-type/expected-bundle.js @@ -186,7 +186,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/legacy-input-type/expected.js b/test/js/samples/legacy-input-type/expected.js index ebadff470f33..33a277bb4a8b 100644 --- a/test/js/samples/legacy-input-type/expected.js +++ b/test/js/samples/legacy-input-type/expected.js @@ -28,7 +28,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/non-imported-component/expected-bundle.js b/test/js/samples/non-imported-component/expected-bundle.js index 045d00b6f902..507161555c2c 100644 --- a/test/js/samples/non-imported-component/expected-bundle.js +++ b/test/js/samples/non-imported-component/expected-bundle.js @@ -202,7 +202,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/non-imported-component/expected.js b/test/js/samples/non-imported-component/expected.js index a335e999b95c..af524aa1aa33 100644 --- a/test/js/samples/non-imported-component/expected.js +++ b/test/js/samples/non-imported-component/expected.js @@ -50,7 +50,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js index eeb33a8bd1e7..df1a55cedadb 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js @@ -164,7 +164,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/onrender-onteardown-rewritten/expected.js b/test/js/samples/onrender-onteardown-rewritten/expected.js index 4116f8fd06a2..ca473e83c7d1 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected.js @@ -24,7 +24,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/setup-method/expected-bundle.js b/test/js/samples/setup-method/expected-bundle.js index 903904bcbd70..db41f989f34b 100644 --- a/test/js/samples/setup-method/expected-bundle.js +++ b/test/js/samples/setup-method/expected-bundle.js @@ -175,7 +175,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/setup-method/expected.js b/test/js/samples/setup-method/expected.js index 2535d611c834..9e33fb392148 100644 --- a/test/js/samples/setup-method/expected.js +++ b/test/js/samples/setup-method/expected.js @@ -35,7 +35,7 @@ function create_main_fragment ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/use-elements-as-anchors/expected-bundle.js b/test/js/samples/use-elements-as-anchors/expected-bundle.js index ad4b5ee79be0..bb489dfe5c6a 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle.js @@ -412,7 +412,7 @@ function create_if_block_4 ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { diff --git a/test/js/samples/use-elements-as-anchors/expected.js b/test/js/samples/use-elements-as-anchors/expected.js index fba00a491f9e..f65d1fe2881b 100644 --- a/test/js/samples/use-elements-as-anchors/expected.js +++ b/test/js/samples/use-elements-as-anchors/expected.js @@ -248,7 +248,7 @@ function create_if_block_4 ( state, component ) { } function SvelteComponent ( options ) { - options = options || {}; + this.options = options; this._state = options.data || {}; this._observers = { From f50a6c50af7c67b73bc91035fafa40d2c50cc10e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 29 Aug 2017 08:43:31 -0400 Subject: [PATCH 3/3] remove test of instantiation without options, its nonsensical --- test/runtime/samples/pass-no-options/_config.js | 8 -------- test/runtime/samples/pass-no-options/main.html | 1 - 2 files changed, 9 deletions(-) delete mode 100644 test/runtime/samples/pass-no-options/_config.js delete mode 100644 test/runtime/samples/pass-no-options/main.html diff --git a/test/runtime/samples/pass-no-options/_config.js b/test/runtime/samples/pass-no-options/_config.js deleted file mode 100644 index 9683944f21b8..000000000000 --- a/test/runtime/samples/pass-no-options/_config.js +++ /dev/null @@ -1,8 +0,0 @@ -export default { - html: '

Just some static HTML

', - - test ( assert, component, target, window ) { - const newComp = new window.SvelteComponent(); - assert.equal(newComp instanceof window.SvelteComponent, true); - } -}; diff --git a/test/runtime/samples/pass-no-options/main.html b/test/runtime/samples/pass-no-options/main.html deleted file mode 100644 index bbf302b39ef3..000000000000 --- a/test/runtime/samples/pass-no-options/main.html +++ /dev/null @@ -1 +0,0 @@ -

Just some static HTML