From e89265d084eb4f30be684a693bbd98276a95c743 Mon Sep 17 00:00:00 2001 From: Hajime-san Date: Sun, 9 Apr 2023 17:47:58 +0900 Subject: [PATCH 1/3] fix: add test dir --- deno.jsonc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/deno.jsonc b/deno.jsonc index e616c2e..0ecc50c 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -17,5 +17,10 @@ "include": ["src", "examples/repo"], "exclude": ["node_modules"] } + }, + "test": { + "files": { + "include": ["src"] + } } } From 3ee79e55c541ec132c4b9826ae84d709f5e79942 Mon Sep 17 00:00:00 2001 From: Hajime-san Date: Sun, 9 Apr 2023 17:48:33 +0900 Subject: [PATCH 2/3] chore: restore newline to main block --- src/mod.ts | 17 ++++++++++------- src/transform.ts | 7 +++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/mod.ts b/src/mod.ts index e047e76..299d2d3 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -3,7 +3,7 @@ import { hasShouldResolveImportedFiles, resolvedModules, } from './resolve_util.ts'; -import { preserveNewLine } from './str.ts'; +import { preserveNewLine, restoreNewLine } from './str.ts'; import { transform } from './transform.ts'; const flags = cli.parse(Deno.args, { @@ -75,12 +75,15 @@ export const main = async (args: { ts.ScriptTarget.ESNext, ); - const result = transform({ - sourceFile, - imports, - tsConfigObject, - printer, - }); + const result = restoreNewLine( + transform({ + sourceFile, + imports, + tsConfigObject, + printer, + }), + tsConfigObject.options.newLine, + ); transformedList.push({ path: targetFileAbsPath, result, diff --git a/src/transform.ts b/src/transform.ts index bd8f21a..c01699e 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -1,6 +1,6 @@ import { ts } from './deps.ts'; import { getModuleSpecifier, ResolvedModuleImport } from './resolve_util.ts'; -import { hasUnicodeStr, restoreNewLine, unescapeUnicodeStr } from './str.ts'; +import { hasUnicodeStr, unescapeUnicodeStr } from './str.ts'; const transformModuleSpecifier = ( imports: Array, @@ -60,7 +60,7 @@ export const transform = (args: { imports: Array; tsConfigObject: ts.ParsedCommandLine; printer: ts.Printer; -}) => { +}): string => { const { sourceFile, imports, tsConfigObject, printer } = args; const transformationResult = ts.transform(sourceFile, [ transformModuleSpecifier(imports), @@ -72,6 +72,5 @@ export const transform = (args: { ts.createSourceFile('', '', ts.ScriptTarget.ESNext), ); // unescape unicode text - const result = hasUnicodeStr(printed) ? unescapeUnicodeStr(printed) : printed; - return restoreNewLine(result, tsConfigObject.options.newLine); + return hasUnicodeStr(printed) ? unescapeUnicodeStr(printed) : printed; }; From 06aea29a2ae7aa4f673812a81c41f47f0bb10ffa Mon Sep 17 00:00:00 2001 From: Hajime-san Date: Sun, 9 Apr 2023 18:12:37 +0900 Subject: [PATCH 3/3] fix: newline string to `//_PRESERVE_NEWLINE_//` --- README.md | 6 +++--- src/str.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7ffc8eb..e9ed128 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ 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. +- 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. ## tools @@ -72,9 +72,9 @@ Please install [Deno](https://deno.land/manual@v1.30.3/getting_started/installat ## command ### remote - dry run - - `deno run --unstable --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.14/bin.ts -b=./src -c=./tsconfig.json -d` + - `deno run --unstable --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.15/bin.ts -b=./src -c=./tsconfig.json -d` - transform - - `deno run --unstable --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.14/bin.ts -b=./src -c=./tsconfig.json -r` + - `deno run --unstable --allow-env --allow-read --allow-write https://deno.land/x/module_specifier_resolver@v1.0.15/bin.ts -b=./src -c=./tsconfig.json -r` ### local - `deno task run-dry` - `deno task run` diff --git a/src/str.ts b/src/str.ts index 070a603..d84d41f 100644 --- a/src/str.ts +++ b/src/str.ts @@ -13,7 +13,7 @@ export const unescapeUnicodeStr = (str: string): string => { return unescape(str.replace(/\\u/g, '%u')); }; -export const NEW_LINE = '\n/* _PRESERVE_NEWLINE_ */'; +export const NEW_LINE = '\n//_PRESERVE_NEWLINE_//\n'; /** * Keep newline to be filled with `NEW_LINE` text before transform AST. @@ -36,5 +36,9 @@ export const restoreNewLine = (str: string, newLineConfig?: ts.NewLineKind) => { : Deno.build.os === 'windows' ? fs.EOL.CRLF : fs.EOL.LF; - return str.replace(/\/\* _PRESERVE_NEWLINE_ \*\//g, newLineStr); + return str + // with newline + .replace(/\/\/_PRESERVE_NEWLINE_\/\/\n/g, newLineStr) + // without newline + .replace(/\/\/_PRESERVE_NEWLINE_\/\//g, ''); };