UNPKG

@maniascript/mslint

Version:
38 lines (37 loc) 1.28 kB
import {} from '../linter/rule.js'; import { ArrayExpression } from '@maniascript/parser'; import { equalLiterals, getNodeText } from '../linter/ast-utils.js'; function keyAlreadyExists(key, previousKeys) { for (const previousKey of previousKeys) { if (equalLiterals(key, previousKey)) { return true; } } return false; } export const noDupeKeys = { meta: { id: 'no-dupe-keys', description: 'Forbid duplicate keys in associative arrays', recommended: true }, create(context) { return { 'ArrayExpression:exit': (node) => { if (node instanceof ArrayExpression && node.isAssociative && node.values.length > 1) { const keys = new Set(); for (const { key } of node.values) { if (key !== undefined) { if (keyAlreadyExists(key, keys)) { context.report(key, `Duplicate key '${getNodeText(key, context.tokens)}'`); } else { keys.add(key); } } } } } }; } };