Skip to content

Commit 852cd0a

Browse files
authored
add JSDoc comments to types generated from v8.json (#1219)
1 parent e48ef6f commit 852cd0a

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

build/generate-docs.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import v8 from '../src/reference/v8.json' with { type: 'json' };
22
import fs from 'fs';
3-
import jsonStringify from 'json-stringify-pretty-compact';
3+
import {formatJSON} from './util';
44

55
/**
66
* This script generates markdown documentation from the JSON schema.
@@ -72,17 +72,6 @@ function topicElement(key: string, value: JsonObject): boolean {
7272
key !== 'sources';
7373
}
7474

75-
/**
76-
* @param obj - object to be formatted
77-
* @returns formatted JSON
78-
*/
79-
function formatJSON(obj: any): string {
80-
return jsonStringify(obj, {
81-
indent: 4,
82-
maxLength: 60
83-
});
84-
}
85-
8675
/**
8776
* Converts the example value to markdown format.
8877
* @param key - the name of the json property

build/generate-style-spec.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
import {writeFileSync} from 'fs';
22
import spec from '../src/reference/v8.json' with {type: 'json'};
33
import {supportsPropertyExpression, supportsZoomExpression} from '../src/util/properties';
4+
import {formatJSON} from './util';
5+
6+
function jsDocComment(property) {
7+
const lines = [];
8+
if (property.doc) {
9+
lines.push(...property.doc.split('\n'));
10+
}
11+
if (property.default) {
12+
if (lines.length) {
13+
lines.push('');
14+
}
15+
lines.push(...jsDocBlock('default', property.default).split('\n'));
16+
}
17+
if (property.example) {
18+
if (lines.length) {
19+
lines.push('');
20+
}
21+
lines.push(...jsDocBlock('example', property.example).split('\n'));
22+
}
23+
24+
if (!lines.length) {
25+
return undefined;
26+
}
27+
return [
28+
'/**',
29+
...lines.map(line => ` * ${line}`),
30+
' */',
31+
].join('\n');
32+
}
33+
34+
function jsDocBlock(tag, value) {
35+
return `@${tag}
36+
\`\`\`json
37+
${formatJSON(value)}
38+
\`\`\``;
39+
}
440

541
function unionType(values) {
642
if (Array.isArray(values)) {
@@ -64,7 +100,9 @@ function propertyType(property) {
64100
}
65101

66102
function propertyDeclaration(key, property) {
67-
return `"${key}"${property.required ? '' : '?'}: ${propertyType(property)}`;
103+
const jsDoc = jsDocComment(property);
104+
const declaration = `"${key}"${property.required ? '' : '?'}: ${propertyType(property)}`;
105+
return jsDoc ? [jsDoc, declaration].join('\n') : declaration;
68106
}
69107

70108
function transitionPropertyDeclaration(key) {
@@ -86,7 +124,12 @@ ${Object.keys(properties)
86124
}
87125
return declarations;
88126
})
89-
.map(declaration => ` ${indent}${declaration}`)
127+
.map(declaration => {
128+
return declaration
129+
.split('\n')
130+
.map(line => ` ${indent}${line}`)
131+
.join('\n');
132+
})
90133
.join(',\n')}
91134
${indent}}`;
92135
}

build/util.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import jsonStringify from 'json-stringify-pretty-compact';
2+
3+
/**
4+
* Formats a JSON value into a reasonably compact and readable JSON string.
5+
*
6+
* @param obj - object to be formatted
7+
* @returns formatted JSON
8+
*/
9+
export function formatJSON(obj: any) {
10+
return jsonStringify(obj, {
11+
indent: 4,
12+
maxLength: 60
13+
});
14+
}

0 commit comments

Comments
 (0)