-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #524 from nobodywasishere/nobody/styling/multi-lin…
…e-curly-block Add `Style/MultilineCurlyBlock`
- Loading branch information
Showing
3 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,36 @@ | ||
require "../../../spec_helper" | ||
|
||
module Ameba::Rule::Style | ||
describe MultilineCurlyBlock do | ||
subject = MultilineCurlyBlock.new | ||
|
||
it "doesn't report if a curly block is on a single line" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
foo { :bar } | ||
CRYSTAL | ||
end | ||
|
||
it "doesn't report for `do`...`end` blocks" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
foo do | ||
:bar | ||
end | ||
CRYSTAL | ||
end | ||
|
||
it "doesn't report for `do`...`end` blocks on a single line" do | ||
expect_no_issues subject, <<-CRYSTAL | ||
foo do :bar end | ||
CRYSTAL | ||
end | ||
|
||
it "reports if there is a multi-line curly block" do | ||
expect_issue subject, <<-CRYSTAL | ||
foo { | ||
# ^ error: Use `do`...`end` instead of curly brackets for multi-line blocks | ||
:bar | ||
} | ||
CRYSTAL | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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,46 @@ | ||
module Ameba::Rule::Style | ||
# A rule that disallows multi-line blocks that use curly brackets | ||
# instead of `do`...`end`. | ||
# | ||
# For example, this is considered invalid: | ||
# | ||
# ``` | ||
# (0..10).map { |i| | ||
# i * 2 | ||
# } | ||
# ``` | ||
# | ||
# And should be rewritten to the following: | ||
# | ||
# ``` | ||
# (0..10).map do |i| | ||
# i * 2 | ||
# end | ||
# ``` | ||
# | ||
# YAML configuration example: | ||
# | ||
# ``` | ||
# Style/MultilineCurlyBlock: | ||
# Enabled: true | ||
# ``` | ||
class MultilineCurlyBlock < Base | ||
include AST::Util | ||
|
||
properties do | ||
since_version "1.7.0" | ||
description "Disallows multi-line blocks using curly block syntax" | ||
end | ||
|
||
MSG = "Use `do`...`end` instead of curly brackets for multi-line blocks" | ||
|
||
def test(source, node : Crystal::Block) | ||
return unless start_location = node.location | ||
return unless end_location = node.end_location | ||
return if start_location.line_number == end_location.line_number | ||
return unless source.code[source.pos(start_location)]? == '{' | ||
|
||
issue_for node, MSG | ||
end | ||
end | ||
end |