Skip to content

Commit c691b6d

Browse files
authored
[PLUTO-1431] Fix json fields ordering (#147)
1 parent 5e83261 commit c691b6d

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

integration-tests/run.ps1

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,30 @@ function Normalize-EslintConfig {
137137
$output += $line
138138
$inRules = $true
139139
} elseif ($inRules -and $line -match '^\s*\}') {
140-
# Sort collected rule lines (since expected file already has properties sorted)
140+
# Sort collected rule lines
141141
$output += ($ruleLines | Sort-Object)
142142
$ruleLines = @()
143143
$inRules = $false
144144
$output += $line
145145
} elseif ($inRules) {
146+
# Normalize JSON object properties within rule configurations
147+
$normalizedLine = $line
148+
if ($line -match '\{[^}]*\}') {
149+
# Extract and sort JSON object properties
150+
if ($line -match '(\{[^}]*\})') {
151+
$jsonObject = $matches[1]
152+
# Remove braces and split by comma
153+
$content = $jsonObject.Trim('{}').Trim()
154+
if ($content) {
155+
$parts = $content -split ',\s*'
156+
$sortedParts = $parts | Sort-Object
157+
$newObject = "{$($sortedParts -join ', ')}"
158+
$normalizedLine = $line -replace [regex]::Escape($jsonObject), $newObject
159+
}
160+
}
161+
}
146162
# Collect rule lines for sorting
147-
$ruleLines += $line
163+
$ruleLines += $normalizedLine
148164
} else {
149165
$output += $line
150166
}

integration-tests/run.sh

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ normalize_yaml_config() {
152152
# Normalize ESLint configuration files (.mjs/.js)
153153
normalize_eslint_config() {
154154
local file=$1
155-
# Sort the rule lines within the rules object for consistent comparison
155+
# Sort the rule lines within the rules object and normalize JSON object properties
156156
awk '
157157
/rules: \{/ {
158158
print;
@@ -171,8 +171,49 @@ normalize_eslint_config() {
171171
next
172172
}
173173
inRules {
174+
# Normalize JSON object properties within rule configurations
175+
line = $0
176+
# Look for JSON objects like {"key1": value1, "key2": value2}
177+
if (match(line, /\{[^}]*\}/)) {
178+
# Extract the JSON object
179+
obj_start = RSTART
180+
obj_len = RLENGTH
181+
before = substr(line, 1, obj_start-1)
182+
obj = substr(line, obj_start, obj_len)
183+
after = substr(line, obj_start+obj_len)
184+
185+
# Parse and sort the object properties
186+
if (match(obj, /^\{.*\}$/)) {
187+
# Remove braces and split by comma
188+
content = substr(obj, 2, length(obj)-2)
189+
gsub(/^\s+|\s+$/, "", content) # trim spaces
190+
191+
if (content != "") {
192+
# Split by comma (simple approach)
193+
n = split(content, parts, /,\s*/)
194+
# Sort the parts
195+
for (i = 1; i <= n; i++) {
196+
for (j = i+1; j <= n; j++) {
197+
if (parts[i] > parts[j]) {
198+
temp = parts[i]
199+
parts[i] = parts[j]
200+
parts[j] = temp
201+
}
202+
}
203+
}
204+
# Reconstruct the object
205+
new_obj = "{"
206+
for (i = 1; i <= n; i++) {
207+
if (i > 1) new_obj = new_obj ", "
208+
new_obj = new_obj parts[i]
209+
}
210+
new_obj = new_obj "}"
211+
line = before new_obj after
212+
}
213+
}
214+
}
174215
# Collect rule lines for sorting
175-
rules[NR] = $0
216+
rules[NR] = line
176217
next
177218
}
178219
{ print }

0 commit comments

Comments
 (0)