Skip to content

Commit 94dfeec

Browse files
authored
Merge pull request #2083 from sveltejs/gh-2038
remove internal gubbins when using bind:props
2 parents 845dd47 + a50c84e commit 94dfeec

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed

src/compile/Component.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export default class Component {
6969
reactive_declaration_nodes: Set<Node> = new Set();
7070
has_reactive_assignments = false;
7171
injected_reactive_declaration_vars: Set<string> = new Set();
72+
helpers: Set<string> = new Set();
7273

7374
indirectDependencies: Map<string, Set<string>> = new Map();
7475

@@ -114,10 +115,6 @@ export default class Component {
114115
this.componentOptions = process_component_options(this, this.ast.html.children);
115116
this.namespace = namespaces[this.componentOptions.namespace] || this.componentOptions.namespace;
116117

117-
if (this.componentOptions.props) {
118-
this.has_reactive_assignments = true;
119-
}
120-
121118
if (compileOptions.customElement === true && !this.componentOptions.tag) {
122119
throw new Error(`No tag name specified`); // TODO better error
123120
}
@@ -131,6 +128,13 @@ export default class Component {
131128
this.walk_module_js();
132129
this.walk_instance_js_pre_template();
133130

131+
if (this.componentOptions.props) {
132+
this.has_reactive_assignments = true;
133+
134+
const variable = this.var_lookup.get(this.componentOptions.props_object);
135+
variable.reassigned = true;
136+
}
137+
134138
this.name = this.getUniqueName(name);
135139
this.fragment = new Fragment(this, ast.html);
136140

@@ -196,14 +200,12 @@ export default class Component {
196200

197201
const banner = `/* ${this.file ? `${this.file} ` : ``}generated by Svelte v${"__VERSION__"} */`;
198202

199-
const helpers = new Set();
200-
201203
// TODO use same regex for both
202204
result = result.replace(compileOptions.generate === 'ssr' ? /(@+|#+)(\w*(?:-\w*)?)/g : /(@+)(\w*(?:-\w*)?)/g, (match: string, sigil: string, name: string) => {
203205
if (sigil === '@') {
204206
if (internal_exports.has(name)) {
205207
if (compileOptions.dev && internal_exports.has(`${name}Dev`)) name = `${name}Dev`;
206-
helpers.add(name);
208+
this.helpers.add(name);
207209
}
208210

209211
return this.alias(name);
@@ -212,7 +214,7 @@ export default class Component {
212214
return sigil.slice(1) + name;
213215
});
214216

215-
const importedHelpers = Array.from(helpers)
217+
const importedHelpers = Array.from(this.helpers)
216218
.sort()
217219
.map(name => {
218220
const alias = this.alias(name);
@@ -761,9 +763,14 @@ export default class Component {
761763
});
762764
}
763765

766+
// can't use the @ trick here, because we're
767+
// manipulating the underlying magic string
768+
component.helpers.add('exclude_internal_props');
769+
const exclude_internal_props = component.alias('exclude_internal_props');
770+
764771
const suffix = code.original[declarator.end] === ';'
765-
? ` = $$props`
766-
: ` = $$props;`
772+
? ` = ${exclude_internal_props}($$props)`
773+
: ` = ${exclude_internal_props}($$props);`
767774

768775
if (declarator.id.end === declarator.end) {
769776
code.appendLeft(declarator.end, suffix);

src/internal/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,8 @@ export function get_slot_context(definition, ctx, fn) {
6060
: ctx.$$scope.ctx;
6161
}
6262

63+
export function exclude_internal_props(props) {
64+
const result = {};
65+
for (const k in props) if (k[0] !== '$') result[k] = props[k];
66+
return result;
67+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<svelte:options bind:props/>
2+
3+
<script>
4+
let props;
5+
</script>
6+
7+
<p>{JSON.stringify(props)}</p>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default {
2+
props: {
3+
x: 1
4+
},
5+
6+
html: `
7+
<p>{"x":1}</p>
8+
`,
9+
10+
test({ assert, component, target }) {
11+
component.x = 2;
12+
13+
assert.htmlEqual(target.innerHTML, `
14+
<p>{"x":2}</p>
15+
`);
16+
}
17+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
import RenderProps from './RenderProps.svelte';
3+
4+
export let x;
5+
</script>
6+
7+
<RenderProps {x}>
8+
<p>some (unused) slotted content, to create an internal prop</p>
9+
</RenderProps>

0 commit comments

Comments
 (0)