Skip to content

Commit 5e3ee4e

Browse files
committed
Merge branch 'feat/2174_dev_runtime_warning_when_passing_unknown_props' of https://github.com/colincasey/svelte into colincasey-feat/2174_dev_runtime_warning_when_passing_unknown_props
2 parents ca8c856 + 05fb05b commit 5e3ee4e

File tree

8 files changed

+55
-5
lines changed

8 files changed

+55
-5
lines changed

src/compile/render-dom/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ export default function dom(
134134
});
135135

136136
if (component.compile_options.dev) {
137-
// TODO check no uunexpected props were passed, as well as
138137
// checking that expected ones were passed
139138
const expected = props.filter(prop => !prop.initialised);
140139

@@ -395,6 +394,16 @@ export default function dom(
395394
return $name;
396395
});
397396

397+
let unknownPropsCheck;
398+
if (component.compile_options.dev && writable_props.length) {
399+
unknownPropsCheck = deindent`
400+
const writableProps = [${writable_props.map(prop => `'${prop.export_name}'`).join(', ')}];
401+
Object.keys($$props).forEach(key => {
402+
if (!writableProps.includes(key)) console.warn(\`<${component.tag}> was created with unknown attribute '\${key}'\`);
403+
});
404+
`;
405+
}
406+
398407
builder.add_block(deindent`
399408
function ${definition}(${args.join(', ')}) {
400409
${reactive_store_declarations.length > 0 && `let ${reactive_store_declarations.join(', ')};`}
@@ -404,6 +413,8 @@ export default function dom(
404413
${resubscribable_reactive_store_unsubscribers}
405414
406415
${component.javascript}
416+
417+
${unknownPropsCheck}
407418
408419
${component.slots.size && `let { $$slots = {}, $$scope } = $$props;`}
409420

test/js/samples/debug-empty/expected.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ function create_fragment(ctx) {
6565
function instance($$self, $$props, $$invalidate) {
6666
let { name } = $$props;
6767

68+
const writableProps = ['name'];
69+
Object.keys($$props).forEach(key => {
70+
if (!writableProps.includes(key)) console.warn(`<Component> was created with unknown attribute '${key}'`);
71+
});
72+
6873
$$self.$set = $$props => {
6974
if ('name' in $$props) $$invalidate('name', name = $$props.name);
7075
};
@@ -93,4 +98,4 @@ class Component extends SvelteComponentDev {
9398
}
9499
}
95100

96-
export default Component;
101+
export default Component;

test/js/samples/debug-foo-bar-baz-things/expected.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ function create_fragment(ctx) {
151151
function instance($$self, $$props, $$invalidate) {
152152
let { things, foo, bar, baz } = $$props;
153153

154+
const writableProps = ['things', 'foo', 'bar', 'baz'];
155+
Object.keys($$props).forEach(key => {
156+
if (!writableProps.includes(key)) console.warn(`<Component> was created with unknown attribute '${key}'`);
157+
});
158+
154159
$$self.$set = $$props => {
155160
if ('things' in $$props) $$invalidate('things', things = $$props.things);
156161
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
@@ -215,4 +220,4 @@ class Component extends SvelteComponentDev {
215220
}
216221
}
217222

218-
export default Component;
223+
export default Component;

test/js/samples/debug-foo/expected.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ function create_fragment(ctx) {
151151
function instance($$self, $$props, $$invalidate) {
152152
let { things, foo } = $$props;
153153

154+
const writableProps = ['things', 'foo'];
155+
Object.keys($$props).forEach(key => {
156+
if (!writableProps.includes(key)) console.warn(`<Component> was created with unknown attribute '${key}'`);
157+
});
158+
154159
$$self.$set = $$props => {
155160
if ('things' in $$props) $$invalidate('things', things = $$props.things);
156161
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
@@ -191,4 +196,4 @@ class Component extends SvelteComponentDev {
191196
}
192197
}
193198

194-
export default Component;
199+
export default Component;

test/js/samples/dev-warning-missing-data-computed/expected.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ function instance($$self, $$props, $$invalidate) {
6565

6666
let bar;
6767

68+
const writableProps = ['foo'];
69+
Object.keys($$props).forEach(key => {
70+
if (!writableProps.includes(key)) console.warn(`<Component> was created with unknown attribute '${key}'`);
71+
});
72+
6873
$$self.$set = $$props => {
6974
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
7075
};
@@ -97,4 +102,4 @@ class Component extends SvelteComponentDev {
97102
}
98103
}
99104

100-
export default Component;
105+
export default Component;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let foo = undefined;
3+
</script>
4+
5+
<div>{foo}</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
compileOptions: {
3+
dev: true
4+
},
5+
6+
warnings: [
7+
`<Foo> was created with unknown attribute 'fo'`
8+
]
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import Foo from './Foo.svelte';
3+
</script>
4+
5+
<Foo fo="sho"/>

0 commit comments

Comments
 (0)