Skip to content

Commit

Permalink
Enhance createPicker transformation.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
HichemTab-tech committed Dec 14, 2024
1 parent 3584244 commit 1fb455c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-runtime-picker",
"version": "1.0.1",
"version": "1.0.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
".": {
Expand Down
41 changes: 30 additions & 11 deletions src/ts-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,33 @@ export function transform(code: string, filePath: string): string {
const sourceFile = project.addSourceFileAtPathIfExists(filePath)
|| project.createSourceFile(filePath, code, { overwrite: true });

// Find and replace `createPicker<MyInterface>()`
// Resolve the alias or name for `createPicker` from "ts-runtime-picker"
let createPickerAlias: string | null = null;

const importDeclarations = sourceFile.getImportDeclarations();

for (const importDecl of importDeclarations) {
if (importDecl.getModuleSpecifierValue() === "ts-runtime-picker") {
const namedImports = importDecl.getNamedImports();
for (const namedImport of namedImports) {
if (namedImport.getName() === "createPicker") {
createPickerAlias = namedImport.getAliasNode()?.getText() || "createPicker";
}
}
}
}

// If we didn't find `createPicker` imported from "ts-runtime-picker", skip the transformation
if (!createPickerAlias) {
return sourceFile.getFullText();
}

// Find and replace `createPicker<MyInterface>()` or its alias
const calls = sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression);

for (const call of calls) {
const expression = call.getExpression().getText();
if (expression === "createPicker" && call.getTypeArguments().length > 0) {
if (expression === createPickerAlias && call.getTypeArguments().length > 0) {
const typeArg = call.getTypeArguments()[0];
const typeName = typeArg.getText();

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

// Replace `createPicker<MyInterface>()` with runtime implementation
call.replaceWithText(`
(obj) => {
const keys = [${keys.join(",")}];
return keys.reduce((acc, key) => {
if (key in obj) acc[key] = obj[key];
return acc;
}, {});
}
`);
call.replaceWithText(`(_obj) => {
const _keys = [${keys.join(",")}];
return _keys.reduce((_acc, _key) => {
if (_key in _obj) _acc[_key] = _obj[_key];
return _acc;
}, {});
}`);
}
}

Expand Down

0 comments on commit 1fb455c

Please sign in to comment.