Skip to content

Commit 89bd624

Browse files
authored
chore: streamline with legacy (#506)
* chore: streamline with legacy * chore: code review * chore: code review * chore: code review
1 parent 1e96be7 commit 89bd624

File tree

4 files changed

+81
-47
lines changed

4 files changed

+81
-47
lines changed

npm-shrinkwrap.json

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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+
const items = section[property];
57+
58+
if (Array.isArray(items)) {
59+
const enrichedItems = section.source
60+
? items.map(item => ({ ...item, source: section.source }))
61+
: items;
62+
63+
generatedValue[property].push(...enrichedItems);
5864
}
5965
});
6066
});

src/generators/legacy-json/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export interface SectionBase {
7878
/**
7979
* Stability index of the section.
8080
*/
81-
stability?: string;
81+
stability?: number;
8282

8383
/**
8484
* Descriptive text related to the stability of the section (E.G. "Experimental").

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

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,63 @@ 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 (added_in?.length) {
51+
meta.added = enforceArray(added_in);
52+
}
53+
54+
if (n_api_version?.length) {
55+
meta.napiVersion = enforceArray(n_api_version);
56+
}
57+
58+
if (deprecated_in?.length) {
59+
meta.deprecated = enforceArray(deprecated_in);
60+
}
61+
62+
if (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 =
68+
changes?.length || Object.keys(meta).length > 1;
69+
70+
// Return undefined if the meta object is completely empty
71+
return atLeastOneNonEmptyField ? meta : undefined;
72+
};
5473

5574
/**
5675
* Creates a section from an entry and its heading.
5776
* @param {import('../types.d.ts').HierarchizedEntry} entry - The AST entry.
5877
* @param {HeadingMetadataParent} head - The head node of the entry.
5978
* @returns {import('../types.d.ts').Section} The created section.
6079
*/
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-
});
80+
const createSection = (entry, head) => {
81+
const section = {
82+
textRaw: transformNodesToString(head.children),
83+
name: head.data.name,
84+
type: head.data.type,
85+
introduced_in: entry.introduced_in,
86+
};
87+
88+
const meta = createMeta(entry);
89+
90+
if (meta !== undefined) {
91+
section.meta = meta;
92+
}
93+
94+
return section;
95+
};
6896

6997
/**
7098
* Parses stability metadata and adds it to the section.
@@ -76,7 +104,7 @@ export const createSectionBuilder = () => {
76104
const stabilityInfo = stability.children.map(node => node.data)?.[0];
77105

78106
if (stabilityInfo) {
79-
section.stability = stabilityInfo.index;
107+
section.stability = Number(stabilityInfo.index);
80108
section.stabilityText = stabilityInfo.description;
81109
nodes.shift(); // Remove stability node from processing
82110
}

0 commit comments

Comments
 (0)