Skip to content

Commit 9d499e7

Browse files
committed
handle prop aliases
1 parent 5a291cf commit 9d499e7

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

documentation/docs/98-reference/.generated/client-warnings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ To fix it, `bind:` to the value instead of just passing a property (i.e. in this
171171
### ownership_invalid_mutation
172172
173173
```
174-
%component% mutated property `%prop%` from parent component %owner%, which did not declare it as a binding. This is strongly discouraged. Consider passing props to child components that mutate them with `bind:` (e.g. `bind:%prop%={...}` instead of `%prop%={...}`), or use a callback instead
174+
%component% mutated property `%name%` from parent component %owner%, which did not declare it as a binding. This is strongly discouraged. Consider passing props to child components that mutate them with `bind:` (e.g. `bind:%prop%={...}` instead of `%prop%={...}`), or use a callback instead
175175
```
176176
177177
Consider the following code:

packages/svelte/messages/client-warnings/warnings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ To fix it, `bind:` to the value instead of just passing a property (i.e. in this
140140
141141
## ownership_invalid_mutation
142142
143-
> %component% mutated property `%prop%` from parent component %owner%, which did not declare it as a binding. This is strongly discouraged. Consider passing props to child components that mutate them with `bind:` (e.g. `bind:%prop%={...}` instead of `%prop%={...}`), or use a callback instead
143+
> %component% mutated property `%name%` from parent component %owner%, which did not declare it as a binding. This is strongly discouraged. Consider passing props to child components that mutate them with `bind:` (e.g. `bind:%prop%={...}` instead of `%prop%={...}`), or use a callback instead
144144
145145
Consider the following code:
146146

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/utils.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,5 +341,10 @@ export function validate_mutation(node, context, expression) {
341341

342342
path.unshift(b.literal(name.name));
343343

344-
return b.call('$$ownership_validator.mutation', b.array(path), expression);
344+
return b.call(
345+
'$$ownership_validator.mutation',
346+
b.literal(binding.prop_alias),
347+
b.array(path),
348+
expression
349+
);
345350
}

packages/svelte/src/internal/client/dev/ownership.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,26 @@ export function create_ownership_validator(props) {
115115

116116
return {
117117
/**
118+
* @param {string} prop
118119
* @param {any[]} path
119120
* @param {any} result
120121
*/
121-
mutation: (path, result) => {
122-
const prop_name = path[0];
123-
if (is_bound(props, prop_name) || !parent) {
122+
mutation: (prop, path, result) => {
123+
const name = path[0];
124+
if (is_bound(props, name) || !parent) {
124125
return result;
125126
}
126127

127-
let prop = props[prop_name];
128+
let value = props[name];
128129

129130
for (let i = 1; i < path.length - 1; i++) {
130-
if (!prop?.[STATE_SYMBOL]) {
131+
if (!value?.[STATE_SYMBOL]) {
131132
return result;
132133
}
133-
prop = prop[path[i]];
134+
value = value[path[i]];
134135
}
135136

136-
w.ownership_invalid_mutation(component[FILENAME], prop_name, parent[FILENAME]);
137+
w.ownership_invalid_mutation(component[FILENAME], name, parent[FILENAME], prop);
137138

138139
return result;
139140
},

packages/svelte/src/internal/client/warnings.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,15 @@ export function ownership_invalid_binding(parent, prop, child, owner) {
144144
}
145145

146146
/**
147-
* %component% mutated property `%prop%` from parent component %owner%, which did not declare it as a binding. This is strongly discouraged. Consider passing props to child components that mutate them with `bind:` (e.g. `bind:%prop%={...}` instead of `%prop%={...}`), or use a callback instead
147+
* %component% mutated property `%name%` from parent component %owner%, which did not declare it as a binding. This is strongly discouraged. Consider passing props to child components that mutate them with `bind:` (e.g. `bind:%prop%={...}` instead of `%prop%={...}`), or use a callback instead
148148
* @param {string} component
149-
* @param {string} prop
149+
* @param {string} name
150150
* @param {string} owner
151+
* @param {string} prop
151152
*/
152-
export function ownership_invalid_mutation(component, prop, owner) {
153+
export function ownership_invalid_mutation(component, name, owner, prop) {
153154
if (DEV) {
154-
console.warn(`%c[svelte] ownership_invalid_mutation\n%c${component} mutated property \`${prop}\` from parent component ${owner}, which did not declare it as a binding. This is strongly discouraged. Consider passing props to child components that mutate them with \`bind:\` (e.g. \`bind:${prop}={...}\` instead of \`${prop}={...}\`), or use a callback instead\nhttps://svelte.dev/e/ownership_invalid_mutation`, bold, normal);
155+
console.warn(`%c[svelte] ownership_invalid_mutation\n%c${component} mutated property \`${name}\` from parent component ${owner}, which did not declare it as a binding. This is strongly discouraged. Consider passing props to child components that mutate them with \`bind:\` (e.g. \`bind:${prop}={...}\` instead of \`${prop}={...}\`), or use a callback instead\nhttps://svelte.dev/e/ownership_invalid_mutation`, bold, normal);
155156
} else {
156157
console.warn(`https://svelte.dev/e/ownership_invalid_mutation`);
157158
}

0 commit comments

Comments
 (0)