Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions javascript/packages/linter/docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This page contains documentation for all Herb Linter rules.

- [`erb-comment-syntax`](./erb-comment-syntax.md) - Disallow Ruby comments immediately after ERB tags
- [`erb-no-case-node-children`](./erb-no-case-node-children.md) - Don't use `children` for `case/when` and `case/in` nodes
- [`erb-no-inline-case-conditions`](./erb-no-inline-case-conditions.md) - Disallow inline `case`/`when` and `case`/`in` conditions in a single ERB tag
- [`erb-no-conditional-html-element`](./erb-no-conditional-html-element.md) - Disallow conditional HTML elements
- [`erb-no-empty-tags`](./erb-no-empty-tags.md) - Disallow empty ERB tags
- [`erb-no-extra-newline`](./erb-no-extra-newline.md) - Disallow extra newlines.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Linter Rule: Disallow inline case conditions

**Rule:** `erb-no-inline-case-conditions`

## Description

Disallow placing `case` and its first `when`/`in` condition in the same ERB tag. When a `case` statement and its condition appear in a single ERB tag (e.g., `<% case x when y %>`), the parser cannot reliably process, compile, or format the template. This rule flags such patterns and guides users toward separate ERB tags.

## Rationale

ERB templates that combine `case` with a `when` or `in` condition in a single tag create parsing ambiguity. The parser creates synthetic condition nodes to handle this pattern in non-strict mode, but the resulting AST cannot be reliably formatted or compiled.

Using separate ERB tags for `case` and its conditions:

- Makes the template structure unambiguous for the parser
- Enables proper formatting and compilation
- Improves readability by clearly separating the case expression from its branches
- Follows the conventional ERB style used across the Ruby on Rails ecosystem

## Examples

### ✅ Good

`case`/`when` in separate ERB tags:

```erb
<% case variable %>
<% when "a" %>
A
<% when "b" %>
B
<% else %>
Other
<% end %>
```

`case`/`in` (pattern matching) in separate ERB tags:

```erb
<% case value %>
<% in 1 %>
One
<% in 2 %>
Two
<% else %>
Other
<% end %>
```

### 🚫 Bad

Inline `case`/`when` in a single ERB tag:

```erb
<% case variable when "a" %>
A
<% when "b" %>
B
<% end %>
```

Inline `case`/`in` in a single ERB tag:

```erb
<% case value in 1 %>
One
<% in 2 %>
Two
<% end %>
```

`case`/`when` on separate lines but still in the same ERB tag:

```erb
<% case variable
when "a" %>
A
<% when "b" %>
B
<% end %>
```

## References

\-
2 changes: 2 additions & 0 deletions javascript/packages/linter/src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { RuleClass } from "./types.js"

import { ERBCommentSyntax } from "./rules/erb-comment-syntax.js";
import { ERBNoCaseNodeChildrenRule } from "./rules/erb-no-case-node-children.js"
import { ERBNoInlineCaseConditionsRule } from "./rules/erb-no-inline-case-conditions.js"
import { ERBNoConditionalHTMLElementRule } from "./rules/erb-no-conditional-html-element.js"
import { ERBNoConditionalOpenTagRule } from "./rules/erb-no-conditional-open-tag.js"
import { ERBNoEmptyTagsRule } from "./rules/erb-no-empty-tags.js"
Expand Down Expand Up @@ -68,6 +69,7 @@ import { ParserNoErrorsRule } from "./rules/parser-no-errors.js"
export const rules: RuleClass[] = [
ERBCommentSyntax,
ERBNoCaseNodeChildrenRule,
ERBNoInlineCaseConditionsRule,
ERBNoConditionalHTMLElementRule,
ERBNoConditionalOpenTagRule,
ERBNoEmptyTagsRule,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ParserRule } from "../types.js"
import { BaseRuleVisitor } from "./rule-utils.js"

import type { UnboundLintOffense, LintContext, FullRuleConfig } from "../types.js"
import type { ERBCaseNode, ERBCaseMatchNode, ERBWhenNode, ERBInNode, ParseResult, ParserOptions } from "@herb-tools/core"

