Skip to content

Commit ce109f0

Browse files
authored
Merge pull request #794 from sveltejs/gh-550
attach options to component (#550)
2 parents 502f6b6 + f50a6c5 commit ce109f0

File tree

30 files changed

+58
-51
lines changed

30 files changed

+58
-51
lines changed

src/generators/dom/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ export default function dom(
151151
// TODO deprecate component.teardown()
152152
builder.addBlock(deindent`
153153
function ${name} ( options ) {
154-
options = options || {};
155154
${options.dev &&
156-
`if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );`}
155+
`if ( !options || (!options.target && !options._root) ) throw new Error( "'target' is a required option" );`}
156+
this.options = options;
157157
${generator.usesRefs && `this.refs = {};`}
158158
this._state = ${templateProperties.data
159159
? `@assign( @template.data(), options.data )`

test/js/samples/collapses-text-around-comments/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ function create_main_fragment ( state, component ) {
217217
}
218218

219219
function SvelteComponent ( options ) {
220-
options = options || {};
220+
this.options = options;
221221
this._state = assign( template.data(), options.data );
222222

223223
this._observers = {

test/js/samples/collapses-text-around-comments/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function create_main_fragment ( state, component ) {
5353
}
5454

5555
function SvelteComponent ( options ) {
56-
options = options || {};
56+
this.options = options;
5757
this._state = assign( template.data(), options.data );
5858

5959
this._observers = {

test/js/samples/computed-collapsed-if/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function create_main_fragment ( state, component ) {
165165
}
166166

167167
function SvelteComponent ( options ) {
168-
options = options || {};
168+
this.options = options;
169169
this._state = options.data || {};
170170
this._recompute( {}, this._state, {}, true );
171171

test/js/samples/computed-collapsed-if/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function create_main_fragment ( state, component ) {
2525
}
2626

2727
function SvelteComponent ( options ) {
28-
options = options || {};
28+
this.options = options;
2929
this._state = options.data || {};
3030
this._recompute( {}, this._state, {}, true );
3131

test/js/samples/css-media-query/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function create_main_fragment ( state, component ) {
199199
}
200200

201201
function SvelteComponent ( options ) {
202-
options = options || {};
202+
this.options = options;
203203
this._state = options.data || {};
204204

205205
this._observers = {

test/js/samples/css-media-query/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function create_main_fragment ( state, component ) {
3939
}
4040

4141
function SvelteComponent ( options ) {
42-
options = options || {};
42+
this.options = options;
4343
this._state = options.data || {};
4444

4545
this._observers = {

test/js/samples/each-block-changed-check/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ function create_each_block ( state, each_block_value, comment, i, component ) {
313313
}
314314

315315
function SvelteComponent ( options ) {
316-
options = options || {};
316+
this.options = options;
317317
this._state = options.data || {};
318318

319319
this._observers = {

test/js/samples/each-block-changed-check/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function create_each_block ( state, each_block_value, comment, i, component ) {
140140
}
141141

142142
function SvelteComponent ( options ) {
143-
options = options || {};
143+
this.options = options;
144144
this._state = options.data || {};
145145

146146
this._observers = {

test/js/samples/event-handlers-custom/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ function create_main_fragment ( state, component ) {
210210
}
211211

212212
function SvelteComponent ( options ) {
213-
options = options || {};
213+
this.options = options;
214214
this._state = options.data || {};
215215

216216
this._observers = {

test/js/samples/event-handlers-custom/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function create_main_fragment ( state, component ) {
5050
}
5151

5252
function SvelteComponent ( options ) {
53-
options = options || {};
53+
this.options = options;
5454
this._state = options.data || {};
5555

5656
this._observers = {

test/js/samples/if-block-no-update/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ function select_block_type ( state ) {
252252
}
253253

254254
function SvelteComponent ( options ) {
255-
options = options || {};
255+
this.options = options;
256256
this._state = options.data || {};
257257

258258
this._observers = {

test/js/samples/if-block-no-update/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function select_block_type ( state ) {
8888
}
8989

9090
function SvelteComponent ( options ) {
91-
options = options || {};
91+
this.options = options;
9292
this._state = options.data || {};
9393

9494
this._observers = {

test/js/samples/if-block-simple/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function create_if_block ( state, component ) {
228228
}
229229

230230
function SvelteComponent ( options ) {
231-
options = options || {};
231+
this.options = options;
232232
this._state = options.data || {};
233233

234234
this._observers = {

test/js/samples/if-block-simple/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function create_if_block ( state, component ) {
6464
}
6565

6666
function SvelteComponent ( options ) {
67-
options = options || {};
67+
this.options = options;
6868
this._state = options.data || {};
6969

7070
this._observers = {

test/js/samples/legacy-input-type/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function create_main_fragment ( state, component ) {
186186
}
187187

188188
function SvelteComponent ( options ) {
189-
options = options || {};
189+
this.options = options;
190190
this._state = options.data || {};
191191

192192
this._observers = {

test/js/samples/legacy-input-type/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function create_main_fragment ( state, component ) {
2828
}
2929

3030
function SvelteComponent ( options ) {
31-
options = options || {};
31+
this.options = options;
3232
this._state = options.data || {};
3333

3434
this._observers = {

test/js/samples/non-imported-component/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function create_main_fragment ( state, component ) {
202202
}
203203

204204
function SvelteComponent ( options ) {
205-
options = options || {};
205+
this.options = options;
206206
this._state = options.data || {};
207207

208208
this._observers = {

test/js/samples/non-imported-component/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function create_main_fragment ( state, component ) {
5050
}
5151

5252
function SvelteComponent ( options ) {
53-
options = options || {};
53+
this.options = options;
5454
this._state = options.data || {};
5555

5656
this._observers = {

test/js/samples/onrender-onteardown-rewritten/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function create_main_fragment ( state, component ) {
164164
}
165165

166166
function SvelteComponent ( options ) {
167-
options = options || {};
167+
this.options = options;
168168
this._state = options.data || {};
169169

170170
this._observers = {

test/js/samples/onrender-onteardown-rewritten/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function create_main_fragment ( state, component ) {
2424
}
2525

2626
function SvelteComponent ( options ) {
27-
options = options || {};
27+
this.options = options;
2828
this._state = options.data || {};
2929

3030
this._observers = {

test/js/samples/setup-method/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function create_main_fragment ( state, component ) {
175175
}
176176

177177
function SvelteComponent ( options ) {
178-
options = options || {};
178+
this.options = options;
179179
this._state = options.data || {};
180180

181181
this._observers = {

test/js/samples/setup-method/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function create_main_fragment ( state, component ) {
3535
}
3636

3737
function SvelteComponent ( options ) {
38-
options = options || {};
38+
this.options = options;
3939
this._state = options.data || {};
4040

4141
this._observers = {

test/js/samples/use-elements-as-anchors/expected-bundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ function create_if_block_4 ( state, component ) {
412412
}
413413

414414
function SvelteComponent ( options ) {
415-
options = options || {};
415+
this.options = options;
416416
this._state = options.data || {};
417417

418418
this._observers = {

test/js/samples/use-elements-as-anchors/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ function create_if_block_4 ( state, component ) {
248248
}
249249

250250
function SvelteComponent ( options ) {
251-
options = options || {};
251+
this.options = options;
252252
this._state = options.data || {};
253253

254254
this._observers = {

test/runtime/index.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ describe("runtime", () => {
8181
code.slice(0, startIndex).split('\n').map(x => spaces(x.length)).join('\n') +
8282
code.slice(startIndex).replace(/export default .+/, "");
8383
acorn.parse(es5, { ecmaVersion: 5 });
84+
85+
if (/Object\.assign/.test(es5)) {
86+
throw new Error(
87+
"cannot use Object.assign in generated code, as it is not supported everywhere"
88+
);
89+
}
8490
} catch (err) {
8591
failed.add(dir);
8692
showOutput(cwd, { shared }); // eslint-disable-line no-console
@@ -133,11 +139,6 @@ describe("runtime", () => {
133139
throw err;
134140
}
135141

136-
let usedObjectAssign = false;
137-
Object.assign = () => {
138-
usedObjectAssign = true;
139-
};
140-
141142
global.window = window;
142143

143144
// Put the constructor on window for testing
@@ -151,13 +152,13 @@ describe("runtime", () => {
151152
warnings.push(warning);
152153
};
153154

154-
const component = new SvelteComponent({
155+
const options = Object.assign({}, {
155156
target,
156157
hydrate,
157158
data: config.data
158-
});
159+
}, config.options || {});
159160

160-
Object.assign = Object_assign;
161+
const component = new SvelteComponent(options);
161162

162163
console.warn = warn;
163164

@@ -183,15 +184,7 @@ describe("runtime", () => {
183184
component.destroy();
184185
assert.equal(target.innerHTML, "");
185186
}
186-
187-
if (usedObjectAssign) {
188-
throw new Error(
189-
"cannot use Object.assign in generated code, as it is not supported everywhere"
190-
);
191-
}
192187
} catch (err) {
193-
Object.assign = Object_assign;
194-
195188
if (config.error && !unintendedError) {
196189
config.error(assert, err);
197190
} else {
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
'skip-ssr': true,
3+
html: `<p>from this.options</p>`,
4+
5+
options: {
6+
text: 'from this.options'
7+
},
8+
9+
test(assert, component) {
10+
assert.equal(component.options.text, 'from this.options');
11+
}
12+
};
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<p>{{text}}</p>
2+
3+
<script>
4+
export default {
5+
oncreate() {
6+
this.set({
7+
text: this.options.text
8+
});
9+
}
10+
};
11+
</script>

test/runtime/samples/pass-no-options/_config.js

-8
This file was deleted.

test/runtime/samples/pass-no-options/main.html

-1
This file was deleted.

0 commit comments

Comments
 (0)