Skip to content

Commit 63fc86e

Browse files
committed
chore: streamline with legacy
1 parent 1e96be7 commit 63fc86e

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

src/generators/legacy-json-all/index.mjs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,14 @@ export default {
5353
input.forEach(section => {
5454
// Copy the relevant properties from each section into our output
5555
propertiesToCopy.forEach(property => {
56-
if (section[property]) {
57-
generatedValue[property].push(...section[property]);
56+
if (Array.isArray(section[property])) {
57+
let toPush = section[property];
58+
59+
if (section.source) {
60+
toPush = toPush.map(item => ({ ...item, source: section.source }));
61+
}
62+
63+
generatedValue[property].push(...toPush);
5864
}
5965
});
6066
});

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

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,64 @@ export const createSectionBuilder = () => {
3636
/**
3737
* Creates metadata from a hierarchized entry.
3838
* @param {import('../types.d.ts').HierarchizedEntry} entry - The entry to create metadata from.
39-
* @returns {import('../types.d.ts').Meta} The created metadata.
39+
* @returns {import('../types.d.ts').Meta | undefined} The created metadata, or undefined if all fields are empty.
4040
*/
4141
const createMeta = ({
4242
added_in = [],
4343
n_api_version = [],
4444
deprecated_in = [],
4545
removed_in = [],
4646
changes,
47-
}) => ({
48-
changes,
49-
added: enforceArray(added_in),
50-
napiVersion: enforceArray(n_api_version),
51-
deprecated: enforceArray(deprecated_in),
52-
removed: enforceArray(removed_in),
53-
});
47+
}) => {
48+
const meta = { changes };
49+
50+
if (enforceArray(added_in).length) {
51+
meta.added = enforceArray(added_in);
52+
}
53+
54+
if (enforceArray(n_api_version).length) {
55+
meta.napiVersion = enforceArray(n_api_version);
56+
}
57+
58+
if (enforceArray(deprecated_in).length) {
59+
meta.deprecated = enforceArray(deprecated_in);
60+
}
61+
62+
if (enforceArray(removed_in).length) {
63+
meta.removed = enforceArray(removed_in);
64+
}
65+
66+
// Check if there are any non-empty fields in the meta object
67+
const atLeastOneNonEmptyField = Object.values(meta).some(
68+
value => value.length > 0
69+
);
70+
71+
// Return undefined if the meta object is completely empty
72+
return atLeastOneNonEmptyField ? meta : undefined;
73+
};
5474

5575
/**
5676
* Creates a section from an entry and its heading.
5777
* @param {import('../types.d.ts').HierarchizedEntry} entry - The AST entry.
5878
* @param {HeadingMetadataParent} head - The head node of the entry.
5979
* @returns {import('../types.d.ts').Section} The created section.
6080
*/
61-
const createSection = (entry, head) => ({
62-
textRaw: transformNodesToString(head.children),
63-
name: head.data.name,
64-
type: head.data.type,
65-
meta: createMeta(entry),
66-
introduced_in: entry.introduced_in,
67-
});
81+
const createSection = (entry, head) => {
82+
const section = {
83+
textRaw: transformNodesToString(head.children),
84+
name: head.data.name,
85+
type: head.data.type,
86+
introduced_in: entry.introduced_in,
87+
};
88+
89+
const meta = createMeta(entry);
90+
91+
if (meta !== undefined) {
92+
section.meta = meta;
93+
}
94+
95+
return section;
96+
};
6897

6998
/**
7099
* Parses stability metadata and adds it to the section.
@@ -76,7 +105,7 @@ export const createSectionBuilder = () => {
76105
const stabilityInfo = stability.children.map(node => node.data)?.[0];
77106

78107
if (stabilityInfo) {
79-
section.stability = stabilityInfo.index;
108+
section.stability = Number(stabilityInfo.index);
80109
section.stabilityText = stabilityInfo.description;
81110
nodes.shift(); // Remove stability node from processing
82111
}

0 commit comments

Comments
 (0)