Skip to content

Commit

Permalink
Consdering "use server" case, and remove $ starting variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Dec 3, 2024
1 parent c973ea8 commit 2f7c9af
Show file tree
Hide file tree
Showing 16 changed files with 2,099 additions and 248 deletions.
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": "7.0.0",
"version": "7.0.1",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
16 changes: 7 additions & 9 deletions src/internal/_miscCloneAny.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Resolved } from "../Resolved";

export const _miscCloneAny = <T>(value: T): Resolved<T> =>
$cloneMain(value) as Resolved<T>;
cloneMain(value) as Resolved<T>;

const $cloneMain = (value: any): any => {
const cloneMain = (value: any): any => {
if (value === undefined) return undefined;
else if (typeof value === "object")
if (value === null) return null;
else if (Array.isArray(value)) return value.map($cloneMain);
else if (Array.isArray(value)) return value.map(cloneMain);
else if (value instanceof Date) return new Date(value);
else if (value instanceof Uint8Array) return new Uint8Array(value);
else if (value instanceof Uint8ClampedArray)
Expand All @@ -29,18 +29,16 @@ const $cloneMain = (value: any): any => {
return new File([value], value.name, { type: value.type });
else if (typeof Blob !== "undefined" && value instanceof Blob)
return new Blob([value], { type: value.type });
else if (value instanceof Set) return new Set([...value].map($cloneMain));
else if (value instanceof Set) return new Set([...value].map(cloneMain));
else if (value instanceof Map)
return new Map(
[...value].map(([k, v]) => [$cloneMain(k), $cloneMain(v)]),
);
return new Map([...value].map(([k, v]) => [cloneMain(k), cloneMain(v)]));
else if (value instanceof WeakSet || value instanceof WeakMap)
throw new Error("WeakSet and WeakMap are not supported");
else if (value.valueOf() !== value) return $cloneMain(value.valueOf());
else if (value.valueOf() !== value) return cloneMain(value.valueOf());
else
return Object.fromEntries(
Object.entries(value)
.map(([k, v]) => [k, $cloneMain(v)])
.map(([k, v]) => [k, cloneMain(v)])
.filter(([, v]) => v !== undefined),
);
else if (typeof value === "function") return undefined;
Expand Down
6 changes: 3 additions & 3 deletions src/programmers/ValidateProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export namespace ValidateProgrammer {
),
ts.factory.createExpressionStatement(
ts.factory.createBinaryExpression(
ts.factory.createIdentifier("$report"),
ts.factory.createIdentifier("_report"),
ts.factory.createToken(ts.SyntaxKind.EqualsToken),
ts.factory.createCallExpression(
ts.factory.createAsExpression(
Expand Down Expand Up @@ -223,7 +223,7 @@ export namespace ValidateProgrammer {
value: is.arrow,
}),
StatementFactory.mut({ name: "errors" }),
StatementFactory.mut({ name: "$report" }),
StatementFactory.mut({ name: "_report" }),
],
arrow,
};
Expand Down Expand Up @@ -415,7 +415,7 @@ const create_report_call = (props: {
input: ts.Expression;
}): ts.Expression =>
ts.factory.createCallExpression(
ts.factory.createIdentifier("$report"),
ts.factory.createIdentifier("_report"),
undefined,
[
props.exceptionable ?? ts.factory.createIdentifier("_exceptionable"),
Expand Down
22 changes: 21 additions & 1 deletion src/transformers/FileTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ export namespace FileTransformer {
}),
transformer,
);
const index: number = find_import_injection_index(file);
return ts.factory.updateSourceFile(
file,
[...importer.toStatements(), ...file.statements],
[
...file.statements.slice(0, index),
...importer.toStatements(),
...file.statements.slice(index),
],
false,
file.referencedFiles,
file.typeReferenceDirectives,
Expand Down Expand Up @@ -80,6 +85,21 @@ export namespace FileTransformer {
return null;
}
};

const find_import_injection_index = (file: ts.SourceFile): number => {
let i: number = 0;
for (; i < file.statements.length; ++i) {
const stmt: ts.Statement = file.statements[i]!;
if (
ts.isExpressionStatement(stmt) &&
ts.isStringLiteralLike(stmt.expression) &&
stmt.expression.text.startsWith("use ")
)
continue;
break;
}
return i;
};
}

const isTransformerError = (error: any): error is TransformerError =>
Expand Down
11 changes: 11 additions & 0 deletions test/build/validate-generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import fs from "fs";

const directory: string[] = fs.readdirSync(`${__dirname}/../generate/output`);
for (const file of directory) {
const content: string = fs.readFileSync(
`${__dirname}/../generate/output/${file}`,
"utf8",
);
if (content.includes("const $"))
throw new Error(`$ is not allowed in Svelte5`);
}
25 changes: 25 additions & 0 deletions test/generate/input/generate_index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import typia, { tags } from "typia";

interface ICitizen {
id: string & tags.Format<"uuid">;
name: string & tags.Pattern<"^[A-Z][a-z]+$">;
email: string & tags.Format<"email">;
age: number & tags.Type<"uint32"> & tags.ExclusiveMaximum<100>;
motto: string;
birthdate: Date;
died_at: null | Date;
parent: ICitizen | null;
children: ICitizen[];
}

export const is = typia.createIs<ICitizen>();
export const assert = typia.createAssert<ICitizen>();
export const assertGuard = typia.createAssertGuard<ICitizen>();
export const validate = typia.createValidate<ICitizen>();

export const equals = typia.createEquals<ICitizen>();
export const assertEquals = typia.createAssertEquals<ICitizen>();
export const assertGuardEquals = typia.createAssertGuardEquals<ICitizen>();
export const validateEquals = typia.createValidateEquals<ICitizen>();

export const random = typia.createRandom<ICitizen>();
7 changes: 7 additions & 0 deletions test/generate/input/generate_use.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

'use server';

import typia, { tags } from "typia";

typia.createIs<string & tags.Format<"uuid">>();
20 changes: 10 additions & 10 deletions test/generate/output/generate_http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,18 @@ export const validateQuery = (() => {
[
("string" === typeof input.id &&
(__typia_transform__isFormatUuid._isFormatUuid(input.id) ||
$report(_exceptionable, {
_report(_exceptionable, {
path: _path + ".id",
expected: 'string & Format<"uuid">',
value: input.id,
}))) ||
$report(_exceptionable, {
_report(_exceptionable, {
path: _path + ".id",
expected: '(string & Format<"uuid">)',
value: input.id,
}),
((Array.isArray(input.beta) ||
$report(_exceptionable, {
_report(_exceptionable, {
path: _path + ".beta",
expected: "Array<number>",
value: input.beta,
Expand All @@ -259,20 +259,20 @@ export const validateQuery = (() => {
.map(
(elem: any, _index2: number) =>
("number" === typeof elem && Number.isFinite(elem)) ||
$report(_exceptionable, {
_report(_exceptionable, {
path: _path + ".beta[" + _index2 + "]",
expected: "number",
value: elem,
}),
)
.every((flag: boolean) => flag)) ||
$report(_exceptionable, {
_report(_exceptionable, {
path: _path + ".beta",
expected: "Array<number>",
value: input.beta,
}),
"bigint" === typeof input.gamma ||
$report(_exceptionable, {
_report(_exceptionable, {
path: _path + ".gamma",
expected: "bigint",
value: input.gamma,
Expand All @@ -281,22 +281,22 @@ export const validateQuery = (() => {
const __is = (input: any): input is ISomething =>
"object" === typeof input && null !== input && _io0(input);
let errors: any;
let $report: any;
let _report: any;
const __validate = (input: any): import("typia").IValidation<ISomething> => {
if (false === __is(input)) {
errors = [];
$report = (__typia_transform__validateReport._validateReport as any)(
_report = (__typia_transform__validateReport._validateReport as any)(
errors,
);
((input: any, _path: string, _exceptionable: boolean = true) =>
((("object" === typeof input && null !== input) ||
$report(true, {
_report(true, {
path: _path + "",
expected: "ISomething",
value: input,
})) &&
_vo0(input, _path + "", true)) ||
$report(true, {
_report(true, {
path: _path + "",
expected: "ISomething",
value: input,
Expand Down
Loading

0 comments on commit 2f7c9af

Please sign in to comment.