Skip to content

Commit 879cb1c

Browse files
committed
Simplify solution for var declaration not in block array
1 parent 04d20a0 commit 879cb1c

File tree

3 files changed

+24
-48
lines changed

3 files changed

+24
-48
lines changed

src/compiler/compile/Component.ts

Lines changed: 20 additions & 36 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, Statement, VariableDeclarator } from 'estree';
28+
import { Node, ImportDeclaration, ExportNamedDeclaration, Identifier, ExpressionStatement, AssignmentExpression, Literal, Property, RestElement, ExportDefaultDeclaration, ExportAllDeclaration, FunctionDeclaration, FunctionExpression } 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, x } from 'code-red';
@@ -38,7 +38,6 @@ import compiler_warnings from './compiler_warnings';
3838
import compiler_errors from './compiler_errors';
3939
import { extract_ignores_above_position, extract_svelte_ignore_from_comments } from '../utils/extract_svelte_ignore';
4040
import check_enable_sourcemap from './utils/check_enable_sourcemap';
41-
import { flatten } from '../utils/flatten';
4241

4342
interface ComponentOptions {
4443
namespace?: string;
@@ -1053,16 +1052,6 @@ export default class Component {
10531052
});
10541053
}
10551054

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-
10661055
// transform
10671056
// ```
10681057
// export let { x, y = 123 } = OBJ, z = 456
@@ -1075,21 +1064,13 @@ export default class Component {
10751064
for (let index = 0; index < node.declarations.length; index++) {
10761065
const declarator = node.declarations[index];
10771066
if (declarator.id.type !== 'Identifier') {
1078-
const variable_insert_declarators = [];
1079-
10801067
function get_new_name(local) {
10811068
const variable = component.var_lookup.get(local.name);
1082-
const is_props = variable.export_name && variable.writable;
10831069
if (variable.subscribable) {
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-
}
1070+
inserts.push(get_insert(variable));
10901071
}
10911072

1092-
if (is_props) {
1073+
if (variable.export_name && variable.writable) {
10931074
const alias_name = component.get_unique_name(local.name);
10941075
add_new_props({ type: 'Identifier', name: variable.export_name }, local, alias_name);
10951076
return alias_name;
@@ -1143,9 +1124,6 @@ export default class Component {
11431124
}
11441125

11451126
rename_identifiers(declarator.id);
1146-
1147-
node.declarations.splice(index + 1, 0, ...variable_insert_declarators);
1148-
index += variable_insert_declarators.length;
11491127
} else {
11501128
const { name } = declarator.id;
11511129
const variable = component.var_lookup.get(name);
@@ -1155,34 +1133,40 @@ export default class Component {
11551133
node.declarations.splice(index--, 1);
11561134
}
11571135
if (variable.subscribable && (is_props || declarator.init)) {
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-
}
1136+
inserts.push(get_insert(variable));
11661137
}
11671138
}
11681139
}
11691140

1141+
// Assertion that if we see props, it must be at the top level
1142+
if (props.length > 0 && !(parent.type === 'Program' && Array.isArray(parent[key]))) {
1143+
throw new Error('export is not at the top level');
1144+
}
1145+
11701146
if (Array.isArray(parent[key])) {
1147+
// If the variable declaration is part of some block, that is, among an array of statements
1148+
// then, we add the inserts and the $$props declaration after declaration
11711149
if (inserts.length > 0) {
11721150
inserts.reverse().forEach((insert) => {
11731151
parent[key].splice(index + 1, 0, ...insert);
11741152
});
11751153
}
11761154
if (props.length > 0) {
1155+
// b`` might return a Node array, but the $$props declaration will be flattened later
11771156
parent[key].splice(index + 1, 0, b`let { ${props} } = $$props;`);
11781157
}
11791158
if (node.declarations.length == 0) {
11801159
parent[key].splice(index, 1);
11811160
}
11821161
} else if (inserts.length > 0) {
1183-
this.replace({
1184-
type: 'BlockStatement',
1185-
body: flatten([node, inserts]) as Statement[]
1162+
// If the variable declaration is not part of a block, we instead get a dummy variable setting
1163+
// calling an immediately-invoked function expression containing all the subscription functions
1164+
node.declarations.push({
1165+
type: 'VariableDeclarator',
1166+
id: component.get_unique_name('$$subscription_inserts', scope),
1167+
init: x`(() => {
1168+
${inserts}
1169+
})()`
11861170
});
11871171
}
11881172

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

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

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

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,8 @@ 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-
69-
let foo = writable(0),
70-
_inserts_for_foo = (() => {
71-
$$subscribe_foo();
72-
})();
73-
68+
let foo = writable(0);
69+
$$subscribe_foo();
7470
const click_handler = () => $$subscribe_foo($$invalidate(0, foo = writable(0)));
7571
return [foo, $foo, click_handler];
7672
}

0 commit comments

Comments
 (0)