Skip to content

Commit

Permalink
Merge pull request #794 from sveltejs/gh-550
Browse files Browse the repository at this point in the history
attach options to component (#550)
  • Loading branch information
Rich-Harris authored Aug 29, 2017
2 parents 502f6b6 + f50a6c5 commit ce109f0
Show file tree
Hide file tree
Showing 30 changed files with 58 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/generators/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 )`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/collapses-text-around-comments/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/computed-collapsed-if/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/computed-collapsed-if/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/css-media-query/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/css-media-query/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/each-block-changed-check/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/event-handlers-custom/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/event-handlers-custom/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/if-block-no-update/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ function select_block_type ( state ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/if-block-no-update/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function select_block_type ( state ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/if-block-simple/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function create_if_block ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/if-block-simple/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function create_if_block ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/legacy-input-type/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/legacy-input-type/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/non-imported-component/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/non-imported-component/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/onrender-onteardown-rewritten/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/setup-method/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/setup-method/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function create_main_fragment ( state, component ) {
}

function SvelteComponent ( options ) {
options = options || {};
this.options = options;
this._state = options.data || {};

this._observers = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/use-elements-as-anchors/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/use-elements-as-anchors/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
25 changes: 9 additions & 16 deletions test/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions test/runtime/samples/options/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
'skip-ssr': true,
html: `<p>from this.options</p>`,

options: {
text: 'from this.options'
},

test(assert, component) {
assert.equal(component.options.text, 'from this.options');
}
};
11 changes: 11 additions & 0 deletions test/runtime/samples/options/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p>{{text}}</p>

<script>
export default {
oncreate() {
this.set({
text: this.options.text
});
}
};
</script>
8 changes: 0 additions & 8 deletions test/runtime/samples/pass-no-options/_config.js

This file was deleted.

1 change: 0 additions & 1 deletion test/runtime/samples/pass-no-options/main.html

This file was deleted.

0 comments on commit ce109f0

Please sign in to comment.