From 6182bc12bac387dbed9efa55afb7b9cb3b97b91d Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Tue, 25 Feb 2025 03:13:55 -0800 Subject: [PATCH 1/2] Ensure the property order of the released data matches internal schema --- scripts/build/index.ts | 6 +-- scripts/lib/stringify-and-order-properties.ts | 51 ++++++++++--------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/scripts/build/index.ts b/scripts/build/index.ts index a7862cd98041b5..7676e21898017f 100644 --- a/scripts/build/index.ts +++ b/scripts/build/index.ts @@ -6,7 +6,6 @@ import { relative } from 'node:path'; import { fileURLToPath } from 'node:url'; import esMain from 'es-main'; -import stringify from 'fast-json-stable-stringify'; import { compareVersions } from 'compare-versions'; import { marked } from 'marked'; @@ -16,6 +15,7 @@ import compileTS from '../generate-types.js'; import { walk } from '../../utils/index.js'; import { WalkOutput } from '../../utils/walk.js'; import bcd from '../../index.js'; +import stringifyAndOrderProperties from '../lib/stringify-and-order-properties.js'; import mirrorSupport from './mirror.js'; @@ -195,8 +195,8 @@ export const createDataBundle = async (): Promise => { applyTransforms(bcd); return { - ...bcd, __meta: generateMeta(), + ...bcd, }; }; @@ -208,7 +208,7 @@ export const createDataBundle = async (): Promise => { const writeData = async () => { const dest = new URL('data.json', targetdir); const data = await createDataBundle(); - await fs.writeFile(dest, stringify(data)); + await fs.writeFile(dest, stringifyAndOrderProperties(data, null)); logWrite(dest, 'data'); }; diff --git a/scripts/lib/stringify-and-order-properties.ts b/scripts/lib/stringify-and-order-properties.ts index 725948f9b456e3..773faaba200632 100644 --- a/scripts/lib/stringify-and-order-properties.ts +++ b/scripts/lib/stringify-and-order-properties.ts @@ -82,6 +82,7 @@ const doOrder = (value: T, order: string[]): T => { */ export const stringifyReleases = ( releases: Record, + indent: any, ): string => { const indentStep = ' '; // Constant with the indent step that sortStringify will use @@ -90,7 +91,7 @@ export const stringifyReleases = ( let result = ''; for (let i = 0; i < sortedKeys.length; i++) { const k = sortedKeys[i]; - const v = JSON.stringify(releases[k], null, 2).replace(/\n/g, '\n '); + const v = JSON.stringify(releases[k], null, indent).replace(/\n/g, '\n '); // Add it to the result result += `${indentStep}"${k}": ${v}`; @@ -100,10 +101,12 @@ export const stringifyReleases = ( result += ','; } - // We always need a carriage return - result += '\n'; + // We always need a carriage return unless we're squashing the format + if (indent) result += '\n'; } - return `*#*#{\n${result}}#*#*`; // Close the brace and return the string + return `*#*#{${indent ? '\n' : ''}${result}}#*#*` + .replace(/"/g, '*"*') + .replace(/\\n/g, '*\\n*'); // Close the brace and return the string }; /** @@ -154,23 +157,23 @@ export const orderProperties = (key: string, value: any): any => { // Order properties for browsers if ('browsers' in value) { - const browser = Object.keys(value.browsers)[0]; + for (const browser in value.browsers) { + value.browsers[browser] = doOrder( + value.browsers[browser] as BrowserStatement, + propOrder.browsers.browser, + ); - value.browsers[browser] = doOrder( - value.browsers[browser] as BrowserStatement, - propOrder.browsers.browser, - ); + for (const r of Object.keys(value.browsers[browser].releases)) { + value.browsers[browser].releases[r] = doOrder( + value.browsers[browser].releases[r] as ReleaseStatement, + propOrder.browsers.release, + ); + } - for (const r of Object.keys(value.browsers[browser].releases)) { - value.browsers[browser].releases[r] = doOrder( - value.browsers[browser].releases[r] as ReleaseStatement, - propOrder.browsers.release, + value.browsers[browser].releases = stringifyReleases( + value.browsers[browser].releases, ); } - - value.browsers[browser].releases = stringifyReleases( - value.browsers[browser].releases, - ); } } return value; @@ -181,7 +184,7 @@ export const orderProperties = (key: string, value: any): any => { * @param rawdata The object to stringify * @returns The stringified object */ -const stringifyAndOrderProperties = (rawdata: any): string => { +const stringifyAndOrderProperties = (rawdata: any, indent: any = 2): string => { if (rawdata instanceof Object) { rawdata = JSON.stringify(rawdata); } @@ -189,14 +192,14 @@ const stringifyAndOrderProperties = (rawdata: any): string => { if ('browsers' in data) { // Browser data needs to be stringified in a special way due to the release data - return JSON.stringify(data, null, 2) - .replace('"*#*#', '') - .replace('#*#*"', '') - .replace(/\\n/g, '\n ') - .replace(/\\"/g, '"'); + return JSON.stringify(data, null, indent) + .replace(/"\*#\*#/g, '') + .replace(/#\*#\*"/g, '') + .replace(/\*\\n\*/g, '\n') + .replace(/\*\\"\*/g, '"'); } - return JSON.stringify(data, null, 2); + return JSON.stringify(data, null, indent); }; export default stringifyAndOrderProperties; From ca5afb02ef8d1c9ab8d79a3c0a77cef9664ee300 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Tue, 25 Feb 2025 03:22:22 -0800 Subject: [PATCH 2/2] Fix ESLint reported issues --- scripts/build/index.ts | 2 +- scripts/lib/stringify-and-order-properties.ts | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts/build/index.ts b/scripts/build/index.ts index 7676e21898017f..fc24d4a54e46a5 100644 --- a/scripts/build/index.ts +++ b/scripts/build/index.ts @@ -195,8 +195,8 @@ export const createDataBundle = async (): Promise => { applyTransforms(bcd); return { - __meta: generateMeta(), ...bcd, + __meta: generateMeta(), }; }; diff --git a/scripts/lib/stringify-and-order-properties.ts b/scripts/lib/stringify-and-order-properties.ts index 773faaba200632..3b119961ba57be 100644 --- a/scripts/lib/stringify-and-order-properties.ts +++ b/scripts/lib/stringify-and-order-properties.ts @@ -102,7 +102,9 @@ export const stringifyReleases = ( } // We always need a carriage return unless we're squashing the format - if (indent) result += '\n'; + if (indent) { + result += '\n'; + } } return `*#*#{${indent ? '\n' : ''}${result}}#*#*` .replace(/"/g, '*"*') @@ -118,7 +120,7 @@ export const stringifyReleases = ( * @param value The value of the key * @returns The new value */ -export const orderProperties = (key: string, value: any): any => { +export const orderProperties = (key: string, value: any, indent: any): any => { if (value instanceof Object) { // Order properties for data if ('__compat' in value) { @@ -172,6 +174,7 @@ export const orderProperties = (key: string, value: any): any => { value.browsers[browser].releases = stringifyReleases( value.browsers[browser].releases, + indent, ); } } @@ -188,7 +191,7 @@ const stringifyAndOrderProperties = (rawdata: any, indent: any = 2): string => { if (rawdata instanceof Object) { rawdata = JSON.stringify(rawdata); } - const data = JSON.parse(rawdata, orderProperties); + const data = JSON.parse(rawdata, (k, v) => orderProperties(k, v, indent)); if ('browsers' in data) { // Browser data needs to be stringified in a special way due to the release data