@@ -25,10 +25,10 @@ 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 } from 'estree' ;
28+ import { Node , ImportDeclaration , ExportNamedDeclaration , Identifier , ExpressionStatement , AssignmentExpression , Literal , Property , RestElement , ExportDefaultDeclaration , ExportAllDeclaration , FunctionDeclaration , FunctionExpression , Statement , VariableDeclarator } from 'estree' ;
2929import add_to_set from './utils/add_to_set' ;
3030import 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' ;
3232import { is_reserved_keyword } from './utils/reserved_keywords' ;
3333import { apply_preprocessor_sourcemap } from '../utils/mapped_code' ;
3434import 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 }
0 commit comments