@@ -806,7 +806,7 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression
806806 }
807807
808808 const object = val as Record < string | symbol , unknown >
809- const propertyDescriptors : Property [ ] = [ ]
809+ const propertyDescriptors : [ string | symbol , ObjectExpression ] [ ] = [ ]
810810 for ( const key of Reflect . ownKeys ( val ) ) {
811811 // TODO [>=4] Throw an error for getters.
812812 const child = object [ key ]
@@ -823,29 +823,28 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression
823823 if ( writable ) {
824824 propertyDescriptor . push ( property ( 'writable' , literal ( true ) ) )
825825 }
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+ ] )
832830 } else if (
833831 context &&
834832 childContext &&
835833 namedContexts . indexOf ( childContext ) >= namedContexts . indexOf ( context )
836834 ) {
837835 if ( key === '__proto__' ) {
838- propertyDescriptors . push (
839- property ( key , {
836+ propertyDescriptors . push ( [
837+ key ,
838+ {
840839 type : 'ObjectExpression' ,
841840 properties : [
842841 property ( 'value' , generate ( child ) ) ,
843842 property ( 'configurable' , literal ( true ) ) ,
844843 property ( 'enumerable' , literal ( true ) ) ,
845844 property ( 'writable' , literal ( true ) )
846845 ]
847- } )
848- )
846+ }
847+ ] )
849848 } else {
850849 childContext . assignment = {
851850 type : 'AssignmentExpression' ,
@@ -871,24 +870,29 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression
871870 }
872871
873872 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 = [
877883 {
878884 type : 'ObjectExpression' ,
879- properties : propertyDescriptors
885+ properties : propertyDescriptors . map ( ( [ key , expression ] ) => property ( key , expression ) )
880886 }
881- ] )
887+ ]
888+ }
889+
890+ if ( ! context ) {
891+ return methodCall ( identifier ( 'Object' ) , name , [ objectExpression , ...args ] )
882892 }
883893
884894 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 ] ) ,
892896 context . assignment
893897 )
894898 }
0 commit comments