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

Add Lint/RequireParentheses #510

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
47 changes: 47 additions & 0 deletions spec/ameba/rule/lint/logical_without_paren_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "../../../spec_helper"

module Ameba::Rule::Lint
describe LogicalWithoutParenthesis do
subject = LogicalWithoutParenthesis.new

it "passes if logical operator in call args has parenthesis" do
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved
expect_no_issues subject, <<-CRYSTAL
if foo.includes?("bar" || foo.includes? "batz")
puts "this code is bug-free"
end

if foo.includes?("bar") || foo.includes?("batz")
puts "this code is bug-free"
end

form.add("query", "val_1" || "val_2")
form.add "query", ("val_1" || "val_2")
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved
form.add "query", "val_1" || "val_2"
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved
CRYSTAL
end

it "passes if logical operator in assignment call" do
expect_no_issues subject, <<-CRYSTAL
hello.there = "world" || method.call
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved
hello.there ||= "world" || method.call
CRYSTAL
end

it "passes if logical operator in square bracket call" do
expect_no_issues subject, <<-CRYSTAL
hello["world" || :thing]
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved
hello["world" || :thing]?
this.is[1 || method.call]
CRYSTAL
end

it "fails if logical operator in call args doesn't have parenthesis" do
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved
expect_issue subject, <<-CRYSTAL
if foo.includes? "bar" || foo.includes? "batz"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Logical operator in method args without parenthesis is not allowed
puts "this code is not bug-free"
end
CRYSTAL
end
end
end
48 changes: 48 additions & 0 deletions src/ameba/rule/lint/logical_without_paren.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Ameba::Rule::Lint
# A rule that disallows logical operators in method args without parenthesis.
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved
#
# For example, this is considered invalid:
#
# ```
# if foo.includes? "bar" || foo.includes? "batz"
# end
# ```
#
# And need to be written as:
#
# ```
# if foo.includes?("bar") || foo.includes?("batz")
# end
# ```
#
# YAML configuration example:
#
# ```
# Lint/LogicalWithoutParenthesis:
# Enabled: true
# ```
class LogicalWithoutParenthesis < Base
properties do
description "Disallows logical operators in method args without parenthesis"
end

MSG = "Logical operator in method args without parenthesis is not allowed"
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved

ALLOWED_CALL_NAMES = %w{[]? []}

def test(source, node : Crystal::Call)
return if node.args.empty? ||
node.has_parentheses? ||
node.name.ends_with?('=') ||
node.name.in?(ALLOWED_CALL_NAMES)

node.args.each do |arg|
if arg.is_a?(Crystal::BinaryOp)
if (right = arg.right).is_a?(Crystal::Call)
issue_for node, MSG unless right.args.empty?
end
end
end
end
end
end