Skip to content

Commit 36e458b

Browse files
authored
fix(legacy-json): more identical output (#526)
* fix(legacy-json): more identical output * fixup! * no __promote * dont clone stability, use ref * tests
1 parent a5258a9 commit 36e458b

File tree

14 files changed

+283
-207
lines changed

14 files changed

+283
-207
lines changed

src/generators/jsx-ast/constants.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,5 @@ export const TYPES_WITH_METHOD_SIGNATURES = [
150150
'method',
151151
'classMethod',
152152
];
153+
154+
export const TRIMMABLE_PADDING_REGEX = /^[\s:]+/;

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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { h as createElement } from 'hastscript';
22

33
import createQueries from '../../../utils/queries/index.mjs';
4+
import { TRIMMABLE_PADDING_REGEX } from '../constants.mjs';
45

56
/**
67
* Determines if a node looks like part of a type annotation.
@@ -124,7 +125,10 @@ export const parseListIntoProperties = node => {
124125

125126
// Clean up leading whitespace in remaining description
126127
if (children[0]?.type === 'text') {
127-
children[0].value = children[0].value.trimStart();
128+
children[0].value = children[0].value.replace(
129+
TRIMMABLE_PADDING_REGEX,
130+
''
131+
);
128132
}
129133

130134
properties.push({
@@ -133,7 +137,7 @@ export const parseListIntoProperties = node => {
133137
// The remaining children are the description
134138
desc: children,
135139
// Is there a list within this list?
136-
sublist: sublists.find(createQueries.UNIST.isTypedList),
140+
sublist: sublists.find(createQueries.UNIST.isLooselyTypedList),
137141
});
138142
}
139143

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

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

9898
// Parse parameters from the list, if found
9999
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: 13 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
},

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

Lines changed: 13 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,18 @@ 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 stabilityIdx = content.children.indexOf(stability.children[0]);
113+
114+
if (stabilityIdx) {
115+
nodes.splice(stabilityIdx - 1, 1);
116+
}
112117
}
113118
};
114119

@@ -153,7 +158,7 @@ export const createSectionBuilder = () => {
153158
* @param {import('../types.d.ts').Section} parent - The parent section.
154159
*/
155160
const addToParent = (section, parent) => {
156-
const key = SECTION_TYPE_PLURALS[section.type] || 'miscs';
161+
const key = SECTION_TYPE_PLURALS[section.type] || 'properties';
157162

158163
parent[key] ??= [];
159164
parent[key].push(section);

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

Lines changed: 13 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,9 @@ 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]);
113118
}
114119
break;
115120

@@ -119,9 +124,10 @@ export function parseList(section, nodes) {
119124
break;
120125

121126
default:
122-
// If no specific handling, re-add the list for further processing
123-
if (list) {
124-
nodes.unshift(list);
125-
}
127+
removeList = false;
128+
}
129+
130+
if (removeList && list) {
131+
nodes.splice(listIdx, 1);
126132
}
127133
}

src/metadata.mjs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ const createMetadata = slugger => {
4747
*
4848
* @param {StabilityIndexParent} stability The stability index node to be added
4949
*/
50-
addStability: stability => {
51-
// We clone the stability to ensure that we don't accidentally override it
52-
// with later mutations below on the `.create` method
53-
internalMetadata.stability.children.push({ ...stability });
54-
},
50+
addStability: stability =>
51+
internalMetadata.stability.children.push(stability),
5552
/**
5653
* Set the Metadata (from YAML if exists) properties to the current Metadata entry
5754
* it also allows for extra data (such as Stability Index) and miscellaneous data to be set

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(

0 commit comments

Comments
 (0)