class ERBNoInlineCaseConditionsVisitor extends BaseRuleVisitor {
visitERBCaseNode(node: ERBCaseNode): void {
this.checkConditions(node, "when")
this.visitChildNodes(node)
}

visitERBCaseMatchNode(node: ERBCaseMatchNode): void {
this.checkConditions(node, "in")
this.visitChildNodes(node)
}

private checkConditions(node: ERBCaseNode | ERBCaseMatchNode, type: string): void {
if (!node.conditions || node.conditions.length === 0) return

for (const condition of node.conditions as (ERBWhenNode | ERBInNode)[]) {
if (condition.tag_opening === null) {
this.addOffense(
`A \`case\` statement with \`${type}\` conditions in a single ERB tag cannot be reliably parsed, compiled, and formatted. Use separate ERB tags for \`case\` and its conditions (e.g., \`<% case x %>\` followed by \`<% ${type} y %>\`).`,
node.location,
)
break
}
}
}
}

export class ERBNoInlineCaseConditionsRule extends ParserRule {
name = "erb-no-inline-case-conditions"

get defaultConfig(): FullRuleConfig {
return {
enabled: true,
severity: "warning",
}
}

get parserOptions(): Partial<ParserOptions> {
return { strict: false }
}

check(result: ParseResult, context?: Partial<LintContext>): UnboundLintOffense[] {
const visitor = new ERBNoInlineCaseConditionsVisitor(this.name, context)

visitor.visit(result.value)

return visitor.offenses
}
}
1 change: 1 addition & 0 deletions javascript/packages/linter/src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from "./herb-disable-comment-base.js"

export * from "./erb-comment-syntax.js"
export * from "./erb-no-case-node-children.js"
export * from "./erb-no-inline-case-conditions.js"
export * from "./erb-no-conditional-open-tag.js"
export * from "./erb-no-empty-tags.js"
export * from "./erb-no-extra-newline.js"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import dedent from "dedent"

import { describe, test } from "vitest"

import { ERBNoInlineCaseConditionsRule } from "../../src/rules/erb-no-inline-case-conditions.js"
import { createLinterTest } from "../helpers/linter-test-helper.js"

const { expectNoOffenses, expectWarning, assertOffenses } = createLinterTest(ERBNoInlineCaseConditionsRule)

describe("ERBNoInlineCaseConditionsRule", () => {
describe("case/when", () => {
test("valid case/when in separate ERB tags", () => {
expectNoOffenses(dedent`
<% case variable %>
<% when "a" %>
A
<% when "b" %>
B
<% else %>
Other
<% end %>
`)
})

test("invalid inline case/when", () => {
expectWarning('A `case` statement with `when` conditions in a single ERB tag cannot be reliably parsed, compiled, and formatted. Use separate ERB tags for `case` and its conditions (e.g., `<% case x %>` followed by `<% when y %>`).')

assertOffenses(dedent`
<% case variable when "a" %>
A
<% when "b" %>
B
<% end %>
`)
})

test("invalid case/when on newline in same tag", () => {
expectWarning('A `case` statement with `when` conditions in a single ERB tag cannot be reliably parsed, compiled, and formatted. Use separate ERB tags for `case` and its conditions (e.g., `<% case x %>` followed by `<% when y %>`).')

assertOffenses(`<% case variable\n when "a" %>\n A\n<% when "b" %>\n B\n<% end %>`)
})
})

describe("case/in", () => {
test("valid case/in in separate ERB tags", () => {
expectNoOffenses(dedent`
<% case value %>
<% in 1 %>
One
<% in 2 %>
Two
<% else %>
Other
<% end %>
`)
})

test("invalid inline case/in", () => {
expectWarning('A `case` statement with `in` conditions in a single ERB tag cannot be reliably parsed, compiled, and formatted. Use separate ERB tags for `case` and its conditions (e.g., `<% case x %>` followed by `<% in y %>`).')

assertOffenses(dedent`
<% case value in 1 %>
One
<% in 2 %>
Two
<% end %>
`)
})

test("invalid case/in on newline in same tag", () => {
expectWarning('A `case` statement with `in` conditions in a single ERB tag cannot be reliably parsed, compiled, and formatted. Use separate ERB tags for `case` and its conditions (e.g., `<% case x %>` followed by `<% in y %>`).')

assertOffenses(`<% case value\n in 1 %>\n One\n<% in 2 %>\n Two\n<% end %>`)
})
})
})
Loading