Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for unknown props when creating component #2840

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"/>