Skip to content

Commit 278d988

Browse files
pushkintaylorzane
pushkin
authored andcommitted
check for unknown props even if component doesn't have writable props (sveltejs#4454)
1 parent 7132fc0 commit 278d988

File tree

7 files changed

+45
-2
lines changed

7 files changed

+45
-2
lines changed

src/compiler/compile/render_dom/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ export default function dom(
259259
inject_state;
260260
if (has_invalidate) {
261261
args.push(x`$$props`, x`$$invalidate`);
262+
} else if (component.compile_options.dev) {
263+
// $$props arg is still needed for unknown prop check
264+
args.push(x`$$props`);
262265
}
263266

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

302305
const has_definition = (
306+
component.compile_options.dev ||
303307
(instance_javascript && instance_javascript.length > 0) ||
304308
filtered_props.length > 0 ||
305309
uses_props ||
@@ -379,7 +383,7 @@ export default function dom(
379383
});
380384

381385
let unknown_props_check;
382-
if (component.compile_options.dev && !component.var_lookup.has('$$props') && writable_props.length) {
386+
if (component.compile_options.dev && !component.var_lookup.has('$$props')) {
383387
unknown_props_check = b`
384388
const writable_props = [${writable_props.map(prop => x`'${prop.export_name}'`)}];
385389
@_Object.keys($$props).forEach(key => {

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

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ function create_fragment(ctx) {
5050
function instance($$self, $$props, $$invalidate) {
5151
let obj = { x: 5 };
5252
let kobzol = 5;
53+
const writable_props = [];
54+
55+
Object.keys($$props).forEach(key => {
56+
if (!~writable_props.indexOf(key) && key.slice(0, 2) !== "$$") console.warn(`<Component> was created with unknown prop '${key}'`);
57+
});
58+
5359
$$self.$capture_state = () => ({ obj, kobzol });
5460

5561
$$self.$inject_state = $$props => {

test/js/samples/debug-no-dependencies/expected.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,20 @@ function create_fragment(ctx) {
134134
return block;
135135
}
136136

137+
function instance($$self, $$props) {
138+
const writable_props = [];
139+
140+
Object.keys($$props).forEach(key => {
141+
if (!~writable_props.indexOf(key) && key.slice(0, 2) !== "$$") console.warn(`<Component> was created with unknown prop '${key}'`);
142+
});
143+
144+
return [];
145+
}
146+
137147
class Component extends SvelteComponentDev {
138148
constructor(options) {
139149
super(options);
140-
init(this, options, null, create_fragment, safe_not_equal, {});
150+
init(this, options, instance, create_fragment, safe_not_equal, {});
141151

142152
dispatch_dev("SvelteRegisterComponent", {
143153
component: this,

test/js/samples/loop-protect/expected.js

+8
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import {
66
detach_dev,
77
dispatch_dev,
88
element,
9+
globals,
910
init,
1011
insert_dev,
1112
loop_guard,
1213
noop,
1314
safe_not_equal
1415
} from "svelte/internal";
1516

17+
const { console: console_1 } = globals;
1618
const file = undefined;
1719

1820
function create_fragment(ctx) {
@@ -102,6 +104,12 @@ function instance($$self, $$props, $$invalidate) {
102104
} while (true);
103105
}
104106

107+
const writable_props = [];
108+
109+
Object.keys($$props).forEach(key => {
110+
if (!~writable_props.indexOf(key) && key.slice(0, 2) !== "$$") console_1.warn(`<Component> was created with unknown prop '${key}'`);
111+
});
112+
105113
function div_binding($$value) {
106114
binding_callbacks[$$value ? "unshift" : "push"](() => {
107115
$$invalidate(0, node = $$value);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Foo
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 prop '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)