Skip to content

Commit

Permalink
Merge pull request #14 from Hajime-san/fix-numeric-literals
Browse files Browse the repository at this point in the history
fix: preserve Numeric Literals and BigInt Literals
  • Loading branch information
Hajime-san authored Aug 28, 2023
2 parents b21aae5 + 522396f commit 021f249
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ Please install [Deno](https://deno.land/[email protected]/getting_started/installat
## command
### remote
- dry run
- `deno run --allow-env --allow-read --allow-write https://deno.land/x/[email protected].17/bin.ts -b=./src -c=./tsconfig.json -d`
- `deno run --allow-env --allow-read --allow-write https://deno.land/x/[email protected].18/bin.ts -b=./src -c=./tsconfig.json -d`
- transform
- `deno run --allow-env --allow-read --allow-write https://deno.land/x/[email protected].17/bin.ts -b=./src -c=./tsconfig.json -r`
- `deno run --allow-env --allow-read --allow-write https://deno.land/x/[email protected].18/bin.ts -b=./src -c=./tsconfig.json -r`
### local
- `deno task run-dry`
- `deno task run`
Expand Down
25 changes: 24 additions & 1 deletion src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,35 @@ import {
import { hasUnicodeStr, unescapeUnicodeStr } from './str.ts';

const transformModuleSpecifier = (
sourceFile: ts.SourceFile,
imports: Array<ResolvedModuleImport>,
) => {
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');
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions src/transform_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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`,
);
});
});

0 comments on commit 021f249

Please sign in to comment.