@@ -7,12 +7,33 @@ export function transform(code: string, filePath: string): string {
7
7
const sourceFile = project . addSourceFileAtPathIfExists ( filePath )
8
8
|| project . createSourceFile ( filePath , code , { overwrite : true } ) ;
9
9
10
- // Find and replace `createPicker<MyInterface>()`
10
+ // Resolve the alias or name for `createPicker` from "ts-runtime-picker"
11
+ let createPickerAlias : string | null = null ;
12
+
13
+ const importDeclarations = sourceFile . getImportDeclarations ( ) ;
14
+
15
+ for ( const importDecl of importDeclarations ) {
16
+ if ( importDecl . getModuleSpecifierValue ( ) === "ts-runtime-picker" ) {
17
+ const namedImports = importDecl . getNamedImports ( ) ;
18
+ for ( const namedImport of namedImports ) {
19
+ if ( namedImport . getName ( ) === "createPicker" ) {
20
+ createPickerAlias = namedImport . getAliasNode ( ) ?. getText ( ) || "createPicker" ;
21
+ }
22
+ }
23
+ }
24
+ }
25
+
26
+ // If we didn't find `createPicker` imported from "ts-runtime-picker", skip the transformation
27
+ if ( ! createPickerAlias ) {
28
+ return sourceFile . getFullText ( ) ;
29
+ }
30
+
31
+ // Find and replace `createPicker<MyInterface>()` or its alias
11
32
const calls = sourceFile . getDescendantsOfKind ( SyntaxKind . CallExpression ) ;
12
33
13
34
for ( const call of calls ) {
14
35
const expression = call . getExpression ( ) . getText ( ) ;
15
- if ( expression === "createPicker" && call . getTypeArguments ( ) . length > 0 ) {
36
+ if ( expression === createPickerAlias && call . getTypeArguments ( ) . length > 0 ) {
16
37
const typeArg = call . getTypeArguments ( ) [ 0 ] ;
17
38
const typeName = typeArg . getText ( ) ;
18
39
@@ -26,15 +47,13 @@ export function transform(code: string, filePath: string): string {
26
47
const keys = interfaceDecl . getProperties ( ) . map ( prop => `"${ prop . getName ( ) } "` ) ;
27
48
28
49
// Replace `createPicker<MyInterface>()` with runtime implementation
29
- call . replaceWithText ( `
30
- (obj) => {
31
- const keys = [${ keys . join ( "," ) } ];
32
- return keys.reduce((acc, key) => {
33
- if (key in obj) acc[key] = obj[key];
34
- return acc;
35
- }, {});
36
- }
37
- ` ) ;
50
+ call . replaceWithText ( `(_obj) => {
51
+ const _keys = [${ keys . join ( "," ) } ];
52
+ return _keys.reduce((_acc, _key) => {
53
+ if (_key in _obj) _acc[_key] = _obj[_key];
54
+ return _acc;
55
+ }, {});
56
+ }` ) ;
38
57
}
39
58
}
40
59
0 commit comments