MCPcopy Index your code
hub / github.com/codeaashu/claude-code / checkDuplicateKeysInJson

Function checkDuplicateKeysInJson

src/keybindings/validate.ts:258–307  ·  view source on GitHub ↗
(
  jsonString: string,
)

Source from the content-addressed store, hash-verified

256 * and "enter" in Confirmation).
257 */
258export 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.

Callers 2

loadKeybindingsFunction · 0.85

Calls 3

getMethod · 0.65
setMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected