Skip to content

Commit

Permalink
Complement #633 - support both TS 4.x and TS 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed May 19, 2023
1 parent f6a85d2 commit bfd5488
Show file tree
Hide file tree
Showing 24 changed files with 140 additions and 115 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "4.0.0-dev.20230519",
"version": "4.0.0-dev.20230519-3",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand All @@ -11,7 +11,7 @@
"benchmark": "npm run build:benchmark && node measure/benchmark",
"benchmark:generate": "ts-node benchmark/generate && npm run build:benchmark",
"test:generate": "npx ts-node src/executable/typia generate --input test/features --output test/generated/output --project test/tsconfig.json",
"test:template": "npx tsc && ts-node -P build/tsconfig.json build/test.ts --allowPlugins",
"test:template": "npx tsc && ts-node -P build/tsconfig.json build/test.ts",
"----------------------------------------------": "",
"build": "rimraf lib && tsc --removeComments --declaration false && tsc --emitDeclarationOnly",
"build:test": "npx tsc && rimraf bin && tsc -p test/tsconfig.json --allowPlugins",
Expand Down
6 changes: 3 additions & 3 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "typescript-json",
"version": "4.0.0-dev.20230519",
"version": "4.0.0-dev.20230519-3",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"scripts": {
"benchmark": "npm run build:benchmark && node measure/benchmark",
"benchmark:generate": "ts-node benchmark/generate && npm run build:benchmark",
"test:generate": "npx ts-node src/executable/typia generate --input test/features --output test/generated/output --project test/tsconfig.json",
"test:template": "npx tsc && ts-node -P build/tsconfig.json build/test.ts --allowPlugins",
"test:template": "npx tsc && ts-node -P build/tsconfig.json build/test.ts",
"----------------------------------------------": "",
"build": "rimraf lib && tsc --removeComments --declaration false && tsc --emitDeclarationOnly",
"build:test": "npx tsc && rimraf bin && tsc -p test/tsconfig.json --allowPlugins",
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "4.0.0-dev.20230519"
"typia": "4.0.0-dev.20230519-3"
},
"peerDependencies": {
"typescript": ">= 4.5.2"
Expand Down
23 changes: 21 additions & 2 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type ts from "typescript/lib/tsclibrary";

import { FileTransformer } from "./transformers/FileTransformer";
import { ITransformOptions } from "./transformers/ITransformOptions";

const transform: ts.CustomTransformersModuleFactory = ({ typescript }) => ({
const regular: ts.CustomTransformersModuleFactory = ({ typescript }) => ({
create: (info) => ({
before: [
(context) => (file) =>
Expand All @@ -17,4 +18,22 @@ const transform: ts.CustomTransformersModuleFactory = ({ typescript }) => ({
],
}),
});
export = transform;

const patch = (
program: ts.Program,
options?: ITransformOptions,
): ts.TransformerFactory<ts.SourceFile> => {
const tsc = require("ts");
return FileTransformer.transform({
tsc,
program,
compilerOptions: program.getCompilerOptions(),
checker: program.getTypeChecker(),
printer: tsc.createPrinter(),
options: options || {},
});
};

const transform = (x: any, y?: any) =>
x.typescript ? regular(x) : patch(x, y);
export = transform as typeof regular | typeof patch;
14 changes: 10 additions & 4 deletions src/utils/TsSymbolUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export namespace TsSymbolUtil {
const text: string = TsNodeUtil.getSourceFile(tsc)(
node,
).text.substring(range.pos, range.end);
return filter(text).join("\n");
return transform(text).join("\n");
};

export const getCommentTags =
Expand Down Expand Up @@ -71,7 +71,10 @@ export namespace TsSymbolUtil {
.replace(`@${tagName}`, "")
.split("\n")
.map((str) => trim(str))
.filter((str) => !!str.length)
.filter(
(str, i, array) =>
(i !== 0 && i !== array.length - 1) || !!str.length,
)
.join("\n");
return comment.length
? {
Expand All @@ -81,7 +84,7 @@ export namespace TsSymbolUtil {
: tag;
};

const filter = (text: string): string[] => {
const transform = (text: string): string[] => {
const elements: string[] = text.split("\n");
const first: number = lastIndexOf(elements)((elem) =>
elem.trim().startsWith("/**"),
Expand All @@ -98,7 +101,10 @@ export namespace TsSymbolUtil {
elem = elem.substring(0, elem.lastIndexOf("*/"));
return trim(elem);
})
.filter((elem) => elem.length > 0);
.filter(
(str, i, array) =>
(i !== 0 && i !== array.length - 1) || !!str.length,
);
};

const lastIndexOf =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const test_application_ajv_ObjectJsonTag = _test_application("ajv")(
},
title: {
title: "something",
description: "Titled property.\n@title something",
description: "Titled property.\n\n@title something",
"x-typia-jsDocTags": [
{
name: "title",
Expand All @@ -55,7 +55,7 @@ export const test_application_ajv_ObjectJsonTag = _test_application("ajv")(
complicate_title: {
title: "something weirdo with tag",
description:
"Complicate title.\n@title something weirdo with {@link something} tag",
"Complicate title.\n\n@title something weirdo with {@link something} tag",
"x-typia-jsDocTags": [
{
name: "title",
Expand Down Expand Up @@ -87,7 +87,7 @@ export const test_application_ajv_ObjectJsonTag = _test_application("ajv")(
"complicate_title",
],
description:
"Complicate title.\n@title something weirdo with {@link something} tag",
"Complicate title.\n\n@title something weirdo with {@link something} tag",
"x-typia-jsDocTags": [],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const test_application_ajv_TagCustom = _test_application("ajv")(
properties: {
id: {
description:
"Regular feature supported by typia\n@format uuid",
"Regular feature supported by typia\n\n@format uuid",
"x-typia-metaTags": [
{
kind: "format",
Expand All @@ -43,7 +43,7 @@ export const test_application_ajv_TagCustom = _test_application("ajv")(
},
dollar: {
description:
'Custom feature composed with "$" + number\n@dollar',
'Custom feature composed with "$" + number\n\n@dollar',
"x-typia-jsDocTags": [
{
name: "dollar",
Expand All @@ -55,7 +55,7 @@ export const test_application_ajv_TagCustom = _test_application("ajv")(
},
postfix: {
description:
'Custom feature composed with string + "abcd"\n@postfix abcd',
'Custom feature composed with string + "abcd"\n\n@postfix abcd',
"x-typia-jsDocTags": [
{
name: "postfix",
Expand All @@ -73,7 +73,7 @@ export const test_application_ajv_TagCustom = _test_application("ajv")(
},
log: {
description:
"Custom feature meaning x^y\n@powerOf 10",
"Custom feature meaning x^y\n\n@powerOf 10",
"x-typia-jsDocTags": [
{
name: "powerOf",
Expand All @@ -91,7 +91,7 @@ export const test_application_ajv_TagCustom = _test_application("ajv")(
},
},
required: ["id", "dollar", "postfix", "log"],
description: "Custom feature meaning x^y\n@powerOf 10",
description: "Custom feature meaning x^y\n\n@powerOf 10",
"x-typia-jsDocTags": [],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const test_application_ajv_TagFormat = _test_application("ajv")(
properties: {
uuid: {
description:
"Universally Unique Identifier.\n@format uuid",
"Universally Unique Identifier.\n\n@format uuid",
"x-typia-metaTags": [
{
kind: "format",
Expand All @@ -42,7 +42,7 @@ export const test_application_ajv_TagFormat = _test_application("ajv")(
format: "uuid",
},
email: {
description: "Email address\n@format email",
description: "Email address\n\n@format email",
"x-typia-metaTags": [
{
kind: "format",
Expand All @@ -66,7 +66,7 @@ export const test_application_ajv_TagFormat = _test_application("ajv")(
format: "email",
},
url: {
description: "URL address.\n@format url",
description: "URL address.\n\n@format url",
"x-typia-metaTags": [
{
kind: "format",
Expand All @@ -90,7 +90,7 @@ export const test_application_ajv_TagFormat = _test_application("ajv")(
format: "url",
},
ipv4: {
description: "IPv4 address.\n@format ipv4",
description: "IPv4 address.\n\n@format ipv4",
"x-typia-metaTags": [
{
kind: "format",
Expand All @@ -114,7 +114,7 @@ export const test_application_ajv_TagFormat = _test_application("ajv")(
format: "ipv4",
},
ipv6: {
description: "IPv6 address.\n@format ipv6",
description: "IPv6 address.\n\n@format ipv6",
"x-typia-metaTags": [
{
kind: "format",
Expand All @@ -138,7 +138,7 @@ export const test_application_ajv_TagFormat = _test_application("ajv")(
format: "ipv6",
},
date: {
description: "Date only.\n@format date",
description: "Date only.\n\n@format date",
"x-typia-metaTags": [
{
kind: "format",
Expand All @@ -162,7 +162,7 @@ export const test_application_ajv_TagFormat = _test_application("ajv")(
format: "date",
},
date_time: {
description: "Date and time.\n@format date-time",
description: "Date and time.\n\n@format date-time",
"x-typia-metaTags": [
{
kind: "format",
Expand All @@ -187,7 +187,7 @@ export const test_application_ajv_TagFormat = _test_application("ajv")(
},
datetime: {
description:
"Date and time with only lowercase characters.\n@format datetime",
"Date and time with only lowercase characters.\n\n@format datetime",
"x-typia-metaTags": [
{
kind: "format",
Expand All @@ -212,7 +212,7 @@ export const test_application_ajv_TagFormat = _test_application("ajv")(
},
dateTime: {
description:
"Date and time with camelCase.\n@format dateTime",
"Date and time with camelCase.\n\n@format dateTime",
"x-typia-metaTags": [
{
kind: "format",
Expand All @@ -237,7 +237,7 @@ export const test_application_ajv_TagFormat = _test_application("ajv")(
},
custom: {
description:
"A custom format string.\n@format my-custom-format",
"A custom format string.\n\n@format my-custom-format",
"x-typia-jsDocTags": [
{
name: "format",
Expand Down Expand Up @@ -268,7 +268,7 @@ export const test_application_ajv_TagFormat = _test_application("ajv")(
"custom",
],
description:
"A custom format string.\n@format my-custom-format",
"A custom format string.\n\n@format my-custom-format",
"x-typia-jsDocTags": [],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const test_application_ajv_TagMatrix = _test_application("ajv")(
properties: {
matrix: {
description:
"Doubled array.\n@items 3\n@format uuid",
"Doubled array.\n\n@items 3\n@format uuid",
"x-typia-metaTags": [
{
kind: "items",
Expand Down Expand Up @@ -54,7 +54,7 @@ export const test_application_ajv_TagMatrix = _test_application("ajv")(
type: "array",
items: {
description:
"Doubled array.\n@items 3\n@format uuid",
"Doubled array.\n\n@items 3\n@format uuid",
"x-typia-metaTags": [
{
kind: "items",
Expand Down Expand Up @@ -90,7 +90,7 @@ export const test_application_ajv_TagMatrix = _test_application("ajv")(
type: "array",
items: {
description:
"Doubled array.\n@items 3\n@format uuid",
"Doubled array.\n\n@items 3\n@format uuid",
"x-typia-metaTags": [
{
kind: "items",
Expand Down Expand Up @@ -130,7 +130,7 @@ export const test_application_ajv_TagMatrix = _test_application("ajv")(
},
},
required: ["matrix"],
description: "Doubled array.\n@items 3\n@format uuid",
description: "Doubled array.\n\n@items 3\n@format uuid",
"x-typia-jsDocTags": [],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const test_application_ajv_TagType = _test_application("ajv")(
properties: {
int: {
description:
"Integer type.\nThis is to explain {@link int} value in {@link TagType.Type}.\n@type int\n@example https://example.com",
"Integer type.\n\nThis is to explain {@link int} value in {@link TagType.Type}.\n\n@type int\n@example https://example.com",
"x-typia-metaTags": [
{
kind: "type",
Expand Down Expand Up @@ -53,7 +53,7 @@ export const test_application_ajv_TagType = _test_application("ajv")(
type: "integer",
},
uint: {
description: "Unsigned integer type.\n@type uint",
description: "Unsigned integer type.\n\n@type uint",
"x-typia-metaTags": [
{
kind: "type",
Expand All @@ -78,7 +78,7 @@ export const test_application_ajv_TagType = _test_application("ajv")(
},
},
required: ["int", "uint"],
description: "Unsigned integer type.\n@type uint",
description: "Unsigned integer type.\n\n@type uint",
"x-typia-jsDocTags": [],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const test_application_swagger_ObjectJsonTag = _test_application(
},
title: {
title: "something",
description: "Titled property.\n@title something",
description: "Titled property.\n\n@title something",
"x-typia-jsDocTags": [
{
name: "title",
Expand All @@ -54,7 +54,7 @@ export const test_application_swagger_ObjectJsonTag = _test_application(
complicate_title: {
title: "something weirdo with tag",
description:
"Complicate title.\n@title something weirdo with {@link something} tag",
"Complicate title.\n\n@title something weirdo with {@link something} tag",
"x-typia-jsDocTags": [
{
name: "title",
Expand Down Expand Up @@ -87,7 +87,7 @@ export const test_application_swagger_ObjectJsonTag = _test_application(
"complicate_title",
],
description:
"Complicate title.\n@title something weirdo with {@link something} tag",
"Complicate title.\n\n@title something weirdo with {@link something} tag",
"x-typia-jsDocTags": [],
},
},
Expand Down
Loading

0 comments on commit bfd5488

Please sign in to comment.