Skip to content

Commit 842c8f6

Browse files
committed
fix(legacy-json): more identical output
1 parent 776dd0b commit 842c8f6

File tree

12 files changed

+138
-82
lines changed

12 files changed

+138
-82
lines changed

src/generators/jsx-ast/utils/buildContent.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export const processEntry = (entry, remark) => {
229229
// Transform typed lists into property tables
230230
visit(
231231
content,
232-
createQueries.UNIST.isTypedList,
232+
createQueries.UNIST.isStronglyTypedList,
233233
(node, idx, parent) => (parent.children[idx] = createPropertyTable(node))
234234
);
235235

src/generators/jsx-ast/utils/buildPropertyTable.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const parseListIntoProperties = node => {
124124

125125
// Clean up leading whitespace in remaining description
126126
if (children[0]?.type === 'text') {
127-
children[0].value = children[0].value.trimStart();
127+
children[0].value = children[0].value.replace(/^[\s:]+/, '');
128128
}
129129

130130
properties.push({
@@ -133,7 +133,7 @@ export const parseListIntoProperties = node => {
133133
// The remaining children are the description
134134
desc: children,
135135
// Is there a list within this list?
136-
sublist: sublists.find(createQueries.UNIST.isTypedList),
136+
sublist: sublists.find(createQueries.UNIST.isLooselyTypedList),
137137
});
138138
}
139139

src/generators/jsx-ast/utils/buildSignature.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const getFullName = ({ name, text }, fallback = name) => {
8989
*/
9090
export default ({ children }, { data }, idx) => {
9191
// Try to locate the parameter list immediately following the heading
92-
const listIdx = children.findIndex(createQueries.UNIST.isTypedList);
92+
const listIdx = children.findIndex(createQueries.UNIST.isStronglyTypedList);
9393

9494
// Parse parameters from the list, if found
9595
const params =

src/generators/legacy-json/constants.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const DEFAULT_EXPRESSION = /\s*\*\*Default:\*\*\s*([^]+)$/i;
1212

1313
// Grabs the parameters from a method's signature
1414
// ex/ 'new buffer.Blob([sources[, options]])'.match(PARAM_EXPRESSION) === ['([sources[, options]])', '[sources[, options]]']
15-
export const PARAM_EXPRESSION = /\((.+)\);?$/;
15+
export const PARAM_EXPRESSION = /\(([^)]+)\);?$/;
1616

1717
// The plurals associated with each section type.
1818
export const SECTION_TYPE_PLURALS = {

src/generators/legacy-json/utils/__tests__/parseList.test.mjs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import {
88
parseList,
99
} from '../parseList.mjs';
1010

11+
const validTypedList = [
12+
{ type: 'inlineCode', value: 'option' }, // inline code
13+
{ type: 'text', value: ' ' }, // space
14+
{
15+
type: 'link',
16+
children: [{ type: 'text', value: '<boolean>' }], // link with < value
17+
},
18+
{ type: 'text', value: ' option description' },
19+
];
20+
1121
describe('transformTypeReferences', () => {
1222
it('replaces template syntax with curly braces', () => {
1323
const result = transformTypeReferences('`<string>`');
@@ -90,7 +100,7 @@ describe('parseList', () => {
90100
children: [
91101
{
92102
type: 'paragraph',
93-
children: [{ type: 'text', value: '{string} description' }],
103+
children: validTypedList,
94104
},
95105
],
96106
},
@@ -134,9 +144,7 @@ describe('parseList', () => {
134144
children: [
135145
{
136146
type: 'paragraph',
137-
children: [
138-
{ type: 'text', value: 'param1 {string} first parameter' },
139-
],
147+
children: validTypedList,
140148
},
141149
// This is a nested typed list
142150
{
@@ -146,15 +154,7 @@ describe('parseList', () => {
146154
children: [
147155
{
148156
type: 'paragraph',
149-
children: [
150-
{ type: 'inlineCode', value: 'option' }, // inline code
151-
{ type: 'text', value: ' ' }, // space
152-
{
153-
type: 'link',
154-
children: [{ type: 'text', value: '<boolean>' }], // link with < value
155-
},
156-
{ type: 'text', value: ' option description' },
157-
],
157+
children: validTypedList,
158158
},
159159
],
160160
},
@@ -167,6 +167,9 @@ describe('parseList', () => {
167167
];
168168

169169
parseList(section, nodes);
170+
171+
console.log(section);
172+
170173
assert.equal(section.params[0].options.length, 1);
171174
});
172175
});

src/generators/legacy-json/utils/buildSection.mjs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const createSectionBuilder = () => {
5353

5454
meta.changes = changes;
5555

56-
if (n_api_version?.length) {
56+
if (typeof n_api_version === 'number' || n_api_version?.length) {
5757
meta.napiVersion = enforceArray(n_api_version);
5858
}
5959

@@ -102,13 +102,17 @@ export const createSectionBuilder = () => {
102102
* @param {Array} nodes - The remaining AST nodes.
103103
* @param {import('../types.d.ts').HierarchizedEntry} entry - The entry providing stability information.
104104
*/
105-
const parseStability = (section, nodes, { stability }) => {
106-
const stabilityInfo = stability.children.map(node => node.data)?.[0];
105+
const parseStability = (section, nodes, { stability, content }) => {
106+
const stabilityNode = stability.children[0];
107107

108-
if (stabilityInfo) {
109-
section.stability = Number(stabilityInfo.index);
110-
section.stabilityText = stabilityInfo.description;
111-
nodes.shift(); // Remove stability node from processing
108+
if (stabilityNode) {
109+
section.stability = Number(stabilityNode.data.index);
110+
section.stabilityText = stabilityNode.data.description;
111+
112+
const nodeToRemove = content.children.findIndex(
113+
({ data }) => data === stabilityNode.data
114+
);
115+
nodes.splice(nodeToRemove - 1, 1);
112116
}
113117
};
114118

@@ -153,7 +157,10 @@ export const createSectionBuilder = () => {
153157
* @param {import('../types.d.ts').Section} parent - The parent section.
154158
*/
155159
const addToParent = (section, parent) => {
156-
const key = SECTION_TYPE_PLURALS[section.type] || 'miscs';
160+
const key =
161+
SECTION_TYPE_PLURALS[section.__promote ?? section.type] || 'miscs';
162+
163+
delete section.__promote;
157164

158165
parent[key] ??= [];
159166
parent[key].push(section);

src/generators/legacy-json/utils/parseList.mjs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const extractPattern = (text, pattern, key, current) => {
4747
export function parseListItem(child) {
4848
const current = {};
4949

50-
const subList = child.children.find(createQueries.UNIST.isTypedList);
50+
const subList = child.children.find(createQueries.UNIST.isLooselyTypedList);
5151

5252
// Extract and clean raw text from the node, excluding nested lists
5353
current.textRaw = transformTypeReferences(
@@ -89,10 +89,13 @@ export function parseListItem(child) {
8989
* @param {import('@types/mdast').RootContent[]} nodes
9090
*/
9191
export function parseList(section, nodes) {
92-
const list = nodes[0]?.type === 'list' ? nodes.shift() : null;
92+
const listIdx = nodes.findIndex(createQueries.UNIST.isStronglyTypedList);
93+
const list = nodes[listIdx];
9394

9495
const values = list ? list.children.map(parseListItem) : [];
9596

97+
let removeList = true;
98+
9699
// Update the section based on its type and parsed values
97100
switch (section.type) {
98101
case 'ctor':
@@ -109,7 +112,12 @@ export function parseList(section, nodes) {
109112
case 'property':
110113
// For properties, update type and other details if values exist
111114
if (values.length) {
112-
leftHandAssign(section, values[0]);
115+
delete values[0].name;
116+
117+
Object.assign(section, values[0]);
118+
119+
// TODO(@avivkeller): There is probably a better way to do this.
120+
section.__promote = 'property';
113121
}
114122
break;
115123

@@ -119,9 +127,10 @@ export function parseList(section, nodes) {
119127
break;
120128

121129
default:
122-
// If no specific handling, re-add the list for further processing
123-
if (list) {
124-
nodes.unshift(list);
125-
}
130+
removeList = false;
131+
}
132+
133+
if (removeList && list) {
134+
nodes.splice(listIdx, 1);
126135
}
127136
}

src/utils/parser/constants.mjs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,20 @@ const FUNCTION_CALL = '\\([^)]*\\)';
3737
// Matches "bar":
3838
// Group 1: foo[bar]
3939
// Group 2: foo.bar
40-
const PROPERTY = `${CAMEL_CASE}(?:(\\[${CAMEL_CASE}\\])|\\.(\\w+))`;
40+
const PROPERTY = `${CAMEL_CASE}(?:(\\[[^\\]]+\\])|\\.(\\w+))`;
4141

4242
// An array of objects defining the different types of API doc headings we want to
4343
// capture and their respective regex to match against the heading text.
4444
// The regex are case-insensitive.
4545
export const DOC_API_HEADING_TYPES = [
4646
{
4747
type: 'method',
48-
regex: new RegExp(`^\`${PROPERTY}${FUNCTION_CALL}\`$`, 'i'),
48+
regex: new RegExp(
49+
`^\`(?:${PROPERTY}|(${CAMEL_CASE}))${FUNCTION_CALL}\`$`,
50+
'i'
51+
),
4952
},
50-
{ type: 'event', regex: /^Event: +`'?([^']+)'`$/i },
53+
{ type: 'event', regex: /^Event: +`'?([^`]*?)'?`$/i },
5154
{
5255
type: 'class',
5356
regex: new RegExp(

src/utils/queries/__tests__/index.test.mjs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,23 @@ describe('createQueries', () => {
108108
});
109109

110110
describe('UNIST', () => {
111-
describe('isTypedList', () => {
111+
describe('isStronglyTypedList', () => {
112112
it('returns false for non-list nodes', () => {
113113
strictEqual(
114-
createQueries.UNIST.isTypedList({ type: 'paragraph', children: [] }),
114+
createQueries.UNIST.isStronglyTypedList({
115+
type: 'paragraph',
116+
children: [],
117+
}),
115118
false
116119
);
117120
});
118121

119122
it('returns false for empty lists', () => {
120123
strictEqual(
121-
createQueries.UNIST.isTypedList({ type: 'list', children: [] }),
124+
createQueries.UNIST.isStronglyTypedList({
125+
type: 'list',
126+
children: [],
127+
}),
122128
false
123129
);
124130
});
@@ -211,7 +217,7 @@ describe('createQueries', () => {
211217

212218
cases.forEach(({ name, node, expected }) => {
213219
it(`returns ${expected} for ${name}`, () => {
214-
strictEqual(createQueries.UNIST.isTypedList(node), expected);
220+
strictEqual(createQueries.UNIST.isStronglyTypedList(node), expected);
215221
});
216222
});
217223
});

src/utils/queries/constants.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
export const DOC_API_STABILITY_SECTION_REF_URL =
55
'documentation.html#stability-index';
66

7-
export const VALID_JAVASCRIPT_PROPERTY = /^[.a-z0-9$_]+$/i;
7+
export const VALID_JAVASCRIPT_PROPERTY = /^[.a-z0-9$_'-]+$/i;

0 commit comments

Comments
 (0)