@@ -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,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
0 commit comments