Skip to content

Commit f8d8ad5

Browse files
authored
chore(remark): reduce drilling (#729)
1 parent a9b6527 commit f8d8ad5

File tree

13 files changed

+95
-127
lines changed

13 files changed

+95
-127
lines changed

src/generators/ast/generate.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import { STABILITY_INDEX_URL } from './constants.mjs';
1010
import getConfig from '../../utils/configuration/index.mjs';
1111
import { withExt } from '../../utils/file.mjs';
1212
import { QUERIES } from '../../utils/queries/index.mjs';
13-
import { getRemark } from '../../utils/remark.mjs';
14-
15-
const remarkProcessor = getRemark();
13+
import { getRemark as remark } from '../../utils/remark.mjs';
1614

1715
/**
1816
* Process a chunk of markdown files in a worker thread.
@@ -40,7 +38,7 @@ export async function processChunk(inputSlice, itemIndices) {
4038
const relativePath = sep + withExt(relative(parent, path));
4139

4240
results.push({
43-
tree: remarkProcessor.parse(value),
41+
tree: remark().parse(value),
4442
// The path is the relative path minus the extension
4543
path: relativePath,
4644
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('transformHeadingNode (deprecation Type -> AlertBox level)', () => {
2929
const parent = makeParent('Documentation');
3030
const node = parent.children[0];
3131

32-
transformHeadingNode(entry, {}, node, 0, parent);
32+
transformHeadingNode(entry, node, 0, parent);
3333

3434
const alert = parent.children[1];
3535
const levelAttr = alert.attributes.find(a => a.name === 'level');
@@ -43,7 +43,7 @@ describe('transformHeadingNode (deprecation Type -> AlertBox level)', () => {
4343
const parent = makeParent('Runtime');
4444
const node = parent.children[0];
4545

46-
transformHeadingNode(entry, {}, node, 0, parent);
46+
transformHeadingNode(entry, node, 0, parent);
4747

4848
const alert = parent.children[1];
4949
const levelAttr = alert.attributes.find(a => a.name === 'level');
@@ -57,7 +57,7 @@ describe('transformHeadingNode (deprecation Type -> AlertBox level)', () => {
5757
const parent = makeParent('SomeOtherThing');
5858
const node = parent.children[0];
5959

60-
transformHeadingNode(entry, {}, node, 0, parent);
60+
transformHeadingNode(entry, node, 0, parent);
6161

6262
const alert = parent.children[1];
6363
const levelAttr = alert.attributes.find(a => a.name === 'level');

src/generators/jsx-ast/utils/__tests__/types.test.mjs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import assert from 'node:assert/strict';
2-
import { describe, it } from 'node:test';
2+
import { describe, it, mock } from 'node:test';
3+
4+
// Mock remark
5+
mock.module('../../../../utils/remark.mjs', {
6+
namedExports: {
7+
getRemarkRecma: () => ({
8+
runSync: () => ({
9+
body: [{ expression: 'mock-expression' }],
10+
}),
11+
}),
12+
},
13+
});
314

4-
import {
15+
const {
516
classifyTypeNode,
617
extractPropertyName,
718
extractTypeAnnotations,
819
parseListIntoProperties,
9-
} from '../types.mjs';
10-
11-
// Mock remark processor for tests
12-
const remark = {
13-
runSync: () => ({
14-
body: [{ expression: 'mock-expression' }],
15-
}),
16-
};
20+
} = await import('../types.mjs');
1721

1822
describe('classifyTypeNode', () => {
1923
it('returns 2 for union separator text node', () => {
@@ -183,7 +187,7 @@ describe('extractTypeAnnotations', () => {
183187
{ type: 'text', value: ' description follows' },
184188
];
185189

186-
const result = extractTypeAnnotations(nodes, remark);
190+
const result = extractTypeAnnotations(nodes);
187191

188192
assert.strictEqual(result, 'mock-expression');
189193
assert.strictEqual(nodes.length, 1);
@@ -204,7 +208,7 @@ describe('extractTypeAnnotations', () => {
204208
{ type: 'text', value: ' description' },
205209
];
206210

207-
const result = extractTypeAnnotations(nodes, remark);
211+
const result = extractTypeAnnotations(nodes);
208212

209213
assert.strictEqual(result, 'mock-expression');
210214
assert.strictEqual(nodes.length, 1);
@@ -220,7 +224,7 @@ describe('extractTypeAnnotations', () => {
220224
{ type: 'text', value: ' | ' },
221225
];
222226

223-
const result = extractTypeAnnotations(nodes, remark);
227+
const result = extractTypeAnnotations(nodes);
224228

225229
assert.strictEqual(result, 'mock-expression');
226230
assert.strictEqual(nodes.length, 0);
@@ -232,7 +236,7 @@ describe('extractTypeAnnotations', () => {
232236
{ type: 'emphasis', children: [{ type: 'text', value: 'emphasized' }] },
233237
];
234238

235-
const result = extractTypeAnnotations(nodes, remark);
239+
const result = extractTypeAnnotations(nodes);
236240

237241
assert.strictEqual(result, undefined);
238242
assert.strictEqual(nodes.length, 2);
@@ -248,7 +252,7 @@ describe('extractTypeAnnotations', () => {
248252
{ type: 'text', value: ' | ' }, // This shouldn't be consumed
249253
];
250254

251-
const result = extractTypeAnnotations(nodes, remark);
255+
const result = extractTypeAnnotations(nodes);
252256

253257
assert.strictEqual(result, 'mock-expression');
254258
assert.strictEqual(nodes.length, 2);
@@ -259,7 +263,7 @@ describe('extractTypeAnnotations', () => {
259263
it('handles empty nodes array', () => {
260264
const nodes = [];
261265

262-
const result = extractTypeAnnotations(nodes, remark);
266+
const result = extractTypeAnnotations(nodes);
263267

264268
assert.strictEqual(result, undefined);
265269
assert.strictEqual(nodes.length, 0);
@@ -275,7 +279,7 @@ describe('extractTypeAnnotations', () => {
275279
{ type: 'text', value: ' description' },
276280
];
277281

278-
const result = extractTypeAnnotations(nodes, remark);
282+
const result = extractTypeAnnotations(nodes);
279283

280284
assert.strictEqual(result, 'mock-expression');
281285
assert.strictEqual(nodes.length, 1);
@@ -300,7 +304,7 @@ describe('parseListIntoProperties', () => {
300304
],
301305
};
302306

303-
const result = parseListIntoProperties(node, remark);
307+
const result = parseListIntoProperties(node);
304308

305309
assert.deepStrictEqual(result, [
306310
{
@@ -333,7 +337,7 @@ describe('parseListIntoProperties', () => {
333337
],
334338
};
335339

336-
const result = parseListIntoProperties(node, remark);
340+
const result = parseListIntoProperties(node);
337341

338342
assert.deepStrictEqual(result, [
339343
{
@@ -362,7 +366,7 @@ describe('parseListIntoProperties', () => {
362366
],
363367
};
364368

365-
const result = parseListIntoProperties(node, remark);
369+
const result = parseListIntoProperties(node);
366370

367371
assert.deepStrictEqual(result, [
368372
{
@@ -388,7 +392,7 @@ describe('parseListIntoProperties', () => {
388392
],
389393
};
390394

391-
const result = parseListIntoProperties(node, remark);
395+
const result = parseListIntoProperties(node);
392396

393397
assert.deepStrictEqual(result, [
394398
{
@@ -416,7 +420,7 @@ describe('parseListIntoProperties', () => {
416420
],
417421
};
418422

419-
const result = parseListIntoProperties(node, remark);
423+
const result = parseListIntoProperties(node);
420424

421425
assert.deepStrictEqual(result, [
422426
{
@@ -460,7 +464,7 @@ describe('parseListIntoProperties', () => {
460464
],
461465
};
462466

463-
const result = parseListIntoProperties(node, remark);
467+
const result = parseListIntoProperties(node);
464468

465469
assert.deepStrictEqual(result, [
466470
{
@@ -507,7 +511,7 @@ describe('parseListIntoProperties', () => {
507511
],
508512
};
509513

510-
const result = parseListIntoProperties(node, remark);
514+
const result = parseListIntoProperties(node);
511515

512516
assert.deepStrictEqual(result, [
513517
{
@@ -542,7 +546,7 @@ describe('parseListIntoProperties', () => {
542546
],
543547
};
544548

545-
const result = parseListIntoProperties(node, remark);
549+
const result = parseListIntoProperties(node);
546550

547551
assert.deepStrictEqual(result, [
548552
{
@@ -561,7 +565,7 @@ describe('parseListIntoProperties', () => {
561565
children: [],
562566
};
563567

564-
const result = parseListIntoProperties(node, remark);
568+
const result = parseListIntoProperties(node);
565569

566570
assert.deepStrictEqual(result, []);
567571
});
@@ -605,7 +609,7 @@ describe('parseListIntoProperties', () => {
605609
],
606610
};
607611

608-
const result = parseListIntoProperties(node, remark);
612+
const result = parseListIntoProperties(node);
609613
assert.deepStrictEqual(result, [
610614
{
611615
children: [

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

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import {
3030
populate,
3131
} from '../../../utils/configuration/templates.mjs';
3232
import { UNIST } from '../../../utils/queries/index.mjs';
33+
import { getRemarkRecma as remark } from '../../../utils/remark.mjs';
3334

3435
/**
3536
* Processes lifecycle and change history data into a sorted array of change entries.
3637
* @param {import('../../metadata/types').MetadataEntry} entry - The metadata entry
37-
* @param {import('unified').Processor} remark - The remark processor
3838
*/
39-
export const gatherChangeEntries = (entry, remark) => {
39+
export const gatherChangeEntries = entry => {
4040
// Lifecycle changes (e.g., added, deprecated)
4141
const lifecycleChanges = Object.entries(LIFECYCLE_LABELS)
4242
.filter(([field]) => entry[field])
@@ -48,7 +48,8 @@ export const gatherChangeEntries = (entry, remark) => {
4848
// Explicit changes with parsed JSX labels
4949
const explicitChanges = (entry.changes || []).map(change => ({
5050
versions: enforceArray(change.version),
51-
label: remark.runSync(remark.parse(change.description)).body[0].expression,
51+
label: remark().runSync(remark().parse(change.description)).body[0]
52+
.expression,
5253
url: change['pr-url'],
5354
}));
5455

@@ -58,10 +59,9 @@ export const gatherChangeEntries = (entry, remark) => {
5859
/**
5960
* Creates a JSX ChangeHistory element or returns null if no changes.
6061
* @param {import('../../metadata/types').MetadataEntry} entry - The metadata entry
61-
* @param {import('unified').Processor} remark - The remark processor
6262
*/
63-
export const createChangeElement = (entry, remark) => {
64-
const changes = gatherChangeEntries(entry, remark);
63+
export const createChangeElement = entry => {
64+
const changes = gatherChangeEntries(entry);
6565

6666
if (!changes.length) {
6767
return null;
@@ -197,22 +197,15 @@ const getLevelFromDeprecationType = typeText => {
197197
/**
198198
* Transforms a heading node by injecting metadata, source links, and signatures.
199199
* @param {import('../../metadata/types').MetadataEntry} entry - The API metadata entry
200-
* @param {import('unified').Processor} remark - The remark processor
201200
* @param {import('../../metadata/types').HeadingNode} node - The heading node to transform
202201
* @param {number} index - The index of the node in its parent's children array
203202
* @param {import('unist').Parent} parent - The parent node containing the heading
204203
*/
205-
export const transformHeadingNode = async (
206-
entry,
207-
remark,
208-
node,
209-
index,
210-
parent
211-
) => {
204+
export const transformHeadingNode = async (entry, node, index, parent) => {
212205
// Replace heading node with our enhanced heading element
213206
parent.children[index] = createHeadingElement(
214207
node,
215-
createChangeElement(entry, remark)
208+
createChangeElement(entry)
216209
);
217210

218211
if (entry.api === 'deprecations' && node.depth === 3) {
@@ -254,9 +247,8 @@ export const transformHeadingNode = async (
254247
/**
255248
* Processes a single API documentation entry's content
256249
* @param {import('../../metadata/types').MetadataEntry} entry - The API metadata entry to process
257-
* @param {import('unified').Processor} remark - The remark processor
258250
*/
259-
export const processEntry = (entry, remark) => {
251+
export const processEntry = entry => {
260252
// Deep copy content to avoid mutations on original
261253
const content = structuredClone(entry.content);
262254

@@ -265,15 +257,14 @@ export const processEntry = (entry, remark) => {
265257

266258
// Visit and transform headings with metadata and links
267259
visit(content, UNIST.isHeading, (...args) =>
268-
transformHeadingNode(entry, remark, ...args)
260+
transformHeadingNode(entry, ...args)
269261
);
270262

271263
// Transform typed lists into property tables
272264
visit(
273265
content,
274266
UNIST.isStronglyTypedList,
275-
(node, idx, parent) =>
276-
(parent.children[idx] = createSignatureTable(node, remark))
267+
(node, idx, parent) => (parent.children[idx] = createSignatureTable(node))
277268
);
278269

279270
return content;
@@ -284,19 +275,13 @@ export const processEntry = (entry, remark) => {
284275
* @param {Array<import('../../metadata/types').MetadataEntry>} entries - API documentation metadata entries
285276
* @param {ReturnType<import('./buildBarProps.mjs').buildSideBarProps>} sideBarProps - Props for the sidebar component
286277
* @param {ReturnType<buildMetaBarProps>} metaBarProps - Props for the meta bar component
287-
* @param {import('unified').Processor} remark - The remark processor
288278
*/
289-
export const createDocumentLayout = (
290-
entries,
291-
sideBarProps,
292-
metaBarProps,
293-
remark
294-
) =>
279+
export const createDocumentLayout = (entries, sideBarProps, metaBarProps) =>
295280
createTree('root', [
296281
createJSXElement(JSX_IMPORTS.Layout.name, {
297282
sideBarProps,
298283
metaBarProps,
299-
children: entries.map(entry => processEntry(entry, remark)),
284+
children: entries.map(processEntry),
300285
}),
301286
]);
302287

@@ -307,23 +292,21 @@ export const createDocumentLayout = (
307292
* @param {Array<import('../../metadata/types').MetadataEntry>} metadataEntries - API documentation metadata entries
308293
* @param {import('../../metadata/types').MetadataEntry} head - Main API metadata entry with version information
309294
* @param {Object} sideBarProps - Props for the sidebar component
310-
* @param {import('unified').Processor} remark - Remark processor instance for markdown processing
311295
* @returns {Promise<JSXContent>}
312296
*/
313-
const buildContent = async (metadataEntries, head, sideBarProps, remark) => {
297+
const buildContent = async (metadataEntries, head, sideBarProps) => {
314298
// Build props for the MetaBar from head and entries
315299
const metaBarProps = buildMetaBarProps(head, metadataEntries);
316300

317301
// Create root document AST with all layout components and processed content
318302
const root = createDocumentLayout(
319303
metadataEntries,
320304
sideBarProps,
321-
metaBarProps,
322-
remark
305+
metaBarProps
323306
);
324307

325308
// Run remark processor to transform AST (parse markdown, plugins, etc.)
326-
const ast = await remark.run(root);
309+
const ast = await remark().run(root);
327310

328311
// The final MDX content is the expression in the Program's first body node
329312
return { ...ast.body[0].expression, data: head };

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,9 @@ export const insertSignatureCodeBlock = ({ children }, { data }, idx) => {
135135
* Renders a table of properties based on parsed metadata from a Markdown list.
136136
*
137137
* @param {import('mdast').List} node
138-
* @param {import('unified').Processor} remark - The remark processor
139138
*/
140-
export const createSignatureTable = (node, remark) => {
141-
const items = parseListIntoProperties(node, remark);
139+
export const createSignatureTable = node => {
140+
const items = parseListIntoProperties(node);
142141

143142
return createJSXElement(JSX_IMPORTS.FunctionSignature.name, {
144143
title: items.length === 1 && 'kind' in items[0] ? null : 'Attributes',

0 commit comments

Comments
 (0)