Skip to content

Commit

Permalink
Merge pull request #1 from Hajime-san/fix-encoding
Browse files Browse the repository at this point in the history
fix encoding
  • Loading branch information
Hajime-san authored Feb 9, 2023
2 parents b44aaa0 + 911d979 commit 7ede2b7
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 32 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,52 @@ This tool transforms your local TypeScript code that gives you to rewrite file e
## transform examples
```ts
export { foo } from "./foo"
transform to
// transform to
export { foo } from "./foo.(ts|tsx|d.ts)"
```

```ts
import { bar } from "./bar"
transform to
// transform to
import { bar } from "./bar.(ts|tsx|d.ts)"
```

## limitation
- Can't resolve `paths` alias of TypeScript compiler options.
- Can't resolve `import()` syntax, commonly called `dynamic import`.
- Can't keep `newline` of original source code.
- Can't keep `single quatation` or `duble quatation` and `semicolon` of original source code.

## command
### remote
- dry run
- `deno run --unstable --allow-env --allow-read https://deno.land/x/[email protected].5/bin.ts -b=./src -c=./tsconfig.json -d`
- `deno run --unstable --allow-env --allow-read https://deno.land/x/[email protected].6/bin.ts -b=./src -c=./tsconfig.json -d`
- transform
- `deno run --unstable --allow-env --allow-read --allow-write https://deno.land/x/[email protected].5/bin.ts -b=./src -c=./tsconfig.json -r`
- `deno run --unstable --allow-env --allow-read --allow-write https://deno.land/x/[email protected].6/bin.ts -b=./src -c=./tsconfig.json -r`
### local
- `deno task run-dry`
- `deno task run`

### arguments
| key | description | type | default |
|-----|-----|-----|-----|
| -b | local of base directory | `string` | `./` |
| -b | local of base directory | `string` | `.` |
| -c | local of base `tsconfig.json` | `string` | `./tsconfig.json` |
| -d | dry run | `boolean` | `false` |
| -r | enable repl interface | `boolean` | `false` |

## tips
After you ran `bin.ts`, you should run `npx tsc --noEmit` due to check correctness of transformation by this tool.
- `tsconfig.json` example
```json
{
"compilerOptions": {
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"noEmit": true
```json
{
"compilerOptions": {
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"noEmit": true
}
}
}
```
```

## License
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
3 changes: 3 additions & 0 deletions examples/repo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import { ComponentC } from './ComponentC';
import style from './style.css';
import './sideEffect';

const str = '😎';

// a comment
createRoot(document.getElementById('root') as HTMLElement)
.render(
<React.StrictMode>
<ComponentA />
<ComponentB />
<ComponentC />
<p>{str}</p>
</React.StrictMode>,
);
14 changes: 0 additions & 14 deletions examples/repo/src/Main/App.tsx

This file was deleted.

10 changes: 7 additions & 3 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cli, io, path, ts, walk } from './dev_deps.ts';
import { relativeFilePath } from './path.ts';
import { hasUnicodeStr, unescapeUnicodeStr } from './str.ts';

type NodeLike = ts.Node | ts.Expression;

Expand Down Expand Up @@ -147,11 +148,13 @@ export const transform = (args: {
transformeModuleSpecifier(imports),
], tsConfigObject.options);

return printer.printNode(
const result = printer.printNode(
ts.EmitHint.Unspecified,
transformationResult.transformed[0],
ts.createSourceFile('', '', ts.ScriptTarget.ESNext),
);
// unescape unicode text
return hasUnicodeStr(result) ? unescapeUnicodeStr(result) : result;
};

const flags = cli.parse(Deno.args, {
Expand All @@ -175,9 +178,10 @@ export const main = async (args: {
},
}) => {
const { basePath, options } = args;
const _basePath = basePath ?? './';
const _basePath = basePath ?? '.';
const tsConfigPath = options?.tsConfigPath ?? './tsconfig.json';
const match = [/\.js$/, /\.mjs$/, /\.ts$/, /\.mts$/, /\.jsx$/, /\.tsx$/];
const skip = [/node_modules/];
const decoder = new TextDecoder('utf-8');
const tsConfigJson = await Deno.readFile(tsConfigPath);
const tsConfigObject = ts.parseJsonConfigFileContent(
Expand All @@ -192,7 +196,7 @@ export const main = async (args: {
result: string;
}> = [];

for await (const entry of walk(_basePath, { match })) {
for await (const entry of walk(_basePath, { match, skip })) {
if (entry.isFile) {
const targetPath = entry.path;
const currentFileAbsPath = path.resolve(targetPath);
Expand Down
6 changes: 4 additions & 2 deletions src/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ Deno.test('transform', async (t) => {
transform({
sourceFile: ts.createSourceFile(
'./src/App.tsx',
`import { ComponentA } from './ComponentA';`,
`import { ComponentA } from './ComponentA';\n` +
`const str = '😎';\n`,
ts.ScriptTarget.ESNext,
),
imports: [
Expand All @@ -59,7 +60,8 @@ Deno.test('transform', async (t) => {
tsConfigObject: tsConfigMockObject,
printer: ts.createPrinter(),
}),
`import { ComponentA } from "./ComponentA.tsx";\n`,
`import { ComponentA } from "./ComponentA.tsx";\n` +
`const str = "😎";\n`,
);
});
});
7 changes: 7 additions & 0 deletions src/str.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const hasUnicodeStr = (str: string): boolean => {
return /\\u.{4}/gi.test(str);
};

export const unescapeUnicodeStr = (str: string): string => {
return unescape(str.replace(/\\u/g, '%u'));
};
12 changes: 12 additions & 0 deletions src/str_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { asserts } from './dev_deps.ts';
import { hasUnicodeStr, unescapeUnicodeStr } from './str.ts';
const { assertEquals } = asserts;

Deno.test('hasUnicodeStr', () => {
// \uD83D\uDE0E is 😎
assertEquals(hasUnicodeStr('\\uD83D\\uDE0E'), true);
});

Deno.test('unescapeUnicodeStr', () => {
assertEquals(unescapeUnicodeStr('\uD83D\uDE0E'), '😎');
});

0 comments on commit 7ede2b7

Please sign in to comment.