Skip to content
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

[Formatter] Fix space between call name and parenthesized arg #11523

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,9 @@ describe Crystal::Formatter do
assert_format "foo(\n# x\n1,\n\n# y\nz: 2\n)", "foo(\n # x\n 1,\n\n # y\n z: 2\n)"
assert_format "foo(\n# x\n1,\n\n# y\nz: 2,\n\n# a\nb: 3)", "foo(\n # x\n 1,\n\n # y\n z: 2,\n\n # a\n b: 3)"
assert_format "foo(\n 1, # hola\n2, # chau\n )", "foo(\n 1, # hola\n 2, # chau\n)"
assert_format "foo (1)", "foo(1)"
assert_format "foo (1), 2"
assert_format "foo (1; 2)"
assert_format "def foo(\n\n#foo\nx,\n\n#bar\nz\n)\nend", "def foo(\n # foo\n x,\n\n # bar\n z\n)\nend"
assert_format "def foo(\nx, #foo\nz #bar\n)\nend", "def foo(\n x, # foo\n z # bar\n)\nend"
assert_format "a = 1;;; b = 2", "a = 1; b = 2"
Expand Down
12 changes: 6 additions & 6 deletions spec/compiler/lexer/lexer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -525,36 +525,36 @@ describe "Lexer" do
lexer = Lexer.new ":a!=:a"
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
token.value.should eq("a")
token = lexer.next_token
token.type.should eq(:"!=")
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
token.value.should eq("a")
end

it "lexes symbol followed by ==" do
lexer = Lexer.new ":a==:a"
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
token.value.should eq("a")
token = lexer.next_token
token.type.should eq(:"==")
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
token.value.should eq("a")
end

it "lexes symbol followed by ===" do
lexer = Lexer.new ":a===:a"
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
token.value.should eq("a")
token = lexer.next_token
token.type.should eq(:"===")
token = lexer.next_token
token.type.should eq(:SYMBOL)
token.value.should eq ("a")
token.value.should eq("a")
end

it "lexes != after identifier (#4815)" do
Expand Down
44 changes: 22 additions & 22 deletions spec/std/big/big_decimal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -219,32 +219,32 @@ describe BigDecimal do
end

it "can be converted from other types" do
1.to_big_d.should eq (BigDecimal.new(1))
"1.5".to_big_d.should eq (BigDecimal.new(15, 1))
"+1.5".to_big_d.should eq (BigDecimal.new(15, 1))
BigInt.new(15).to_big_d.should eq (BigDecimal.new(15, 0))
1.5.to_big_d.should eq (BigDecimal.new(15, 1))
1.5.to_big_f.to_big_d.should eq (BigDecimal.new(15, 1))
1.to_big_d.should eq(BigDecimal.new(1))
"1.5".to_big_d.should eq(BigDecimal.new(15, 1))
"+1.5".to_big_d.should eq(BigDecimal.new(15, 1))
BigInt.new(15).to_big_d.should eq(BigDecimal.new(15, 0))
1.5.to_big_d.should eq(BigDecimal.new(15, 1))
1.5.to_big_f.to_big_d.should eq(BigDecimal.new(15, 1))
1.5.to_big_r.to_big_d.should eq(BigDecimal.new(15, 1))
end

it "can be converted from scientific notation" do
"10.01e1".to_big_d.should eq (BigDecimal.new("100.1"))
"10.01e-1".to_big_d.should eq (BigDecimal.new("1.001"))
"6.033e2".to_big_d.should eq (BigDecimal.new("603.3"))
"603.3e-2".to_big_d.should eq (BigDecimal.new("6.033"))
"-0.123e12".to_big_d.should eq (BigDecimal.new("-123000000000"))
"0.123e12".to_big_d.should eq (BigDecimal.new("123000000000"))
"0.123e+12".to_big_d.should eq (BigDecimal.new("123000000000"))
"-0.123e-7".to_big_d.should eq (BigDecimal.new("-0.0000000123"))
"-0.1e-7".to_big_d.should eq (BigDecimal.new("-0.00000001"))
"0.1e-7".to_big_d.should eq (BigDecimal.new("0.00000001"))
"1.0e-8".to_big_d.should eq (BigDecimal.new("0.00000001"))
"10e-8".to_big_d.should eq (BigDecimal.new("0.0000001"))
"1.0e+8".to_big_d.should eq (BigDecimal.new("100000000"))
"10e+8".to_big_d.should eq (BigDecimal.new("1000000000"))
"10E+8".to_big_d.should eq (BigDecimal.new("1000000000"))
"10E8".to_big_d.should eq (BigDecimal.new("1000000000"))
"10.01e1".to_big_d.should eq(BigDecimal.new("100.1"))
"10.01e-1".to_big_d.should eq(BigDecimal.new("1.001"))
"6.033e2".to_big_d.should eq(BigDecimal.new("603.3"))
"603.3e-2".to_big_d.should eq(BigDecimal.new("6.033"))
"-0.123e12".to_big_d.should eq(BigDecimal.new("-123000000000"))
"0.123e12".to_big_d.should eq(BigDecimal.new("123000000000"))
"0.123e+12".to_big_d.should eq(BigDecimal.new("123000000000"))
"-0.123e-7".to_big_d.should eq(BigDecimal.new("-0.0000000123"))
"-0.1e-7".to_big_d.should eq(BigDecimal.new("-0.00000001"))
"0.1e-7".to_big_d.should eq(BigDecimal.new("0.00000001"))
"1.0e-8".to_big_d.should eq(BigDecimal.new("0.00000001"))
"10e-8".to_big_d.should eq(BigDecimal.new("0.0000001"))
"1.0e+8".to_big_d.should eq(BigDecimal.new("100000000"))
"10e+8".to_big_d.should eq(BigDecimal.new("1000000000"))
"10E+8".to_big_d.should eq(BigDecimal.new("1000000000"))
"10E8".to_big_d.should eq(BigDecimal.new("1000000000"))
end

