Skip to content

Commit

Permalink
Merge pull request #727 from samchon/features/comments
Browse files Browse the repository at this point in the history
Complement #726 - much more exact comments analyzing
  • Loading branch information
samchon authored Jul 29, 2023
2 parents f8daa5e + 1f71062 commit f692638
Show file tree
Hide file tree
Showing 32 changed files with 226 additions and 228 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": "4.1.9",
"version": "4.1.10",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "4.1.9",
"version": "4.1.10",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -68,7 +68,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "4.1.9"
"typia": "4.1.10"
},
"peerDependencies": {
"typescript": ">= 4.7.4"
Expand Down
68 changes: 20 additions & 48 deletions src/factories/CommentFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,25 @@ export namespace CommentFactory {
// NEW FEATURE OF TS 5.2
const elements: readonly (ts.JSDoc | ts.JSDocTag)[] =
ts.getJSDocCommentsAndTags(node);
if (elements.length === 0) return undefined;

const content: string[] = [];
for (const comment of elements)
if (ts.isJSDoc(comment)) content.push(comment.getFullText());
else {
for (const comment of elements) {
if (ts.isJSDoc(comment)) {
const parsed: string | undefined = ts.getTextOfJSDocComment(
comment.comment,
);
if (parsed) content.push(parsed);
}
const output: string = transform(
content.map((line) => line.split("\r\n").join("\n")),
).join("\n");
if (parsed?.length) {
content.push(parsed);
if (comment.tags?.length) content.push("");
}
for (const tag of comment.tags ?? [])
content.push(parseJSDocTag(tag));
} else content.push(parseJSDocTag(comment));
}
const output: string = content
.map((line) => line.split("\r\n").join("\n"))
.join("\n");
return output.length ? output : undefined;
};

Expand All @@ -58,44 +65,9 @@ export namespace CommentFactory {
.join("");
}

const transform = (elements: string[]): string[] => {
const first: number = lastIndexOf(elements)((elem) =>
elem.trim().startsWith("/**"),
);
const last: number = lastIndexOf(elements)((elem) =>
elem.trim().endsWith("*/"),
);

const cut: string[] = elements.slice(first, last + 1);
return cut
.map((elem, i) => {
if (i === 0) elem = elem.substring(elem.lastIndexOf("/**") + 3);
if (i === cut.length - 1)
elem = elem.substring(0, elem.lastIndexOf("*/"));
return trim(elem);
})
.filter(
(str, i, array) =>
(i !== 0 && i !== array.length - 1) || !!str.length,
);
};

const lastIndexOf =
<T>(array: T[]) =>
(pred: (elem: T) => boolean) => {
for (let i = array.length - 1; i >= 0; i--)
if (pred(array[i]!)) return i;
return -1;
};

const trim = (str: string): string => {
const vulnerable = (ch: string) =>
ch === " " || ch === "\n" || ch === "\t" || ch === "*";
let start: number;
let end: number;
for (start = 0; start < str.length; ++start)
if (!vulnerable(str.charAt(start))) break;
for (end = str.length - 1; end >= 0; --end)
if (!vulnerable(str.charAt(end))) break;
return start > end ? "" : str.substring(start, end + 1).trim();
const parseJSDocTag = (tag: ts.JSDocTag): string => {
const parsed: string | undefined = ts.getTextOfJSDocComment(tag.comment);
return parsed?.length
? `@${tag.tagName.text} ${parsed}`
: `@${tag.tagName.text}`;
};
52 changes: 39 additions & 13 deletions test/issues/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,58 @@ import typia from "typia";
*
* It will also contain the link tag like {@link IAttachmentFile}.
*
* @authot Samchon
* @author Samchon
* @deprecated
* @test
*/
export interface IBbsArticle {
export interface IBbsArticle extends IBbsArticle.IStore {
/**
* Primary Key.
*
* @format uuid
*/
id: string;

title: string;

body: string;

/**
* @format date-time
*/
created_at: string;
}
export namespace IBbsArticle {
export interface IStore {
/**
* @minLength 3
* @maxLength 50
* @doYouKnowThisTag
*/
title: string;
body: string;
files: IAttachmentFile[];
}
}

export interface IAttachmentFile {
/**
* @minLengt 1
* @maxLength 255
*/
name: string | null;

/**
* @minLength 1
* @maxLength 8
*/
extension: string | null;

/**
* @format url
*/
url: string;
}

const article = typia.metadata<[IBbsArticle]>().collection.objects[0];
const file = typia.metadata<[IAttachmentFile]>().collection.objects[0];

console.log(
JSON.stringify(
typia.metadata<[IBbsArticle]>().collection.objects[0].description,
null,
4,
),
article.description,
article.properties.map((p) => p.description),
file.properties.map((p) => p.description),
);
4 changes: 2 additions & 2 deletions test/schemas/json/ajv/ObjectJsonTag.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"title": {
"title": "something",
"description": "Titled property.\n *\n * @title something",
"description": "Titled property.\n\n@title something",
"x-typia-jsDocTags": [
{
"name": "title",
Expand All @@ -48,7 +48,7 @@
},
"complicate_title": {
"title": "something weirdo with {@link something } tag",
"description": "Complicate title.\n *\n * @title something weirdo with {@link something} tag",
"description": "Complicate title.\n\n@title something weirdo with {@link something } tag",
"x-typia-jsDocTags": [
{
"name": "title",
Expand Down
20 changes: 10 additions & 10 deletions test/schemas/json/ajv/TagArray.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "object",
"properties": {
"items": {
"description": "@items 3\n * @format uuid",
"description": "@items 3\n@format uuid",
"x-typia-metaTags": [
{
"kind": "items",
Expand Down Expand Up @@ -53,7 +53,7 @@
"x-typia-optional": false,
"type": "array",
"items": {
"description": "@items 3\n * @format uuid",
"description": "@items 3\n@format uuid",
"x-typia-metaTags": [
{
"kind": "items",
Expand Down Expand Up @@ -91,7 +91,7 @@
}
},
"minItems": {
"description": "@minItems 3\n * @minimum 3",
"description": "@minItems 3\n@minimum 3",
"x-typia-metaTags": [
{
"kind": "minItems",
Expand Down Expand Up @@ -126,7 +126,7 @@
"x-typia-optional": false,
"type": "array",
"items": {
"description": "@minItems 3\n * @minimum 3",
"description": "@minItems 3\n@minimum 3",
"x-typia-metaTags": [
{
"kind": "minItems",
Expand Down Expand Up @@ -165,7 +165,7 @@
"minItems": 3
},
"maxItems": {
"description": "@maxItems 7\n * @maxLength 7\n * @maximum 7",
"description": "@maxItems 7\n@maxLength 7\n@maximum 7",
"x-typia-metaTags": [
{
"kind": "maxItems",
Expand Down Expand Up @@ -215,7 +215,7 @@
"items": {
"oneOf": [
{
"description": "@maxItems 7\n * @maxLength 7\n * @maximum 7",
"description": "@maxItems 7\n@maxLength 7\n@maximum 7",
"x-typia-metaTags": [
{
"kind": "maxItems",
Expand Down Expand Up @@ -265,7 +265,7 @@
"maxLength": 7
},
{
"description": "@maxItems 7\n * @maxLength 7\n * @maximum 7",
"description": "@maxItems 7\n@maxLength 7\n@maximum 7",
"x-typia-metaTags": [
{
"kind": "maxItems",
Expand Down Expand Up @@ -315,7 +315,7 @@
"maximum": 7
}
],
"description": "@maxItems 7\n * @maxLength 7\n * @maximum 7",
"description": "@maxItems 7\n@maxLength 7\n@maximum 7",
"x-typia-metaTags": [
{
"kind": "maxItems",
Expand Down Expand Up @@ -365,7 +365,7 @@
"maxItems": 7
},
"both": {
"description": "@minItems 3\n * @maxItems 7\n * @format uuid",
"description": "@minItems 3\n@maxItems 7\n@format uuid",
"x-typia-metaTags": [
{
"kind": "minItems",
Expand Down Expand Up @@ -413,7 +413,7 @@
"x-typia-optional": false,
"type": "array",
"items": {
"description": "@minItems 3\n * @maxItems 7\n * @format uuid",
"description": "@minItems 3\n@maxItems 7\n@format uuid",
"x-typia-metaTags": [
{
"kind": "minItems",
Expand Down
6 changes: 3 additions & 3 deletions test/schemas/json/ajv/TagAtomicUnion.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"value": {
"oneOf": [
{
"description": "@minimum 3\n * @minLength 3\n * @maxLength 7",
"description": "@minimum 3\n@minLength 3\n@maxLength 7",
"x-typia-metaTags": [
{
"kind": "minimum",
Expand Down Expand Up @@ -71,7 +71,7 @@
"maxLength": 7
},
{
"description": "@minimum 3\n * @minLength 3\n * @maxLength 7",
"description": "@minimum 3\n@minLength 3\n@maxLength 7",
"x-typia-metaTags": [
{
"kind": "minimum",
Expand Down Expand Up @@ -121,7 +121,7 @@
"minimum": 3
}
],
"description": "@minimum 3\n * @minLength 3\n * @maxLength 7",
"description": "@minimum 3\n@minLength 3\n@maxLength 7",
"x-typia-metaTags": [
{
"kind": "minimum",
Expand Down
8 changes: 4 additions & 4 deletions test/schemas/json/ajv/TagCustom.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "object",
"properties": {
"id": {
"description": "Regular feature supported by typia\n *\n * @format uuid",
"description": "Regular feature supported by typia\n\n@format uuid",
"x-typia-metaTags": [
{
"kind": "format",
Expand All @@ -35,7 +35,7 @@
"format": "uuid"
},
"dollar": {
"description": "Custom feature composed with \"$\" + number\n *\n * @dollar",
"description": "Custom feature composed with \"$\" + number\n\n@dollar",
"x-typia-jsDocTags": [
{
"name": "dollar"
Expand All @@ -46,7 +46,7 @@
"type": "string"
},
"postfix": {
"description": "Custom feature composed with string + \"abcd\"\n *\n * @postfix abcd",
"description": "Custom feature composed with string + \"abcd\"\n\n@postfix abcd",
"x-typia-jsDocTags": [
{
"name": "postfix",
Expand All @@ -63,7 +63,7 @@
"type": "string"
},
"log": {
"description": "Custom feature meaning x^y\n *\n * @powerOf 10",
"description": "Custom feature meaning x^y\n\n@powerOf 10",
"x-typia-jsDocTags": [
{
"name": "powerOf",
Expand Down
Loading

0 comments on commit f692638

Please sign in to comment.