@@ -36,14 +36,13 @@ export function transformBindingPattern(
3636 propertyAccessStack : ts . PropertyName [ ] = [ ]
3737) : lua . Statement [ ] {
3838 const result : lua . Statement [ ] = [ ] ;
39- const isObjectBindingPattern = ts . isObjectBindingPattern ( pattern ) ;
4039
4140 for ( const [ index , element ] of pattern . elements . entries ( ) ) {
4241 if ( ts . isOmittedExpression ( element ) ) continue ;
4342
4443 if ( ts . isArrayBindingPattern ( element . name ) || ts . isObjectBindingPattern ( element . name ) ) {
4544 // nested binding pattern
46- const propertyName = isObjectBindingPattern
45+ const propertyName = ts . isObjectBindingPattern ( pattern )
4746 ? element . propertyName
4847 : ts . createNumericLiteral ( String ( index + 1 ) ) ;
4948
@@ -73,24 +72,37 @@ export function transformBindingPattern(
7372 continue ;
7473 }
7574
76- if ( isObjectBindingPattern ) {
77- const elements = pattern . elements as ts . NodeArray < ts . BindingElement > ;
78- const usedProperties = elements . map ( e =>
79- lua . createTableFieldExpression (
80- lua . createBooleanLiteral ( true ) ,
81- lua . createStringLiteral (
82- ( ( e . propertyName ?? e . name ) as ts . Identifier ) . text ,
83- e . propertyName ?? e . name
84- )
85- )
75+ if ( ts . isObjectBindingPattern ( pattern ) ) {
76+ const excludedProperties : ts . Identifier [ ] = [ ] ;
77+
78+ for ( const element of pattern . elements ) {
79+ // const { ...x } = ...;
80+ // ~~~~
81+ if ( element . dotDotDotToken ) continue ;
82+
83+ // const { x } = ...;
84+ // ~
85+ if ( ts . isIdentifier ( element . name ) && ! element . propertyName ) {
86+ excludedProperties . push ( element . name ) ;
87+ }
88+
89+ // const { x: ... } = ...;
90+ // ~~~~~~
91+ if ( element . propertyName && element . name && ts . isIdentifier ( element . propertyName ) ) {
92+ excludedProperties . push ( element . propertyName ) ;
93+ }
94+ }
95+
96+ const excludedPropertiesTable = excludedProperties . map ( e =>
97+ lua . createTableFieldExpression ( lua . createBooleanLiteral ( true ) , lua . createStringLiteral ( e . text , e ) )
8698 ) ;
8799
88100 expression = transformLuaLibFunction (
89101 context ,
90102 LuaLibFeature . ObjectRest ,
91103 undefined ,
92104 tableExpression ,
93- lua . createTableExpression ( usedProperties )
105+ lua . createTableExpression ( excludedPropertiesTable )
94106 ) ;
95107 } else {
96108 expression = transformLuaLibFunction (
@@ -104,7 +116,7 @@ export function transformBindingPattern(
104116 } else {
105117 expression = lua . createTableIndexExpression (
106118 tableExpression ,
107- isObjectBindingPattern ? propertyName : lua . createNumericLiteral ( index + 1 )
119+ ts . isObjectBindingPattern ( pattern ) ? propertyName : lua . createNumericLiteral ( index + 1 )
108120 ) ;
109121 }
110122
0 commit comments