pelusa 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/pelusa/lint.rb CHANGED
@@ -7,6 +7,7 @@ require 'pelusa/lint/else_clauses'
7
7
  require 'pelusa/lint/properties'
8
8
  require 'pelusa/lint/collection_wrappers'
9
9
  require 'pelusa/lint/short_identifiers'
10
+ require 'pelusa/lint/long_identifiers'
10
11
  require 'pelusa/lint/case_statements'
11
12
  require 'pelusa/lint/many_arguments'
12
13
  require 'pelusa/lint/eval_usage'
@@ -26,6 +27,7 @@ module Pelusa
26
27
  Properties,
27
28
  CollectionWrappers,
28
29
  ShortIdentifiers,
30
+ LongIdentifiers,
29
31
  ManyArguments,
30
32
  EvalUsage
31
33
  ]
@@ -29,11 +29,7 @@ module Pelusa
29
29
  __iterate = Iterator.new do |node|
30
30
  if body = get_body_from_node[node]
31
31
  if node.line != [body].flatten.first.line
32
- if body.is_a?(Array)
33
- @violations.merge body.map(&:line)
34
- else
35
- @violations << body.line
36
- end
32
+ @violations.merge Array(body).map(&:line)
37
33
  end
38
34
  end
39
35
  end
@@ -0,0 +1,58 @@
1
+ module Pelusa
2
+ module Lint
3
+ class LongIdentifiers
4
+ def initialize
5
+ @violations = Set.new
6
+ end
7
+
8
+ def check(klass)
9
+ initialize
10
+ iterate_lines!(klass)
11
+
12
+ return SuccessfulAnalysis.new(name) if @violations.empty?
13
+
14
+ FailedAnalysis.new(name, formatted_violations) do |violations|
15
+ "These names are too long: #{violations.join(', ')}"
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def name
22
+ "Uses names of adequate length (less than #{limit})"
23
+ end
24
+
25
+ def limit
26
+ Pelusa.configuration['LongIdentifiers'].fetch('limit', 20)
27
+ end
28
+
29
+ def iterate_lines!(klass)
30
+ iterator = Iterator.new do |node|
31
+ if node.respond_to?(:name)
32
+ name = node.name.respond_to?(:name) ? node.name.name.to_s : node.name.to_s
33
+ if name =~ /[a-z]/ && name.length > limit
34
+ next if name =~ /^[A-Z]/ # Ignore constants
35
+ @violations << [name, node.line]
36
+ end
37
+ end
38
+ end
39
+ Array(klass).each(&iterator)
40
+ end
41
+
42
+ def formatted_violations
43
+ grouped_violations = @violations.inject({}) do |hash, (name, line)|
44
+ hash[name] ||= []
45
+ hash[name] << line
46
+ hash
47
+ end
48
+
49
+ violations = []
50
+
51
+ grouped_violations.each_pair do |name, lines|
52
+ violations << "#{name} (line #{lines.join(', ')})"
53
+ end
54
+ violations
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,6 +1,8 @@
1
1
  module Pelusa
2
2
  module Lint
3
3
  class ShortIdentifiers
4
+ RESERVED_NAMES = ['p', 'pp', 'id']
5
+
4
6
  def initialize
5
7
  @violations = Set.new
6
8
  end
@@ -26,7 +28,7 @@ module Pelusa
26
28
  iterator = Iterator.new do |node|
27
29
  if node.respond_to?(:name)
28
30
  name = node.name.respond_to?(:name) ? node.name.name.to_s : node.name.to_s
29
- if name =~ /[a-z]/ && name.length < 3 && !["p", "pp"].include?(name)
31
+ if name =~ /[a-z]/ && name.length < 3 && !RESERVED_NAMES.include?(name)
30
32
  next if name =~ /^[A-Z]/ # Ignore constants
31
33
  @violations << [name, node.line]
32
34
  end
@@ -1,3 +1,3 @@
1
1
  module Pelusa
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ module Pelusa
4
+ module Lint
5
+ describe LongIdentifiers do
6
+ before do
7
+ @lint = LongIdentifiers.new
8
+ end
9
+
10
+ describe '#check' do
11
+ describe 'when the class contains no long identifiers' do
12
+ it 'returns a SuccessAnalysis' do
13
+ klass = """
14
+ class Foo
15
+ def initialize
16
+ not_long_identifier = nil
17
+ end
18
+ end""".to_ast
19
+
20
+ analysis = @lint.check(klass)
21
+ analysis.successful?.must_equal true
22
+ end
23
+ end
24
+
25
+ describe 'when the class contains a long identifier' do
26
+ it 'returns a FailureAnalysis' do
27
+ klass = """
28
+ class Foo
29
+ def initialize
30
+ it_is_long_identifier = nil
31
+ end
32
+ end""".to_ast
33
+
34
+ analysis = @lint.check(klass)
35
+ analysis.failed?.must_equal true
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -22,6 +22,21 @@ module Pelusa
22
22
  end