it "is comparable with other types" do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/concurrent_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe "concurrent" do
spawn do
3.times do |i|
sleep 40.milliseconds
chan.send (i + 1)
chan.send(i + 1)
end
end
spawn do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/enum_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe Enum do
end

it "for private enum" do
PrivateEnum.from_value(0).should eq (PrivateEnum::FOO)
PrivateEnum.from_value(0).should eq(PrivateEnum::FOO)
end
end

Expand Down
12 changes: 6 additions & 6 deletions spec/std/mime_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,22 @@ describe MIME do
it "parses media types" do
MIME.init(load_defaults: false)
MIME.register(".parse-media-type1", "text/html; charset=utf-8")
MIME.extensions("text/html").should contain (".parse-media-type1")
MIME.extensions("text/html").should contain(".parse-media-type1")

MIME.register(".parse-media-type2", "text/html; foo = bar; bar= foo ;")
MIME.extensions("text/html").should contain (".parse-media-type2")
MIME.extensions("text/html").should contain(".parse-media-type2")

MIME.register(".parse-media-type3", "foo/bar")
MIME.extensions("foo/bar").should contain (".parse-media-type3")
MIME.extensions("foo/bar").should contain(".parse-media-type3")

MIME.register(".parse-media-type4", " form-data ; name=foo")
MIME.extensions("form-data").should contain (".parse-media-type4")
MIME.extensions("form-data").should contain(".parse-media-type4")

MIME.register(".parse-media-type41", %(FORM-DATA;name="foo"))
MIME.extensions("form-data").should contain (".parse-media-type41")
MIME.extensions("form-data").should contain(".parse-media-type41")

MIME.register(".parse-media-type5", %( FORM-DATA ; name="foo"))
MIME.extensions("form-data").should contain (".parse-media-type5")
MIME.extensions("form-data").should contain(".parse-media-type5")

expect_raises ArgumentError, "Invalid media type" do
MIME.register(".parse-media-type6", ": inline; attachment; filename=foo.html")
Expand Down
68 changes: 34 additions & 34 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1020,57 +1020,57 @@ describe "String" do

describe "partition" do
describe "by char" do
it { "hello".partition('h').should eq ({"", "h", "ello"}) }
it { "hello".partition('o').should eq ({"hell", "o", ""}) }
it { "hello".partition('l').should eq ({"he", "l", "lo"}) }
it { "hello".partition('x').should eq ({"hello", "", ""}) }
it { "hello".partition('h').should eq({"", "h", "ello"}) }
it { "hello".partition('o').should eq({"hell", "o", ""}) }
it { "hello".partition('l').should eq({"he", "l", "lo"}) }
it { "hello".partition('x').should eq({"hello", "", ""}) }
end

describe "by string" do
it { "hello".partition("h").should eq ({"", "h", "ello"}) }
it { "hello".partition("o").should eq ({"hell", "o", ""}) }
it { "hello".partition("l").should eq ({"he", "l", "lo"}) }
it { "hello".partition("ll").should eq ({"he", "ll", "o"}) }
it { "hello".partition("x").should eq ({"hello", "", ""}) }
it { "hello".partition("h").should eq({"", "h", "ello"}) }
it { "hello".partition("o").should eq({"hell", "o", ""}) }
it { "hello".partition("l").should eq({"he", "l", "lo"}) }
it { "hello".partition("ll").should eq({"he", "ll", "o"}) }
it { "hello".partition("x").should eq({"hello", "", ""}) }
end

