diff --git a/lib/cane/abc_check.rb b/lib/cane/abc_check.rb index 0cb0705..8d6277a 100644 --- a/lib/cane/abc_check.rb +++ b/lib/cane/abc_check.rb @@ -76,7 +76,9 @@ def calculate_abc(method_node) def label_for(node) # A default case is deliberately omitted since I know of no way this # could fail and want it to fail fast. - node.detect {|x| [:@ident, :@op].include?(x[0]) }[1] + node.detect {|x| + [:@ident, :@op, :@kw, :@const, :@backtick].include?(x[0]) + }[1] end def count_nodes(node, types) diff --git a/spec/abc_check_spec.rb b/spec/abc_check_spec.rb index 1f85c03..9eaaccc 100644 --- a/spec/abc_check_spec.rb +++ b/spec/abc_check_spec.rb @@ -70,4 +70,58 @@ def self.complex_method(a) violations[0].should be_instance_of(Cane::AbcMaxViolation) violations[0].columns.should == [file_name, "Harness > complex_method", 2] end + + it 'creates an AbcMaxViolation for methods named after keywords' do + # Seen in the wild in actionpack: + # lib/action_controller/vendor/html-scanner/html/tokenizer.rb + file_name = make_file(<<-RUBY) + class Harness + def next(a) + b = a + return b if b > 3 + end + end + RUBY + + violations = described_class.new(files: file_name, max: 1).violations + violations.length.should == 1 + violations[0].should be_instance_of(Cane::AbcMaxViolation) + violations[0].columns.should == [file_name, "Harness > next", 2] + end + + it 'creates an AbcMaxViolation for methods named after constants' do + # Seen in the wild in actionpack: + # lib/action_dispatch/http/request.rb + file_name = make_file(<<-RUBY) + class Harness + def GET(a) + b = a + return b if b > 3 + end + end + RUBY + + violations = described_class.new(files: file_name, max: 1).violations + violations.length.should == 1 + violations[0].should be_instance_of(Cane::AbcMaxViolation) + violations[0].columns.should == [file_name, "Harness > GET", 2] + end + + it 'creates an AbcMaxViolation for backtick override' do + # Seen in the wild in actionpack: + # lib/active_support/core_ext/kernel/agnostics.rb + file_name = make_file(<<-RUBY) + class Harness + def `(a) + b = a + return b if b > 3 + end + end + RUBY + + violations = described_class.new(files: file_name, max: 1).violations + violations.length.should == 1 + violations[0].should be_instance_of(Cane::AbcMaxViolation) + violations[0].columns.should == [file_name, "Harness > `", 2] + end end