-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
217 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import ts from "typescript"; | ||
|
||
import { MetadataCollection } from "../factories/MetadataCollection"; | ||
import { MetadataFactory } from "../factories/MetadataFactory"; | ||
|
||
import { Metadata } from "../metadata/Metadata"; | ||
|
||
import { IProject } from "../transformers/IProject"; | ||
|
||
import { Atomic } from "../typings/Atomic"; | ||
|
||
import { ArrayUtil } from "../utils/ArrayUtil"; | ||
|
||
export namespace LiteralsProgrammer { | ||
export const generate = (project: IProject) => (type: ts.Type) => { | ||
const meta: Metadata = MetadataFactory.generate( | ||
project.checker, | ||
new MetadataCollection(), | ||
type, | ||
{ | ||
resolve: true, | ||
constant: true, | ||
validate: (meta) => { | ||
const length: number = | ||
meta.constants | ||
.map((c) => c.values.length) | ||
.reduce((a, b) => a + b, 0) + | ||
meta.atomics.filter((type) => type === "boolean") | ||
.length; | ||
if (0 === length) throw new Error(ErrorMessages.NO); | ||
else if (meta.size() !== length) | ||
throw new Error(ErrorMessages.ONLY); | ||
}, | ||
}, | ||
); | ||
const values: Set<Atomic.Type> = new Set([ | ||
...ArrayUtil.flat<Atomic.Type>(meta.constants.map((c) => c.values)), | ||
...(meta.atomics.filter((type) => type === "boolean").length | ||
? [true, false] | ||
: []), | ||
]); | ||
return ts.factory.createAsExpression( | ||
ts.factory.createArrayLiteralExpression( | ||
[...values].map((v) => | ||
typeof v === "boolean" | ||
? v | ||
? ts.factory.createTrue() | ||
: ts.factory.createFalse() | ||
: typeof v === "number" | ||
? ts.factory.createNumericLiteral(v) | ||
: typeof v === "bigint" | ||
? ts.factory.createBigIntLiteral(v.toString()) | ||
: ts.factory.createStringLiteral(v), | ||
), | ||
true, | ||
), | ||
ts.factory.createTypeReferenceNode("const"), | ||
); | ||
}; | ||
} | ||
|
||
enum ErrorMessages { | ||
NO = "Error on typia.literals(): no literal type found.", | ||
ONLY = "Error on typia.literals(): only literal type allowed.", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/transformers/features/miscellaneous/LiteralsTransformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import ts from "typescript"; | ||
|
||
import { LiteralsProgrammer } from "../../../programmers/LiteralsProgrammer"; | ||
|
||
import { IProject } from "../../IProject"; | ||
|
||
export namespace LiteralsTransformer { | ||
export function transform( | ||
project: IProject, | ||
_modulo: ts.LeftHandSideExpression, | ||
expression: ts.CallExpression, | ||
): ts.Expression { | ||
// CHECK GENERIC ARGUMENT EXISTENCE | ||
if (!expression.typeArguments?.[0]) throw new Error(NOT_SPECIFIED); | ||
|
||
// GET TYPE INFO | ||
const node: ts.TypeNode = expression.typeArguments[0]; | ||
const type: ts.Type = project.checker.getTypeFromTypeNode(node); | ||
|
||
if (type.isTypeParameter()) throw new Error(NO_GENERIC_ARGUMENT); | ||
|
||
// DO TRANSFORM | ||
return LiteralsProgrammer.generate(project)(type); | ||
} | ||
} | ||
|
||
const NOT_SPECIFIED = | ||
"Error on typia.literals(): generic argument is not specified."; | ||
const NO_GENERIC_ARGUMENT = | ||
"Error on typia.literals(): non-specified generic argument."; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import typia from "../../../src"; | ||
import { primitive_equal_to } from "../../helpers/primitive_equal_to"; | ||
|
||
export function test_literals_constants(): void { | ||
const values = typia.literals<Values>(); | ||
values.sort(); | ||
|
||
if (!primitive_equal_to(values, ["A", "B", 1, 2, 3].sort())) | ||
throw Error( | ||
"Bug on typia.literals(): failed to understand constant type.", | ||
); | ||
} | ||
|
||
type Values = "A" | "B" | 1 | 2 | 3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import typia from "../../../src"; | ||
import { primitive_equal_to } from "../../helpers/primitive_equal_to"; | ||
|
||
export function test_literals_enumeration(): void { | ||
const values = typia.literals<TestEnum>(); | ||
values.sort(); | ||
|
||
if (!primitive_equal_to(values, ["A", "B", 1, 2, 3].sort())) | ||
throw Error( | ||
"Bug on typia.literals(): failed to understand enumeration type.", | ||
); | ||
} | ||
|
||
enum TestEnum { | ||
A = "A", | ||
B = "B", | ||
one = 1, | ||
two = 2, | ||
three = 3, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters