From eab5499f8ebd97ee6d57990f1682d030f668a152 Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Tue, 8 May 2018 22:18:15 +0300 Subject: [PATCH] Add a workaround for https://github.com/crystal-lang/crystal/pull/6032 --- spec/ameba/ast/branch_spec.cr | 22 ++++++++++------------ spec/ameba/rule/unused_argument_spec.cr | 11 +++++++++++ spec/ameba/rule/useless_assign_spec.cr | 12 ++++++++++++ src/ameba.cr | 1 + src/ameba/support/ast.cr | 12 ++++++++++++ 5 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 src/ameba/support/ast.cr diff --git a/spec/ameba/ast/branch_spec.cr b/spec/ameba/ast/branch_spec.cr index 484ffe8c..bff1efd5 100644 --- a/spec/ameba/ast/branch_spec.cr +++ b/spec/ameba/ast/branch_spec.cr @@ -121,18 +121,16 @@ module Ameba::AST end context "Crystal::Case" do - # FIXME: - # https://github.com/crystal-lang/crystal/pull/6032 - # it "constructs a branch in cond" do - # branch = branch_of_assign_in_def %( - # def method(a) - # case (a = 2) - # when true then nil - # end - # end - # ) - # branch.to_s.should eq "(a = 2)" - # end + it "constructs a branch in cond" do + branch = branch_of_assign_in_def %( + def method(a) + case (a = 2) + when true then nil + end + end + ) + branch.to_s.should eq "(a = 2)" + end it "constructs a branch in when" do branch = branch_of_assign_in_def %( diff --git a/spec/ameba/rule/unused_argument_spec.cr b/spec/ameba/rule/unused_argument_spec.cr index 2df81e0c..89952860 100644 --- a/spec/ameba/rule/unused_argument_spec.cr +++ b/spec/ameba/rule/unused_argument_spec.cr @@ -137,6 +137,17 @@ module Ameba::Rule subject.catch(s).should be_valid end + it "doesn't report if arg if referenced in case" do + s = Source.new %( + def foo(a) + case a + when /foo/ + end + end + ) + subject.catch(s).should be_valid + end + context "super" do it "reports if variable is not referenced implicitly by super" do s = Source.new %( diff --git a/spec/ameba/rule/useless_assign_spec.cr b/spec/ameba/rule/useless_assign_spec.cr index 9bc2d596..2f85c7df 100644 --- a/spec/ameba/rule/useless_assign_spec.cr +++ b/spec/ameba/rule/useless_assign_spec.cr @@ -582,6 +582,18 @@ module Ameba::Rule s.errors.first.location.to_s.should eq ":5:17" s.errors.last.location.to_s.should eq ":7:17" end + + it "doesn't report if assignment is referenced in cond" do + s = Source.new %( + def method + a = 2 + case a + when /foo/ + end + end + ) + subject.catch(s).should be_valid + end end context "binary operator" do diff --git a/src/ameba.cr b/src/ameba.cr index 3c4ce27f..437d2770 100644 --- a/src/ameba.cr +++ b/src/ameba.cr @@ -2,6 +2,7 @@ require "./ameba/*" require "./ameba/ast/**" require "./ameba/rule/*" require "./ameba/formatter/*" +require "./ameba/support/*" # Ameba's entry module. # diff --git a/src/ameba/support/ast.cr b/src/ameba/support/ast.cr new file mode 100644 index 00000000..1b7f5d87 --- /dev/null +++ b/src/ameba/support/ast.cr @@ -0,0 +1,12 @@ +{% if Crystal::VERSION == "0.24.2" %} + # workaround for https://github.com/crystal-lang/crystal/pull/6032 + module Crystal + class Case < ASTNode + def accept_children(visitor) + @cond.try &.accept visitor + @whens.each &.accept visitor + @else.try &.accept visitor + end + end + end +{% end %}