diff --git a/README.md b/README.md index 6af69c3..9b79e2e 100644 --- a/README.md +++ b/README.md @@ -71,9 +71,9 @@ Please install [Deno](https://deno.land/manual@v1.30.3/getting_started/installat ## command ### remote - dry run - - `deno run --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.17/bin.ts -b=./src -c=./tsconfig.json -d` + - `deno run --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.18/bin.ts -b=./src -c=./tsconfig.json -d` - transform - - `deno run --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.17/bin.ts -b=./src -c=./tsconfig.json -r` + - `deno run --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.18/bin.ts -b=./src -c=./tsconfig.json -r` ### local - `deno task run-dry` - `deno task run` diff --git a/src/transform.ts b/src/transform.ts index a58ba30..4576d6c 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -7,12 +7,35 @@ import { import { hasUnicodeStr, unescapeUnicodeStr } from './str.ts'; const transformModuleSpecifier = ( + sourceFile: ts.SourceFile, imports: Array, ) => { return (context: ts.TransformationContext) => (rootNode: ts.Node) => { const visit = (node: ts.Node): ts.Node => { const newNode = ts.visitEachChild(node, visit, context); + // Preserve Numeric Literals + // + // const numericSeparators = 100_000; + // const octal = 0001; + // const hex = 0x00111; + // const binary = 0b0011; + // https://github.com/microsoft/TypeScript/issues/53182 + if (ts.isNumericLiteral(newNode)) { + return context.factory.createNumericLiteral( + newNode.getText(sourceFile), + ); + } + + // Preserve BigInt Literals + // + // const previouslyMaxSafeInteger = 9007199254740991n; + if (ts.isBigIntLiteral(newNode)) { + return context.factory.createBigIntLiteral( + newNode.getText(sourceFile), + ); + } + // Transform "import call" // // const foo = import('./foo'); @@ -91,7 +114,7 @@ export const transform = (args: { }): string => { const { sourceFile, imports, tsConfigObject, printer } = args; const transformationResult = ts.transform(sourceFile, [ - transformModuleSpecifier(imports), + transformModuleSpecifier(sourceFile, imports), ], tsConfigObject.options); const printed = printer.printNode( diff --git a/src/transform_test.ts b/src/transform_test.ts index 089a2df..3168a0e 100644 --- a/src/transform_test.ts +++ b/src/transform_test.ts @@ -11,7 +11,9 @@ Deno.test('transform', async (t) => { sourceFile: ts.createSourceFile( './src/App.tsx', `import { ComponentA } from './ComponentA';\n` + - `const str = '😎';\n`, + `const str = '😎';\n` + + `const bigIntLiterals = 0o777777777777n;\n` + + `const numericSeparators = 100_000;\n`, ts.ScriptTarget.ESNext, ), imports: [ @@ -21,7 +23,9 @@ Deno.test('transform', async (t) => { printer: ts.createPrinter(), }), `import { ComponentA } from "./ComponentA.tsx";\n` + - `const str = "😎";\n`, + `const str = "😎";\n` + + `const bigIntLiterals = 0o777777777777n;\n` + + `const numericSeparators = 100_000;\n`, ); }); });