Skip to content

Commit 34204d7

Browse files
committed
code review
1 parent a002cc3 commit 34204d7

File tree

4 files changed

+53
-49
lines changed

4 files changed

+53
-49
lines changed

src/generators/jsx/utils/ast.mjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,5 @@ function createAttributeNode(name, value) {
6767
}
6868

6969
// For primitives, use simple string conversion
70-
return createTree('mdxJsxAttribute', {
71-
name,
72-
value: String(value),
73-
});
70+
return createTree('mdxJsxAttribute', { name, value: String(value) });
7471
}

src/generators/jsx/utils/buildBarProps.mjs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const buildSideBarDocPages = (groupedModules, headNodes) =>
2727
* @param {ApiDocMetadataEntry} head - Main API metadata entry
2828
* @param {Array<ApiDocMetadataEntry>} entries - All API metadata entries
2929
*/
30-
export function buildMetaBarProps(head, entries) {
30+
export const buildMetaBarProps = (head, entries) => {
3131
// Extract text content for reading time calculation
3232
let textContent = '';
3333
entries.forEach(entry => {
@@ -36,16 +36,18 @@ export function buildMetaBarProps(head, entries) {
3636
});
3737
});
3838

39+
const headings = entries
40+
.filter(entry => entry.heading?.data?.name)
41+
.map(entry => ({
42+
depth: entry.heading.depth,
43+
value: entry.heading.data.name,
44+
}));
45+
3946
return {
40-
headings: entries
41-
.filter(entry => entry.heading?.data?.name)
42-
.map(entry => ({
43-
depth: entry.heading.depth,
44-
value: entry.heading.data.name,
45-
})),
47+
headings,
4648
addedIn: head.introduced_in || head.added_in || '',
4749
readingTime: readingTime(textContent).text,
4850
viewAs: [['JSON', `${head.api}.json`]],
4951
editThisPage: `${DOC_API_BLOB_EDIT_BASE_URL}${head.api}.md`,
5052
};
51-
}
53+
};

src/generators/jsx/utils/buildContent.mjs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,34 @@ import { buildMetaBarProps } from './buildBarProps.mjs';
2323
* @param {import('unist').Parent} parent - The parent node containing the stability node
2424
* @returns {[typeof SKIP]} Visitor instruction to skip the node
2525
*/
26-
function transformStabilityNode(node, index, parent) {
27-
const { data } = node;
26+
const transformStabilityNode = ({ data }, index, parent) => {
2827
parent.children[index] = createJSXElement('AlertBox', {
2928
children: data.description,
3029
level: STABILITY_LEVELS[data.index],
3130
title: data.index,
3231
});
32+
3333
return [SKIP];
34-
}
34+
};
3535

