Skip to content

Commit 0891265

Browse files
committed
tmp
1 parent 78f73f2 commit 0891265

11 files changed

+99
-50
lines changed

src/generators/json-all/index.mjs

+5-8
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,10 @@ export default {
3535
const generatedValue = {
3636
$schema: './node-doc-all-schema.jsonc',
3737
modules: [],
38-
text: []
39-
}
38+
text: [],
39+
};
4040

41-
const propertiesToIgnore = [
42-
'$schema',
43-
'source',
44-
]
41+
const propertiesToIgnore = ['$schema', 'source'];
4542

4643
input.forEach(section => {
4744
const copiedSection = {};
@@ -50,7 +47,7 @@ export default {
5047
if (!propertiesToIgnore.includes(key)) {
5148
copiedSection[key] = section[key];
5249
}
53-
})
50+
});
5451

5552
switch (section.type) {
5653
case 'module':
@@ -62,7 +59,7 @@ export default {
6259
default:
6360
throw new TypeError(`unsupported root section type ${section.type}`);
6461
}
65-
})
62+
});
6663

6764
if (output) {
6865
// Current directory path relative to the `index.mjs` file

src/generators/json-all/schema.jsonc

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"text": {
1313
"type": "array",
14-
"items": { "$ref": "../json/schema.jsonc/#/definitions/Text" }
15-
}
14+
"items": { "$ref": "../json/schema.jsonc/#/definitions/Text" },
15+
},
1616
},
1717
}

src/generators/json/constants.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict';

src/generators/json/generated.d.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ export type Module = SectionBase & {
3434
properties?: Property[];
3535
[k: string]: unknown;
3636
};
37+
export type Text = SectionBase;
3738
/**
3839
* Node.js version number
3940
*/
4041
export type NodeCoreVersion = string;
4142
export type Class = SectionBase & {
4243
type: 'class';
43-
'@constructor'?: MethodSignature[];
44-
methods?: Method[];
45-
staticMethods?: Method[];
46-
properties?: Property[];
44+
'@constructor': MethodSignature[];
45+
methods: Method[];
46+
staticMethods: Method[];
47+
properties: Property[];
4748
[k: string]: unknown;
4849
};
4950
/**
@@ -69,7 +70,6 @@ export type Property = SectionBase & {
6970
mutable?: boolean;
7071
[k: string]: unknown;
7172
};
72-
export type Text = SectionBase;
7373

7474
/**
7575
* Common properties found at the root of each document.
@@ -97,6 +97,10 @@ export interface SectionBase {
9797
* Description of the section.
9898
*/
9999
description?: string;
100+
/**
101+
* Sections that just hold further text on this section.
102+
*/
103+
text?: Text[];
100104
/**
101105
* https://jsdoc.app/tags-example
102106
*/

src/generators/json/index.mjs

+9-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export default {
2727
// This should be kept in sync with the JSON schema version
2828
version: '2.0.0',
2929

30-
description: 'TODO',
30+
description:
31+
'This generator is responsible for generating the JSON representation of the docs.',
3132

3233
dependsOn: 'ast',
3334

@@ -76,7 +77,7 @@ export default {
7677
if (output) {
7778
await writeFile(
7879
join(output, `${node.api}.json`),
79-
JSON.stringify(section)
80+
JSON.stringify(section, null, 2)
8081
);
8182
}
8283
});
@@ -86,13 +87,13 @@ export default {
8687
const baseDir = import.meta.dirname;
8788

8889
// Read the contents of the JSON schema
89-
const schemaString = await readFile(
90-
join(baseDir, 'schema.jsonc'),
91-
'utf8'
92-
);
90+
// const schemaString = await readFile(
91+
// join(baseDir, 'schema.jsonc'),
92+
// 'utf8'
93+
// );
9394

94-
// Parse the JSON schema into an object
95-
const schema = await jsoncParse(schemaString);
95+
// // Parse the JSON schema into an object
96+
// const schema = await jsoncParse(schemaString);
9697

9798
// Write the parsed JSON schema to the output directory
9899
// await writeFile(

src/generators/json/schema.jsonc

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
2-
// TODO vscode doesn't support 2020-12 yet
3-
// https://github.com/microsoft/vscode/issues/165219
2+
/**
3+
* NOTE: if you modify this, please:
4+
* - Bump the version in the $id property and in the generator
5+
* - Run `tools/generate-json-types.mjs`
6+
*/
7+
48
"$schema": "http://json-schema.org/draft-07/schema#",
59
"$id": "[email protected]", // This should be kept in sync with the generator version
610
"title": "Node.js API Documentation Schema",
@@ -142,7 +146,6 @@
142146
"items": { "$ref": "#/definitions/Property" },
143147
},
144148
},
145-
// TODO what else to be required
146149
"required": ["type", "@module", "@see"],
147150
},
148151
],

src/generators/json/utils/createMethodSection.mjs

+45-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,48 @@ import { findParentSection } from './findParentSection.mjs';
99

