Skip to content

Commit 64812d9

Browse files
committed
tmp
Signed-off-by: flakey5 <[email protected]>
1 parent e4dbbe4 commit 64812d9

File tree

7 files changed

+101
-11
lines changed

7 files changed

+101
-11
lines changed

src/generators/json-all/util/generateJsonSchema.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const JSON_SCHEMA_URL = `https://nodejs.org/docs/${DOC_NODE_VERSION}/api/node-do
77

88
export const generateJsonSchema = () => ({
99
$schema: 'http://json-schema.org/draft-07/schema#',
10-
$id: `nodejs-api-doc-all@${jsonAll.version}`,
10+
$id: `nodejs-api-doc-all@v${jsonAll.version}`,
1111
title: 'Node.js API Documentation Schema (All)',
1212
readOnly: true,
1313

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// @ts-check
2+
'use strict';
3+
4+
import test, { describe } from 'node:test';
5+
import assert from 'node:assert';
6+
7+
`
8+
#### \`url.username\`
9+
10+
* Type: {string}
11+
12+
Gets and sets the username portion of the URL.
13+
`;
14+
15+
describe('extracts type correctly', () => {
16+
const supportedFormats = [
17+
'{integer} **Default:** 8192',
18+
'{integer|boolean} bla bla bla',
19+
'{boolean}',
20+
'Type: {Function} bla bla bla',
21+
];
22+
23+
for (const format of supportedFormats) {
24+
test(format, () => {
25+
/**
26+
* @type {import('../../utils/createSectionBase.mjs').HierarchizedEntry}
27+
*/
28+
const entry = {
29+
hierarchyChildren: [],
30+
api: 'bla',
31+
slug: 'asd',
32+
api_doc_source: 'doc/api/something.md',
33+
changes: [],
34+
heading: {
35+
type: 'heading',
36+
depth: 1,
37+
children: [],
38+
data: {
39+
text: '`asd.something`',
40+
name: '`asd.something`',
41+
depth: 3,
42+
slug: '`asd.something`',
43+
type: 'property',
44+
},
45+
},
46+
stability: {
47+
type: 'root',
48+
children: [],
49+
},
50+
content: {
51+
type: 'root',
52+
children: [
53+
{
54+
type: 'list',
55+
children: [
56+
{
57+
type: 'listItem',
58+
children: [
59+
{
60+
type: 'paragraph',
61+
// children:
62+
}
63+
]
64+
}
65+
]
66+
}
67+
],
68+
},
69+
tags: [],
70+
yaml_position: {},
71+
};
72+
});
73+
}
74+
});

src/generators/json/__tests__/utils/createSectionBase.test.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// @ts-check
2+
'use strict';
3+
24
import test, { describe } from 'node:test';
35
import assert from 'node:assert';
46
import {

src/generators/json/utils/createMethodSection.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// @ts-check
22
'use strict';
33

4-
import { assertAstType } from '../../../utils/assertAstType.mjs';
4+
import {
5+
assertAstType,
6+
assertAstTypeOptional,
7+
} from '../../../utils/assertAstType.mjs';
58
import { GeneratorError } from '../../../utils/generator-error.mjs';
69
import { METHOD_RETURN_TYPE_EXTRACTOR } from '../constants.mjs';
710
import { findParentSection } from './findParentSection.mjs';
@@ -25,7 +28,7 @@ export const createMethodSectionBuilder = () => {
2528
* documented properties.
2629
*/
2730
const paragraph = assertAstType(children[0], 'paragraph');
28-
const list = children[1] ? assertAstType(children[1], 'list') : undefined;
31+
const list = assertAstTypeOptional(children[1], 'list');
2932

3033
/**
3134
* @type {import('../generated.d.ts').MethodParameter | import('../generated.d.ts').MethodReturnType}

src/generators/json/utils/createPropertySection.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const createPropertySectionBuilder = () => {
3939
return undefined;
4040
}
4141

42-
const firstListElement = listNode.children[0].children[0];
42+
const firstListElement = structuredClone(listNode.children[0].children[0]);
4343

4444
if (firstListElement.type !== 'paragraph') {
4545
throw new TypeError(
@@ -184,7 +184,7 @@ export const createPropertySectionBuilder = () => {
184184
* @param {import('../generated.d.ts').Property} section The method section
185185
*/
186186
return (entry, section) => {
187-
const listElement = parseType(structuredClone(entry), section);
187+
const listElement = parseType(entry, section);
188188

189189
if (listElement) {
190190
parseReferenceLink(listElement, section);

src/generators/json/utils/createSectionBase.mjs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const createSectionBaseBuilder = () => {
3434
* @param {number} depth
3535
* @returns {typeof ENTRY_TO_SECTION_TYPE[string]}
3636
*/
37-
const determineType = (header, entry) => {
37+
const determineType = header => {
3838
const fallback = header.depth === 1 ? 'module' : 'text';
3939

4040
// doc/api/process.md's parent section shouldn't have a defined type, but
@@ -47,10 +47,6 @@ export const createSectionBaseBuilder = () => {
4747
return 'module';
4848
}
4949

50-
// if (header?.data.type === 'global') {
51-
// console.log(entry);
52-
// }
53-
5450
return ENTRY_TO_SECTION_TYPE[header?.data.type ?? fallback];
5551
};
5652

@@ -202,7 +198,7 @@ export const createSectionBaseBuilder = () => {
202198
return entry => {
203199
const [, ...nodes] = entry.content.children;
204200

205-
const type = determineType(entry.heading, entry);
201+
const type = determineType(entry.heading);
206202

207203
/**
208204
* @type {import('../generated.d.ts').SectionBase}

src/utils/assertAstType.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,18 @@ export function assertAstType(node, type) {
3535

3636
return node;
3737
}
38+
39+
/**
40+
* @template {keyof NodeTypes} T
41+
*
42+
* @param {import('mdast').Node | undefined} node
43+
* @param {T} type
44+
* @returns {NodeTypes[T] | undefined}
45+
*/
46+
export function assertAstTypeOptional(node, type) {
47+
if (!node) {
48+
return undefined;
49+
}
50+
51+
return assertAstType(node, type);
52+
}

0 commit comments

Comments
 (0)