Skip to content

Commit 8f7b1dc

Browse files
committed
Fix issue of having var store declarations in for loop initialization
1 parent cf03a66 commit 8f7b1dc

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

src/compiler/compile/Component.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ 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, Statement } from 'estree';
28+
import { Node, ImportDeclaration, ExportNamedDeclaration, Identifier, ExpressionStatement, AssignmentExpression, Literal, Property, RestElement, ExportDefaultDeclaration, ExportAllDeclaration, FunctionDeclaration, FunctionExpression, Statement, VariableDeclarator } from 'estree';
2929
import add_to_set from './utils/add_to_set';
3030
import check_graph_for_cycles from './utils/check_graph_for_cycles';
31-
import { print, b } from 'code-red';
31+
import { print, b, x } from 'code-red';
3232
import { is_reserved_keyword } from './utils/reserved_keywords';
3333
import { apply_preprocessor_sourcemap } from '../utils/mapped_code';
3434
import Element from './nodes/Element';
@@ -1053,6 +1053,16 @@ export default class Component {
10531053
});
10541054
}
10551055

1056+
function create_insert_vars(name: string, insert: Node[]): VariableDeclarator {
1057+
return {
1058+
type: 'VariableDeclarator',
1059+
id: component.get_unique_name(`_inserts_for_${name}`),
1060+
init: x`(() => {
1061+
${insert}
1062+
})()`
1063+
};
1064+
}
1065+
10561066
// transform
10571067
// ```
10581068
// export let { x, y = 123 } = OBJ, z = 456
@@ -1065,13 +1075,21 @@ export default class Component {
10651075
for (let index = 0; index < node.declarations.length; index++) {
10661076
const declarator = node.declarations[index];
10671077
if (declarator.id.type !== 'Identifier') {
1078+
const variable_insert_declarators = [];
1079+
10681080
function get_new_name(local) {
10691081
const variable = component.var_lookup.get(local.name);
1082+
const is_props = variable.export_name && variable.writable;
10701083
if (variable.subscribable) {
1071-
inserts.push(get_insert(variable));
1084+
const insert = get_insert(variable);
1085+
if (is_props) {
1086+
inserts.push(insert);
1087+
} else {
1088+
variable_insert_declarators.push(create_insert_vars(local.name, insert));
1089+
}
10721090
}
10731091

1074-
if (variable.export_name && variable.writable) {
1092+
if (is_props) {
10751093
const alias_name = component.get_unique_name(local.name);
10761094
add_new_props({ type: 'Identifier', name: variable.export_name }, local, alias_name);
10771095
return alias_name;
@@ -1125,6 +1143,9 @@ export default class Component {
11251143
}
11261144

11271145
rename_identifiers(declarator.id);
1146+
1147+
node.declarations.splice(index + 1, 0, ...variable_insert_declarators);
1148+
index += variable_insert_declarators.length;
11281149
} else {
11291150
const { name } = declarator.id;
11301151
const variable = component.var_lookup.get(name);
@@ -1134,7 +1155,14 @@ export default class Component {
11341155
node.declarations.splice(index--, 1);
11351156
}
11361157
if (variable.subscribable && (is_props || declarator.init)) {
1137-
inserts.push(get_insert(variable));
1158+
const insert = get_insert(variable);
1159+
if (declarator.init && !is_props) {
1160+
node.declarations.splice(index + 1, 0, create_insert_vars(name, insert));
1161+
1162+
index += 1;
1163+
} else {
1164+
inserts.push(insert);
1165+
}
11381166
}
11391167
}
11401168
}

test/js/samples/component-store-access-invalidate/expected.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ function create_fragment(ctx) {
4141

4242
function instance($$self, $$props, $$invalidate) {
4343
let $foo;
44-
const foo = writable(0);
45-
component_subscribe($$self, foo, value => $$invalidate(0, $foo = value));
44+
45+
const foo = writable(0),
46+
$inserts_for_foo = (() => {
47+
component_subscribe($$self, foo, value => $$invalidate(0, $foo = value));
48+
})();
49+
4650
return [$foo, foo];
4751
}
4852

test/js/samples/component-store-reassign-invalidate/expected.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ function instance($$self, $$props, $$invalidate) {
6565
$$subscribe_foo = () => ($$unsubscribe_foo(), $$unsubscribe_foo = subscribe(foo, $$value => $$invalidate(1, $foo = $$value)), foo);
6666

6767
$$self.$$.on_destroy.push(() => $$unsubscribe_foo());
68-
let foo = writable(0);
69-
$$subscribe_foo();
68+
69+
let foo = writable(0),
70+
$inserts_for_foo = (() => {
71+
$$subscribe_foo();
72+
})();
73+
7074
const click_handler = () => $$subscribe_foo($$invalidate(0, foo = writable(0)));
7175
return [foo, $foo, click_handler];
7276
}

0 commit comments

Comments
 (0)