@@ -806,7 +806,7 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression
806
806
}
807
807
808
808
const object = val as Record < string | symbol , unknown >
809
- const propertyDescriptors : Property [ ] = [ ]
809
+ const propertyDescriptors : [ string | symbol , ObjectExpression ] [ ] = [ ]
810
810
for ( const key of Reflect . ownKeys ( val ) ) {
811
811
// TODO [>=4] Throw an error for getters.
812
812
const child = object [ key ]
@@ -823,29 +823,28 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression
823
823
if ( writable ) {
824
824
propertyDescriptor . push ( property ( 'writable' , literal ( true ) ) )
825
825
}
826
- propertyDescriptors . push (
827
- property ( key , {
828
- type : 'ObjectExpression' ,
829
- properties : propertyDescriptor
830
- } )
831
- )
826
+ propertyDescriptors . push ( [
827
+ key ,
828
+ { type : 'ObjectExpression' , properties : propertyDescriptor }
829
+ ] )
832
830
} else if (
833
831
context &&
834
832
childContext &&
835
833
namedContexts . indexOf ( childContext ) >= namedContexts . indexOf ( context )
836
834
) {
837
835
if ( key === '__proto__' ) {
838
- propertyDescriptors . push (
839
- property ( key , {
836
+ propertyDescriptors . push ( [
837
+ key ,
838
+ {
840
839
type : 'ObjectExpression' ,
841
840
properties : [
842
841
property ( 'value' , generate ( child ) ) ,
843
842
property ( 'configurable' , literal ( true ) ) ,
844
843
property ( 'enumerable' , literal ( true ) ) ,
845
844
property ( 'writable' , literal ( true ) )
846
845
]
847
- } )
848
- )
846
+ }
847
+ ] )
849
848
} else {
850
849
childContext . assignment = {
851
850
type : 'AssignmentExpression' ,
@@ -871,24 +870,29 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression
871
870
}
872
871
873
872
if ( propertyDescriptors . length ) {
874
- if ( ! context ) {
875
- return methodCall ( identifier ( 'Object' ) , 'defineProperties' , [
876
- objectExpression ,
873
+ let name : string
874
+ let args : Expression [ ]
875
+
876
+ if ( propertyDescriptors . length === 1 ) {
877
+ const [ [ key , expression ] ] = propertyDescriptors
878
+ name = 'defineProperty'
879
+ args = [ typeof key === 'string' ? literal ( key ) : symbolToEstree ( key ) , expression ]
880
+ } else {
881
+ name = 'defineProperties'
882
+ args = [
877
883
{
878
884
type : 'ObjectExpression' ,
879
- properties : propertyDescriptors
885
+ properties : propertyDescriptors . map ( ( [ key , expression ] ) => property ( key , expression ) )
880
886
}
881
- ] )
887
+ ]
888
+ }
889
+
890
+ if ( ! context ) {
891
+ return methodCall ( identifier ( 'Object' ) , name , [ objectExpression , ...args ] )
882
892
}
883
893
884
894
context . assignment = replaceAssignment (
885
- methodCall ( identifier ( 'Object' ) , 'defineProperties' , [
886
- identifier ( context . name ! ) ,
887
- {
888
- type : 'ObjectExpression' ,
889
- properties : propertyDescriptors
890
- }
891
- ] ) ,
895
+ methodCall ( identifier ( 'Object' ) , name , [ identifier ( context . name ! ) , ...args ] ) ,
892
896
context . assignment
893
897
)
894
898
}
0 commit comments