Skip to content

Commit

Permalink
Merge pull request #11 from Hajime-san/add-import-keyword
Browse files Browse the repository at this point in the history
add import keyword
  • Loading branch information
Hajime-san authored Apr 9, 2023
2 parents f1f8d60 + 8aa30be commit 4ea272a
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 5 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ root

## limitation
- Can't resolve `paths` alias of TypeScript compiler options.
- Can't resolve `import()` syntax, commonly called `dynamic import`.
- Plase be careful if your code have the text `//_PRESERVE_NEWLINE_//` which will be replace newline, because of that keeps original newline before tsc compiler optimize it.
- Can't keep `single quatation` or `duble quatation` , `semicolon` and `indatation` of original source code.

Expand All @@ -72,9 +71,9 @@ Please install [Deno](https://deno.land/[email protected]/getting_started/installat
## command
### remote
- dry run
- `deno run --unstable --allow-env --allow-read --allow-write https://deno.land/x/[email protected].15/bin.ts -b=./src -c=./tsconfig.json -d`
- `deno run --unstable --allow-env --allow-read --allow-write https://deno.land/x/[email protected].16/bin.ts -b=./src -c=./tsconfig.json -d`
- transform
- `deno run --unstable --allow-env --allow-read --allow-write https://deno.land/x/[email protected].15/bin.ts -b=./src -c=./tsconfig.json -r`
- `deno run --unstable --allow-env --allow-read --allow-write https://deno.land/x/[email protected].16/bin.ts -b=./src -c=./tsconfig.json -r`
### local
- `deno task run-dry`
- `deno task run`
Expand Down
2 changes: 1 addition & 1 deletion bin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { main } from "./src/mod.ts";
import { main } from "./src/bin_internal.ts";

await main();
2 changes: 2 additions & 0 deletions examples/repo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ComponentB } from './ComponentB';
import { _ComponentC } from './ComponentC';
import { ComponentC } from './ComponentC';
import { ComponentD } from './ComponentD';
const ComponentE = React.lazy(() => import('./ComponentE'));
import style from './style.css';
import './sideEffect';

Expand All @@ -19,6 +20,7 @@ createRoot(document.getElementById('root') as HTMLElement)
<ComponentB />
<ComponentC />
<ComponentD />
<ComponentE />
<p>{str}</p>
</React.StrictMode>,
);
3 changes: 3 additions & 0 deletions examples/repo/src/ComponentE.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const ComponentE = () => {
return <div>ComponentE</div>;
};
File renamed without changes.
21 changes: 21 additions & 0 deletions src/resolve_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ export const getModuleSpecifier = <T extends HasModuleSpecifierNode>(args: {
};
};

export const getExpressionArguments = (args: {
node: ts.CallExpression;
imports: ReturnType<typeof resolvedModules>;
}): {
expressionArguments: Array<string>;
node: ts.CallExpression;
} => {
const { node, imports } = args;
const expressionArguments = node.arguments.map((argument) => {
if (isTokenObject(argument)) {
return imports.find((v) => v.original === argument.text)?.resolved ??
argument.text;
}
}).filter((v) => typeof v !== 'undefined') as Array<string>;

return {
expressionArguments,
node,
};
};

export type ResolvedModuleImport = {
original: string;
resolved: string;
Expand Down
19 changes: 19 additions & 0 deletions src/resolve_util_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { asserts } from './dev_deps.ts';
import { path } from './deps.ts';
import {
getExpressionArguments,
getModuleSpecifier,
hasShouldResolveImportedFiles,
isTokenObject,
Expand All @@ -9,6 +10,7 @@ import {
} from './resolve_util.ts';
import {
externalLibImportDeclaration,
localCallExpression,
localSourceImportDeclaration,
tsConfigMockObject,
} from './tests/fixture/mod.ts';
Expand Down Expand Up @@ -157,3 +159,20 @@ Deno.test('getModuleSpecifier', async (t) => {
);
});
});

Deno.test('getExpressionArguments', async (t) => {
await t.step('local module', () => {
assertEquals(
getExpressionArguments({
node: localCallExpression,
imports: [
{ original: './ComponentE', resolved: './ComponentE.tsx' },
],
}),
{
expressionArguments: ['./ComponentE.tsx'],
node: localCallExpression,
},
);
});
});
10 changes: 10 additions & 0 deletions src/tests/fixture/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const externalLibImportDeclaration = ts.factory.createImportDeclaration(
undefined,
);

const localCallExpression = ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier('System'),
'register',
),
undefined,
[ts.factory.createStringLiteral('./ComponentE')],
);

const tsConfigMockObject: ts.ParsedCommandLine = {
options: {
jsx: 4,
Expand Down Expand Up @@ -64,6 +73,7 @@ const tsConfigMockObject: ts.ParsedCommandLine = {

export {
externalLibImportDeclaration,
localCallExpression,
localSourceImportDeclaration,
tsConfigMockObject,
};
28 changes: 27 additions & 1 deletion src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ts } from './deps.ts';
import { getModuleSpecifier, ResolvedModuleImport } from './resolve_util.ts';
import {
getExpressionArguments,
getModuleSpecifier,
ResolvedModuleImport,
} from './resolve_util.ts';
import { hasUnicodeStr, unescapeUnicodeStr } from './str.ts';

const transformModuleSpecifier = (
Expand All @@ -9,6 +13,28 @@ const transformModuleSpecifier = (
const visit = (node: ts.Node): ts.Node => {
const newNode = ts.visitEachChild(node, visit, context);

// Transform "import call"
//
// const foo = import('./foo');
// to
// const foo = import('./foo.(ts|tsx)');
if (
ts.isCallExpression(newNode) &&
newNode.expression.kind === ts.SyntaxKind.ImportKeyword
) {
const { expressionArguments, node } = getExpressionArguments({
node: newNode,
imports,
});
return context.factory.updateCallExpression(
node,
node.expression,
node.typeArguments,
expressionArguments.map((argument) =>
context.factory.createStringLiteral(argument)
),
);
}
// Transform "aggregating modules"
//
// export { foo } from "./foo"
Expand Down

0 comments on commit 4ea272a

Please sign in to comment.