describe "by regex" do
it { "hello".partition(/h/).should eq ({"", "h", "ello"}) }
it { "hello".partition(/o/).should eq ({"hell", "o", ""}) }
it { "hello".partition(/l/).should eq ({"he", "l", "lo"}) }
it { "hello".partition(/ll/).should eq ({"he", "ll", "o"}) }
it { "hello".partition(/.l/).should eq ({"h", "el", "lo"}) }
it { "hello".partition(/.h/).should eq ({"hello", "", ""}) }
it { "hello".partition(/h./).should eq ({"", "he", "llo"}) }
it { "hello".partition(/o./).should eq ({"hello", "", ""}) }
it { "hello".partition(/.o/).should eq ({"hel", "lo", ""}) }
it { "hello".partition(/x/).should eq ({"hello", "", ""}) }
it { "hello".partition(/h/).should eq({"", "h", "ello"}) }
it { "hello".partition(/o/).should eq({"hell", "o", ""}) }
it { "hello".partition(/l/).should eq({"he", "l", "lo"}) }
it { "hello".partition(/ll/).should eq({"he", "ll", "o"}) }
it { "hello".partition(/.l/).should eq({"h", "el", "lo"}) }
it { "hello".partition(/.h/).should eq({"hello", "", ""}) }
it { "hello".partition(/h./).should eq({"", "he", "llo"}) }
it { "hello".partition(/o./).should eq({"hello", "", ""}) }
it { "hello".partition(/.o/).should eq({"hel", "lo", ""}) }
it { "hello".partition(/x/).should eq({"hello", "", ""}) }
end
end

describe "rpartition" do
describe "by char" do
it { "hello".rpartition('l').should eq ({"hel", "l", "o"}) }
it { "hello".rpartition('o').should eq ({"hell", "o", ""}) }
it { "hello".rpartition('h').should eq ({"", "h", "ello"}) }
it { "hello".rpartition('l').should eq({"hel", "l", "o"}) }
it { "hello".rpartition('o').should eq({"hell", "o", ""}) }
it { "hello".rpartition('h').should eq({"", "h", "ello"}) }
end

describe "by string" do
it { "hello".rpartition("l").should eq ({"hel", "l", "o"}) }
it { "hello".rpartition("x").should eq ({"", "", "hello"}) }
it { "hello".rpartition("o").should eq ({"hell", "o", ""}) }
it { "hello".rpartition("h").should eq ({"", "h", "ello"}) }
it { "hello".rpartition("ll").should eq ({"he", "ll", "o"}) }
it { "hello".rpartition("lo").should eq ({"hel", "lo", ""}) }
it { "hello".rpartition("he").should eq ({"", "he", "llo"}) }
it { "hello".rpartition("l").should eq({"hel", "l", "o"}) }
it { "hello".rpartition("x").should eq({"", "", "hello"}) }
it { "hello".rpartition("o").should eq({"hell", "o", ""}) }
it { "hello".rpartition("h").should eq({"", "h", "ello"}) }
it { "hello".rpartition("ll").should eq({"he", "ll", "o"}) }
it { "hello".rpartition("lo").should eq({"hel", "lo", ""}) }
it { "hello".rpartition("he").should eq({"", "he", "llo"}) }
end

describe "by regex" do
it { "hello".rpartition(/.l/).should eq ({"he", "ll", "o"}) }
it { "hello".rpartition(/ll/).should eq ({"he", "ll", "o"}) }
it { "hello".rpartition(/.o/).should eq ({"hel", "lo", ""}) }
it { "hello".rpartition(/.e/).should eq ({"", "he", "llo"}) }
it { "hello".rpartition(/l./).should eq ({"hel", "lo", ""}) }
it { "hello".rpartition(/.l/).should eq({"he", "ll", "o"}) }
it { "hello".rpartition(/ll/).should eq({"he", "ll", "o"}) }
it { "hello".rpartition(/.o/).should eq({"hel", "lo", ""}) }
it { "hello".rpartition(/.e/).should eq({"", "he", "llo"}) }
it { "hello".rpartition(/l./).should eq({"hel", "lo", ""}) }
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/std/yaml/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe "YAML serialization" do
end

it "can parse string that looks like a number" do
String.from_yaml(%(1.2)).should eq ("1.2")
String.from_yaml(%(1.2)).should eq("1.2")
end

it "does Path.from_yaml" do
Expand Down
10 changes: 10 additions & 0 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2662,6 +2662,16 @@ module Crystal
# so we remove the space between "as" and "(".
skip_space if special_call

# If the call has a single argument which is a parenthesized `Expressions`,
# we skip whitespace between the method name and the arg. The parenthesized
# arg is transformed into a call with parenthesis: `foo (a)` becomes `foo(a)`.
if node.args.size == 1 &&
!node.named_args && !node.block_arg && !node.block &&
(expressions = node.args[0].as?(Expressions)) &&
expressions.keyword == :"(" && expressions.expressions.size == 1
skip_space
end

if @token.type == :"("
slash_is_regex!
next_token
Expand Down