Skip to content

Commit

Permalink
Complement #509 - make generator works
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Feb 25, 2023
1 parent ead9da5 commit 2844567
Show file tree
Hide file tree
Showing 8,022 changed files with 456,664 additions and 230,916 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
25 changes: 25 additions & 0 deletions build/internal/__TypeRemover.ts
@@ -0,0 +1,25 @@
import fs from "fs";
import path from "path";

export namespace __TypeRemover {
export async function remove(location: string): Promise<void> {
const directory: string[] = await fs.promises.readdir(location);
for (const file of directory) {
const next: string = path.join(location, file);
const stat: fs.Stats = await fs.promises.stat(next);
if (stat.isDirectory()) {
await remove(next);
continue;
} else if (file.endsWith(".ts") === false) continue;

const content: string = await fs.promises.readFile(next, "utf8");
if (
content.indexOf("__type") !== -1 ||
content.indexOf("__object") !== -1 ||
file.indexOf("Alias") !== -1 ||
file.indexOf("UltimateUnion") !== -1
)
await fs.promises.rm(next);
}
}
}
12 changes: 9 additions & 3 deletions build/test.ts
Expand Up @@ -4,6 +4,7 @@ import fs from "fs";
import { TestApplicationGenerator } from "./internal/TestApplicationGenerator";
import { TestFeature } from "./internal/TestFeature";
import { TestStructure } from "./internal/TestStructure";
import { __TypeRemover } from "./internal/__TypeRemover";

const emit = process.emit;
(process as any).emit = function (name: string, ...args: any[]) {
Expand Down Expand Up @@ -84,16 +85,15 @@ function script(
feat.spoilable && struct.SPOILERS
? ` ${struct.name}.SPOILERS,`
: null,
feat.random
? ` typia.createAssert<typia.Primitive<${struct.name}>>(),`
: null,
feat.random ? `typia.createAssert<${struct.name}>(),` : null,
`);\n`,
];
return elements.filter((e) => e !== null).join("\n");
}