1010
export const createMethodSectionBuilder = () => {
1111
/**
12+
* TODO docs
13+
* @param {HierarchizedEntry} entry The AST entry
14+
* @returns {Record<string, import('../generated.d.ts').MethodParameter> | undefined}
15+
*/
16+
const parseParameters = entry => {
17+
const [, ...nodes] = entry.content.children;
18+
19+
const listNode = nodes.find(node => node.type === 'list');
20+
21+
if (!listNode) {
22+
// Method doesn't take in any parameters
23+
return undefined;
24+
}
25+
26+
/**
27+
* @type {Record<string, import('../generated.d.ts').MethodParameter>}
28+
*/
29+
const parameters = {};
30+
31+
listNode.children.forEach(({ children }) => {
32+
// console.log(children)
33+
// if (children.length !== 1) {
34+
// console.log(JSON.stringify(children, null, 2))
35+
// }
36+
});
37+
38+
return parameters;
39+
};
40+
41+
/**
42+
* TODO docs
1243
* @param {HierarchizedEntry} entry The AST entry
1344
* @param {import('../generated.d.ts').Method} section The method section
1445
*/
1546
const parseSignatures = (entry, section) => {
1647
section.signatures = [];
1748

18-
49+
// Parse all the parameters and store them in a name:section map
50+
const parameters = parseParameters(entry, section);
51+
52+
// Parse the value of entry.heading.data.text to get the order of parameters and which are optional
53+
// console.log(entry.heading.data.text);
1954
};
2055

2156
/**
@@ -26,18 +61,23 @@ export const createMethodSectionBuilder = () => {
2661
return (entry, section) => {
2762
parseSignatures(entry, section);
2863

29-
// TODO are there any other places that an exposed method can be defined?
3064
const parent = findParentSection(section, ['class', 'module']);
3165

3266
// Add this section to the parent if it exists
3367
if (parent) {
34-
if (!Array.isArray(parent.methods)) {
68+
// Put static methods in `staticMethods` property and non-static methods
69+
// in the `methods` property
70+
const property = entry.heading.data.text.startsWith('Static method:')
71+
? 'staticMethods'
72+
: 'methods';
73+
74+
if (!Array.isArray(parent[property])) {
3575
throw new TypeError(
36-
`expected parent.methods to be an array, got type ${typeof parent.methods} instead (parent type=${parent.type})`
76+
`expected parent[${property}] to be an array, got type ${typeof parent[property]} instead (parent type=${parent.type})`
3777
);
3878
}
3979

40-
parent.methods.push(section);
80+
parent[property].push(section);
4181
}
4282
};
4383
};

src/generators/json/utils/createSection.mjs

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const createSectionBuilder = () => {
1818
const createMethodSection = createMethodSectionBuilder();
1919

2020
/**
21-
* TODO docs
21+
* Creates the properties that exist in the root of a document
2222
* @param {ApiDocMetadataEntry} head The head metadata entry
2323
* @returns {import('../generated.d.ts').DocumentRoot}
2424
*/
@@ -45,8 +45,10 @@ export const createSectionBuilder = () => {
4545
/**
4646
* @type {import('../types.d.ts').Section}
4747
*/
48-
const section = (createSectionBase(entry, parent?.type));
48+
const section = createSectionBase(entry, parent?.type);
4949

50+
// Temporarily add the parent section to the section so we have access to
51+
// it and can easily traverse through them when we need to
5052
section.parent = parent;
5153

5254
switch (section.type) {
@@ -76,6 +78,7 @@ export const createSectionBuilder = () => {
7678

7779
handleChildren(entry, section);
7880

81+
// Remove the parent property we added to the section earlier
7982
delete section.parent;
8083

8184
// if (parent) {

src/generators/json/utils/createSectionBase.mjs

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// @ts-check
22
'use strict';
33

4+
import { enforceArray } from './enforceArray.mjs';
5+
46
/**
57
* @typedef {import('../../legacy-json/types.d.ts').HierarchizedEntry} HierarchizedEntry
68
*/
@@ -20,17 +22,6 @@ const ENTRY_TO_SECTION_TYPE = /** @type {const} */ ({
2022
text: 'text',
2123
});
2224

23-
/**
24-
* Converts a value to an array.
25-
* @template T
26-
* @param {T | T[]} val - The value to convert.
27-
* @returns {T[]} The value as an array.
28-
*/
29-
const enforceArray = val => (Array.isArray(val) ? val : [val]);
30-
31-
/**
32-
*
33-
*/
3425
export const createSectionBaseBuilder = () => {
3526
/**
3627
* @param {import('mdast').RootContent} headingNode
@@ -118,7 +109,7 @@ export const createSectionBaseBuilder = () => {
118109
};
119110

120111
/**
121-
* TODO docs
112+
* Adds the deprecated property to the section if needed.
122113
* @param {import('../generated.d.ts').SectionBase} section
123114
* @param {HierarchizedEntry} entry
124115
*/
@@ -131,7 +122,7 @@ export const createSectionBaseBuilder = () => {
131122
};
132123

133124
/**
134-
* TODO docs
125+
* Adds the stability property to the section.
135126
* @param {import('../generated.d.ts').SectionBase} section
136127
* @param {HierarchizedEntry} entry
137128
*/
@@ -149,7 +140,7 @@ export const createSectionBaseBuilder = () => {
149140
};
150141

151142
/**
152-
* TODO docs
143+
* Adds the properties relating to versioning to the section.
153144
* @param {import('../generated.d.ts').SectionBase} section
154145
* @param {HierarchizedEntry} entry
155146
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
/**
4+
* Converts a value to an array.
5+
* @template T
6+
* @param {T | T[]} val - The value to convert.
7+
* @returns {T[]} The value as an array.
8+
*/
9+
export const enforceArray = val => (Array.isArray(val) ? val : [val]);

src/generators/json/utils/findParentSection.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
'use strict';
22

3+
import { enforceArray } from './enforceArray.mjs';
4+
35
/**
46
* Finds the closest parent section with the specified type(s).
57
* @param {import('../types.d.ts').Section} section
68
* @param {import('../generated.d.ts').SectionBase['type'] | Array<import('../generated.d.ts').SectionBase['type']>} type
79
* @returns {import('../types.d.ts').Section | undefined}
810
*/
911
export function findParentSection(section, type) {
10-
if (!Array.isArray(type)) {
11-
type = [type];
12-
}
12+
type = enforceArray(type);
1313

1414
let parent = section.parent;
1515

0 commit comments

Comments
 (0)