Skip to content

Commit

Permalink
ImportProgrammer.IOptions for multiple transformers case.
Browse files Browse the repository at this point in the history
When `@nestia/core` calls `AssertProgrammer` like functions, the transformed code occurs duplicated importment statement error. To solve this problem, I've decided to add `ImportTransformer.IOptions.internalPrefix` to avoid such situation.

For reference, the `typia`'s transformer will assign the `typia_transform_` prefix to there, and `@nestia/core` will do `nestia_core_transform_`.
  • Loading branch information
samchon committed Oct 21, 2024
1 parent 24c4ad9 commit 3611a72
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 324 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@
"suppress-warnings": "^1.0.2",
"tstl": "^3.0.0",
"uuid": "^9.0.1",
"typia": "../typia-7.0.0-dev.20241020.tgz"
"typia": "../typia-7.0.0-dev.20241021-2.tgz"
}
}
2 changes: 1 addition & 1 deletion errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"typescript": "^5.3.2"
},
"dependencies": {
"typia": "../typia-7.0.0-dev.20241020.tgz"
"typia": "../typia-7.0.0-dev.20241021-2.tgz"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "7.0.0-dev.20241020",
"version": "7.0.0-dev.20241021-2",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "7.0.0-dev.20241020",
"version": "7.0.0-dev.20241021-2",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "7.0.0-dev.20241020"
"typia": "7.0.0-dev.20241021-2"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.7.0"
Expand Down
24 changes: 16 additions & 8 deletions src/programmers/ImportProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { MapUtil } from "../utils/MapUtil";

export class ImportProgrammer {
private readonly assets_: Map<string, IAsset> = new Map();
private readonly options_: Readonly<ImportProgrammer.IOptions>;

public constructor(options?: Partial<ImportProgrammer.IOptions>) {
this.options_ = {
internalPrefix: options?.internalPrefix ?? "",
};
}

/* -----------------------------------------------------------
ENROLLMENTS
Expand Down Expand Up @@ -56,15 +63,10 @@ export class ImportProgrammer {
*/
public internal(name: string): ts.PropertyAccessExpression {
if (name.startsWith("$") === false) name = `$${name}`;
this.namespace({
file: `typia/lib/internal/${name}.js`,
name: alias(name),
type: false,
});
return ts.factory.createPropertyAccessExpression(
this.namespace({
file: `typia/lib/internal/${name}.js`,
name: alias(name),
name: this.alias(name),
type: false,
}),
name,
Expand Down Expand Up @@ -95,6 +97,10 @@ export class ImportProgrammer {
}));
}

private alias(name: string): string {
return `__${this.options_.internalPrefix}${name}`;
}

/* -----------------------------------------------------------
PROGRAM STATEMENTS
----------------------------------------------------------- */
Expand Down Expand Up @@ -156,6 +162,10 @@ export class ImportProgrammer {
}

export namespace ImportProgrammer {
export interface IOptions {
internalPrefix: string;
}

export interface IDefault {
file: string;
name: string;
Expand All @@ -180,5 +190,3 @@ interface IAsset {
namespace: ImportProgrammer.INamespace | null;
instances: Map<string, ImportProgrammer.IInstance>;
}

const alias = (str: string) => `__${str}`;
4 changes: 3 additions & 1 deletion src/transformers/FileTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export namespace FileTransformer {
(file: ts.SourceFile): ts.SourceFile => {
if (file.isDeclarationFile) return file;

const importer: ImportProgrammer = new ImportProgrammer();
const importer: ImportProgrammer = new ImportProgrammer({
internalPrefix: "typia_transform_",
});
const context: ITypiaContext = {
...environments,
transformer,
Expand Down
2 changes: 1 addition & 1 deletion test-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
"typescript": "^5.4.5"
},
"dependencies": {
"typia": "../typia-7.0.0-dev.20241020.tgz"
"typia": "../typia-7.0.0-dev.20241021-2.tgz"
}
}
104 changes: 54 additions & 50 deletions test/generate/output/generate_json.ts

Large diffs are not rendered by default.

Loading

0 comments on commit 3611a72

Please sign in to comment.