Skip to content

Commit 2a4cbe3

Browse files
aduh95avivkeller
andauthored
fix: filter out new keys from legacy JSON generator (#595)
* fix: filter out new keys from legacy JSON generator * Remove TODO comment for additional JSON keys Removed commented TODO note about additional keys in JSON output. * readd TODO * add to legacy json-all generator * Apply suggestion from @aduh95 * Apply suggestion from @aduh95 * move to separate function * squash! * fixup! format * squash! order * fixup! squash! order * squash! only filter and order top-level keys --------- Co-authored-by: Aviv Keller <me@aviv.sh>
1 parent 07a57f8 commit 2a4cbe3

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import { writeFile } from 'node:fs/promises';
44
import { join } from 'node:path';
55

6+
import { legacyToJSON } from '../../utils/generators.mjs';
7+
68
/**
79
* This generator consolidates data from the `legacy-json` generator into a single
810
* JSON file (`all.json`).
@@ -83,10 +85,7 @@ export default {
8385
}
8486

8587
if (output) {
86-
await writeFile(
87-
join(output, 'all.json'),
88-
JSON.stringify(generatedValue, null, 2)
89-
);
88+
await writeFile(join(output, 'all.json'), legacyToJSON(generatedValue));
9089
}
9190

9291
return generatedValue;

src/generators/legacy-json/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { writeFile } from 'node:fs/promises';
44
import { join } from 'node:path';
55

66
import { createSectionBuilder } from './utils/buildSection.mjs';
7-
import { groupNodesByModule } from '../../utils/generators.mjs';
7+
import { groupNodesByModule, legacyToJSON } from '../../utils/generators.mjs';
88

99
const buildSection = createSectionBuilder();
1010

@@ -78,7 +78,7 @@ export default {
7878
for (const section of chunkResult) {
7979
const out = join(output, `${section.api}.json`);
8080

81-
await writeFile(out, JSON.stringify(section, null, 2));
81+
await writeFile(out, legacyToJSON(section));
8282
}
8383
}
8484

src/utils/generators.mjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,63 @@ export const sortChanges = (changes, key = 'version') => {
109109
*/
110110
export const leftHandAssign = (target, source) =>
111111
Object.keys(source).forEach(k => k in target || (target[k] = source[k]));
112+
113+
/**
114+
* Transforms an object to JSON output consistent with the JSON version.
115+
* @param {Object} section - The source object
116+
* @param section.api
117+
* @param section.type
118+
* @param section.source
119+
* @param section.introduced_in
120+
* @param section.meta
121+
* @param section.stability
122+
* @param section.stabilityText
123+
* @param section.classes
124+
* @param section.methods
125+
* @param section.properties
126+
* @param section.miscs
127+
* @param section.modules
128+
* @param section.globals
129+
* @returns {string} - The JSON output
130+
*/
131+
export const legacyToJSON = ({
132+
api,
133+
type,
134+
source,
135+
introduced_in,
136+
meta,
137+
stability,
138+
stabilityText,
139+
classes,
140+
methods,
141+
properties,
142+
miscs,
143+
modules,
144+
globals,
145+
}) =>
146+
JSON.stringify(
147+
{
148+
type,
149+
source,
150+
introduced_in,
151+
...(api === 'report'
152+
? {
153+
stability,
154+
stabilityText,
155+
meta,
156+
}
157+
: {
158+
meta,
159+
stability,
160+
stabilityText,
161+
}),
162+
classes,
163+
methods,
164+
properties,
165+
miscs,
166+
...(api === 'index' ? undefined : { modules }),
167+
globals,
168+
},
169+
null,
170+
2
171+
);

0 commit comments

Comments
 (0)