@@ -71,63 +71,30 @@ function Normalize-Config {
7171 $output
7272 }
7373 { $_ -in @ (' mjs' , ' js' ) } {
74- # For JavaScript config files (like ESLint), use Node.js to parse and sort JSON objects
75- if (Get-Command node - ErrorAction SilentlyContinue) {
76- $tempScript = @"
77- const fs = require('fs');
78- let content = fs.readFileSync('$file ', 'utf8');
79-
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- }
111- }
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;
122- };
123-
124- console.log(sortRulesObject(content));
125- "@
126- $tempScript | node
127- } else {
128- # Fallback if Node.js is not available
129- Get-Content $file
74+ # For JavaScript config files (like ESLint), sort the rule lines within the rules object
75+ $content = Get-Content $file
76+ $output = @ ()
77+ $inRules = $false
78+ $ruleLines = @ ()
79+
80+ foreach ($line in $content ) {
81+ if ($line -match ' rules: \{' ) {
82+ $output += $line
83+ $inRules = $true
84+ } elseif ($inRules -and $line -match ' ^\s*\}' ) {
85+ # Sort collected rule lines and add them
86+ $output += ($ruleLines | Sort-Object )
87+ $ruleLines = @ ()
88+ $inRules = $false
89+ $output += $line
90+ } elseif ($inRules ) {
91+ # Collect rule lines for sorting
92+ $ruleLines += $line
93+ } else {
94+ $output += $line
95+ }
13096 }
97+ $output
13198 }
13299 { $_ -in @ (' rc' , ' conf' , ' ini' , ' xml' ) } {
133100 Get-Content $file | ForEach-Object {
0 commit comments