import { ParserRule, BaseAutofixContext } from "../types.js" import { ControlFlowTrackingVisitor, ControlFlowType } from "../utils/rule-utils.js" import { getAttributeName } from "@herb-tools/core" import type { UnboundLintOffense, LintContext, FullRuleConfig } from "../types.js" import type { HTMLOpenTagNode, HTMLAttributeNode, ParseResult, Location, ERBOpenTagNode, ParserOptions } from "@herb-tools/core" interface ControlFlowState { previousBranchAttributes: Set previousControlFlowAttributes: Set } interface BranchState { previousBranchAttributes: Set } class NoDuplicateAttributesVisitor extends ControlFlowTrackingVisitor< BaseAutofixContext, ControlFlowState, BranchState > { private tagAttributes = new Set() private currentBranchAttributes = new Set() private controlFlowAttributes = new Set() visitHTMLOpenTagNode(node: HTMLOpenTagNode): void { this.resetAttributeSets() super.visitHTMLOpenTagNode(node) } visitERBOpenTagNode(node: ERBOpenTagNode): void { this.resetAttributeSets() super.visitERBOpenTagNode(node) } private resetAttributeSets(): void { this.tagAttributes = new Set() this.currentBranchAttributes = new Set() this.controlFlowAttributes = new Set() } visitHTMLAttributeNode(node: HTMLAttributeNode): void { this.checkAttribute(node) } protected onEnterControlFlow(_controlFlowType: ControlFlowType, wasAlreadyInControlFlow: boolean): ControlFlowState { const stateToRestore: ControlFlowState = { previousBranchAttributes: this.currentBranchAttributes, previousControlFlowAttributes: this.controlFlowAttributes, } this.currentBranchAttributes = new Set() if (!wasAlreadyInControlFlow) { this.controlFlowAttributes = new Set() } return stateToRestore } protected onExitControlFlow( controlFlowType: ControlFlowType, wasAlreadyInControlFlow: boolean, stateToRestore: ControlFlowState, ): void { if (controlFlowType === ControlFlowType.CONDITIONAL && !wasAlreadyInControlFlow) { this.controlFlowAttributes.forEach((attr) => this.tagAttributes.add(attr)) } this.currentBranchAttributes = stateToRestore.previousBranchAttributes this.controlFlowAttributes = stateToRestore.previousControlFlowAttributes } protected onEnterBranch(): BranchState { const stateToRestore: BranchState = { previousBranchAttributes: this.currentBranchAttributes, } if (this.isInControlFlow) { this.currentBranchAttributes = new Set() } return stateToRestore } protected onExitBranch(_stateToRestore: BranchState): void {} private checkAttribute(attributeNode: HTMLAttributeNode): void { const identifier = getAttributeName(attributeNode) if (!identifier) return this.processAttributeDuplicate(identifier, attributeNode) } private processAttributeDuplicate(identifier: string, attributeNode: HTMLAttributeNode): void { if (!this.isInControlFlow) { this.handleHTMLAttribute(identifier, attributeNode) return } if (this.currentControlFlowType === ControlFlowType.LOOP) { this.handleLoopAttribute(identifier, attributeNode) } else { this.handleConditionalAttribute(identifier, attributeNode) } this.currentBranchAttributes.add(identifier) } private handleHTMLAttribute(identifier: string, attributeNode: HTMLAttributeNode): void { if (this.tagAttributes.has(identifier)) { this.addDuplicateAttributeOffense(identifier, attributeNode.name!.location) } this.tagAttributes.add(identifier) } private handleLoopAttribute(identifier: string, attributeNode: HTMLAttributeNode): void { if (this.currentBranchAttributes.has(identifier)) { this.addSameLoopIterationOffense(identifier, attributeNode.name!.location) return } if (this.tagAttributes.has(identifier)) { this.addDuplicateAttributeOffense(identifier, attributeNode.name!.location) return } this.addLoopWillDuplicateOffense(identifier, attributeNode.name!.location) } private handleConditionalAttribute(identifier: string, attributeNode: HTMLAttributeNode): void { if (this.currentBranchAttributes.has(identifier)) { this.addSameBranchOffense(identifier, attributeNode.name!.location) return } if (this.tagAttributes.has(identifier)) { this.addDuplicateAttributeOffense(identifier, attributeNode.name!.location) } this.controlFlowAttributes.add(identifier) } private addDuplicateAttributeOffense(identifier: string, location: Location): void { this.addOffense( `Duplicate attribute \`${identifier}\`. Browsers only use the first occurrence and ignore duplicate attributes. Remove the duplicate or merge the values.`, location, ) } private addSameLoopIterationOffense(identifier: string, location: Location): void { this.addOffense( `Duplicate attribute \`${identifier}\` in same loop iteration. Each iteration will produce an element with duplicate attributes. Remove one or merge the values.`, location, ) } private addLoopWillDuplicateOffense(identifier: string, location: Location): void { this.addOffense( `Attribute \`${identifier}\` inside loop will appear multiple times on this element. Use a dynamic attribute name like \`${identifier}-<%= index %>\` or move the attribute outside the loop.`, location, ) } private addSameBranchOffense(identifier: string, location: Location): void { this.addOffense( `Duplicate attribute \`${identifier}\` in same branch. This branch will produce an element with duplicate attributes. Remove one or merge the values.`, location, ) } } export class HTMLNoDuplicateAttributesRule extends ParserRule { static ruleName = "html-no-duplicate-attributes" static introducedIn = this.version("0.4.0") static defaultEnabledIn = this.version("0.4.0") get defaultConfig(): FullRuleConfig { return { enabled: true, severity: "error" } } get parserOptions(): Partial { return { action_view_helpers: true } } check(result: ParseResult, context?: Partial): UnboundLintOffense[] { const visitor = new NoDuplicateAttributesVisitor(this.ruleName, context) visitor.visit(result.value) return visitor.offenses } }