Skip to content

Linter: Introduce ControlFlowTrackingVisitor to handle control flow#444

Merged
marcoroth merged 2 commits intomainfrom
control-flow-tracking-visitor
Aug 25, 2025
Merged

Linter: Introduce ControlFlowTrackingVisitor to handle control flow#444
marcoroth merged 2 commits intomainfrom
control-flow-tracking-visitor

Conversation

@marcoroth
Copy link
Owner

@marcoroth marcoroth commented Aug 25, 2025

This pull request introduces a new ControlFlowTrackingVisitor base class which allows linter rules to track state across branches and loops.

With this new visitor we are now able to track state more strategically in branches.

interface MyRuleState {
  counters: Map<string, number>
  flags: boolean[]
}

interface MyBranchState {
  savedCounters: Map<string, number>
}

class MyCustomRuleVisitor extends ControlFlowTrackingVisitor<MyRuleState, MyBranchState> {
  protected onEnterControlFlow(controlFlowType: ControlFlowType, wasAlreadyInControlFlow: boolean): MyRuleState {
    return { counters: new Map(), flags: [] }
  }

  protected onExitControlFlow(controlFlowType: ControlFlowType, wasAlreadyInControlFlow: boolean, stateToRestore: MyRuleState): void {
    this.processCounters(stateToRestore.counters)
  }
}

Using this new infrastructure, we are now able to refactor the html-no-duplicate-ids rule to not flag IDs when they are in mutually exclusive branches.

This now doesn't get flagged anymore:

<% if some_condition? %>
  <span id="id-1">content1</span>
<% else %>
  <span id="id-1">content2</span>
<% end %>

This resolves #441 and resolves #273.

@marcoroth marcoroth merged commit 80e708b into main Aug 25, 2025
7 checks passed
@marcoroth marcoroth deleted the control-flow-tracking-visitor branch August 25, 2025 20:05
marcoroth added a commit that referenced this pull request Dec 29, 2025
Resolves #458

This implements detection of duplicate attributes in control flow by
leveraging the ControlFlowTrackingVisitor introduced in #444.

The intended behavior is described in the test cases, so suggestions for
improvement are welcome!

---------

Co-authored-by: Marco Roth <marco.roth@intergga.ch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request linter refactor

Projects

None yet

1 participant