Skip to content

Commit 1273f97

Browse files
authored
Merge pull request #3811 from sveltejs/gh-3508-alt
Alternative fix for #3508
2 parents da824c0 + 9d11827 commit 1273f97

File tree

80 files changed

+137
-110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+137
-110
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Also:
1010
* Fix edge cases in matching selectors against elements ([#1710](https://github.com/sveltejs/svelte/issues/1710))
1111
* Fix several bugs related to interaction of `{...spread}` attributes with other features ([#2721](https://github.com/sveltejs/svelte/issues/2721), [#2916](https://github.com/sveltejs/svelte/issues/2916), [#3421](https://github.com/sveltejs/svelte/issues/3421), [#3681](https://github.com/sveltejs/svelte/issues/3681), [#3764](https://github.com/sveltejs/svelte/issues/3764), [#3790](https://github.com/sveltejs/svelte/issues/3790))
1212
* Allow exiting a reactive block early with `break $` ([#2828](https://github.com/sveltejs/svelte/issues/2828))
13+
* Fix binding to props that have been renamed with `export { ... as ... }` ([#3508](https://github.com/sveltejs/svelte/issues/3508))
1314
* Fix application of style scoping class in cases of ambiguity ([#3544](https://github.com/sveltejs/svelte/issues/3544))
1415
* Check attributes have changed before setting them to avoid image flicker ([#3579](https://github.com/sveltejs/svelte/pull/3579))
1516
* Fix generating malformed code for `{@debug}` tags with no dependencies ([#3588](https://github.com/sveltejs/svelte/issues/3588))

src/compiler/compile/render_dom/index.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import add_to_set from '../utils/add_to_set';
77
import { extract_names } from '../utils/scope';
88
import { invalidate } from '../utils/invalidate';
99
import Block from './Block';
10-
import { ClassDeclaration, FunctionExpression, Node, Statement } from 'estree';
10+
import { ClassDeclaration, FunctionExpression, Node, Statement, ObjectExpression } from 'estree';
1111

1212
export default function dom(
1313
component: Component,
@@ -223,7 +223,7 @@ export default function dom(
223223

224224
component.rewrite_props(({ name, reassigned, export_name }) => {
225225
const value = `$${name}`;
226-
226+
227227
const insert = (reassigned || export_name)
228228
? b`${`$$subscribe_${name}`}()`
229229
: b`@component_subscribe($$self, ${name}, #value => $$invalidate('${value}', ${value} = #value))`;
@@ -425,12 +425,9 @@ export default function dom(
425425
`);
426426
}
427427

428-
const prop_names = x`[]`;
429-
430-
// TODO find a more idiomatic way of doing this
431-
props.forEach(v => {
432-
(prop_names as any).elements.push({ type: 'Literal', value: v.export_name });
433-
});
428+
const prop_names = x`{
429+
${props.map(v => p`${v.export_name}: ${v.export_name === v.name ? 0 : x`"${v.name}"`}}`)}
430+
}` as ObjectExpression;
434431

435432
if (options.customElement) {
436433
const declaration = b`

src/runtime/internal/Component.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler';
22
import { current_component, set_current_component } from './lifecycle';
3-
import { blank_object, is_function, run, run_all, noop } from './utils';
3+
import { blank_object, is_function, run, run_all, noop, has_prop } from './utils';
44
import { children } from './dom';
55
import { transition_in } from './transitions';
66

@@ -12,7 +12,7 @@ interface T$$ {
1212
update: () => void;
1313
callbacks: any;
1414
after_update: any[];
15-
props: any;
15+
props: Record<string, 0 | string>;
1616
fragment: null|any;
1717
not_equal: any;
1818
before_update: any[];
@@ -22,9 +22,11 @@ interface T$$ {
2222
}
2323

2424
export function bind(component, name, callback) {
25-
if (component.$$.props.indexOf(name) === -1) return;
26-
component.$$.bound[name] = callback;
27-
callback(component.$$.ctx[name]);
25+
if (has_prop(component.$$.props, name)) {
26+
name = component.$$.props[name] || name;
27+
component.$$.bound[name] = callback;
28+
callback(component.$$.ctx[name]);
29+
}
2830
}
2931

3032
export function mount_component(component, target, anchor) {
@@ -70,18 +72,18 @@ function make_dirty(component, key) {
7072
component.$$.dirty[key] = true;
7173
}
7274

73-
export function init(component, options, instance, create_fragment, not_equal, prop_names) {
75+
export function init(component, options, instance, create_fragment, not_equal, props) {
7476
const parent_component = current_component;
7577
set_current_component(component);
7678

77-
const props = options.props || {};
79+
const prop_values = options.props || {};
7880

7981
const $$: T$$ = component.$$ = {
8082
fragment: null,
8183
ctx: null,
8284

8385
// state
84-
props: prop_names,
86+
props,
8587
update: noop,
8688
not_equal,
8789
bound: blank_object(),
@@ -101,14 +103,14 @@ export function init(component, options, instance, create_fragment, not_equal, p
101103
let ready = false;
102104

103105
$$.ctx = instance
104-
? instance(component, props, (key, ret, value = ret) => {
106+
? instance(component, prop_values, (key, ret, value = ret) => {
105107
if ($$.ctx && not_equal($$.ctx[key], $$.ctx[key] = value)) {
106108
if ($$.bound[key]) $$.bound[key](value);
107109
if (ready) make_dirty(component, key);
108110
}
109111
return ret;
110112
})
111-
: props;
113+
: prop_values;
112114

113115
$$.update();
114116
ready = true;

src/runtime/internal/dom.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { has_prop } from "./utils";
2+
13
export function append(target: Node, node: Node) {
24
target.appendChild(node);
35
}
@@ -29,7 +31,7 @@ export function object_without_properties<T, K extends keyof T>(obj: T, exclude:
2931
const target = {} as Pick<T, Exclude<keyof T, K>>;
3032
for (const k in obj) {
3133
if (
32-
Object.prototype.hasOwnProperty.call(obj, k)
34+
has_prop(obj, k)
3335
// @ts-ignore
3436
&& exclude.indexOf(k) === -1
3537
) {

src/runtime/internal/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,5 @@ export function set_store_value(store, ret, value = ret) {
105105
store.set(value);
106106
return ret;
107107
}
108+
109+
export const has_prop = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);

test/js/samples/action-custom-event-handler/expected.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function handleFoo(bar) {
3939
}
4040

4141
function foo(node, callback) {
42-
42+
4343
}
4444

4545
function instance($$self, $$props, $$invalidate) {
@@ -56,7 +56,7 @@ function instance($$self, $$props, $$invalidate) {
5656
class Component extends SvelteComponent {
5757
constructor(options) {
5858
super();
59-
init(this, options, instance, create_fragment, safe_not_equal, ["bar"]);
59+
init(this, options, instance, create_fragment, safe_not_equal, { bar: 0 });
6060
}
6161
}
6262

test/js/samples/action/expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function link(node) {
5252
class Component extends SvelteComponent {
5353
constructor(options) {
5454
super();
55-
init(this, options, null, create_fragment, safe_not_equal, []);
55+
init(this, options, null, create_fragment, safe_not_equal, {});
5656
}
5757
}
5858

test/js/samples/bind-online/expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function instance($$self, $$props, $$invalidate) {
4242
class Component extends SvelteComponent {
4343
constructor(options) {
4444
super();
45-
init(this, options, instance, create_fragment, safe_not_equal, []);
45+
init(this, options, instance, create_fragment, safe_not_equal, {});
4646
}
4747
}
4848

test/js/samples/bind-open/expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function instance($$self, $$props, $$invalidate) {
5858
class Component extends SvelteComponent {
5959
constructor(options) {
6060
super();
61-
init(this, options, instance, create_fragment, safe_not_equal, ["open"]);
61+
init(this, options, instance, create_fragment, safe_not_equal, { open: 0 });
6262
}
6363
}
6464

test/js/samples/bind-width-height/expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function instance($$self, $$props, $$invalidate) {
5656
class Component extends SvelteComponent {
5757
constructor(options) {
5858
super();
59-
init(this, options, instance, create_fragment, safe_not_equal, ["w", "h"]);
59+
init(this, options, instance, create_fragment, safe_not_equal, { w: 0, h: 0 });
6060
}
6161
}
6262

0 commit comments

Comments
 (0)