Skip to content

Commit 478281f

Browse files
authored
feat: support deprecated on schema (#33)
1 parent 2a9deb6 commit 478281f

File tree

5 files changed

+106
-263
lines changed

5 files changed

+106
-263
lines changed

compile-to-definitions/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const preprocessSchema = (schema, root = schema, path = []) => {
118118
}
119119
schema.properties[key] = {
120120
description: result.description,
121+
deprecated: result.deprecated,
121122
anyOf: [property],
122123
};
123124
} else if (
@@ -128,6 +129,7 @@ const preprocessSchema = (schema, root = schema, path = []) => {
128129
const result = resolvePath(root, property.oneOf[0].$ref);
129130
schema.properties[key] = {
130131
description: property.description || result.description,
132+
deprecated: property.deprecated || result.deprecated,
131133
anyOf: property.oneOf,
132134
};
133135
preprocessSchema(schema.properties[key], root, [...path, key]);

generate-types/index.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -621,12 +621,34 @@ const printError = (diagnostic) => {
621621
*/
622622
const getDocumentation = (symbol) => {
623623
if (!symbol) return "";
624-
const comments = symbol.getDocumentationComment(checker);
625-
if (comments.length === 0) return "";
626-
return `\n/**\n * ${comments
627-
.map((c) => c.text)
628-
.join("")
629-
.replace(/\n+/g, "\n * ")}\n */\n`;
624+
const normalizeText = (parts) =>
625+
ts
626+
.displayPartsToString(parts || [])
627+
.replace(/\r\n?/g, "\n")
628+
.replace(/\n+/g, "\n")
629+
.trim();
630+
631+
let commentText = normalizeText(symbol.getDocumentationComment(checker));
632+
const deprecatedTags = symbol
633+
.getJsDocTags(checker)
634+
.filter((tag) => tag.name === "deprecated");
635+
636+
if (!commentText && deprecatedTags.length === 0) return "";
637+
638+
const lines = commentText ? commentText.split("\n") : [];
639+
for (const tag of deprecatedTags) {
640+
const text = normalizeText(tag.text);
641+
if (text && !commentText) {
642+
lines.push(...text.split("\n"));
643+
lines.push("@deprecated");
644+
} else {
645+
lines.push(text ? `@deprecated ${text}` : "@deprecated");
646+
}
647+
}
648+
649+
return `\n/**\n${lines
650+
.map((line) => (line ? ` * ${line}` : " *"))
651+
.join("\n")}\n */\n`;
630652
};
631653

632654
/**

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
"ajv": "^8.1.0",
3131
"commondir": "^1.0.1",
3232
"glob": "^7.1.6",
33-
"json-schema-to-typescript": "^9.1.1",
33+
"json-schema-to-typescript": "^15.0.4",
3434
"terser": "^5.32.0",
3535
"yargs": "^16.1.1"
36-
}
36+
},
37+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
3738
}

precompile-schemas/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const schemas = glob.sync(schemasGlob, { cwd: root, absolute: true });
109109
const EXCLUDED_PROPERTIES = [
110110
"title",
111111
"description",
112+
"deprecated",
112113
"cli",
113114
"implements",
114115
"tsType",

0 commit comments

Comments
 (0)