Skip to content

Commit b3fa128

Browse files
authored
[prism] Fix extra newline after require kwarg (#765)
* Fixtures * Emit method names as their own token type * Add require assignment
1 parent 26472e6 commit b3fa128

File tree

7 files changed

+69
-9
lines changed

7 files changed

+69
-9
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
ThingToCall.call(
2+
require: "not a regular require",
3+
other: "value"
4+
)
5+
6+
require = bees!
7+
foo(require: bar)
8+
9+
{
10+
:require => foo,
11+
:other => 1
12+
}
13+
14+
"require#{foo}"
15+
16+
x = {require: 1, other: 2}
17+
y = {require:, other:}
18+
19+
def thing(require:)
20+
require
21+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
ThingToCall.call(
2+
require: "not a regular require",
3+
other: "value"
4+
)
5+
6+
require = bees!
7+
foo(require: bar)
8+
9+
{
10+
:require => foo,
11+
:other => 1
12+
}
13+
14+
"require#{foo}"
15+
16+
x = {require: 1, other: 2}
17+
y = {require:, other:}
18+
19+
def thing(require:)
20+
require
21+
end

librubyfmt/src/format.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,19 @@ pub fn format_ident(ps: &mut ParserState, ident: Ident) {
834834
}
835835
}
836836

837+
pub fn format_method_ident(ps: &mut ParserState, ident: Ident) {
838+
if ps.at_start_of_line() {
839+
ps.emit_indent();
840+
}
841+
842+
ps.on_line(ident.2.0);
843+
ps.emit_method_name(ident.1);
844+
845+
if ps.at_start_of_line() {
846+
ps.emit_newline();
847+
}
848+
}
849+
837850
pub fn format_const(ps: &mut ParserState, c: Const) {
838851
if ps.at_start_of_line() {
839852
ps.emit_indent();
@@ -2496,7 +2509,7 @@ fn format_call_chain_elements(
24962509
if ident.1 == ".()" {
24972510
ps.emit_ident(".");
24982511
} else {
2499-
format_ident(ps, ident);
2512+
format_method_ident(ps, ident);
25002513
}
25012514
ps.shift_comments();
25022515
}

librubyfmt/src/format_prism.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,11 +1747,8 @@ fn format_call_node<'src>(
17471747
} else {
17481748
method_name
17491749
};
1750-
handle_string_at_offset(
1751-
ps,
1752-
method_ident,
1753-
start_loc_for_call_node_in_chain(&call_node),
1754-
);
1750+
ps.at_offset(start_loc_for_call_node_in_chain(&call_node));
1751+
ps.emit_method_name(method_ident);
17551752
}
17561753

17571754
// Calls with only empty parens (`foo ()`) are treated as calls without args

librubyfmt/src/intermediary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ impl<'src> Intermediary<'src> {
130130
prev.set_gets_indented()
131131
}
132132
}
133-
ConcreteLineToken::DirectPart { part } => {
134-
if part == "require" && self.tokens.last().map(|t| t.is_indent()).unwrap_or(false) {
133+
ConcreteLineToken::MethodName { name } => {
134+
if name == "require" && self.tokens.last().map(|t| t.is_indent()).unwrap_or(false) {
135135
self.current_line_metadata.set_has_require();
136136
}
137137
}

librubyfmt/src/line_tokens.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub enum ConcreteLineToken<'src> {
3838
ModKeyword { contents: &'static str },
3939
ConditionalKeyword { contents: &'static str },
4040
DirectPart { part: Cow<'src, str> },
41+
MethodName { name: Cow<'src, str> },
4142
CommaSpace,
4243
Comma,
4344
Space,
@@ -81,6 +82,7 @@ impl<'src> ConcreteLineToken<'src> {
8182
Self::DefKeyword => Cow::Borrowed("def"),
8283
Self::ModuleKeyword => Cow::Borrowed("module"),
8384
Self::DirectPart { part } => part,
85+
Self::MethodName { name } => name,
8486
Self::CommaSpace => Cow::Borrowed(", "),
8587
Self::Comma => Cow::Borrowed(","),
8688
Self::Space => Cow::Borrowed(" "),
@@ -126,7 +128,9 @@ impl<'src> ConcreteLineToken<'src> {
126128
Keyword { keyword: contents }
127129
| ModKeyword { contents }
128130
| ConditionalKeyword { contents } => contents.len(),
129-
Op { op: contents } | DirectPart { part: contents } => contents.len(),
131+
Op { op: contents } | DirectPart { part: contents } | MethodName { name: contents } => {
132+
contents.len()
133+
}
130134
LTStringContent { content: contents }
131135
| Comment { contents }
132136
| HeredocClose { symbol: contents } => contents.len(),

librubyfmt/src/parser_state.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ impl<'src> ParserState<'src> {
455455
self.push_concrete_token(ConcreteLineToken::DirectPart { part: ident.into() });
456456
}
457457

458+
pub(crate) fn emit_method_name(&mut self, name: impl Into<Cow<'src, str>>) {
459+
self.push_concrete_token(ConcreteLineToken::MethodName { name: name.into() });
460+
}
461+
458462
pub(crate) fn emit_newline(&mut self) {
459463
self.shift_comments();
460464
self.push_concrete_token(ConcreteLineToken::HardNewLine);

0 commit comments

Comments
 (0)