async function main(): Promise<void> {
process.chdir(__dirname + "/..");
cp.execSync("npx rimraf test/generated");

const structures: TestStructure<any>[] = await load();

Expand All @@ -115,6 +115,12 @@ async function main(): Promise<void> {
await TestApplicationGenerator.schema();

// GENERATE TRANSFORMED FEATURES
cp.execSync(
"npx ts-node src/executable/typia generate --input test/features --output test/generated/output --project test/tsconfig.json",
);
cp.execSync("npx rimraf test/generated/output/application");
await __TypeRemover.remove(__dirname + "/../test/generated");
cp.execSync("npm run prettier", { stdio: "inherit" });
}
main().catch((exp) => {
console.log(exp);
Expand Down
3 changes: 1 addition & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "3.5.7",
"version": "3.6.0-dev.20230225",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand All @@ -23,7 +23,6 @@
"package:deprecate": "npm deprecate typescript-json \"Renamed to typia\"",
"prettier": "prettier --write ./**/*.ts",
"test": "node bin/test",
"test:features:generator": "rimraf test/generated && ts-node src/executable/typia generate --input test/features --output test/generated/output",
"test:generate": "ts-node -P build/tsconfig.json -C ttypescript build/test.ts",
"test:manual": "node test/manual",
"test:application:replace": "node test/features/application/replace && npm run prettier"
Expand Down
24 changes: 23 additions & 1 deletion src/Primitive.ts
Expand Up @@ -36,7 +36,9 @@ type _Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;

type _Primitive<Instance> = _ValueOf<Instance> extends object
? Instance extends object
? Instance extends IJsonable<infer Raw>
? Instance extends _Native
? {}
: Instance extends IJsonable<infer Raw>
? _ValueOf<Raw> extends object
? Raw extends object
? _PrimitiveObject<Raw> // object would be primitified
Expand All @@ -62,6 +64,26 @@ type _ValueOf<Instance> = _IsValueOf<Instance, Boolean> extends true
? string
: Instance;

type _Native =
| Set<any>
| Map<any, any>
| WeakSet<any>
| WeakMap<any, any>
| Uint8Array
| Uint8ClampedArray
| Uint16Array
| Uint32Array
| BigUint64Array
| Int8Array
| Int16Array
| Int32Array
| BigInt64Array
| Float32Array
| Float64Array
| ArrayBuffer
| SharedArrayBuffer
| DataView;

type _IsValueOf<
Instance,
Object extends IValueOf<any>,
Expand Down
2 changes: 1 addition & 1 deletion src/factories/TypeFactory.ts
Expand Up @@ -55,7 +55,7 @@ export namespace TypeFactory {
symbol?: ts.Symbol,
): string {
// PRIMITIVE
symbol ||= type.aliasSymbol || type.getSymbol();
symbol ??= type.aliasSymbol ?? type.getSymbol();
if (symbol === undefined) return checker.typeToString(type);

// UNION OR INTERSECT
Expand Down
11 changes: 9 additions & 2 deletions src/factories/TypiaFileFactory.ts
Expand Up @@ -43,7 +43,7 @@ export namespace TypiaFileFactory {
await gather(container)(props.input)(props.output);
return container;
})(),
config,
config.compilerOptions,
);

// DO TRANSFORM
Expand All @@ -56,8 +56,15 @@ export namespace TypiaFileFactory {
path.resolve(file.fileName).indexOf(props.input) !== -1,
),
[
transform(program),
ImportTransformer.transform(props.input)(props.output),
transform(
program,
(config.compilerOptions.plugins ?? []).find(
(p: any) =>
p.transform === "typia/lib/transform" ||
p.transform === "../src/transform.ts",
) ?? {},
),
],
program.getCompilerOptions(),
);
Expand Down
29 changes: 6 additions & 23 deletions src/programmers/AssertCloneProgrammer.ts
Expand Up @@ -22,13 +22,12 @@ export namespace AssertCloneProgrammer {
TypeFactory.keyword("any"),
),
],
ts.factory.createTypeReferenceNode("typia.Primitive", [
project.checker.typeToTypeNode(
ts.factory.createTypeReferenceNode(
`typia.Primitive<${TypeFactory.getFullName(
project.checker,
type,
undefined,
undefined,
) ?? TypeFactory.keyword("any"),
]),
)}>`,
),
undefined,
ts.factory.createBlock([
StatementFactory.constant(
Expand Down Expand Up @@ -64,24 +63,8 @@ export namespace AssertCloneProgrammer {
[ts.factory.createIdentifier("input")],
),
),
ts.factory.createExpressionStatement(
ts.factory.createIdentifier(
`/* ${TypeFactory.getFullName(
project.checker,
type,
type.getSymbol(),
)} */`,
),
),
ts.factory.createReturnStatement(
ts.factory.createAsExpression(
ts.factory.createIdentifier("output"),
project.checker.typeToTypeNode(
type,
undefined,
undefined,
) ?? TypeFactory.keyword("any"),
),
ts.factory.createIdentifier("output"),
),
]),
);
Expand Down
11 changes: 5 additions & 6 deletions src/programmers/AssertParseProgrammer.ts
Expand Up @@ -21,13 +21,12 @@ export namespace AssertParseProgrammer {
TypeFactory.keyword("string"),
),
],
ts.factory.createTypeReferenceNode("typia.Primitive", [
project.checker.typeToTypeNode(
ts.factory.createTypeReferenceNode(
`typia.Primitive<${TypeFactory.getFullName(
project.checker,
type,
undefined,
undefined,
) ?? TypeFactory.keyword("any"),
]),
)}>`,
),
undefined,
ts.factory.createBlock([
StatementFactory.constant(
Expand Down
13 changes: 4 additions & 9 deletions src/programmers/AssertProgrammer.ts
Expand Up @@ -46,7 +46,9 @@ export namespace AssertProgrammer {
TypeFactory.keyword("any"),
),
],
undefined,
ts.factory.createTypeReferenceNode(
TypeFactory.getFullName(project.checker, type),
),
undefined,
ts.factory.createBlock(
[
Expand All @@ -63,14 +65,7 @@ export namespace AssertProgrammer {
),
),
ts.factory.createReturnStatement(
ts.factory.createAsExpression(
ts.factory.createIdentifier("input"),
project.checker.typeToTypeNode(
type,
undefined,
undefined,
) ?? TypeFactory.keyword("any"),
),
ts.factory.createIdentifier(`input`),
),
],
true,
Expand Down
5 changes: 3 additions & 2 deletions src/programmers/AssertPruneProgrammer.ts
Expand Up @@ -22,8 +22,9 @@ export namespace AssertPruneProgrammer {
TypeFactory.keyword("any"),
),
],
project.checker.typeToTypeNode(type, undefined, undefined) ??
TypeFactory.keyword("any"),
ts.factory.createTypeReferenceNode(
TypeFactory.getFullName(project.checker, type),
),
undefined,
ts.factory.createBlock([
StatementFactory.constant(
Expand Down
6 changes: 1 addition & 5 deletions src/programmers/AssertStringifyProgrammer.ts
Expand Up @@ -19,11 +19,7 @@ export namespace AssertStringifyProgrammer {
[
IdentifierFactory.parameter(
"input",
project.checker.typeToTypeNode(
type,
undefined,
undefined,
) ?? TypeFactory.keyword("any"),
TypeFactory.keyword("any"),
),
],
TypeFactory.keyword("string"),
Expand Down
7 changes: 3 additions & 4 deletions src/programmers/CheckerProgrammer.ts
Expand Up @@ -132,10 +132,8 @@ export namespace CheckerProgrammer {
ts.factory.createTypePredicateNode(
undefined,
"input",
project.checker.typeToTypeNode(
type,
undefined,
undefined,
ts.factory.createTypeReferenceNode(
TypeFactory.getFullName(project.checker, type),
),
),
},
Expand Down Expand Up @@ -194,6 +192,7 @@ export namespace CheckerProgrammer {
is: config.joiner.is,
required: config.joiner.required,
full: config.joiner.full,
type: TypeFactory.keyword("boolean"),
},
};
if (config.numeric === true)
Expand Down
32 changes: 22 additions & 10 deletions src/programmers/CloneProgrammer.ts
Expand Up @@ -148,7 +148,16 @@ export namespace CloneProgrammer {
unions.push({
type: "native",
is: () => ExpressionFactory.isInstanceOf(input, native),
value: () => ts.factory.createIdentifier("{}"),
value: () =>
native === "Boolean" ||
native === "Number" ||
native === "String"
? ts.factory.createCallExpression(
IdentifierFactory.join(input, "valueOf"),
undefined,
undefined,
)
: ts.factory.createIdentifier("{}"),
});

// OBJECTS
Expand Down Expand Up @@ -177,7 +186,10 @@ export namespace CloneProgrammer {
undefined,
last,
);
return last;
return ts.factory.createAsExpression(
last,
TypeFactory.keyword("any"),
);
};

const decode_to_json =
Expand Down Expand Up @@ -301,16 +313,16 @@ export namespace CloneProgrammer {
): FeatureProgrammer.IConfig => ({
types: {
input: (type) =>
project.checker.typeToTypeNode(type, undefined, undefined) ??
TypeFactory.keyword("any"),
ts.factory.createTypeReferenceNode(
TypeFactory.getFullName(project.checker, type),
),
output: (type) =>
ts.factory.createTypeReferenceNode("typia.Primitive", [
project.checker.typeToTypeNode(
ts.factory.createTypeReferenceNode(
`typia.Primitive<${TypeFactory.getFullName(
project.checker,
type,
undefined,
undefined,
) ?? TypeFactory.keyword("any"),
]),
)}>`,
),
},
functors: FUNCTORS,
unioners: UNIONERS,
Expand Down
10 changes: 8 additions & 2 deletions src/programmers/FeatureProgrammer.ts
Expand Up @@ -165,6 +165,11 @@ export namespace FeatureProgrammer {
expected: string,
explore: IExplore,
) => ts.Expression;

/**
* Return type.
*/
type?: ts.TypeNode;
}
export interface IGenerator {
/**
Expand Down Expand Up @@ -311,7 +316,7 @@ export namespace FeatureProgrammer {
PARAMETERS(config)(TypeFactory.keyword("any"))(
ValueFactory.INPUT(),
),
undefined,
config.objector.type ?? TypeFactory.keyword("any"),
undefined,
config.objector.joiner(
ts.factory.createIdentifier("input"),
Expand All @@ -333,7 +338,7 @@ export namespace FeatureProgrammer {
PARAMETERS(config)(TypeFactory.keyword("any"))(
ValueFactory.INPUT(),
),
undefined,
TypeFactory.keyword("any"),
undefined,
explorer(
input,
Expand Down Expand Up @@ -490,6 +495,7 @@ const PARAMETERS =
IdentifierFactory.parameter(
"_exceptionable",
TypeFactory.keyword("boolean"),
ts.factory.createTrue(),
),
);
return (input: ts.Identifier) => [
Expand Down

0 comments on commit 2844567

Please sign in to comment.