Skip to content

Commit

Permalink
check for unknown props even if component doesn't have writable props (
Browse files Browse the repository at this point in the history
  • Loading branch information
pushkin authored Feb 29, 2020
1 parent 37a2d6c commit b6aaa44
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ export default function dom(
inject_state;
if (has_invalidate) {
args.push(x`$$props`, x`$$invalidate`);
} else if (component.compile_options.dev) {
// $$props arg is still needed for unknown prop check
args.push(x`$$props`);
}

const has_create_fragment = block.has_content();
Expand Down Expand Up @@ -300,6 +303,7 @@ export default function dom(
const initial_context = renderer.context.slice(0, i + 1);

const has_definition = (
component.compile_options.dev ||
(instance_javascript && instance_javascript.length > 0) ||
filtered_props.length > 0 ||
uses_props ||
Expand Down Expand Up @@ -379,7 +383,7 @@ export default function dom(
});

let unknown_props_check;
if (component.compile_options.dev && !component.var_lookup.has('$$props') && writable_props.length) {
if (component.compile_options.dev && !component.var_lookup.has('$$props')) {
unknown_props_check = b`
const writable_props = [${writable_props.map(prop => x`'${prop.export_name}'`)}];
@_Object.keys($$props).forEach(key => {
Expand Down
6 changes: 6 additions & 0 deletions test/js/samples/debug-hoisted/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ function create_fragment(ctx) {
function instance($$self, $$props, $$invalidate) {
let obj = { x: 5 };
let kobzol = 5;
const writable_props = [];

Object.keys($$props).forEach(key => {
if (!~writable_props.indexOf(key) && key.slice(0, 2) !== "$$") console.warn(`<Component> was created with unknown prop '${key}'`);
});

$$self.$capture_state = () => ({ obj, kobzol });

$$self.$inject_state = $$props => {
Expand Down
12 changes: 11 additions & 1 deletion test/js/samples/debug-no-dependencies/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,20 @@ function create_fragment(ctx) {
return block;
}

function instance($$self, $$props) {
const writable_props = [];

Object.keys($$props).forEach(key => {
if (!~writable_props.indexOf(key) && key.slice(0, 2) !== "$$") console.warn(`<Component> was created with unknown prop '${key}'`);
});

return [];
}

class Component extends SvelteComponentDev {
constructor(options) {
super(options);
init(this, options, null, create_fragment, safe_not_equal, {});
init(this, options, instance, create_fragment, safe_not_equal, {});

dispatch_dev("SvelteRegisterComponent", {
component: this,
Expand Down
8 changes: 8 additions & 0 deletions test/js/samples/loop-protect/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
detach_dev,
dispatch_dev,
element,
globals,
init,
insert_dev,
loop_guard,
noop,
safe_not_equal
} from "svelte/internal";

const { console: console_1 } = globals;
const file = undefined;

function create_fragment(ctx) {
Expand Down Expand Up @@ -102,6 +104,12 @@ function instance($$self, $$props, $$invalidate) {
} while (true);
}

const writable_props = [];

Object.keys($$props).forEach(key => {
if (!~writable_props.indexOf(key) && key.slice(0, 2) !== "$$") console_1.warn(`<Component> was created with unknown prop '${key}'`);
});

function div_binding($$value) {
binding_callbacks[$$value ? "unshift" : "push"](() => {
$$invalidate(0, node = $$value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
9 changes: 9 additions & 0 deletions test/runtime/samples/dev-warning-unknown-props-2/_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 prop 'fo'`
]
};
5 changes: 5 additions & 0 deletions test/runtime/samples/dev-warning-unknown-props-2/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 b6aaa44

Please sign in to comment.