@@ -2,17 +2,43 @@ import * as ts from "typescript";
2
2
3
3
const fullyInitializedObjExprs = new Map < ts . Expression , boolean > ( ) ;
4
4
5
- function propertyAssignmentFromSpreadProperty (
6
- property : ts . Symbol ,
7
- sourceObjectName : string
8
- ) : ts . PropertyAssignment {
9
- return ts . createPropertyAssignment (
10
- property . name ,
11
- ts . createPropertyAccess (
12
- ts . createIdentifier ( sourceObjectName ) ,
13
- ts . createIdentifier ( property . name )
14
- )
15
- ) ;
5
+ export default function transformer (
6
+ program : ts . Program
7
+ ) : ts . TransformerFactory < ts . SourceFile > {
8
+ const checker = program . getTypeChecker ( ) ;
9
+
10
+ return context => {
11
+ const visit : ts . Visitor = node => {
12
+ if (
13
+ ts . isSpreadAssignment ( node ) &&
14
+ ts . isObjectLiteralExpression ( node . parent )
15
+ ) {
16
+ const type = checker . getTypeAtLocation ( node . expression ) ;
17
+ const properties = checker . getPropertiesOfType ( type ) ;
18
+
19
+ if (
20
+ isSeenObject ( node . expression ) ||
21
+ isFullyDefinedObject ( type , properties )
22
+ ) {
23
+ objectFullyDefined ( node . parent ) ;
24
+
25
+ return properties . map ( prop =>
26
+ propertyAssignmentFromSpreadProperty (
27
+ prop ,
28
+ node . expression . getText ( )
29
+ )
30
+ ) ;
31
+ }
32
+
33
+ objectNotFullyDefined ( node . parent ) ;
34
+
35
+ return node ;
36
+ }
37
+ return ts . visitEachChild ( node , child => visit ( child ) , context ) ;
38
+ } ;
39
+
40
+ return node => ts . visitNode ( node , visit ) ;
41
+ } ;
16
42
}
17
43
18
44
function isSeenObject ( identifier : ts . Expression ) : boolean {
@@ -53,41 +79,15 @@ function isFullyDefinedObject(type: ts.Type, properties: ts.Symbol[]) {
53
79
) ;
54
80
}
55
81
56
- export default function transformer (
57
- program : ts . Program
58
- ) : ts . TransformerFactory < ts . SourceFile > {
59
- const checker = program . getTypeChecker ( ) ;
60
-
61
- return context => {
62
- const visit : ts . Visitor = node => {
63
- if (
64
- ts . isSpreadAssignment ( node ) &&
65
- ts . isObjectLiteralExpression ( node . parent )
66
- ) {
67
- const type = checker . getTypeAtLocation ( node . expression ) ;
68
- const properties = checker . getPropertiesOfType ( type ) ;
69
-
70
- if (
71
- isSeenObject ( node . expression ) ||
72
- isFullyDefinedObject ( type , properties )
73
- ) {
74
- objectFullyDefined ( node . parent ) ;
75
-
76
- return properties . map ( prop =>
77
- propertyAssignmentFromSpreadProperty (
78
- prop ,
79
- node . expression . getText ( )
80
- )
81
- ) ;
82
- }
83
-
84
- objectNotFullyDefined ( node . parent ) ;
85
-
86
- return node ;
87
- }
88
- return ts . visitEachChild ( node , child => visit ( child ) , context ) ;
89
- } ;
90
-
91
- return node => ts . visitNode ( node , visit ) ;
92
- } ;
82
+ function propertyAssignmentFromSpreadProperty (
83
+ property : ts . Symbol ,
84
+ sourceObjectName : string
85
+ ) : ts . PropertyAssignment {
86
+ return ts . createPropertyAssignment (
87
+ property . name ,
88
+ ts . createPropertyAccess (
89
+ ts . createIdentifier ( sourceObjectName ) ,
90
+ ts . createIdentifier ( property . name )
91
+ )
92
+ ) ;
93
93
}
0 commit comments