Skip to content

Commit 5fd237f

Browse files
committed
[PLUTO-1431] sort eslint rules
1 parent f3da737 commit 5fd237f

File tree

2 files changed

+86
-22
lines changed

2 files changed

+86
-22
lines changed

integration-tests/run.ps1

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,53 @@ function Normalize-Config {
7575
if (Get-Command node -ErrorAction SilentlyContinue) {
7676
$tempScript = @"
7777
const fs = require('fs');
78-
const content = fs.readFileSync('$file', 'utf8');
78+
let content = fs.readFileSync('$file', 'utf8');
7979
80-
// Simple approach: sort object properties in JSON-like structures
81-
const sortObjectProperties = (str) => {
82-
return str.replace(/\{[^{}]*\}/g, (match) => {
83-
if (match.includes(':')) {
84-
// Extract properties and sort them
85-
const props = match.slice(1, -1).split(',').map(p => p.trim()).sort();
86-
return '{' + props.join(', ') + '}';
80+
// More sophisticated approach: find and sort the rules object specifically
81+
const sortRulesObject = (str) => {
82+
// Find the rules object
83+
const rulesMatch = str.match(/rules:\s*\{([^}]+(?:\{[^}]*\}[^}]*)*)\}/);
84+
if (rulesMatch) {
85+
const rulesContent = rulesMatch[1];
86+
87+
// Split on commas but be careful about nested objects
88+
const rules = [];
89+
let current = '';
90+
let depth = 0;
91+
let inString = false;
92+
let stringChar = '';
93+
94+
for (let i = 0; i < rulesContent.length; i++) {
95+
const char = rulesContent[i];
96+
current += char;
97+
98+
if (!inString && (char === '\"' || char === \"'\")) {
99+
inString = true;
100+
stringChar = char;
101+
} else if (inString && char === stringChar && rulesContent[i-1] !== '\\\\') {
102+
inString = false;
103+
} else if (!inString) {
104+
if (char === '{') depth++;
105+
else if (char === '}') depth--;
106+
else if (char === ',' && depth === 0) {
107+
rules.push(current.slice(0, -1).trim());
108+
current = '';
109+
}
110+
}
87111
}
88-
return match;
89-
});
112+
if (current.trim()) rules.push(current.trim());
113+
114+
// Sort the rules
115+
rules.sort();
116+
117+
// Reconstruct
118+
const sortedRules = 'rules: {\\n' + rules.map(rule => ' ' + rule).join(',\\n') + '\\n }';
119+
return str.replace(/rules:\s*\{([^}]+(?:\{[^}]*\}[^}]*)*)\}/, sortedRules);
120+
}
121+
return str;
90122
};
91123
92-
console.log(sortObjectProperties(content));
124+
console.log(sortRulesObject(content));
93125
"@
94126
$tempScript | node
95127
} else {

integration-tests/run.sh

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,53 @@ normalize_config() {
2828
if command -v node >/dev/null 2>&1; then
2929
node -e "
3030
const fs = require('fs');
31-
const content = fs.readFileSync('$file', 'utf8');
31+
let content = fs.readFileSync('$file', 'utf8');
3232
33-
// Simple approach: sort object properties in JSON-like structures
34-
const sortObjectProperties = (str) => {
35-
return str.replace(/\{[^{}]*\}/g, (match) => {
36-
if (match.includes(':')) {
37-
// Extract properties and sort them
38-
const props = match.slice(1, -1).split(',').map(p => p.trim()).sort();
39-
return '{' + props.join(', ') + '}';
33+
// More sophisticated approach: find and sort the rules object specifically
34+
const sortRulesObject = (str) => {
35+
// Find the rules object
36+
const rulesMatch = str.match(/rules:\s*\{([^}]+(?:\{[^}]*\}[^}]*)*)\}/);
37+
if (rulesMatch) {
38+
const rulesContent = rulesMatch[1];
39+
40+
// Split on commas but be careful about nested objects
41+
const rules = [];
42+
let current = '';
43+
let depth = 0;
44+
let inString = false;
45+
let stringChar = '';
46+
47+
for (let i = 0; i < rulesContent.length; i++) {
48+
const char = rulesContent[i];
49+
current += char;
50+
51+
if (!inString && (char === '\"' || char === \"'\")) {
52+
inString = true;
53+
stringChar = char;
54+
} else if (inString && char === stringChar && rulesContent[i-1] !== '\\\\') {
55+
inString = false;
56+
} else if (!inString) {
57+
if (char === '{') depth++;
58+
else if (char === '}') depth--;
59+
else if (char === ',' && depth === 0) {
60+
rules.push(current.slice(0, -1).trim());
61+
current = '';
62+
}
63+
}
4064
}
41-
return match;
42-
});
65+
if (current.trim()) rules.push(current.trim());
66+
67+
// Sort the rules
68+
rules.sort();
69+
70+
// Reconstruct
71+
const sortedRules = 'rules: {\\n' + rules.map(rule => ' ' + rule).join(',\\n') + '\\n }';
72+
return str.replace(/rules:\s*\{([^}]+(?:\{[^}]*\}[^}]*)*)\}/, sortedRules);
73+
}
74+
return str;
4375
};
4476
45-
console.log(sortObjectProperties(content));
77+
console.log(sortRulesObject(content));
4678
"
4779
else
4880
# Fallback if Node.js is not available

0 commit comments

Comments
 (0)