@@ -25,7 +25,7 @@ import TemplateScope from './nodes/shared/TemplateScope';
2525import fuzzymatch from '../utils/fuzzymatch' ;
2626import get_object from './utils/get_object' ;
2727import 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' ;
2929import add_to_set from './utils/add_to_set' ;
3030import check_graph_for_cycles from './utils/check_graph_for_cycles' ;
3131import { print , b , x } from 'code-red' ;
@@ -38,7 +38,6 @@ import compiler_warnings from './compiler_warnings';
3838import compiler_errors from './compiler_errors' ;
3939import { extract_ignores_above_position , extract_svelte_ignore_from_comments } from '../utils/extract_svelte_ignore' ;
4040import check_enable_sourcemap from './utils/check_enable_sourcemap' ;
41- import { flatten } from '../utils/flatten' ;
4241
4342interface 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,18 +1064,11 @@ 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 ) ;
10821069 const is_props = variable . export_name && variable . writable ;
10831070 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- }
1071+ inserts . push ( get_insert ( variable ) ) ;
10901072 }
10911073
10921074 if ( is_props ) {
@@ -1143,9 +1125,6 @@ export default class Component {
11431125 }
11441126
11451127 rename_identifiers ( declarator . id ) ;
1146-
1147- node . declarations . splice ( index + 1 , 0 , ...variable_insert_declarators ) ;
1148- index += variable_insert_declarators . length ;
11491128 } else {
11501129 const { name } = declarator . id ;
11511130 const variable = component . var_lookup . get ( name ) ;
@@ -1155,34 +1134,40 @@ export default class Component {
11551134 node . declarations . splice ( index -- , 1 ) ;
11561135 }
11571136 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- }
1137+ inserts . push ( get_insert ( variable ) ) ;
11661138 }
11671139 }
11681140 }
11691141
1142+ // Assertion that if we see props, it must be at the top level
1143+ if ( props . length > 0 && ! ( parent . type === 'Program' && Array . isArray ( parent [ key ] ) ) ) {
1144+ throw new Error ( 'export is not at the top level' ) ;
1145+ }
1146+
11701147 if ( Array . isArray ( parent [ key ] ) ) {
1148+ // If the variable declaration is part of some block, that is, among an array of statements
1149+ // then, we add the inserts and the $$props declaration after declaration
11711150 if ( inserts . length > 0 ) {
11721151 inserts . reverse ( ) . forEach ( ( insert ) => {
11731152 parent [ key ] . splice ( index + 1 , 0 , ...insert ) ;
11741153 } ) ;
11751154 }
11761155 if ( props . length > 0 ) {
1156+ // b`` might return a Node array, but the $$props declaration will be flattened later
11771157 parent [ key ] . splice ( index + 1 , 0 , b `let { ${ props } } = $$props;` ) ;
11781158 }
11791159 if ( node . declarations . length == 0 ) {
11801160 parent [ key ] . splice ( index , 1 ) ;
11811161 }
11821162 } else if ( inserts . length > 0 ) {
1183- this . replace ( {
1184- type : 'BlockStatement' ,
1185- body : flatten ( [ node , inserts ] ) as Statement [ ]
1163+ // If the variable declaration is not part of a block, we instead get a dummy variable setting
1164+ // calling an immediately-invoked function expression containing all the subscription functions
1165+ node . declarations . push ( {
1166+ type : 'VariableDeclarator' ,
1167+ id : component . get_unique_name ( '$$subscription_inserts' , scope ) ,
1168+ init : x `(() => {
1169+ ${ inserts }
1170+ })()`
11861171 } ) ;
11871172 }
11881173
0 commit comments