3636
/**
3737
* Creates a history of changes for an API element
3838
*
3939
* @param {ApiDocMetadataEntry} entry - The metadata entry containing change information
4040
* @returns {import('unist').Node|null} JSX element representing change history or null if no changes
4141
*/
42-
function createChangeElement(entry) {
42+
const createChangeElement = entry => {
4343
// Collect changes from version fields (added, deprecated, etc.)
4444
const changeEntries = Object.entries(CHANGE_TYPES)
45+
// Do we have this field?
4546
.filter(([field]) => entry[field])
46-
.map(([field, label]) => {
47-
const versions = enforceArray(entry[field]);
48-
return {
49-
versions,
50-
label: `${label}: ${versions.join(', ')}`,
51-
};
52-
});
47+
// Get the versions as an array
48+
.map(([field, label]) => [enforceArray(entry[field]), label])
49+
// Create the change entry
50+
.map(([versions, label]) => ({
51+
versions,
52+
label: `${label}: ${versions.join(', ')}`,
53+
}));
5354

5455
// Add explicit changes if they exist
5556
if (entry.changes?.length) {
@@ -61,22 +62,26 @@ function createChangeElement(entry) {
6162
changeEntries.push(...explicitChanges);
6263
}
6364

64-
if (!changeEntries.length) return null;
65+
if (!changeEntries.length) {
66+
return null;
67+
}
6568

6669
// Sort by version, newest first and create the JSX element
6770
return createJSXElement('ChangeHistory', {
6871
changes: sortChanges(changeEntries, 'versions'),
6972
});
70-
}
73+
};
7174

7275
/**
7376
* Creates a source link element if a source link is available
7477
*
7578
* @param {string|undefined} sourceLink - The source link path
7679
* @returns {import('hastscript').Element|null} The source link element or null if no source link
7780
*/
78-
function createSourceLink(sourceLink) {
79-
if (!sourceLink) return null;
81+
const createSourceLink = sourceLink => {
82+
if (!sourceLink) {
83+
return null;
84+
}
8085

8186
return createElement('span', [
8287
'Source Code: ',
@@ -86,7 +91,7 @@ function createSourceLink(sourceLink) {
8691
sourceLink
8792
),
8893
]);
89-
}
94+
};
9095

9196
/**
9297
* Enhances a heading node with metadata, source links, and styling
@@ -97,7 +102,7 @@ function createSourceLink(sourceLink) {
97102
* @param {import('unist').Parent} parent - The parent node containing the heading
98103
* @returns {[typeof SKIP]} Visitor instruction to skip the node
99104
*/
100-
function transformHeadingNode(entry, node, index, parent) {
105+
const transformHeadingNode = (entry, node, index, parent) => {
101106
const { data, children } = node;
102107
const headerChildren = [
103108
createElement(`h${data.depth + 1}`, [
@@ -127,15 +132,15 @@ function transformHeadingNode(entry, node, index, parent) {
127132
}
128133

129134
return [SKIP];
130-
}
135+
};
131136

132137
/**
133138
* Processes an API documentation entry by applying transformations to its content
134139
*
135140
* @param {ApiDocMetadataEntry} entry - The API metadata entry to process
136141
* @returns {import('unist').Node} The processed content
137142
*/
138-
function processEntry(entry) {
143+
const processEntry = entry => {
139144
// Create a copy to avoid modifying the original
140145
const content = structuredClone(entry.content);
141146

@@ -146,7 +151,7 @@ function processEntry(entry) {
146151
);
147152

148153
return content;
149-
}
154+
};
150155

151156
/**
152157
* Creates the overall content structure with processed entries
@@ -156,7 +161,7 @@ function processEntry(entry) {
156161
* @param {Object} metaBarProps - Props for the meta bar component
157162
* @returns {import('unist').Node} The root node of the content tree
158163
*/
159-
function createContentStructure(entries, sideBarProps, metaBarProps) {
164+
const createContentStructure = (entries, sideBarProps, metaBarProps) => {
160165
return createTree('root', [
161166
createJSXElement('NavBar'),
162167
createJSXElement('Article', {
@@ -170,7 +175,7 @@ function createContentStructure(entries, sideBarProps, metaBarProps) {
170175
],
171176
}),
172177
]);
173-
}
178+
};
174179

175180
/**
176181
* Transforms API metadata entries into processed MDX content
@@ -181,12 +186,7 @@ function createContentStructure(entries, sideBarProps, metaBarProps) {
181186
* @param {import('unified').Processor} remark - Remark processor instance for markdown processing
182187
* @returns {string} The stringified MDX content
183188
*/
184-
export default function buildContent(
185-
metadataEntries,
186-
head,
187-
sideBarProps,
188-
remark
189-
) {
189+
const buildContent = (metadataEntries, head, sideBarProps, remark) => {
190190
const metaBarProps = buildMetaBarProps(head, metadataEntries);
191191

192192
const root = createContentStructure(
@@ -196,4 +196,6 @@ export default function buildContent(
196196
);
197197

198198
return remark.runSync(root);
199-
}
199+
};
200+
201+
export default buildContent;

src/generators/jsx/utils/transformer.mjs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ import { TAG_TRANSFORMS } from '../constants.mjs';
88
* @returns {Object} The transformed tree (modified in place)
99
*/
1010
export default () => tree => {
11-
visit(tree, 'raw', node => {
12-
node.type = 'text';
13-
});
14-
15-
visit(tree, 'element', node => {
16-
const replacement = TAG_TRANSFORMS[node.tagName];
17-
if (replacement) {
18-
node.tagName = replacement;
11+
visit(tree, ['raw', 'element'], node => {
12+
// TODO(@avivkeller): Our parsers shouldn't return raw nodes
13+
// When they mistake "<Type>" for an HTML node, rather than
14+
// the string type that it is.
15+
if (node.type === 'raw') {
16+
node.type = 'text';
17+
} else {
18+
const replacement = TAG_TRANSFORMS[node.tagName];
19+
if (replacement) {
20+
node.tagName = replacement;
21+
}
1922
}
2023
});
2124
};

0 commit comments

Comments
 (0)