Skip to content

Commit

Permalink
Fix #573 - exact import path generation
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Apr 7, 2023
1 parent 0d60af4 commit e642ae4
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 359 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function assertStringify<T>(input: T): string; // safe and faster

// MISC
export function random<T>(g?: Partial<IRandomGenerator>): Primitive<T>;
export function literals<T extends Atomic.Type>(): T[];
export function clone<T>(input: T): Primitive<T>; // deep clone
export function prune<T extends object>(input: T): void; // erase extra props
// +) isClone, assertClone, validateClone
Expand Down Expand Up @@ -257,7 +258,8 @@ export function createAssertStringify<T>(): (input: T) => string;
### Miscellaneous
```typescript
export function random<T>(g?: Partial<IRandomGenerator>): Primitive<T>;
export function random<T>(g?: Partial<IRandomGenerator>): Primitive<T>;
export function literals<T extends Atomic.Type>(): T[];
export function clone<T>(input: T): Primitive<T>; // deep copy
export function prune<T>(input: T): void; // remove superfluous properties
// +) isClone, assertClone, validateClone
Expand Down
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": "3.7.1",
"version": "3.7.2",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion packages/typescript-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function assertStringify<T>(input: T): string; // safe and faster

// MISC
export function random<T>(g?: Partial<IRandomGenerator>): Primitive<T>;
export function literals<T extends Atomic.Type>(): T[];
export function clone<T>(input: T): Primitive<T>; // deep clone
export function prune<T extends object>(input: T): void; // erase extra props
// +) isClone, assertClone, validateClone
Expand Down Expand Up @@ -260,7 +261,8 @@ export function createAssertStringify<T>(): (input: T) => string;
### Miscellaneous
```typescript
export function random<T>(g?: Partial<IRandomGenerator>): Primitive<T>;
export function random<T>(g?: Partial<IRandomGenerator>): Primitive<T>;
export function literals<T extends Atomic.Type>(): T[];
export function clone<T>(input: T): Primitive<T>; // deep copy
export function prune<T>(input: T): void; // remove superfluous properties
// +) isClone, assertClone, validateClone
Expand Down
6 changes: 3 additions & 3 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "3.7.1",
"version": "3.7.2",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand All @@ -19,7 +19,7 @@
"prettier": "prettier --write ./**/*.ts",
"-----------------------------------------------": "",
"issue": "node test/issue",
"issue:generate": "ts-node src/executable/typia generate --input test/issues/generate --output test/issues/generate/output --project test/tsconfig.json",
"issue:generate": "ts-node src/executable/typia generate --input test/issues/generate/input --output test/issues/generate --project test/tsconfig.json",
"test": "node bin/test",
"test:manual": "node test/manual",
"------------------------------------------------": "",
Expand Down Expand Up @@ -74,6 +74,6 @@
"src"
],
"dependencies": {
"typia": "3.7.1"
"typia": "3.7.2"
}
}
12 changes: 9 additions & 3 deletions src/transformers/ImportTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ export namespace ImportTransformer {
const location: string = path.resolve(from, text);
if (location.indexOf(top) === 0) return node;

const replaced: string = (() => {
const simple: string = path
.relative(to, location)
.split(path.sep)
.join("/");
return simple[0] === "." ? simple : `./${simple}`;
})();

return ts.factory.createImportDeclaration(
undefined,
node.importClause,
ts.factory.createStringLiteral(
path.relative(to, location).split(path.sep).join("/"),
),
ts.factory.createStringLiteral(replaced),
node.assertClause,
);
};
Expand Down
21 changes: 17 additions & 4 deletions test/issues/generate/532.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import typia from "../../../src";
import { ObjectPrimitive } from "../../structures/ObjectPrimitive";

interface A {
a: string;
}
interface B {
b: string;
}
type Union = A | B;

