Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 714007e

Browse filesBrowse files
nbudinMiriam Lauter
andcommitted
Embedded formatter indentation fixes
* Change the strategy for how we indent squiggly heredocs: dedent the entire heredoc one level (to be on the same level as the statement that started it) and then indent the formatted doc, using a hard line break to separate the ending heredoc token * Don't consider empty lines as part of the common leading whitespace calculation Co-authored-by: Miriam Lauter <mlauter@actbluetech.com>
1 parent a3b5d9d commit 714007e
Copy full SHA for 714007e

File tree

Expand file treeCollapse file tree

2 files changed

+74
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+74
-4
lines changed
Open diff view settings
Collapse file

‎src/ruby/embed.ts‎

Copy file name to clipboardExpand all lines: src/ruby/embed.ts
+12-4Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ import type { Plugin, Ruby } from "../types";
22
import prettier from "../prettier";
33
import { literallineWithoutBreakParent } from "../utils";
44

5-
const { group, indent, lineSuffix, mapDoc, markAsRoot, stripTrailingHardline } =
6-
prettier;
5+
const {
6+
group,
7+
indent,
8+
dedent,
9+
lineSuffix,
10+
mapDoc,
11+
markAsRoot,
12+
stripTrailingHardline
13+
} = prettier;
714

815
const parsers: Record<string, string> = {
916
css: "css",
@@ -47,6 +54,7 @@ function getCommonLeadingWhitespace(content: string) {
4754
return content
4855
.split("\n")
4956
.slice(0, -1)
57+
.filter((line) => line.trim().length > 0)
5058
.reduce((minimum, line) => {
5159
const matched = pattern.exec(line);
5260
const length = matched ? matched[0].length : 0;
@@ -109,9 +117,9 @@ const embed: Plugin.Embed<Ruby.AnyNode> = (path, print, textToDoc) => {
109117
return [
110118
path.call(print, "beging"),
111119
lineSuffix(
112-
group([
120+
dedent([
113121
indent(markAsRoot(formatted)),
114-
literallineWithoutBreakParent,
122+
{ type: "line", hard: true },
115123
ending.trim()
116124
])
117125
)
Collapse file

‎test/js/ruby/embed.test.ts‎

Copy file name to clipboardExpand all lines: test/js/ruby/embed.test.ts
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,66 @@ describe("embed", () => {
7878

7979
return expect(content).toChangeFormat(expected);
8080
});
81+
82+
test("keeps parent indentation", () => {
83+
const content = ruby(`
84+
some_block do
85+
another_block do
86+
x += 1
87+
description <<~JS
88+
// This is a DSL method on the another_block inner block.
89+
// This is another line of the string.
90+
JS
91+
end
92+
end
93+
`);
94+
95+
return expect(content).toMatchFormat();
96+
});
97+
98+
test("correctly indents nested code while keeping parent indentation", () => {
99+
const content = ruby(`
100+
some_block do
101+
another_block do
102+
x += 1
103+
description <<~JS
104+
[1, function () { return 2; }, 3];
105+
JS
106+
end
107+
end
108+
`);
109+
const expected = ruby(`
110+
some_block do
111+
another_block do
112+
x += 1
113+
description <<~JS
114+
[
115+
1,
116+
function () {
117+
return 2;
118+
},
119+
3
120+
];
121+
JS
122+
end
123+
end
124+
`);
125+
126+
return expect(content).toChangeFormat(expected);
127+
});
128+
129+
test("doesn't consider empty lines as part of the common leading whitespace", () => {
130+
const content = ruby(`
131+
some_block do
132+
x += 1
133+
description <<~MARKDOWN
134+
This is a line. It's followed by two literal line breaks.
135+
136+
This is another line of the string.
137+
MARKDOWN
138+
end
139+
`);
140+
141+
return expect(content).toMatchFormat();
142+
});
81143
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.