Skip to content

Commit daaeb49

Browse files
committed
feat: remove comment-json dependency and enhance sorter functionality
1 parent 7cae40f commit daaeb49

File tree

3 files changed

+53
-94
lines changed

3 files changed

+53
-94
lines changed

package-lock.json

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

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"@webref/events": "^1.11.3",
5151
"@webref/idl": "^3.46.1",
5252
"bcd-idl-mapper": "^2.3.0",
53-
"comment-json": "^4.2.5",
5453
"cpx2": "^8.0.0",
5554
"danger": "^12.2.0",
5655
"eslint": "^9.9.0",

src/build/sorter.ts

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,67 @@
1-
import * as fs from "fs";
2-
import commentJson from "comment-json";
1+
import { readFileSync } from "fs";
2+
import { parse, ParseError, visit } from "jsonc-parser";
3+
import { URL } from "url";
34

4-
// A helper function to recursively sort object keys alphabetically.
5+
// Recursively sort object keys
56
function sortObjectKeys<T>(obj: T): T {
67
if (obj && typeof obj === "object" && !Array.isArray(obj)) {
7-
const sorted = {} as any;
8-
Object.keys(obj)
9-
.sort()
10-
.forEach((key) => {
11-
// Recursively sort keys if the value is an object
12-
sorted[key] = sortObjectKeys((obj as any)[key]);
13-
});
14-
return sorted;
8+
const sorted: Record<string, unknown> = {};
9+
for (const key of Object.keys(obj).sort()) {
10+
sorted[key] = sortObjectKeys((obj as any)[key]);
11+
}
12+
return sorted as T;
1513
}
1614
return obj;
1715
}
1816

19-
export function sortFiles() {
20-
const files = ["overridingTypes", "removedTypes", "addedTypes"];
21-
files.forEach((file) => {
22-
const filePath = new URL(`../../inputfiles/${file}.jsonc`, import.meta.url); // Replace with your JSONC file path
17+
// Check if the content has trailing commas
18+
function hasTrailingCommas(content: string): boolean {
19+
let foundTrailingComma = false;
2320

24-
// Read the JSONC file content
25-
const fileContent = fs.readFileSync(filePath, "utf-8");
21+
visit(content, {
22+
onObjectEnd: (offset) => {
23+
const lastChar = content[offset - 1];
24+
if (lastChar === ",") {
25+
foundTrailingComma = true;
26+
}
27+
},
28+
onArrayEnd: (offset) => {
29+
const lastChar = content[offset - 1];
30+
if (lastChar === ",") {
31+
foundTrailingComma = true;
32+
}
33+
},
34+
});
35+
36+
return foundTrailingComma;
37+
}
2638

27-
// Parse the JSONC file while preserving comments
28-
const parsed = commentJson.parse(fileContent, undefined, true);
39+
export function sortFiles(): void {
40+
const filenames = ["overridingTypes", "removedTypes", "addedTypes"];
2941

30-
// Sort the object keys alphabetically (recursively)
31-
const sortedObject = sortObjectKeys(parsed);
42+
for (const name of filenames) {
43+
const filePath = new URL(`../../inputfiles/${name}.jsonc`, import.meta.url);
44+
const content = readFileSync(filePath, "utf-8");
3245

33-
// Stringify the sorted object back to JSONC format (preserving comments)
34-
const sortedJsonC = commentJson.stringify(sortedObject, null, 2);
46+
const errors: ParseError[] = [];
47+
const parsed = parse(content, errors, { allowTrailingComma: true });
3548

36-
// Write the sorted content back to the file
37-
fs.writeFileSync(filePath, sortedJsonC, "utf-8");
38-
});
49+
if (errors.length > 0) {
50+
throw new Error(`❌ Syntax error(s) found in ${name}.jsonc:`);
51+
}
3952

40-
console.log("JSONC file keys sorted successfully.");
53+
if (hasTrailingCommas(content)) {
54+
throw new Error(`❌ Trailing comma detected in ${name}.jsonc`);
55+
}
56+
57+
const sorted = sortObjectKeys(parsed);
58+
const originalStr = JSON.stringify(parsed);
59+
const sortedStr = JSON.stringify(sorted);
60+
61+
if (originalStr !== sortedStr) {
62+
throw new Error(`❌ Keys are not sorted in ${name}.jsonc`);
63+
}
64+
65+
console.log(`✅ ${name}.jsonc is valid, sorted, and clean.`);
66+
}
4167
}

0 commit comments

Comments
 (0)