From 1a04d2a24896ab834c2efdb0815f52ac76ab51a9 Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Wed, 22 Nov 2023 22:30:04 +0900 Subject: [PATCH] Warning message when `jsDocParsingMode` is not hacked. As TypeScript starts skipping parsing `JSDocComment`s since TypeScript v5.3 update but it is possible to revive the `JSDocComment`s' parsing, by hacking the `jsDocParsingMode` value of `tsc.js`, I've decided to print a warning message from `typia` when the `jsDocParsingMode` value is not hacked. Therefore, it is possible to running the `typia` as before even when the `jsDocParsingMode` is not hacked, and user can ignore the warning message if he (or she) does not use any comment tags or not generating JSON schema with description comments. - Related issue: https://github.com/microsoft/TypeScript/pull/55739 --- package.json | 4 +-- packages/typescript-json/package.json | 4 +-- src/transformers/FileTransformer.ts | 44 ++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 0413486ef8..dba0e62f97 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typia", - "version": "5.3.0-dev.20231120", + "version": "5.3.0-dev.20231122", "description": "Superfast runtime validators with only one line", "main": "lib/index.js", "typings": "lib/index.d.ts", @@ -97,7 +97,7 @@ "@types/nested-error-stacks": "^2.1.0", "@types/node": "^18.15.12", "@types/physical-cpu-count": "^2.0.0", - "@types/ts-expose-internals": "npm:ts-expose-internals@5.3.0-beta", + "@types/ts-expose-internals": "npm:ts-expose-internals@5.3.2", "@types/uuid": "^8.3.4", "@typescript-eslint/eslint-plugin": "^5.59.11", "@typescript-eslint/parser": "^5.59.11", diff --git a/packages/typescript-json/package.json b/packages/typescript-json/package.json index 1c3234cb9c..0c28c5f992 100644 --- a/packages/typescript-json/package.json +++ b/packages/typescript-json/package.json @@ -1,6 +1,6 @@ { "name": "typescript-json", - "version": "5.3.0-dev.20231120", + "version": "5.3.0-dev.20231122", "description": "Superfast runtime validators with only one line", "main": "lib/index.js", "typings": "lib/index.d.ts", @@ -72,7 +72,7 @@ }, "homepage": "https://typia.io", "dependencies": { - "typia": "5.3.0-dev.20231120" + "typia": "5.3.0-dev.20231122" }, "peerDependencies": { "typescript": ">=4.8.0 <5.3.0" diff --git a/src/transformers/FileTransformer.ts b/src/transformers/FileTransformer.ts index bff341d2c7..7ab8867810 100644 --- a/src/transformers/FileTransformer.ts +++ b/src/transformers/FileTransformer.ts @@ -1,30 +1,38 @@ import ts from "typescript"; +import { Singleton } from "../utils/Singleton"; + import { IProject } from "./IProject"; import { NodeTransformer } from "./NodeTransformer"; import { TransformerError } from "./TransformerError"; export namespace FileTransformer { export const transform = - (project: Omit) => + (environments: Omit) => (context: ts.TransformationContext) => (file: ts.SourceFile): ts.SourceFile => { if (file.isDeclarationFile) return file; + + const project: IProject = { + ...environments, + context, + }; + checkJsDocParsingMode.get(project, file); + return ts.visitEachChild( file, - (node) => iterate_node({ ...project, context })(context)(node), + (node) => iterate_node(project)(node), context, ); }; const iterate_node = (project: IProject) => - (context: ts.TransformationContext) => (node: ts.Node): ts.Node => ts.visitEachChild( try_transform_node(project)(node) ?? node, - (child) => iterate_node(project)(context)(child), - context, + (child) => iterate_node(project)(child), + project.context, ); const try_transform_node = @@ -55,3 +63,29 @@ const isTransformerError = (error: any): error is TransformerError => error.constructor.name === "TransformerError" && typeof error.code === "string" && typeof error.message === "string"; + +const checkJsDocParsingMode = new Singleton( + (project: IProject, file: ts.SourceFile) => { + if ( + typeof file.jsDocParsingMode === "number" && + file.jsDocParsingMode !== 0 + ) { + project.extras.addDiagnostic( + ts.createDiagnosticForNode(file, { + code: `(typia setup)` as any, + key: "jsDocParsingMode", + category: ts.DiagnosticCategory.Warning, + message: [ + `Run "npx typia setup" or "npx ts-patch install" command again.`, + ``, + `Since TypeScript v5.3 update, "tsc" no more parses JSDoc comments. Therefore, "typia" also cannot utilize those JSDoc comments, and it would damage some features of "typia" like "comment tags" or "JSON schema" generator.`, + ``, + `To solve this problem, run "npx typia setup" or "ts-patch install" command again to hack the TypeScript compiler to revive the JSDoc parsing.`, + ``, + ` - reference: https://github.com/microsoft/TypeScript/pull/55739`, + ].join("\n"), + }), + ); + } + }, +);