23
23
  end
24
24
 
25
+ describe 'when the class contains short identifier from reserved list' do
26
+ it 'returns a SuccessAnalysis' do
27
+ klass = """
28
+ class Foo
29
+ def initialize
30
+ id = 2
31
+ pp id
32
+ end
33
+ end""".to_ast
34
+
35
+ analysis = @lint.check(klass)
36
+ analysis.successful?.must_equal true
37
+ end
38
+ end
39
+
25
40
  describe 'when the class contains a short identifier' do
26
41
  it 'returns a FailureAnalysis' do
27
42
  klass = """
metadata CHANGED
@@ -1,27 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pelusa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
5
4
  prerelease:
5
+ version: 0.2.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Josep M. Bach
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-29 00:00:00.000000000 Z
12
+ date: 2012-09-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- requirement: &7212 !ruby/object:Gem::Requirement
15
+ prerelease: false
16
+ type: :development
17
+ name: mocha
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
16
23
  none: false
24
+ version_requirements: !ruby/object:Gem::Requirement
17
25
  requirements:
18
26
  - - ! '>='
19
27
  - !ruby/object:Gem::Version
20
28
  version: '0'
21
- name: mocha
22
- version_requirements: *7212
23
- prerelease: false
24
- type: :development
29
+ none: false
25
30
  description: Static analysis Lint-type tool to improve your OO Ruby code
26
31
  email:
27
32
  - josep.m.bach@gmail.com
@@ -54,6 +59,7 @@ files:
54
59
  - lib/pelusa/lint/indentation_level.rb
55
60
  - lib/pelusa/lint/instance_variables.rb
56
61
  - lib/pelusa/lint/line_restriction.rb
62
+ - lib/pelusa/lint/long_identifiers.rb
57
63
  - lib/pelusa/lint/many_arguments.rb
58
64
  - lib/pelusa/lint/properties.rb
59
65
  - lib/pelusa/lint/short_identifiers.rb
@@ -79,6 +85,7 @@ files:
79
85
  - test/pelusa/lint/indentation_level_test.rb
80
86
  - test/pelusa/lint/instance_variables_test.rb
81
87
  - test/pelusa/lint/line_restriction_test.rb
88
+ - test/pelusa/lint/long_identifiers_test.rb
82
89
  - test/pelusa/lint/many_arguments_test.rb
83
90
  - test/pelusa/lint/properties_test.rb
84
91
  - test/pelusa/lint/short_identifiers_test.rb
@@ -93,20 +100,20 @@ rdoc_options: []
93
100
  require_paths:
94
101
  - lib
95
102
  required_ruby_version: !ruby/object:Gem::Requirement
96
- none: false
97
103
  requirements:
98
104
  - - ! '>='
99
105
  - !ruby/object:Gem::Version
100
106
  version: '0'
101
- required_rubygems_version: !ruby/object:Gem::Requirement
102
107
  none: false
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
109
  requirements:
104
110
  - - ! '>='
105
111
  - !ruby/object:Gem::Version
106
112
  version: '0'
113
+ none: false
107
114
  requirements: []
108
115
  rubyforge_project: pelusa
109
- rubygems_version: 1.8.12
116
+ rubygems_version: 1.8.24
110
117
  signing_key:
111
118
  specification_version: 3
112
119
  summary: Static analysis Lint-type tool to improve your OO Ruby code
@@ -128,6 +135,7 @@ test_files:
128
135
  - test/pelusa/lint/indentation_level_test.rb
129
136
  - test/pelusa/lint/instance_variables_test.rb
130
137
  - test/pelusa/lint/line_restriction_test.rb
138
+ - test/pelusa/lint/long_identifiers_test.rb
131
139
  - test/pelusa/lint/many_arguments_test.rb
132
140
  - test/pelusa/lint/properties_test.rb
133
141
  - test/pelusa/lint/short_identifiers_test.rb