-
-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathFunctionalMatchTransformer.ts
More file actions
40 lines (34 loc) · 1.23 KB
/
FunctionalMatchTransformer.ts
File metadata and controls
40 lines (34 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import ts from "typescript";
import { MatchProgrammer } from "../../../programmers/MatchProgrammer";
import { ITransformProps } from "../../ITransformProps";
import { TransformerError } from "../../TransformerError";
export namespace FunctionalMatchTransformer {
export const transform = (props: ITransformProps): ts.Expression => {
// CHECK PARAMETER COUNT
if (props.expression.arguments.length < 2)
throw new TransformerError({
code: `typia.functional.match`,
message: `at least 2 arguments required: input and cases.`,
});
const input = props.expression.arguments[0]!;
const cases = props.expression.arguments[1]!;
const otherwise = props.expression.arguments[2];
// GET TYPE INFO
const inputType: ts.Type =
props.expression.typeArguments && props.expression.typeArguments[0]
? props.context.checker.getTypeFromTypeNode(
props.expression.typeArguments[0],
)
: props.context.checker.getTypeAtLocation(input);
// Use MatchProgrammer to generate optimized conditional statements
return MatchProgrammer.write({
...props,
input,
cases,
otherwise,
inputType,
type: inputType,
name: undefined,
});
};
}