export const checkUnion = typia.createIs<Union>();
export const checkPrimitive = typia.createIs<ObjectPrimitive>();
export const checkUnion = (input: any): input is Union => {
const $io0 = (input: any): boolean => "string" === typeof input.a;
const $io1 = (input: any): boolean => "string" === typeof input.b;
const $iu0 = (input: any): any => (() => {
if (undefined !== input.a)
return $io0(input);
if (undefined !== input.b)
return $io1(input);
return false;
})();
return "object" === typeof input && null !== input && $iu0(input);
};
export const checkPrimitive = (input: any): input is ObjectPrimitive => {
const $io0 = (input: any): boolean => "string" === typeof input.id && ("md" === input.extension || "html" === input.extension || "txt" === input.extension) && "string" === typeof input.title && "string" === typeof input.body && (Array.isArray(input.files) && input.files.every((elem: any) => "object" === typeof elem && null !== elem && $io1(elem))) && "boolean" === typeof input.secret && "string" === typeof input.created_at;
const $io1 = (input: any): boolean => "string" === typeof input.id && "string" === typeof input.name && "string" === typeof input.extension && "string" === typeof input.url && "string" === typeof input.created_at;
return "object" === typeof input && null !== input && $io0(input);
};
43 changes: 43 additions & 0 deletions test/issues/generate/573.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import typia from "../../../src";
import { ISomeOutputDto } from "./structures/ISomeOutputDto";
export const isSomeOutputDto = (input: any): ISomeOutputDto => {
const $guard = (typia.createAssert as any).guard;
const $is_uuid = (typia.createAssert as any).is_uuid;
((input: any, _path: string, _exceptionable: boolean = true): input is ISomeOutputDto => {
const $ao0 = (input: any, _path: string, _exceptionable: boolean = true): boolean => ("string" === typeof input.id && (true === $is_uuid(input.id) || $guard(_exceptionable, {
path: _path + ".id",
expected: "string (@format uuid)",
value: input.id
})) || $guard(_exceptionable, {
path: _path + ".id",
expected: "string",
value: input.id
})) && ("string" === typeof input.name && (3 <= input.name.length || $guard(_exceptionable, {
path: _path + ".name",
expected: "string (@minLength 3)",
value: input.name
})) || $guard(_exceptionable, {
path: _path + ".name",
expected: "string",
value: input.name
})) && ("number" === typeof input.age && (0 <= input.age || $guard(_exceptionable, {
path: _path + ".age",
expected: "number (@minimum 0)",
value: input.age
})) && (100 >= input.age || $guard(_exceptionable, {
path: _path + ".age",
expected: "number (@maximum 100)",
value: input.age
})) || $guard(_exceptionable, {
path: _path + ".age",
expected: "number",
value: input.age
}));
return ("object" === typeof input && null !== input || $guard(true, {
path: _path + "",
expected: "Resolve<ISomeOutputDto>",
value: input
})) && $ao0(input, _path + "", true);
})(input, "$input", true);
return input;
};
9 changes: 9 additions & 0 deletions test/issues/generate/575.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import typia from "../../../src";
const values = [
true,
"A",
"B",
1,
2
] as const;
console.log(values);
13 changes: 13 additions & 0 deletions test/issues/generate/input/532.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import typia from "../../../../src";
import { ObjectPrimitive } from "../../../structures/ObjectPrimitive";

interface A {
a: string;
}
interface B {
b: string;
}
type Union = A | B;

export const checkUnion = typia.createIs<Union>();
export const checkPrimitive = typia.createIs<ObjectPrimitive>();
4 changes: 4 additions & 0 deletions test/issues/generate/input/573.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import typia from "../../../../src";
import { ISomeOutputDto } from "../structures/ISomeOutputDto";

export const isSomeOutputDto = typia.createAssert<ISomeOutputDto>();
4 changes: 4 additions & 0 deletions test/issues/generate/input/575.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import typia from "../../../../src";

const values = typia.literals<true | "A" | "B" | 1 | 2>();
console.log(values);
50 changes: 50 additions & 0 deletions test/issues/generate/input/nestia-282.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import typia from "../../../../src";

const ERROR = {
TOO_LONG_KEY_NAME1: {
result: false,
code: 4000,
data: "Error happens something1.",
},
TOO_LONG_KEY_NAME2: {
result: false,
code: 4000,
data: "Error happens something2.",
},
TOO_LONG_KEY_NAME3: {
result: false,
code: 4000,
data: "Error happens something3.",
},
TOO_LONG_KEY_NAME4: {
result: false,
code: 4000,
data: "Error happens something4.",
},
TOO_LONG_KEY_NAME5: {
result: false,
code: 4000,
data: "Error happens something5.",
},
} as const;

type KeyOfError = keyof typeof ERROR;
type ValueOfError = (typeof ERROR)[KeyOfError];
interface ResponseForm<T> {
result: true;
code: 1000;
data: T;
}

type Try<T, E extends ValueOfError> = ResponseForm<T> | E;

const input: Try<
true,
| typeof ERROR.TOO_LONG_KEY_NAME1
| typeof ERROR.TOO_LONG_KEY_NAME2
| typeof ERROR.TOO_LONG_KEY_NAME3
| typeof ERROR.TOO_LONG_KEY_NAME4
| typeof ERROR.TOO_LONG_KEY_NAME5
> = {} as any;

typia.assert(input);
Loading

0 comments on commit e642ae4

Please sign in to comment.