diff --git a/javascript/packages/linter/docs/rules/README.md b/javascript/packages/linter/docs/rules/README.md
index 993912ecd..d5c873627 100644
--- a/javascript/packages/linter/docs/rules/README.md
+++ b/javascript/packages/linter/docs/rules/README.md
@@ -29,6 +29,7 @@ This page contains documentation for all Herb Linter rules.
#### ERB
+- [`erb-closing-tag-indent`](./erb-closing-tag-indent.md) - Enforce consistent closing ERB tag indentation
- [`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-commented-out-output-tags`](./erb-no-commented-out-output-tags.md) - Disallow commented-out ERB output tags (`<%#=`, `<%# =`)
diff --git a/javascript/packages/linter/docs/rules/erb-closing-tag-indent.md b/javascript/packages/linter/docs/rules/erb-closing-tag-indent.md
new file mode 100644
index 000000000..53be52447
--- /dev/null
+++ b/javascript/packages/linter/docs/rules/erb-closing-tag-indent.md
@@ -0,0 +1,62 @@
+# Linter Rule: Enforce consistent closing ERB tag indentation
+
+**Rule:** `erb-closing-tag-indent`
+
+## Description
+
+This rule enforces that the closing ERB tag (`%>`) is consistently indented relative to its opening tag (`<%` or `<%=`). When an ERB tag spans multiple lines, the closing `%>` must be on its own line and indented to match the column position of the opening tag.
+
+## Rationale
+
+Inconsistent indentation of closing ERB tags makes templates harder to read and maintain. When an ERB tag spans multiple lines, the closing `%>` should visually align with the opening `<%` to clearly show the tag boundaries. Conversely, if the opening tag is on the same line as the content, the closing tag should also be on the same line.
+
+## Examples
+
+### ✅ Good
+
+```erb
+<%= title %>
+```
+
+```erb
+<% if admin? %>
+
Content
+<% end %>
+```
+
+```erb
+<%
+ some_helper(
+ arg1,
+ arg2
+ )
+%>
+```
+
+```erb
+ <%
+ if true
+ %>
+```
+
+### ❌ Bad
+
+```erb
+<% if true
+%>
+```
+
+```erb
+<%
+ if true %>
+```
+
+```erb
+<%
+ if true
+ %>
+```
+
+## References
+
+- [Inspiration: ERB Lint `ClosingErbTagIndent` rule](https://github.com/Shopify/erb_lint/blob/main/lib/erb_lint/linters/closing_erb_tag_indent.rb)
diff --git a/javascript/packages/linter/src/rules.ts b/javascript/packages/linter/src/rules.ts
index e984dc634..741bb3630 100644
--- a/javascript/packages/linter/src/rules.ts
+++ b/javascript/packages/linter/src/rules.ts
@@ -18,12 +18,13 @@ import { ActionViewNoVoidElementContentRule } from "./rules/actionview-no-void-e
import { ActionViewStrictLocalsFirstLineRule } from "./rules/actionview-strict-locals-first-line.js"
import { ActionViewStrictLocalsPartialOnlyRule } from "./rules/actionview-strict-locals-partial-only.js"
+import { ERBClosingTagIndentRule } from "./rules/erb-closing-tag-indent.js"
import { ERBCommentSyntax } from "./rules/erb-comment-syntax.js";
import { ERBNoCaseNodeChildrenRule } from "./rules/erb-no-case-node-children.js"
import { ERBNoCommentedOutOutputTagsRule } from "./rules/erb-no-commented-out-output-tags.js"
-import { ERBNoDebugOutputRule } from "./rules/erb-no-debug-output.js"
import { ERBNoConditionalHTMLElementRule } from "./rules/erb-no-conditional-html-element.js"
import { ERBNoConditionalOpenTagRule } from "./rules/erb-no-conditional-open-tag.js"
+import { ERBNoDebugOutputRule } from "./rules/erb-no-debug-output.js"
import { ERBNoDuplicateBranchElementsRule } from "./rules/erb-no-duplicate-branch-elements.js"
import { ERBNoEmptyControlFlowRule } from "./rules/erb-no-empty-control-flow.js"
import { ERBNoEmptyTagsRule } from "./rules/erb-no-empty-tags.js"
@@ -129,14 +130,15 @@ export const rules: RuleClass[] = [
ActionViewStrictLocalsFirstLineRule,
ActionViewStrictLocalsPartialOnlyRule,
+ ERBClosingTagIndentRule,
ERBCommentSyntax,
ERBNoCaseNodeChildrenRule,
ERBNoCommentedOutOutputTagsRule,
- ERBNoDebugOutputRule,
- ERBNoEmptyControlFlowRule,
ERBNoConditionalHTMLElementRule,
ERBNoConditionalOpenTagRule,
+ ERBNoDebugOutputRule,
ERBNoDuplicateBranchElementsRule,
+ ERBNoEmptyControlFlowRule,
ERBNoEmptyTagsRule,
ERBNoExtraNewLineRule,
ERBNoExtraWhitespaceRule,
@@ -149,8 +151,6 @@ export const rules: RuleClass[] = [
ERBNoOutputInAttributePositionRule,
ERBNoRawOutputInAttributeValueRule,
ERBNoSilentStatementRule,
- ERBNoUnusedExpressionsRule,
- ERBNoUnusedLiteralsRule,
ERBNoSilentTagInAttributeNameRule,
ERBNoStatementInScriptRule,
ERBNoThenInControlFlowRule,
@@ -158,6 +158,8 @@ export const rules: RuleClass[] = [
ERBNoUnsafeJSAttributeRule,
ERBNoUnsafeRawRule,
ERBNoUnsafeScriptInterpolationRule,
+ ERBNoUnusedExpressionsRule,
+ ERBNoUnusedLiteralsRule,
ERBPreferDirectOutputRule,
ERBPreferImageTagHelperRule,
ERBRequireTrailingNewlineRule,
diff --git a/javascript/packages/linter/src/rules/erb-closing-tag-indent.ts b/javascript/packages/linter/src/rules/erb-closing-tag-indent.ts
new file mode 100644
index 000000000..a97b86436
--- /dev/null
+++ b/javascript/packages/linter/src/rules/erb-closing-tag-indent.ts
@@ -0,0 +1,128 @@
+import { BaseRuleVisitor } from "./rule-utils.js"
+import { ParserRule, BaseAutofixContext, Mutable } from "../types.js"
+
+import type { ERBNode, ParseResult } from "@herb-tools/core"
+import type { UnboundLintOffense, LintOffense, LintContext, FullRuleConfig } from "../types.js"
+
+interface ClosingErbTagIndentAutofixContext extends BaseAutofixContext {
+ node: Mutable
+ fixType: "remove-newline" | "add-newline" | "fix-indent"
+ expectedIndent: number
+}
+
+class ClosingErbTagIndentVisitor extends BaseRuleVisitor {
+ visitERBNode(node: ERBNode): void {
+ const openTag = node.tag_opening
+ const closeTag = node.tag_closing
+ const content = node.content
+ if (!openTag || !closeTag || !content) return
+
+ const value = content.value
+ if (!value.length) return
+
+ const startsWithNewline = value.startsWith("\n")
+ const endsWithNewline = this.endsWithNewline(value)
+
+ if (!startsWithNewline && endsWithNewline) {
+ this.addOffense(
+ `Remove newline before \`${closeTag.value}\`. The opening \`${openTag.value}\` is not followed by a newline, so the closing tag should be on the same line.`,
+ closeTag.location,
+ { node, fixType: "remove-newline", expectedIndent: 0 }
+ )
+ } else if (startsWithNewline && !endsWithNewline) {
+ const expectedIndent = openTag.location.start.column
+
+ this.addOffense(
+ `Add newline before \`${closeTag.value}\`. The opening \`${openTag.value}\` is followed by a newline, so the closing tag should be on its own line.`,
+ closeTag.location,
+ { node, fixType: "add-newline", expectedIndent }
+ )
+ } else if (startsWithNewline && endsWithNewline) {
+ const expectedIndent = openTag.location.start.column
+ const actualIndent = this.trailingIndent(value)
+
+ if (actualIndent === expectedIndent) return
+
+ this.addOffense(
+ `Incorrect indentation for \`${closeTag.value}\`. Expected ${expectedIndent} ${expectedIndent === 1 ? "space" : "spaces"} but found ${actualIndent}.`,
+ closeTag.location,
+ { node, fixType: "fix-indent", expectedIndent }
+ )
+ }
+ }
+
+ private endsWithNewline(value: string): boolean {
+ const lastNewlineIndex = value.lastIndexOf("\n")
+ if (lastNewlineIndex === -1) return false
+
+ const afterLastNewline = value.substring(lastNewlineIndex + 1)
+
+ return afterLastNewline.length === 0 || /^\s*$/.test(afterLastNewline)
+ }
+
+ private trailingIndent(value: string): number {
+ const lastNewlineIndex = value.lastIndexOf("\n")
+ if (lastNewlineIndex === -1) return 0
+
+ return value.length - lastNewlineIndex - 1
+ }
+}
+
+export class ERBClosingTagIndentRule extends ParserRule {
+ static autocorrectable = true
+ static reindentAfterAutofix = true
+ static ruleName = "erb-closing-tag-indent"
+ static introducedIn = this.version("unreleased")
+
+ get defaultConfig(): FullRuleConfig {
+ return {
+ enabled: true,
+ severity: "error"
+ }
+ }
+
+ check(result: ParseResult, context?: Partial): UnboundLintOffense[] {
+ const visitor = new ClosingErbTagIndentVisitor(this.ruleName, context)
+
+ visitor.visit(result.value)
+
+ return visitor.offenses
+ }
+
+ autofix(offense: LintOffense, result: ParseResult, _context?: Partial): ParseResult | null {
+ if (!offense.autofixContext) return null
+
+ const { node, fixType, expectedIndent } = offense.autofixContext
+ if (!node.content) return null
+
+ const content = node.content.value
+
+ switch (fixType) {
+ case "add-newline": {
+ const trimmed = content.trimEnd()
+ node.content.value = trimmed + "\n" + " ".repeat(expectedIndent)
+
+ return result
+ }
+
+ case "remove-newline": {
+ const lastNewlineIndex = content.lastIndexOf("\n")
+ if (lastNewlineIndex === -1) return null
+
+ const beforeNewline = content.substring(0, lastNewlineIndex).trimEnd()
+ node.content.value = beforeNewline + " "
+
+ return result
+ }
+
+ case "fix-indent": {
+ const lastNewlineIndex = content.lastIndexOf("\n")
+ if (lastNewlineIndex === -1) return null
+
+ node.content.value = content.substring(0, lastNewlineIndex + 1) + " ".repeat(expectedIndent)
+
+ return result
+ }
+ }
+ }
+}
diff --git a/javascript/packages/linter/src/rules/index.ts b/javascript/packages/linter/src/rules/index.ts
index dd9447292..d207a8798 100644
--- a/javascript/packages/linter/src/rules/index.ts
+++ b/javascript/packages/linter/src/rules/index.ts
@@ -23,6 +23,7 @@ export * from "./actionview-no-void-element-content.js"
export * from "./actionview-strict-locals-first-line.js"
export * from "./actionview-strict-locals-partial-only.js"
+export * from "./erb-closing-tag-indent.js"
export * from "./erb-comment-syntax.js"
export * from "./erb-no-case-node-children.js"
export * from "./erb-no-commented-out-output-tags.js"
diff --git a/javascript/packages/linter/test/__snapshots__/cli.test.ts.snap b/javascript/packages/linter/test/__snapshots__/cli.test.ts.snap
index 153441500..391174258 100644
--- a/javascript/packages/linter/test/__snapshots__/cli.test.ts.snap
+++ b/javascript/packages/linter/test/__snapshots__/cli.test.ts.snap
@@ -26,7 +26,7 @@ test/fixtures/all-rules.html.erb:
Checked 1 file
Offenses 6 warnings (6 offenses across 1 file)
Fixable 0 offenses
- Rules 101 enabled | all rules via --all-rules"
+ Rules 102 enabled | all rules via --all-rules"
`;
exports[`CLI Output Formatting > --all-rules > reports nothing for the fixture with the default rule set 1`] = `
@@ -38,7 +38,7 @@ exports[`CLI Output Formatting > --all-rules > reports nothing for the fixture w
Checked 1 file
Offenses 0 offenses
Fixable 0 offenses
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > --ignore-disable-comments 1`] = `
@@ -152,7 +152,7 @@ test/fixtures/ignored.html.erb:8:8
Offenses 5 errors | 2 warnings (7 offenses across 1 file)
Note 3 additional offenses reported (would have been ignored)
Fixable 7 offenses | 4 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > Excluded Files > skips excluded file in subdirectory with README.md project indicator 1`] = `
@@ -218,7 +218,7 @@ test/fixtures/test-file-with-errors.html.erb:2:22
Checked 1 file
Offenses 2 errors | 1 warning (3 offenses across 1 file)
Fixable 3 offenses | 2 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > GitHub Actions format includes rule codes 1`] = `
@@ -243,12 +243,25 @@ test/fixtures/no-trailing-newline.html.erb:1:29
Checked 1 file
Offenses 1 error | 0 warnings (1 offense across 1 file)
Fixable 1 offense | 1 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > GitHub Actions format includes rule codes 2`] = `
"✓ Using Herb config file at /test/herb/javascript/packages/linter/.herb.yml
+[error] Remove newline before \`%>\`. The opening \`<%=\` is not followed by a newline, so the closing tag should be on the same line. (erb-closing-tag-indent) [Correctable]
+
+test/fixtures/erb-no-extra-whitespace-inside-tags.html.erb:11:0
+
+ 9 │ <%= render partial: "post",
+ 10 │ as: :post
+ → 11 │ %>
+ │ ~~
+ 12 │
+
+
+⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ [1/5] ⎯⎯⎯⎯
+
[error] Remove extra whitespace after \`<%\`. (erb-no-extra-whitespace-inside-tags) [Correctable]
test/fixtures/erb-no-extra-whitespace-inside-tags.html.erb:1:2
@@ -259,7 +272,7 @@ test/fixtures/erb-no-extra-whitespace-inside-tags.html.erb:1:2
3 │ <%
-⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ [1/4] ⎯⎯⎯⎯
+⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ [2/5] ⎯⎯⎯⎯
[error] Remove extra whitespace before \`%>\`. (erb-no-extra-whitespace-inside-tags) [Correctable]
@@ -271,7 +284,7 @@ test/fixtures/erb-no-extra-whitespace-inside-tags.html.erb:1:20
3 │ <%
-⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ [2/4] ⎯⎯⎯⎯
+⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ [3/5] ⎯⎯⎯⎯
[error] Remove extra whitespace after \`<%=\`. (erb-no-extra-whitespace-inside-tags) [Correctable]
@@ -285,7 +298,7 @@ test/fixtures/erb-no-extra-whitespace-inside-tags.html.erb:9:3
11 │ %>
-⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ [3/4] ⎯⎯⎯⎯
+⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ [4/5] ⎯⎯⎯⎯
[error] Avoid unused expressions in silent ERB tags. \`extra_whitespace\` is evaluated but its return value is discarded. Use \`<%= ... %>\` to output the value or remove the expression. (erb-no-unused-expressions)
@@ -300,14 +313,15 @@ test/fixtures/erb-no-extra-whitespace-inside-tags.html.erb:1:4
Rule offenses:
erb-no-extra-whitespace-inside-tags (3 offenses in 1 file)
+ erb-closing-tag-indent (1 offense in 1 file)
erb-no-unused-expressions (1 offense in 1 file)
Summary:
Checked 1 file
- Offenses 4 errors | 0 warnings (4 offenses across 1 file)
- Fixable 4 offenses | 3 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Offenses 5 errors | 0 warnings (5 offenses across 1 file)
+ Fixable 5 offenses | 4 autocorrectable using \`--fix\`
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > Ignores disabled rules 1`] = `
@@ -348,7 +362,7 @@ test/fixtures/ignored.html.erb:6:14
Checked 1 file
Offenses 2 errors | 0 warnings | 3 ignored (2 offenses across 1 file)
Fixable 2 offenses | 2 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > allows tag.attributes in attribute position 1`] = `
@@ -387,7 +401,7 @@ test/fixtures/tag-attributes.html.erb:5:0
Checked 1 file
Offenses 2 warnings (2 offenses across 1 file)
Fixable 2 offenses | 2 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > diplays only parsers errors if one is present 1`] = `
@@ -413,7 +427,7 @@ test/fixtures/parser-errors.html.erb:2:16
Checked 1 file
Offenses 1 error | 0 warnings (1 offense across 1 file)
Fixable 0 offenses
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > displays most violated rules with multiple offenses 1`] = `
@@ -628,7 +642,7 @@ test/fixtures/multiple-rule-offenses.html.erb:4:7
Checked 1 file
Offenses 8 errors | 6 warnings (14 offenses across 1 file)
Fixable 14 offenses | 4 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > displays rule offenses when showing all rules 1`] = `
@@ -725,7 +739,7 @@ test/fixtures/few-rule-offenses.html.erb:6:0
Checked 1 file
Offenses 4 errors | 2 warnings (6 offenses across 1 file)
Fixable 6 offenses | 3 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > formats GitHub Actions output correctly for bad file 1`] = `
@@ -764,7 +778,7 @@ test/fixtures/bad-file.html.erb:1:16
Checked 1 file
Offenses 2 errors | 0 warnings (2 offenses across 1 file)
Fixable 2 offenses | 2 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > formats GitHub Actions output correctly for clean file 1`] = `
@@ -776,7 +790,7 @@ exports[`CLI Output Formatting > formats GitHub Actions output correctly for cle
Checked 1 file
Offenses 0 offenses
Fixable 0 offenses
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > formats GitHub Actions output correctly for file with errors 1`] = `
@@ -836,7 +850,7 @@ test/fixtures/test-file-with-errors.html.erb:2:22
Checked 1 file
Offenses 2 errors | 1 warning (3 offenses across 1 file)
Fixable 3 offenses | 2 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > formats GitHub Actions output with --format=github option 1`] = `
@@ -875,7 +889,7 @@ test/fixtures/test-file-simple.html.erb:2:22
Checked 1 file
Offenses 2 errors | 0 warnings (2 offenses across 1 file)
Fixable 2 offenses | 2 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > formats JSON output correctly for bad file 1`] = `
@@ -922,7 +936,7 @@ exports[`CLI Output Formatting > formats JSON output correctly for bad file 1`]
"summary": {
"filesChecked": 1,
"filesWithOffenses": 1,
- "ruleCount": 86,
+ "ruleCount": 87,
"totalErrors": 2,
"totalHints": 0,
"totalIgnored": 0,
@@ -943,7 +957,7 @@ exports[`CLI Output Formatting > formats JSON output correctly for clean file 1`
"summary": {
"filesChecked": 1,
"filesWithOffenses": 0,
- "ruleCount": 86,
+ "ruleCount": 87,
"totalErrors": 0,
"totalHints": 0,
"totalIgnored": 0,
@@ -1016,7 +1030,7 @@ exports[`CLI Output Formatting > formats JSON output correctly for file with err
"summary": {
"filesChecked": 1,
"filesWithOffenses": 1,
- "ruleCount": 86,
+ "ruleCount": 87,
"totalErrors": 2,
"totalHints": 0,
"totalIgnored": 0,
@@ -1079,7 +1093,7 @@ test/fixtures/test-file-with-errors.html.erb:2:22
Checked 1 file
Offenses 2 errors | 1 warning (3 offenses across 1 file)
Fixable 3 offenses | 2 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > formats simple output correctly 1`] = `
@@ -1098,7 +1112,7 @@ test/fixtures/test-file-simple.html.erb:
Checked 1 file
Offenses 2 errors | 0 warnings (2 offenses across 1 file)
Fixable 2 offenses | 2 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > formats simple output for bad-file correctly 1`] = `
@@ -1117,7 +1131,7 @@ test/fixtures/bad-file.html.erb:
Checked 1 file
Offenses 2 errors | 0 warnings (2 offenses across 1 file)
Fixable 2 offenses | 2 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > formats success output correctly 1`] = `
@@ -1129,7 +1143,7 @@ exports[`CLI Output Formatting > formats success output correctly 1`] = `
Checked 1 file
Offenses 0 offenses
Fixable 0 offenses
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > handles boolean attributes 1`] = `
@@ -1141,7 +1155,7 @@ exports[`CLI Output Formatting > handles boolean attributes 1`] = `
Checked 1 file
Offenses 0 offenses
Fixable 0 offenses
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > handles multiple errors correctly 1`] = `
@@ -1176,7 +1190,7 @@ test/fixtures/bad-file.html.erb:1:16
Checked 1 file
Offenses 2 errors | 0 warnings (2 offenses across 1 file)
Fixable 2 offenses | 2 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > herb:disable rules 1`] = `
@@ -1308,7 +1322,7 @@ test/fixtures/disabled-1.html.erb:14:19
Checked 1 file
Offenses 2 errors | 6 warnings | 8 ignored (8 offenses across 1 file)
Fixable 8 offenses | 1 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > herb:disable rules 2`] = `
@@ -1511,7 +1525,7 @@ test/fixtures/disabled-2.html.erb:2:44
Checked 1 file
Offenses 6 errors | 7 warnings | 5 ignored (13 offenses across 1 file)
Fixable 13 offenses | 6 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
exports[`CLI Output Formatting > uses GitHub Actions format by default when GITHUB_ACTIONS is true 1`] = `
@@ -1571,5 +1585,5 @@ test/fixtures/test-file-with-errors.html.erb:2:22
Checked 1 file
Offenses 2 errors | 1 warning (3 offenses across 1 file)
Fixable 3 offenses | 2 autocorrectable using \`--fix\`
- Rules 86 enabled | 15 not enabled"
+ Rules 87 enabled | 15 not enabled"
`;
diff --git a/javascript/packages/linter/test/autofix/erb-closing-tag-indent.autofix.test.ts b/javascript/packages/linter/test/autofix/erb-closing-tag-indent.autofix.test.ts
new file mode 100644
index 000000000..f0cb4f913
--- /dev/null
+++ b/javascript/packages/linter/test/autofix/erb-closing-tag-indent.autofix.test.ts
@@ -0,0 +1,85 @@
+import { describe, test, expect, beforeAll } from "vitest"
+
+import { Herb } from "@herb-tools/node-wasm"
+
+import { Linter } from "../../src/linter.js"
+import { ERBClosingTagIndentRule } from "../../src/rules/erb-closing-tag-indent.js"
+import dedent from "dedent"
+
+describe("erb-closing-tag-indent autofix", () => {
+ beforeAll(async () => {
+ await Herb.load()
+ })
+
+ test("removes newline before closing tag when opening is not followed by newline", () => {
+ const input = '<%= title\n%>'
+ const expected = '<%= title %>'
+
+ const linter = new Linter(Herb, [ERBClosingTagIndentRule])
+ const result = linter.autofix(input)
+
+ expect(result.source).toBe(expected)
+ expect(result.fixed).toHaveLength(1)
+ })
+
+ test("removes newline and indentation before closing tag", () => {
+ const input = '<%= title\n %>'
+ const expected = '<%= title %>'
+
+ const linter = new Linter(Herb, [ERBClosingTagIndentRule])
+ const result = linter.autofix(input)
+
+ expect(result.source).toBe(expected)
+ expect(result.fixed).toHaveLength(1)
+ })
+
+ test("adds newline before closing tag when opening is followed by newline", () => {
+ const input = '<%=\n title %>'
+ const expected = '<%=\n title\n%>'
+
+ const linter = new Linter(Herb, [ERBClosingTagIndentRule])
+ const result = linter.autofix(input)
+
+ expect(result.source).toBe(expected)
+ expect(result.fixed).toHaveLength(1)
+ })
+
+ test("adds indentation to closing tag to match opening tag", () => {
+ const input = '<%=\n title\n %>'
+ const expected = '<%=\n title\n%>'
+
+ const linter = new Linter(Herb, [ERBClosingTagIndentRule])
+ const result = linter.autofix(input)
+
+ expect(result.source).toBe(expected)
+ expect(result.fixed).toHaveLength(1)
+ })
+
+ test("preserves already correct single-line tags", () => {
+ const input = dedent`
+ <% if admin? %>
+ Hello
+ <% end %>
+ `
+
+ const linter = new Linter(Herb, [ERBClosingTagIndentRule])
+ const result = linter.autofix(input)
+
+ expect(result.source).toBe(input)
+ expect(result.fixed).toHaveLength(0)
+ })
+
+ test("preserves already correct multi-line tags", () => {
+ const input = dedent`
+ <%=
+ title
+ %>
+ `
+
+ const linter = new Linter(Herb, [ERBClosingTagIndentRule])
+ const result = linter.autofix(input)
+
+ expect(result.source).toBe(input)
+ expect(result.fixed).toHaveLength(0)
+ })
+})
diff --git a/javascript/packages/linter/test/rules/erb-closing-tag-indent.test.ts b/javascript/packages/linter/test/rules/erb-closing-tag-indent.test.ts
new file mode 100644
index 000000000..c6b7eb4ae
--- /dev/null
+++ b/javascript/packages/linter/test/rules/erb-closing-tag-indent.test.ts
@@ -0,0 +1,83 @@
+import dedent from "dedent"
+import { describe, test } from "vitest"
+import { ERBClosingTagIndentRule } from "../../src/rules/erb-closing-tag-indent.js"
+import { createLinterTest } from "../helpers/linter-test-helper.js"
+
+const { expectNoOffenses, expectError, assertOffenses } = createLinterTest(ERBClosingTagIndentRule)
+
+describe("ERBClosingTagIndentRule", () => {
+ test("ignores on empty ERB tag", () => {
+ expectNoOffenses(dedent`<% %>`)
+ })
+
+ test("ignores single-line ERB output tag", () => {
+ expectNoOffenses(dedent`<%= title %>`)
+ })
+
+ test("passes on single-line ERB with matching end statement", () => {
+ expectNoOffenses(dedent`
+ <% if admin? %>
+ Content
+ <% end %>
+ `)
+ })
+
+ test("passes on multi-line ERB with matching indent", () => {
+ expectNoOffenses(dedent`
+ <%=
+ some_helper(
+ arg1,
+ arg2
+ )
+ %>
+ `)
+ })
+
+ test("passes on multi-line ERB at beginning of line", () => {
+ expectNoOffenses(dedent`<%=
+title
+%>`)
+ })
+
+ describe("missing newline before closing tag", () => {
+ test("handles closing tag not followed by matching newline", () => {
+ expectError("Add newline before `%>`. The opening `<%=` is followed by a newline, so the closing tag should be on its own line.")
+
+ assertOffenses(dedent`
+ <%=
+ title %>
+ `)
+ })
+ })
+
+ describe("superfluous newline before closing tag", () => {
+ test("handles closing tag followed by additional newline", () => {
+ expectError("Remove newline before `%>`. The opening `<%=` is not followed by a newline, so the closing tag should be on the same line.")
+
+ assertOffenses(dedent`
+ <%= title
+ %>
+ `)
+ })
+ })
+
+ describe("incorrect indentation", () => {
+ test("handles closing tag indented more than opening tag", () => {
+ expectError("Incorrect indentation for `%>`. Expected 0 spaces but found 2.")
+
+ assertOffenses("<%=\n title\n %>")
+ })
+
+ test("handles closing tag indented less than opening tag", () => {
+ expectError("Incorrect indentation for `%>`. Expected 2 spaces but found 0.")
+
+ assertOffenses(" <%=\n title\n%>")
+ })
+
+ test("handles mismatched indent on closing tag", () => {
+ expectError("Incorrect indentation for `%>`. Expected 4 spaces but found 2.")
+
+ assertOffenses(" <%=\n title\n %>")
+ })
+ })
+})