import { describe, test, expect, beforeAll } from "vitest" import { Herb } from "@herb-tools/node-wasm" import { Formatter } from "../../src" import { createExpectFormattedToMatch } from "../helpers" import dedent from "dedent" let formatter: Formatter let expectFormattedToMatch: ReturnType describe("@herb-tools/formatter", () => { beforeAll(async () => { await Herb.load() formatter = new Formatter(Herb, { indentWidth: 2, maxLineLength: 80 }) expectFormattedToMatch = createExpectFormattedToMatch(formatter) }) test("text content", () => { const source = dedent` Hello ` const result = formatter.format(source) expect(result).toEqual(dedent` Hello `) }) test("text content inside element", () => { const source = dedent`
Hello
` const result = formatter.format(source) expect(result).toEqual(dedent`
Hello
`) }) test("short text content stays inline", () => { const source = dedent`

hello

` const result = formatter.format(source) expect(result).toEqual(dedent`

hello

`) }) test("long text content splits across lines", () => { const source = dedent`

This is a very long text that should probably be split across multiple lines because it exceeds the max line length

` const result = formatter.format(source) expect(result).toEqual(dedent`

This is a very long text that should probably be split across multiple lines because it exceeds the max line length

`) }) test("multiline text content splits across lines", () => { const source = dedent`
Multi line
` const result = formatter.format(source) expect(result).toEqual(dedent`
Multi line
`) }) test("mixed inline content stays inline", () => { const source = dedent`

hello bold world

` const result = formatter.format(source) expect(result).toEqual(dedent`

hello bold world

`) }) test("simple mixed content with multiple elements stays inline", () => { const source = dedent`

A bold and italic text

` const result = formatter.format(source) expect(result).toEqual(dedent`

A bold and italic text

`) }) test("any tag can be inline when content is simple", () => { const source = dedent`
Simple
nested
content
` const result = formatter.format(source) expect(result).toEqual(dedent`
Simple
nested
content
`) }) test("mixed content with nested elements splits when too deep", () => { const source = dedent`

hello

complex nested content
world

` const result = formatter.format(source) expect(result).toEqual(dedent`

hello

complex nested content
world

`) }) test("single level nesting stays inline", () => { const source = dedent`

hello

` const result = formatter.format(source) expect(result).toEqual(dedent`

hello

`) }) test("deep nesting with inline elements keeps inner elements inline", () => { const source = dedent`

hello

` const result = formatter.format(source) expect(result).toEqual(dedent`

hello

`) }) test("Block element nested inside inline context", () => { const source = dedent`

hello

` const result = formatter.format(source) expect(result).toEqual(dedent`

hello

`) }) test("root-level text with ERB interpolation", () => { expectFormattedToMatch(dedent` Hello, <%= @name %>, it is <%= @time %>. `) }) test("text with ERB interpolation inside element", () => { expectFormattedToMatch(dedent`
Hello, <%= @name %>, it is <%= @time %>.
`) }) test("period after ERB tag", () => { expectFormattedToMatch(dedent` Today is <%= Date.current %>. `) }) test("exclamation mark after ERB tag", () => { expectFormattedToMatch(dedent` Welcome <%= @user.name %>! `) }) test("question mark after ERB tag", () => { expectFormattedToMatch(dedent` Is this <%= @status %>? `) }) test("semicolon after ERB tag", () => { expectFormattedToMatch(dedent` First item: <%= @item %>; `) }) test("comma should not merge - maintains space", () => { expectFormattedToMatch(dedent` Hello <%= @first %>, how are you? `) }) test("colon after ERB with newline with
", () => { const input = dedent`


<%= Date.current %>: Hello

` const expected = dedent`


<%= Date.current %>: Hello

` const result = formatter.format(input) expect(result).toEqual(expected) }) test("colon after ERB with newline with
", () => { const input = dedent`


<%= Date.current %>: Hello

` const expected = dedent`


<%= Date.current %>: Hello

` const result = formatter.format(input) expect(result).toEqual(expected) }) test("colon after ERB with newline (after
)", () => { const result = formatter.format(dedent`

Bold Heading:
<%= Date.current %>: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

`) expect(result).toEqual(dedent`

Bold Heading:
<%= Date.current %>: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

`) }) test("multiple inline elements with punctuation preserve spacing", () => { const result = formatter.format(dedent`

Visit our store; buy great products!

`) expect(result).toEqual(dedent`

Visit our store; buy great products!

`) }) test("inline element at line end with punctuation on next line", () => { const result = formatter.format(dedent`
Check this : it works!
`) const expected = dedent`
Check this : it works!
` expect(result).toEqual(expected) expectFormattedToMatch(expected) }) test("punctuation glued to an inline element stays glued", () => { const result = formatter.format(dedent`
Check this: it works!
`) expect(result).toEqual(dedent`
Check this: it works!
`) }) test("ERB between inline elements with trailing punctuation", () => { const result = formatter.format(dedent`

Hello world <%= @greeting %>!

`) expect(result).toEqual(dedent`

Hello world <%= @greeting %>!

`) }) test("semicolon after inline element in long text", () => { const result = formatter.format(dedent`
Download the app; install quickly; then restart your device.
`) expect(result).toEqual(dedent`
Download the app; install quickly; then restart your device.
`) }) test("exclamation after ERB following inline element", () => { const result = formatter.format(dedent`

See bold text <%= @value %>!

`) expect(result).toEqual(dedent`

See bold text <%= @value %>!

`) }) test("question mark after nested inline elements", () => { const result = formatter.format(dedent`
Is this correct?
`) expect(result).toEqual(dedent`
Is this correct?
`) }) test("long text with strong and em in between", () => { const result = formatter.format(dedent`
This is a super long text before the strong and em element to check if this works, even if there's a long text after the strong and em element!
`) expect(result).toEqual(dedent`
This is a super long text before the strong and em element to check if this works, even if there's a long text after the strong and em element!
`) }) test("ERB block with non-output tag followed by text without space", () => { expectFormattedToMatch(dedent` <%= link_to "/" do %> <% icon("icon") %>can not insert whitespace here <% end %> `) }) test("text with hyphen before inline bold element preserves no-space boundary", () => { const source = dedent`
This is a div where we still can assume that whitespace can be inserted-infront or after of this bold you can not insert whitespace. Next senctence.
` const result = formatter.format(source) expect(result).toEqual(dedent`
This is a div where we still can assume that whitespace can be inserted-infront or after of this bold you can not insert whitespace. Next senctence.
`) }) // TODO: we need to wait for the parser to transform this as a HTMLElementNode test("ERB block tag with inline content should stay on one line", () => { const source = dedent` <%= tag.span do %>This should stay on one line<% end %> ` const result = formatter.format(source) // TODO: expect(result).toEqual(source) expect(result).toEqual(dedent` <%= tag.span do %> This should stay on one line <% end %> `) }) test("block element with mixed content and user newlines preserves line breaks", () => { expectFormattedToMatch(dedent`
This is some text and some bold text.
`) expectFormattedToMatch(dedent`
This is some text and some bold text.
`) }) test("multiline span with text keeps the author's newlines", () => { const source = dedent` And on the other hand one can not remove whitespace entirely ` const result = formatter.format(source) expect(result).toEqual(dedent` And on the other hand one can not remove whitespace entirely `) }) test("single-line span with text keeps the author's single line", () => { const source = dedent` And on the other hand one can not remove whitespace entirely ` const result = formatter.format(source) expect(result).toEqual(dedent` And on the other hand one can not remove whitespace entirely `) }) test("inline span with text content on single line preserves format", () => { expectFormattedToMatch(dedent` And on the other hand one can not remove whitespace entirely `) }) test("inline span with leading and trailing spaces preserves them", () => { expectFormattedToMatch(dedent` And on the other hand one can not remove whitespace entirely `) }) test("div with multiline text preserves leading and trailing whitespace", () => { expectFormattedToMatch(dedent`
Here the whitespace will not be removed
`) }) test("div with inline text content preserves format", () => { expectFormattedToMatch(dedent`
Here the whitespace will not be removed
`) }) test("div with leading and trailing spaces trims them for inline content", () => { const source = dedent`
Here the whitespace will not be removed
` const result = formatter.format(source) expect(result).toEqual(dedent`
Here the whitespace will not be removed
`) }) test("inline bold with ERB preserves user newlines", () => { expectFormattedToMatch(dedent`

<%= a_thing %> <%= another_thing %>

<%= a_thing %> <%= another_thing %>a

<%= a_thing %> <%= another_thing %> <%= a_thing %> <%= another_thing %>a

`) expectFormattedToMatch(dedent`

<%= a_thing %> <%= another_thing %>

<%= a_thing %> <%= another_thing %>a

`) }) })