Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a warning for misc.prune #1212

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ export { validateClonePure as validateClone };
* - {@link isPrune}
* - {@link validatePrune}
*
* @warning ... ({@link https://github.com/samchon/typia/issues/1205|GitHub Issue})
* @see Warning: ... ({@link https://github.com/samchon/typia/issues/1205|GitHub Issue})
*
* @template T Type of the input value
* @param input Target instance to prune
*
Expand Down
211 changes: 121 additions & 90 deletions src/transformers/internal/GenericTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,102 +3,133 @@ import ts from "typescript";
import { IProject } from "../IProject";
import { TransformerError } from "../TransformerError";

function findTypeNodeInIdentifier(arg: ts.Identifier) {
let flowNode = arg.flowNode;
while (true) {
if (flowNode) {
const { antecedent, node } = flowNode;
if (node && "type" in node && node.type) {
return node.type;
} else if (antecedent && !Array.isArray(antecedent)) {
flowNode = antecedent;
}
}
}
}

function findTypeNode(expr: ts.Expression) {
if (expr.kind === ts.SyntaxKind.Identifier) {
return findTypeNodeInIdentifier(expr as ts.Identifier);
} else if (expr.kind === ts.SyntaxKind.AsExpression) {
return (expr as ts.AsExpression).type;
}
return undefined;
}

export namespace GenericTransformer {
export const scalar =
(method: string) =>
(
programmer: (
project: IProject,
) => (
modulo: ts.LeftHandSideExpression,
) => (type: ts.Type, name: string) => ts.Expression | ts.ArrowFunction,
) =>
(project: IProject) =>
(modulo: ts.LeftHandSideExpression) =>
(expression: ts.CallExpression) => {
// CHECK PARAMETER
if (expression.arguments.length === 0)
throw new TransformerError({
code: `typia.${method}`,
message: `no input value.`,
});
export const scalar =
(method: string) =>
(
programmer: (
project: IProject,
) => (
modulo: ts.LeftHandSideExpression,
) => (type: ts.Type, name: string) => ts.Expression | ts.ArrowFunction,
) =>
(project: IProject) =>
(modulo: ts.LeftHandSideExpression) =>
(expression: ts.CallExpression) => {
// CHECK PARAMETER
if (expression.arguments.length === 0) {
throw new TransformerError({
code: `typia.${method}`,
message: `no input value.`,
});
}

// GET TYPE INFO
const typeArgument = expression.typeArguments?.[0];
const [type, node, generic]: [ts.Type, ts.Node, boolean] = typeArgument
? [project.checker.getTypeFromTypeNode(typeArgument), typeArgument, true]
: [project.checker.getTypeAtLocation(expression.arguments[0]!), expression.arguments[0]!, false];
if (type.isTypeParameter()) {
throw new TransformerError({
code: `typia.${method}`,
message: `non-specified generic argument.`,
});
}

// GET TYPE INFO
const [type, node, generic]: [ts.Type, ts.Node, boolean] =
expression.typeArguments && expression.typeArguments[0]
? [
project.checker.getTypeFromTypeNode(expression.typeArguments[0]),
expression.typeArguments[0],
true,
]
: [
project.checker.getTypeAtLocation(expression.arguments[0]!),
expression.arguments[0]!,
false,
];
if (type.isTypeParameter())
throw new TransformerError({
code: `typia.${method}`,
message: `non-specified generic argument.`,
});
if (method === "misc.prune" && generic) {
const arg = expression.arguments[0];
if (arg) {
const typeNode = findTypeNode(arg);
if (typeNode) {
const inputType = project.checker.getTypeFromTypeNode(typeNode);
if (inputType) {
const isAssignable = project.checker.isTypeAssignableTo(type, inputType);
if (!isAssignable) {
console.log("!isAssignable");
// const diagnostic = ts.createDiagnosticForNode(arg, {
// category: ts.DiagnosticCategory.Warning,
// code: "todo code" as any,
// key: "todo key",
// message: "todo message",
// });
// project.extras.addDiagnostic(diagnostic);
}
}
}
}
}

// DO TRANSFORM
return ts.factory.createCallExpression(
programmer(project)(modulo)(
type,
generic
? node.getFullText().trim()
: name(project.checker)(type)(node),
),
undefined,
expression.arguments,
);
};
// DO TRANSFORM
return ts.factory.createCallExpression(
programmer(project)(modulo)(
type,
generic ? node.getFullText().trim() : name(project.checker)(type)(node),
),
undefined,
expression.arguments,
);
};

export const factory =
(method: string) =>
(
programmer: (
project: IProject,
) => (
modulo: ts.LeftHandSideExpression,
) => (
type: ts.Type,
name: string,
init?: ts.Expression,
) => ts.Expression | ts.ArrowFunction,
) =>
(project: IProject) =>
(modulo: ts.LeftHandSideExpression) =>
(expression: ts.CallExpression) => {
// CHECK GENERIC ARGUMENT EXISTENCE
if (!expression.typeArguments?.[0])
throw new TransformerError({
code: `typia.${method}`,
message: `generic argument is not specified.`,
});
export const factory =
(method: string) =>
(
programmer: (
project: IProject,
) => (
modulo: ts.LeftHandSideExpression,
) => (type: ts.Type, name: string, init?: ts.Expression) => ts.Expression | ts.ArrowFunction,
) =>
(project: IProject) =>
(modulo: ts.LeftHandSideExpression) =>
(expression: ts.CallExpression) => {
// CHECK GENERIC ARGUMENT EXISTENCE
if (!expression.typeArguments?.[0])
throw new TransformerError({
code: `typia.${method}`,
message: `generic argument is not specified.`,
});

// GET TYPE INFO
const node: ts.TypeNode = expression.typeArguments[0];
const type: ts.Type = project.checker.getTypeFromTypeNode(node);
// GET TYPE INFO
const node: ts.TypeNode = expression.typeArguments[0];
const type: ts.Type = project.checker.getTypeFromTypeNode(node);

if (type.isTypeParameter())
throw new TransformerError({
code: `typia.${method}`,
message: `non-specified generic argument.`,
});
if (type.isTypeParameter()) {
throw new TransformerError({
code: `typia.${method}`,
message: `non-specified generic argument.`,
});
}

// DO TRANSFORM
return programmer(project)(modulo)(
type,
node.getFullText().trim(),
expression.arguments[0],
);
};
// DO TRANSFORM
return programmer(project)(modulo)(type, node.getFullText().trim(), expression.arguments[0]);
};

const name =
(checker: ts.TypeChecker) =>
(type: ts.Type) =>
(node: ts.Node): string =>
checker.typeToString(type, node, ts.TypeFormatFlags.NodeBuilderFlagsMask);
const name =
(checker: ts.TypeChecker) =>
(type: ts.Type) =>
(node: ts.Node): string =>
checker.typeToString(type, node, ts.TypeFormatFlags.NodeBuilderFlagsMask);
}
Loading