Skip to content

Commit 17bf6db

Browse files
authored
fix: Array rest property fix (#8553)
Fixes #8552
1 parent e45a1e0 commit 17bf6db

File tree

9 files changed

+157
-7
lines changed

9 files changed

+157
-7
lines changed

src/compiler/compile/Component.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import TemplateScope from './nodes/shared/TemplateScope';
2525
import fuzzymatch from '../utils/fuzzymatch';
2626
import get_object from './utils/get_object';
2727
import Slot from './nodes/Slot';
28-
import { Node, ImportDeclaration, ExportNamedDeclaration, Identifier, ExpressionStatement, AssignmentExpression, Literal, Property, RestElement, ExportDefaultDeclaration, ExportAllDeclaration, FunctionDeclaration, FunctionExpression } from 'estree';
28+
import { Node, ImportDeclaration, ExportNamedDeclaration, Identifier, ExpressionStatement, AssignmentExpression, Literal, Property, RestElement, ExportDefaultDeclaration, ExportAllDeclaration, FunctionDeclaration, FunctionExpression, Pattern, Expression } from 'estree';
2929
import add_to_set from './utils/add_to_set';
3030
import check_graph_for_cycles from './utils/check_graph_for_cycles';
3131
import { print, b } from 'code-red';
@@ -1034,7 +1034,7 @@ export default class Component {
10341034
const inserts = [];
10351035
const props = [];
10361036

1037-
function add_new_props(exported, local, default_value) {
1037+
function add_new_props(exported: Identifier, local: Pattern, default_value: Expression) {
10381038
props.push({
10391039
type: 'Property',
10401040
method: false,
@@ -1064,7 +1064,7 @@ export default class Component {
10641064
for (let index = 0; index < node.declarations.length; index++) {
10651065
const declarator = node.declarations[index];
10661066
if (declarator.id.type !== 'Identifier') {
1067-
function get_new_name(local) {
1067+
function get_new_name(local: Identifier): Identifier {
10681068
const variable = component.var_lookup.get(local.name);
10691069
if (variable.subscribable) {
10701070
inserts.push(get_insert(variable));
@@ -1078,7 +1078,7 @@ export default class Component {
10781078
return local;
10791079
}
10801080

1081-
function rename_identifiers(param: Node) {
1081+
function rename_identifiers(param: Pattern) {
10821082
switch (param.type) {
10831083
case 'ObjectPattern': {
10841084
const handle_prop = (prop: Property | RestElement) => {
@@ -1087,15 +1087,15 @@ export default class Component {
10871087
} else if (prop.value.type === 'Identifier') {
10881088
prop.value = get_new_name(prop.value);
10891089
} else {
1090-
rename_identifiers(prop.value);
1090+
rename_identifiers(prop.value as Pattern);
10911091
}
10921092
};
10931093

10941094
param.properties.forEach(handle_prop);
10951095
break;
10961096
}
10971097
case 'ArrayPattern': {
1098-
const handle_element = (element: Node, index: number, array: Node[]) => {
1098+
const handle_element = (element: Pattern | null, index: number, array: Array<Pattern | null>) => {
10991099
if (element) {
11001100
if (element.type === 'Identifier') {
11011101
array[index] = get_new_name(element);
@@ -1110,7 +1110,11 @@ export default class Component {
11101110
}
11111111

11121112
case 'RestElement':
1113-
param.argument = get_new_name(param.argument);
1113+
if (param.argument.type === 'Identifier') {
1114+
param.argument = get_new_name(param.argument);
1115+
} else {
1116+
rename_identifiers(param.argument);
1117+
}
11141118
break;
11151119

11161120
case 'AssignmentPattern':
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
html: `
3+
<h1>1</h1>
4+
<h1>2</h1>
5+
<h1>3</h1>
6+
<h1>5</h1>
7+
<h1>10</h1>
8+
<h1>20</h1>
9+
<h1>30</h1>
10+
<h1>6</h1>
11+
`
12+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
let [first, second, ...[third, ...[, fifth]]] = [1, 2, 3, 4, 5];
3+
let [one, two, ...[three, ...{ length }]] = [10, 20, 30, 40, 50, 60, 70, 80, 90];
4+
</script>
5+
6+
<h1>{first}</h1>
7+
<h1>{second}</h1>
8+
<h1>{third}</h1>
9+
<h1>{fifth}</h1>
10+
11+
<h1>{one}</h1>
12+
<h1>{two}</h1>
13+
<h1>{three}</h1>
14+
<h1>{length}</h1>
15+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
4+
const THING = { a: 1, b: { c: 2, d: [3, 4, writable(5), 6, 7] }, e: [6], h: 8 };
5+
const default_g = 9;
6+
7+
export let { a, b: { c, d: [d_one, ...[, ...[d_three, ...{ length }]]], f }, e: [e_one], g = default_g } = THING;
8+
export const { a: A, b: { c: C } } = THING;
9+
</script>
10+
11+
<div>
12+
a: {a},
13+
b: {typeof b},
14+
c: {c},
15+
d_one: {d_one},
16+
d_three: {$d_three},
17+
length: {length},
18+
f: {f},
19+
g: {g},
20+
e: {typeof e},
21+
e_one: {e_one},
22+
A: {A},
23+
C: {C}
24+
</div>
25+
<div>{JSON.stringify(THING)}</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
html: `
3+
<div>a: 1, b: undefined, c: 2, d_one: 3, d_three: 5, length: 2, f: undefined, g: 9, e: undefined, e_one: 6, A: 1, C: 2</div>
4+
<div>{"a":1,"b":{"c":2,"d":[3,4,{},6,7]},"e":[6],"h":8}</div>
5+
<br>
6+
<div>a: a, b: undefined, c: 2, d_one: d_one, d_three: 5, length: 7, f: f, g: g, e: undefined, e_one: 6, A: 1, C: 2</div>
7+
<div>{"a":1,"b":{"c":2,"d":[3,4,{},6,7]},"e":[6],"h":8}</div>
8+
`
9+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import A from './A.svelte';
3+
</script>
4+
5+
<A />
6+
<br />
7+
<A a="a" d_one="d_one" list_one="list_one" f="f" list_two_b="list_two_b" g="g" A="A" C="C" length={7} />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script>
2+
import { writable } from "svelte/store";
3+
4+
let default_b = 5;
5+
const LIST = [1, 2, 3, { a: 4 }, [5, writable(6), writable(7), 8]];
6+
export const [
7+
x,
8+
,
9+
...[, { a: list_two_a, b: list_two_b = default_b }, [, ...{ length: y }]]
10+
] = LIST;
11+
export let [
12+
l,
13+
m,
14+
,
15+
...[{ a: n, b: o = default_b }, [p, q, ...[r, ...{ length: s }]]]
16+
] = LIST;
17+
</script>
18+
19+
<div>
20+
x: {x}, list_two_a: {list_two_a}, list_two_b: {list_two_b}, y: {y}, l: {l}, m: {m},
21+
n: {n}, o: {o}, p: {p}, q: {$q}, r: {$r}, s: {s}
22+
</div>
23+
<div>{JSON.stringify(LIST)}</div>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default {
2+
html: `
3+
<div>x: 1, list_two_a: 4, list_two_b: 5, y: 3, l: 1, m: 2, n: 4, o: 5, p: 5, q: 6, r: 7, s: 1</div>
4+
<div>[1,2,3,{"a":4},[5,{},{},8]]</div>
5+
<br><div>x: 1, list_two_a: 4, list_two_b: 5, y: 3, l: l, m: m, n: n, o: o, p: p, q: q, r: r, s: s</div>
6+
<div>[1,2,3,{"a":4},[5,{},{},8]]</div>
7+
`,
8+
9+
async test({ component, assert, target }) {
10+
await component.update();
11+
12+
assert.htmlEqual(target.innerHTML, `
13+
<div>x: 1, list_two_a: 4, list_two_b: 5, y: 3, l: 1, m: 2, n: 4, o: 5, p: 5, q: 6, r: 7, s: 1</div>
14+
<div>[1,2,3,{"a":4},[5,{},{},8]]</div>
15+
<br><div>x: 1, list_two_a: 4, list_two_b: 5, y: 3, l: LL, m: MM, n: NN, o: OO, p: PP, q: QQ, r: RR, s: SS</div>
16+
<div>[1,2,3,{"a":4},[5,{},{},8]]</div>
17+
`);
18+
}
19+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<script>
2+
import A from "./A.svelte";
3+
import { writable } from "svelte/store";
4+
5+
let x = "x",
6+
list_two_a = "list_two_a",
7+
list_two_b = "list_two_b",
8+
y = writable("y"),
9+
l = "l",
10+
m = "m",
11+
n = "n",
12+
o = "o",
13+
p = "p",
14+
q = writable("q"),
15+
r = writable("r"),
16+
s = "s";
17+
18+
export function update() {
19+
x = "XX";
20+
list_two_a = "LIST_TWO_A";
21+
list_two_b = "LIST_TWO_B";
22+
y = writable("YY");
23+
l = "LL";
24+
m = "MM";
25+
n = "NN";
26+
o = "OO";
27+
p = "PP";
28+
q = writable("QQ");
29+
r = writable("RR");
30+
s = "SS";
31+
}
32+
</script>
33+
34+
<A />
35+
<br />
36+
<A {x} {list_two_a} {list_two_b} {y} {l} {m} {n} {o} {p} {q} {r} {s} />

0 commit comments

Comments
 (0)