Skip to content

Commit 1fb455c

Browse files
Enhance createPicker transformation.
Updated package version to 1.0.2 and refined the `createPicker` handling in the TypeScript transformer. The alias resolution ensures compatibility with imported `createPicker` references, improving the runtime transformation logic.
1 parent 3584244 commit 1fb455c

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-runtime-picker",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
".": {

src/ts-transformer.ts

+30-11
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,33 @@ export function transform(code: string, filePath: string): string {
77
const sourceFile = project.addSourceFileAtPathIfExists(filePath)
88
|| project.createSourceFile(filePath, code, { overwrite: true });
99

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
1132
const calls = sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression);
1233

1334
for (const call of calls) {
1435
const expression = call.getExpression().getText();
15-
if (expression === "createPicker" && call.getTypeArguments().length > 0) {
36+
if (expression === createPickerAlias && call.getTypeArguments().length > 0) {
1637
const typeArg = call.getTypeArguments()[0];
1738
const typeName = typeArg.getText();
1839

@@ -26,15 +47,13 @@ export function transform(code: string, filePath: string): string {
2647
const keys = interfaceDecl.getProperties().map(prop => `"${prop.getName()}"`);
2748

2849
// 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+
}`);
3857
}
3958
}
4059

0 commit comments

Comments
 (0)