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 newline #10

Merged
merged 3 commits into from
Apr 9, 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`
Expand Down
5 changes: 5 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@
"include": ["src", "examples/repo"],
"exclude": ["node_modules"]
}
},
"test": {
"files": {
"include": ["src"]
}
}
}
17 changes: 10 additions & 7 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions src/str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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, '');
};
7 changes: 3 additions & 4 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -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<ResolvedModuleImport>,
Expand Down Expand Up @@ -60,7 +60,7 @@ export const transform = (args: {
imports: Array<ResolvedModuleImport>;
tsConfigObject: ts.ParsedCommandLine;
printer: ts.Printer;
}) => {
}): string => {
const { sourceFile, imports, tsConfigObject, printer } = args;
const transformationResult = ts.transform(sourceFile, [
transformModuleSpecifier(imports),
Expand All @@ -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;
};