( jsonString: string, )
| 256 | * and "enter" in Confirmation). |
| 257 | */ |
| 258 | export function checkDuplicateKeysInJson( |
| 259 | jsonString: string, |
| 260 | ): KeybindingWarning[] { |
| 261 | const warnings: KeybindingWarning[] = [] |
| 262 | |
| 263 | // Find each "bindings" block and check for duplicates within it |
| 264 | // Pattern: "bindings" : { ... } |
| 265 | const bindingsBlockPattern = |
| 266 | /"bindings"\s*:\s*\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}/g |
| 267 | |
| 268 | let blockMatch |
| 269 | while ((blockMatch = bindingsBlockPattern.exec(jsonString)) !== null) { |
| 270 | const blockContent = blockMatch[1] |
| 271 | if (!blockContent) continue |
| 272 | |
| 273 | // Find the context for this block by looking backwards |
| 274 | const textBeforeBlock = jsonString.slice(0, blockMatch.index) |
| 275 | const contextMatch = textBeforeBlock.match( |
| 276 | /"context"\s*:\s*"([^"]+)"[^{]*$/, |
| 277 | ) |
| 278 | const context = contextMatch?.[1] ?? 'unknown' |
| 279 | |
| 280 | // Find all keys within this bindings block |
| 281 | const keyPattern = /"([^"]+)"\s*:/g |
| 282 | const keysByName = new Map<string, number>() |
| 283 | |
| 284 | let keyMatch |
| 285 | while ((keyMatch = keyPattern.exec(blockContent)) !== null) { |
| 286 | const key = keyMatch[1] |
| 287 | if (!key) continue |
| 288 | |
| 289 | const count = (keysByName.get(key) ?? 0) + 1 |
| 290 | keysByName.set(key, count) |
| 291 | |
| 292 | if (count === 2) { |
| 293 | // Only warn on the second occurrence |
| 294 | warnings.push({ |
| 295 | type: 'duplicate', |
| 296 | severity: 'warning', |
| 297 | message: `Duplicate key "${key}" in ${context} bindings`, |
| 298 | key, |
| 299 | context, |
| 300 | suggestion: `This key appears multiple times in the same context. JSON uses the last value, earlier values are ignored.`, |
| 301 | }) |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return warnings |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Validate user keybinding config and return all warnings. |
no test coverage detected