Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve Numeric Literals and BigInt Literals #14

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`,
);
});
});