-
-
Notifications
You must be signed in to change notification settings - Fork 116
Formatter: Preserve author-expanded multi-line ERB tags #2059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+291
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
javascript/packages/formatter/test/erb/multiline.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| 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<typeof createExpectFormattedToMatch> | ||
|
|
||
| // https://github.com/marcoroth/herb/issues/1835 | ||
| // | ||
| // When the source has a newline immediately after the opening tag (`<%=` or | ||
| // `<%`), the formatter should keep the tag expanded with the opening and | ||
| // closing delimiters on their own lines, rather than squashing the first line | ||
| // of Ruby onto the opening delimiter. This mirrors how Prettier preserves | ||
| // user-authored expansion of object literals, and how the formatter already | ||
| // renders multi-line ERB comments. | ||
| describe("@herb-tools/formatter", () => { | ||
| beforeAll(async () => { | ||
| await Herb.load() | ||
|
|
||
| formatter = new Formatter(Herb, { | ||
| indentWidth: 2, | ||
| maxLineLength: 80, | ||
| }) | ||
|
|
||
| expectFormattedToMatch = createExpectFormattedToMatch(formatter) | ||
| }) | ||
|
|
||
| describe("multi-line ERB output tags (issue 1835)", () => { | ||
| test("preserves newline after opening tag at top-level", () => { | ||
| expectFormattedToMatch(dedent` | ||
| <%= | ||
| link_to( | ||
| sanitize(t("views.pagination.first")), | ||
| url, | ||
| remote:, | ||
| class: "page-link", | ||
| tabindex: current_page.first? ? -1 : nil | ||
| ) | ||
| %> | ||
| `) | ||
| }) | ||
|
|
||
| test("preserves newline after opening tag inside an element", () => { | ||
| expectFormattedToMatch(dedent` | ||
| <li class="<%= class_names("page-item user-select-none", disabled: current_page.first?) %>"> | ||
| <%= | ||
| link_to( | ||
| sanitize(t("views.pagination.first")), | ||
| url, | ||
| remote:, | ||
| class: "page-link", | ||
| tabindex: current_page.first? ? -1 : nil | ||
| ) | ||
| %> | ||
| </li> | ||
| `) | ||
| }) | ||
|
|
||
| test("preserves newline after opening tag for non-output ERB tags", () => { | ||
| expectFormattedToMatch(dedent` | ||
| <% | ||
| pagination_options = { | ||
| remote:, | ||
| class: "page-link" | ||
| } | ||
| %> | ||
| `) | ||
| }) | ||
|
|
||
| test("still squashes multi-line content without a leading newline", () => { | ||
| expectFormattedToMatch(dedent` | ||
| <%= link_to( | ||
| sanitize(t("views.pagination.first")), | ||
| url, | ||
| remote: | ||
| ) %> | ||
| `) | ||
| }) | ||
|
|
||
| test("balances a tag with a leading newline but inline closing delimiter", () => { | ||
| const source = dedent` | ||
| <%= | ||
| link_to( | ||
| "First", | ||
| url | ||
| ) %> | ||
| ` | ||
| const result = formatter.format(source) | ||
|
|
||
| expect(result).toEqual(dedent` | ||
| <%= | ||
| link_to( | ||
| "First", | ||
| url | ||
| ) | ||
| %> | ||
| `) | ||
| }) | ||
|
|
||
| test("re-indents content relative to the tag's indentation", () => { | ||
| const source = [ | ||
| `<div>`, | ||
| `<%=`, | ||
| `link_to(`, | ||
| ` "First",`, | ||
| ` url`, | ||
| `)`, | ||
| `%>`, | ||
| `</div>`, | ||
| ].join("\n") | ||
|
|
||
| const result = formatter.format(source) | ||
|
|
||
| expect(result).toEqual(dedent` | ||
| <div> | ||
| <%= | ||
| link_to( | ||
| "First", | ||
| url | ||
| ) | ||
| %> | ||
| </div> | ||
| `) | ||
| }) | ||
|
|
||
| test("preserves relative indentation of content lines", () => { | ||
| expectFormattedToMatch(dedent` | ||
| <%= | ||
| content_tag( | ||
| :span, | ||
| title, | ||
| data: { | ||
| controller: "tooltip" | ||
| } | ||
| ) | ||
| %> | ||
| `) | ||
| }) | ||
|
|
||
| test("is idempotent across multiple passes", () => { | ||
| expectFormattedToMatch( | ||
| dedent` | ||
| <li> | ||
| <%= | ||
| link_to( | ||
| "First", | ||
| url | ||
| ) | ||
| %> | ||
| </li> | ||
| `, | ||
| { passes: 3 }, | ||
| ) | ||
| }) | ||
|
|
||
| test("single-line output tags are unaffected", () => { | ||
| expectFormattedToMatch(`<%= title %>`) | ||
| }) | ||
|
|
||
| test("collapses a leading newline when content is a single line", () => { | ||
| const source = `<%=\n title %>` | ||
| const result = formatter.format(source) | ||
|
|
||
| expect(result).toEqual(`<%= title %>`) | ||
| }) | ||
|
|
||
| test("does not expand control-flow opening tags", () => { | ||
| const source = dedent` | ||
| <% | ||
| if current_page.first? %> | ||
| <span>First</span> | ||
| <% end %> | ||
| ` | ||
| const result = formatter.format(source) | ||
|
|
||
| expect(result).toEqual(dedent` | ||
| <% if current_page.first? %> | ||
| <span>First</span> | ||
| <% end %> | ||
| `) | ||
| }) | ||
|
|
||
| test("squashes multi-line ERB inside attribute values", () => { | ||
| const source = dedent` | ||
| <li class="<%= | ||
| class_names( | ||
| "page-item", | ||
| disabled: current_page.first? | ||
| ) | ||
| %>">First</li> | ||
| ` | ||
| const result = formatter.format(source) | ||
|
|
||
| expect(result).toEqual(dedent` | ||
| <li class="<%= class_names( "page-item", disabled: current_page.first? ) %>"> | ||
| First | ||
| </li> | ||
| `) | ||
| }) | ||
|
|
||
| test("does not expand ERB tags in inline text flow", () => { | ||
| expectFormattedToMatch(dedent` | ||
| <p>Showing <%= offset %> of <%= total %> results</p> | ||
| `) | ||
| }) | ||
|
|
||
| test("expanded output tags coexist with heredoc handling (issue 476)", () => { | ||
| expectFormattedToMatch(dedent` | ||
| <%= <<~HTML.html_safe | ||
| <span>First</span> | ||
| HTML | ||
| %> | ||
| `) | ||
| }) | ||
|
|
||
| test("does not re-indent heredoc bodies inside expanded tags", () => { | ||
| expectFormattedToMatch(dedent` | ||
| <%= | ||
| tag.pre(<<~TEXT) | ||
| significant | ||
| leading | ||
| whitespace | ||
| TEXT | ||
| %> | ||
| `) | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.