Skip to content

Commit

Permalink
Merge pull request #12 from Hajime-san/clean-up
Browse files Browse the repository at this point in the history
clean up
  • Loading branch information
Hajime-san authored Apr 10, 2023
2 parents 310a938 + 3f52664 commit d6a9e02
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
12 changes: 9 additions & 3 deletions src/bin_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const main = async (args: {
result: string;
}> = [];

console.log('processing...');

for await (const entry of walk(_basePath, { match, skip })) {
if (entry.isFile) {
const targetPath = entry.path;
Expand Down Expand Up @@ -132,9 +134,10 @@ export const main = async (args: {
const LOG_FILE_NAME = 'module-specifier-resolver.log';

const writeLog = async (): Promise<void> => {
// try remove log file if it exsist or not
try {
await Deno.remove(LOG_FILE_NAME);
} catch (_) {}
} catch (_) { /* noop */ }
await Promise.all(transformedList.map((transformed) => {
return new Promise((resolve, reject) => {
const { path, result } = transformed;
Expand All @@ -159,10 +162,13 @@ ${result}
});
})).then(() => {
console.log(
`%cDry run: ${transformedList.length} files, finished.
${LOG_FILE_NAME}`,
`%cDry run: ${transformedList.length} files, finished.`,
'color: green',
);
console.log(
`%cInfo: ${LOG_FILE_NAME}`,
'color: blue',
);
}).catch((error) => {
throw new Error(error);
});
Expand Down
4 changes: 0 additions & 4 deletions src/resolve_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export const getModuleSpecifier = <T extends HasModuleSpecifierNode>(args: {
imports: ReturnType<typeof resolvedModules>;
}): {
moduleSpecifier: ModuleSpecifierReturnType<T>;
node: T;
} => {
const { node, imports } = args;
let moduleSpecifier: ModuleSpecifierReturnType<T>;
Expand All @@ -82,7 +81,6 @@ export const getModuleSpecifier = <T extends HasModuleSpecifierNode>(args: {
return {
// @ts-ignore Variable 'X' is used before being assigned. deno-ts(2454)
moduleSpecifier,
node,
};
};

Expand All @@ -91,7 +89,6 @@ export const getExpressionArguments = (args: {
imports: ReturnType<typeof resolvedModules>;
}): {
expressionArguments: Array<string>;
node: ts.CallExpression;
} => {
const { node, imports } = args;
const expressionArguments = node.arguments.map((argument) => {
Expand All @@ -103,7 +100,6 @@ export const getExpressionArguments = (args: {

return {
expressionArguments,
node,
};
};

Expand Down
3 changes: 0 additions & 3 deletions src/resolve_util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ Deno.test('getModuleSpecifier', async (t) => {
}),
{
moduleSpecifier: './ComponentA.tsx',
node: localSourceImportDeclaration,
},
);
});
Expand All @@ -154,7 +153,6 @@ Deno.test('getModuleSpecifier', async (t) => {
}),
{
moduleSpecifier: 'react',
node: externalLibImportDeclaration,
},
);
});
Expand All @@ -171,7 +169,6 @@ Deno.test('getExpressionArguments', async (t) => {
}),
{
expressionArguments: ['./ComponentE.tsx'],
node: localCallExpression,
},
);
});
Expand Down
50 changes: 26 additions & 24 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,58 @@ const transformModuleSpecifier = (
ts.isCallExpression(newNode) &&
newNode.expression.kind === ts.SyntaxKind.ImportKeyword
) {
const { expressionArguments, node } = getExpressionArguments({
const { expressionArguments } = getExpressionArguments({
node: newNode,
imports,
});
return context.factory.updateCallExpression(
node,
node.expression,
node.typeArguments,
newNode,
newNode.expression,
newNode.typeArguments,
expressionArguments.map((argument) =>
context.factory.createStringLiteral(argument)
),
);
}
// Transform "aggregating modules"
//
// export { foo } from "./foo"
// to
// export { foo } from "./foo.(ts|tsx|d.ts)"
if (ts.isExportDeclaration(newNode)) {
const { moduleSpecifier, node } = getModuleSpecifier({
const { moduleSpecifier } = getModuleSpecifier({
node: newNode,
imports,
});
return context.factory.updateExportDeclaration(
node,
node.modifiers,
node.isTypeOnly,
node.exportClause,
moduleSpecifier
? context.factory.createStringLiteral(moduleSpecifier)
: undefined,
node.assertClause,
);
// export { foo } from "./foo"
// to
// export { foo } from "./foo.(ts|tsx|d.ts)"
if (moduleSpecifier) {
return context.factory.updateExportDeclaration(
newNode,
newNode.modifiers,
newNode.isTypeOnly,
newNode.exportClause,
context.factory.createStringLiteral(moduleSpecifier),
newNode.assertClause,
);
}
//
// export { foo }
return newNode;
}
// Transform "static import"
//
// import { bar } from "./bar"
// to
// import { bar } from "./bar.(ts|tsx|d.ts)"
if (ts.isImportDeclaration(newNode)) {
const { moduleSpecifier, node } = getModuleSpecifier({
const { moduleSpecifier } = getModuleSpecifier({
node: newNode,
imports,
});
return context.factory.updateImportDeclaration(
node,
node.modifiers,
node.importClause,
newNode,
newNode.modifiers,
newNode.importClause,
context.factory.createStringLiteral(moduleSpecifier),
node.assertClause,
newNode.assertClause,
);
}
return newNode;
Expand Down

0 comments on commit d6a9e02

Please sign in to comment.