Skip to content

Commit

Permalink
Merge branch 'feat/2174_dev_runtime_warning_when_passing_unknown_prop…
Browse files Browse the repository at this point in the history
…s' of https://github.com/colincasey/svelte into colincasey-feat/2174_dev_runtime_warning_when_passing_unknown_props
  • Loading branch information
Rich-Harris committed May 26, 2019
2 parents ca8c856 + 05fb05b commit 5e3ee4e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 5 deletions.
13 changes: 12 additions & 1 deletion src/compile/render-dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export default function dom(
});

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

Expand Down Expand Up @@ -395,6 +394,16 @@ export default function dom(
return $name;
});

let unknownPropsCheck;
if (component.compile_options.dev && writable_props.length) {
unknownPropsCheck = deindent`
const writableProps = [${writable_props.map(prop => `'${prop.export_name}'`).join(', ')}];
Object.keys($$props).forEach(key => {
if (!writableProps.includes(key)) console.warn(\`<${component.tag}> was created with unknown attribute '\${key}'\`);
});
`;
}

builder.add_block(deindent`
function ${definition}(${args.join(', ')}) {
${reactive_store_declarations.length > 0 && `let ${reactive_store_declarations.join(', ')};`}
Expand All @@ -404,6 +413,8 @@ export default function dom(
${resubscribable_reactive_store_unsubscribers}
${component.javascript}
${unknownPropsCheck}
${component.slots.size && `let { $$slots = {}, $$scope } = $$props;`}
Expand Down
7 changes: 6 additions & 1 deletion test/js/samples/debug-empty/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function create_fragment(ctx) {
function instance($$self, $$props, $$invalidate) {
let { name } = $$props;

const writableProps = ['name'];
Object.keys($$props).forEach(key => {
if (!writableProps.includes(key)) console.warn(`<Component> was created with unknown attribute '${key}'`);
});

$$self.$set = $$props => {
if ('name' in $$props) $$invalidate('name', name = $$props.name);
};
Expand Down Expand Up @@ -93,4 +98,4 @@ class Component extends SvelteComponentDev {
}
}

export default Component;
export default Component;
7 changes: 6 additions & 1 deletion test/js/samples/debug-foo-bar-baz-things/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ function create_fragment(ctx) {
function instance($$self, $$props, $$invalidate) {
let { things, foo, bar, baz } = $$props;

const writableProps = ['things', 'foo', 'bar', 'baz'];
Object.keys($$props).forEach(key => {
if (!writableProps.includes(key)) console.warn(`<Component> was created with unknown attribute '${key}'`);
});

$$self.$set = $$props => {
if ('things' in $$props) $$invalidate('things', things = $$props.things);
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
Expand Down Expand Up @@ -215,4 +220,4 @@ class Component extends SvelteComponentDev {
}
}

export default Component;
export default Component;
7 changes: 6 additions & 1 deletion test/js/samples/debug-foo/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ function create_fragment(ctx) {
function instance($$self, $$props, $$invalidate) {
let { things, foo } = $$props;

const writableProps = ['things', 'foo'];
Object.keys($$props).forEach(key => {
if (!writableProps.includes(key)) console.warn(`<Component> was created with unknown attribute '${key}'`);
});

$$self.$set = $$props => {
if ('things' in $$props) $$invalidate('things', things = $$props.things);
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
Expand Down Expand Up @@ -191,4 +196,4 @@ class Component extends SvelteComponentDev {
}
}

export default Component;
export default Component;
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function instance($$self, $$props, $$invalidate) {

let bar;

const writableProps = ['foo'];
Object.keys($$props).forEach(key => {
if (!writableProps.includes(key)) console.warn(`<Component> was created with unknown attribute '${key}'`);
});

$$self.$set = $$props => {
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
};
Expand Down Expand Up @@ -97,4 +102,4 @@ class Component extends SvelteComponentDev {
}
}

export default Component;
export default Component;
5 changes: 5 additions & 0 deletions test/runtime/samples/dev-warning-unknown-props/Foo.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let foo = undefined;
</script>

<div>{foo}</div>
9 changes: 9 additions & 0 deletions test/runtime/samples/dev-warning-unknown-props/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
compileOptions: {
dev: true
},

warnings: [
`<Foo> was created with unknown attribute 'fo'`
]
};
5 changes: 5 additions & 0 deletions test/runtime/samples/dev-warning-unknown-props/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import Foo from './Foo.svelte';
</script>

<Foo fo="sho"/>

0 comments on commit 5e3ee4e

Please sign in to comment.