".repeat(1000), { max_errors: 5 }).errorCount).toBe(5)
})
test("parse() uses the syntax of the given ruby_version", () => {
const source = "<% def greet(name,) = name %>"
expect(Herb.parse(source).options.ruby_version).toBe("4.0")
expect(Herb.parse(source, { ruby_version: null }).options.ruby_version).toBe("4.0")
expect(Herb.parse(source).recursiveErrors()).toHaveLength(1)
expect(Herb.parse(source, { ruby_version: "4.1" }).recursiveErrors()).toHaveLength(0)
const result = Herb.parse(source, { ruby_version: "3.4" })
expect(result.options.ruby_version).toBe("3.4")
expect(result.recursiveErrors().map(error => error.message)).toEqual(["parameter_wild_loose_comma: unexpected `,` in parameters"])
})
test("parse() throws for an unsupported ruby_version", () => {
expect(() => Herb.parse("<%= user %>", { ruby_version: "2.7" })).toThrow('ruby_version "2.7" is not supported')
})
test("parse() tracks locations by default", () => {
const result = Herb.parse('
content
')
const element = result.value.children[0] as any
expect(result.options.track_locations).toBe(true)
expect(result.value.location).not.toBeNull()
expect(element.location).not.toBeNull()
expect(element.open_tag.tag_name.location).not.toBeNull()
expect(element.open_tag.tag_name.range).not.toBeNull()
})
test("parse() with track_locations: false omits locations and ranges", () => {
const result = Herb.parse('
content
', { track_locations: false })
const element = result.value.children[0] as any
expect(result.options.track_locations).toBe(false)
expect(result.value.location).toBeNull()
expect(element.location).toBeNull()
expect(element.open_tag.tag_name.location).toBeNull()
expect(element.open_tag.tag_name.range).toBeNull()
})
test("parse() with track_locations: false keeps the tree shape intact", () => {
const source = '
content
'
const withLocations = Herb.parse(source)
const withoutLocations = Herb.parse(source, { track_locations: false })
expect(withoutLocations.value.type).toBe(withLocations.value.type)
expect(withoutLocations.value.children.map((node) => node.type)).toEqual(
withLocations.value.children.map((node) => node.type),
)
expect(withoutLocations.errors).toHaveLength(withLocations.errors.length)
})
test("lex() keeps token locations when parse locations are disabled", () => {
const result = Herb.lex('
content
')
expect(result.value.tokens[0].location).not.toBeNull()
expect(result.value.tokens[0].range).not.toBeNull()
})
test("parses then_keyword for when clause", () => {
const content = dedent`
<% case value %>
<% when String then "string" %>
<% end %>
`
const result = Herb.parse(content)
const caseNode = result.value.children[0] as any
const whenNode = caseNode.conditions[0]
expect(whenNode.then_keyword).toBeDefined()
expect(whenNode.then_keyword.start.line).toBe(2)
expect(whenNode.then_keyword.start.column).toBe(15)
expect(whenNode.then_keyword.end.line).toBe(2)
expect(whenNode.then_keyword.end.column).toBe(19)
})
})