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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { hasBalancedParentheses, splitByTopLevelComma } from "./string-utils.js"
import type { UnboundLintOffense, LintContext, FullRuleConfig } from "../types.js"
import type { ParseResult, ERBContentNode } from "@herb-tools/core"

export const STRICT_LOCALS_PATTERN = /^locals:\s*\([^)]*\)\s*$/
export const STRICT_LOCALS_PATTERN = /^locals:\s+\([^)]*\)\s*$/

function isValidStrictLocalsFormat(content: string): boolean {
return STRICT_LOCALS_PATTERN.test(content)
Expand Down Expand Up @@ -49,6 +49,10 @@ function detectMissingColonBeforeParens(content: string): boolean {
return /^locals\s+\(/.test(content)
}

function detectMissingSpaceAfterColon(content: string): boolean {
return /^locals:\(/.test(content)
}

function detectMissingParentheses(content: string): boolean {
return /^locals:\s*[^(]/.test(content)
}
Expand Down Expand Up @@ -240,6 +244,11 @@ class ERBStrictLocalsCommentSyntaxVisitor extends BaseRuleVisitor {
return
}

if (detectMissingSpaceAfterColon(commentContent)) {
this.addOffense("Missing space after `locals:`. Rails Strict Locals require a space after the colon: `<%# locals: (...) %>`.", node.location)
return
}

if (detectMissingParentheses(commentContent)) {
this.addOffense("Wrap parameters in parentheses: `locals: (name:)` or `locals: (name: default)`.", node.location)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ describe("ERBStrictLocalsCommentSyntaxRule", () => {
`)
})

test("flags missing space after colon", () => {
expectError("Missing space after `locals:`. Rails Strict Locals require a space after the colon: `<%# locals: (...) %>`.")

assertOffenses(dedent`
<%# locals:() %>
`)
})

test("flags missing space after colon with locals", () => {
expectError("Missing space after `locals:`. Rails Strict Locals require a space after the colon: `<%# locals: (...) %>`.")

assertOffenses(dedent`
<%# locals:(title:) %>
`)
})

test("flags missing parentheses around parameters", () => {
expectError("Wrap parameters in parentheses: `locals: (name:)` or `locals: (name: default)`.")

Expand Down
Loading