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("HTML comment", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) test("HTML comment with no surrounding spaces", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) test("formats HTML comments and ERB comments on the same line", () => { const source = dedent` <%# ERB Comment %> ` const result = formatter.format(source) expect(result).toEqual(dedent` <%# ERB Comment %> `) }) test("formats HTML comments and ERB comments on mutli-line", () => { const source = dedent` <%# ERB Comment %> ` const result = formatter.format(source) expect(result).toEqual(dedent` <%# ERB Comment %> `) }) test("HTML comment with ERB content inside", () => { const source = dedent`
` const result = formatter.format(source) expect(result).toEqual(dedent`
`) }) test("HTML comment with multiple ERB tags inside", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) test("HTML comment with ERB and text mixed", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) test("multi-line HTML comment with ERB content", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) test("multi-line HTML comment with ERB if", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) test.fails("indents multi-line HTML comment with ERB if", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) test("HTML comment spanning multiple lines with inline ERB", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) test("multi-line HTML comment with ERB gets indented", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) test("multi-line HTML comment gets indented", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) test("nested multi-line HTML comment keeps its indentation", () => { expectFormattedToMatch(dedent`
`) }) test("nested multi-line HTML comment preserves relative indentation", () => { expectFormattedToMatch(dedent`
`) }) test("nested multi-line HTML comment gets re-indented", () => { const source = dedent`
` const result = formatter.format(source) expect(result).toEqual(dedent`
`) }) test("nested HTML comment with content on the opening line", () => { const source = dedent`
` const result = formatter.format(source) expect(result).toEqual(dedent`
`) }) test("HTML comment with content on the opening line aligns the closing tag", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) describe("Mixed content with comments", () => { test("div with comment and another element stays multiline", () => { const source = dedent`

Some content

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

Some content

`) }) test("div with comment and text content stays multiline", () => { const source = dedent`
Some text content here
` const result = formatter.format(source) expect(result).toEqual(dedent`
Some text content here
`) }) test("div with comment and ERB expression stays multiline", () => { const source = dedent`
<%= current_user.name %>
` const result = formatter.format(source) expect(result).toEqual(dedent`
<%= current_user.name %>
`) }) test("div with multiple comments and elements stays multiline", () => { const source = dedent`

Title

Content

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

Title

Content

`) }) test("nested elements with comments stay multiline", () => { const source = dedent`
Inline content
` const result = formatter.format(source) expect(result).toEqual(dedent`
Inline content
`) }) test("comment with ERB in mixed content stays multiline", () => { const source = dedent`

Article Title

Article content goes here.

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

Article Title

Article content goes here.

`) }) test("div with only a comment uses multiline formatting", () => { const source = dedent`
` const result = formatter.format(source) expect(result).toEqual(dedent`
`) }) test("span with only a comment keeps the author's newlines", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) test("span with a comment glued to the open tag still collapses", () => { const source = dedent` ` const result = formatter.format(source) expect(result).toEqual(dedent` `) }) }) describe("Trailing comments", () => { test("does not insert a blank line before a closing comment", () => { expectFormattedToMatch(dedent`
blah blah blah
`) }) test("does not insert a blank line before a closing ERB comment", () => { expectFormattedToMatch(dedent`
blah blah blah
<%# closing container %> `) }) test("does not insert a blank line before a closing comment inside an element", () => { expectFormattedToMatch(dedent`
blah blah blah
`) }) test("keeps a blank line the author put before a closing comment", () => { expectFormattedToMatch(dedent`
blah blah blah
`) }) test("still spaces a comment that introduces a following element", () => { const source = dedent`
one
two
` const result = formatter.format(source) expect(result).toEqual(dedent`
one
two
`) }) }) })