guideline 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,18 +8,21 @@ $ gem install guideline
8
8
  ```
9
9
 
10
10
  ## Usage
11
- https://github.com/r7kamura/guideline/tree/master/examples
12
11
 
13
12
  ```
14
- $ ruby examples/long_line_checker.rb
15
- spec/guideline/checker_spec.rb
16
- 27: Line length 88 should be less than 80 characters
13
+ $ guideline
14
+ lib/chatroid/adapter/campfire.rb
15
+ 26: Line length 85 should be less than 80 characters
17
16
 
18
- $ ruby examples/long_method_checker.rb
19
- lib/guideline/checker/long_line_checker.rb
20
- 3: Too long 9 lines method <#check>
17
+ lib/chatroid/adapter/twitter/event.rb
18
+ 48: Line length 87 should be less than 80 characters
21
19
 
22
- lib/guideline/checker/long_method_checker.rb
23
- 6: Too long 9 lines method <#check>
24
- 42: Too long 10 lines method <#on_def>
20
+ spec/chatroid/adapter/twitter/event_spec.rb
21
+ 49: Line length 81 should be less than 80 characters
22
+
23
+ spec/chatroid/adapter/twitter_spec.rb
24
+ 30: Line length 85 should be less than 80 characters
25
+
26
+ lib/chatroid/adapter/twitter.rb
27
+ 19: Too long 12 lines method <#stream>
25
28
  ```
data/bin/guideline CHANGED
@@ -2,32 +2,18 @@
2
2
 
3
3
  $LOAD_PATH.unshift File.expand_path("../../lib/", __FILE__)
4
4
  require "guideline"
5
- require "yaml"
6
- require "slop"
7
- require "active_support/all"
5
+ include Guideline
8
6
 
9
- def parse_options
10
- Slop.parse(:help => true) do
11
- banner "Usage: guidline [directory] [options]"
12
- on :c, :config, "path to guideline.yml"
13
- end
14
- end
7
+ options = Runner.parse(ARGV)
15
8
 
16
- def load_config(options)
17
- path = options[:config] || File.expand_path("../../guideline.yml", __FILE__)
18
- hash = YAML.load_file(path)
19
- HashWithIndifferentAccess.new(hash)
20
- end
9
+ checkers = CheckerFactory.new(
10
+ options[:config],
11
+ HashCommaChecker,
12
+ LongLineChecker,
13
+ LongMethodChecker,
14
+ HardTabIndentChecker
15
+ ).create
21
16
 
22
- options = parse_options
23
- config = load_config(options)
24
-
25
- Guideline::Visitor.new(
26
- :only => ARGV[0],
27
- :checker => Guideline::CheckerFactory.new(
28
- config,
29
- Guideline::LongLineChecker,
30
- Guideline::LongMethodChecker,
31
- Guideline::HardTabIndentChecker
32
- ).create
33
- ).check.render
17
+ visitor = Visitor.new(:only => ARGV[0], :checker => checkers)
18
+ visitor.visit
19
+ visitor.render
@@ -0,0 +1,128 @@
1
+ require "ripper"
2
+
3
+ module Guideline
4
+ class HashCommaChecker < Checker
5
+ def check(path)
6
+ HashParser.parse(path.read) do |line|
7
+ report(
8
+ :message => "There should be a comma after the last value of Hash",
9
+ :line => line,
10
+ :path => path
11
+ )
12
+ end
13
+ end
14
+
15
+ class HashParser < Ripper
16
+ class << self
17
+ attr_reader :callback
18
+
19
+ def parse(*, &callback)
20
+ @callback = callback
21
+ super
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def on_lbrace(token)
28
+ push_stacks
29
+ end
30
+
31
+ def on_rbrace(token)
32
+ call(lineno) if has_error?
33
+ pop_stacks
34
+ end
35
+
36
+ def on_lbracket(token)
37
+ push_stacks
38
+ end
39
+
40
+ def on_rbracket(token)
41
+ pop_stacks
42
+ end
43
+
44
+ def on_lparen(token)
45
+ push_stacks
46
+ end
47
+
48
+ def on_rparen(token)
49
+ pop_stacks
50
+ end
51
+
52
+ def on_kw(token)
53
+ case token
54
+ when "do"
55
+ push_stacks
56
+ when "end"
57
+ pop_stacks
58
+ end
59
+ end
60
+
61
+ def on_op(token)
62
+ increase_key if token == "=>"
63
+ end
64
+
65
+ def on_comma(token)
66
+ increase_comma
67
+ end
68
+
69
+ def on_label(token)
70
+ increase_key
71
+ end
72
+
73
+ def on_nl(token)
74
+ increase_line
75
+ end
76
+ alias_method :on_ignored_nl, :on_nl
77
+
78
+ def call(method)
79
+ callback.call(method)
80
+ end
81
+
82
+ def callback
83
+ self.class.callback
84
+ end
85
+
86
+ def has_error?
87
+ line[-1] && key[-1] && comma[-1] &&
88
+ line[-1] > 0 && key[-1] > comma[-1]
89
+ end
90
+
91
+ def key
92
+ @key ||= []
93
+ end
94
+
95
+ def line
96
+ @line ||= []
97
+ end
98
+
99
+ def comma
100
+ @comma ||= []
101
+ end
102
+
103
+ def pop_stacks
104
+ key.pop
105
+ line.pop
106
+ comma.pop
107
+ end
108
+
109
+ def push_stacks
110
+ key << 0
111
+ line << 0
112
+ comma << 0
113
+ end
114
+
115
+ def increase_key
116
+ key[-1] += 1 if key[-1]
117
+ end
118
+
119
+ def increase_line
120
+ line[-1] += 1 if line[-1]
121
+ end
122
+
123
+ def increase_comma
124
+ comma[-1] += 1 if comma[-1]
125
+ end
126
+ end
127
+ end
128
+ end
@@ -43,7 +43,7 @@ module Guideline
43
43
  end
44
44
 
45
45
  def length
46
- @line.split(//).length
46
+ @line.split(//).length - 1
47
47
  end
48
48
 
49
49
  def lineno
@@ -0,0 +1,55 @@
1
+ require "yaml"
2
+ require "slop"
3
+
4
+ module Guideline
5
+ module Runner
6
+ extend self
7
+
8
+ def parse(argv)
9
+ hash = Parser.parse(argv)
10
+ hash[:config] = load_config(hash[:config])
11
+ hash
12
+ end
13
+
14
+ private
15
+
16
+ def load_config(path)
17
+ hash = load_yaml(path || default_config_path)
18
+ HashWithIndifferentAccess.new(hash)
19
+ end
20
+
21
+ def load_yaml(path)
22
+ YAML.load_file(path)
23
+ rescue Errno::ENOENT
24
+ raise "No such config file - #{path}"
25
+ end
26
+
27
+ def default_config_path
28
+ File.expand_path("../../../guideline.yml", __FILE__)
29
+ end
30
+
31
+ class Parser
32
+ def self.parse(argv)
33
+ new(argv).parse
34
+ end
35
+
36
+ def initialize(argv)
37
+ @argv = argv
38
+ end
39
+
40
+ def parse
41
+ slop.parse(@argv)
42
+ slop.to_hash
43
+ end
44
+
45
+ private
46
+
47
+ def slop
48
+ @slop ||= Slop.new(:help => true) do
49
+ banner "Usage: guidline [directory] [options]"
50
+ on :c=, :config=, "path to config YAML file"
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module Guideline
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -10,8 +10,12 @@ module Guideline
10
10
  @block = block
11
11
  end
12
12
 
13
- def check
14
- travel
13
+ def visit
14
+ paths.each do |path|
15
+ checkers.each do |checker|
16
+ checker.check(path)
17
+ end
18
+ end
15
19
  self
16
20
  end
17
21
 
@@ -25,14 +29,6 @@ module Guideline
25
29
 
26
30
  private
27
31
 
28
- def travel
29
- paths.each do |path|
30
- checkers.each do |checker|
31
- checker.check(path)
32
- end
33
- end
34
- end
35
-
36
32
  def paths
37
33
  PathFinder.find(options)
38
34
  end
data/lib/guideline.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  require "guideline/version"
2
2
  require "guideline/visitor"
3
3
  require "guideline/error"
4
+ require "guideline/runner"
4
5
  require "guideline/checker_factory"
5
6
  require "guideline/checkers/checker"
6
7
  require "guideline/checkers/long_line_checker"
7
8
  require "guideline/checkers/long_method_checker"
8
9
  require "guideline/checkers/hard_tab_indent_checker"
10
+ require "guideline/checkers/hash_comma_checker"
@@ -19,10 +19,10 @@ module Guideline
19
19
  end
20
20
 
21
21
  it "creates instances of given classes with given config" do
22
- LongLineChecker.should_receive(:new).with(:max => 80).and_call_original
23
- LongMethodChecker.should_receive(:new).with(:max => 10).and_call_original
24
22
  checkers[0].should be_a LongLineChecker
25
23
  checkers[1].should be_a LongMethodChecker
24
+ checkers[0].send(:max).should == 80
25
+ checkers[1].send(:max).should == 10
26
26
  end
27
27
  end
28
28
  end
@@ -26,7 +26,7 @@ module Guideline
26
26
  end
27
27
  end
28
28
 
29
- context "when there is no too long line" do
29
+ context "when there is no hard tab indent" do
30
30
  let(:content) do
31
31
  <<-EOF
32
32
  def foo
@@ -0,0 +1,102 @@
1
+ require "spec_helper"
2
+
3
+ module Guideline
4
+ describe HashCommaChecker do
5
+ describe "#check" do
6
+ subject do
7
+ checker.check(path)
8
+ checker
9
+ end
10
+
11
+ let(:checker) do
12
+ described_class.new
13
+ end
14
+
15
+ let(:path) do
16
+ mock(:read => script)
17
+ end
18
+
19
+ context "when there is no last comma in sigleline hash" do
20
+ let(:script) do
21
+ <<-EOF
22
+ { :a => 1 }
23
+ EOF
24
+ end
25
+
26
+ specify "checker does not detect error" do
27
+ should_not have_error
28
+ end
29
+ end
30
+
31
+ context "when there is last comma in sigleline hash" do
32
+ let(:script) do
33
+ <<-EOF
34
+ { :a => 1, }
35
+ EOF
36
+ end
37
+
38
+ specify "checker does not detect error" do
39
+ should_not have_error
40
+ end
41
+ end
42
+
43
+ context "when there is no last comma in multiline hash" do
44
+ let(:script) do
45
+ <<-EOF
46
+ {
47
+ :a => 1
48
+ }
49
+ EOF
50
+ end
51
+
52
+ specify "checker detects error" do
53
+ should have_error
54
+ end
55
+ end
56
+
57
+ context "when there is no last comma in multiline hash with array" do
58
+ let(:script) do
59
+ <<-EOF
60
+ {
61
+ :a => [1, 2, 3]
62
+ }
63
+ EOF
64
+ end
65
+
66
+ specify "checker detects error" do
67
+ should have_error
68
+ end
69
+ end
70
+
71
+ context "when there is last comma in multiline hash" do
72
+ let(:script) do
73
+ <<-EOF
74
+ {
75
+ :a => 1,
76
+ }
77
+ EOF
78
+ end
79
+
80
+ specify "checker does not detect error" do
81
+ should_not have_error
82
+ end
83
+ end
84
+
85
+ context "when there is no last comma in multiline nested hash" do
86
+ let(:script) do
87
+ <<-EOF
88
+ {
89
+ :a => {
90
+ :b => 1,
91
+ }
92
+ }
93
+ EOF
94
+ end
95
+
96
+ specify "checker detects error" do
97
+ should have_error
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -15,7 +15,7 @@ module Guideline
15
15
  let(:content) do
16
16
  <<-EOF
17
17
  def foo
18
- too looooooooooooooooooooooooooooooooooooooooooooooooooooong line
18
+ too looooooooooooooooooooooooong line
19
19
  end
20
20
  EOF
21
21
  end
@@ -30,7 +30,7 @@ module Guideline
30
30
  let(:content) do
31
31
  <<-EOF
32
32
  def foo
33
- not long line
33
+ not loooooooooooooooooooooooong line
34
34
  end
35
35
  EOF
36
36
  end
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ module Guideline
4
+ describe Runner do
5
+ describe ".parse" do
6
+ subject do
7
+ described_class.parse(argv)
8
+ end
9
+
10
+ let(:path) do
11
+ File.expand_path("../../../guideline.yml", __FILE__)
12
+ end
13
+
14
+ context "when config option is passed" do
15
+ let(:argv) do
16
+ ["--config", path]
17
+ end
18
+
19
+ it "parses ARGV and returns options with config hash" do
20
+ should have_key(:config)
21
+ end
22
+ end
23
+
24
+ context "when config option is passed in short version" do
25
+ let(:argv) do
26
+ ["-c", path]
27
+ end
28
+
29
+ it "parses ARGV and returns options with config hash" do
30
+ should have_key(:config)
31
+ end
32
+ end
33
+
34
+ context "when config option is not passed" do
35
+ let(:argv) do
36
+ []
37
+ end
38
+
39
+ it "parses ARGV and returns options with config hash" do
40
+ should have_key(:config)
41
+ end
42
+ end
43
+
44
+ context "when config option is wrong" do
45
+ let(:argv) do
46
+ ["--config", "wrong config path"]
47
+ end
48
+
49
+ it do
50
+ expect { subject }.to raise_error(StandardError)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -22,14 +22,10 @@ module Guideline
22
22
  mock
23
23
  end
24
24
 
25
- describe "#check" do
26
- subject do
27
- visitor.check
28
- end
29
-
25
+ describe "#visit" do
30
26
  it "calls checker#check with matched path" do
31
27
  checker.should_receive(:check).with(path)
32
- subject
28
+ visitor.visit
33
29
  end
34
30
  end
35
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guideline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-22 00:00:00.000000000 Z
12
+ date: 2012-11-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: slop
16
- requirement: &70129049410700 !ruby/object:Gem::Requirement
16
+ requirement: &70304744680520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70129049410700
24
+ version_requirements: *70304744680520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: active_support
27
- requirement: &70129049409360 !ruby/object:Gem::Requirement
27
+ requirement: &70304744680100 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70129049409360
35
+ version_requirements: *70304744680100
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: i18n
38
- requirement: &70129049423300 !ruby/object:Gem::Requirement
38
+ requirement: &70304744679680 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70129049423300
46
+ version_requirements: *70304744679680
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70129049419980 !ruby/object:Gem::Requirement
49
+ requirement: &70304744679180 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.12.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70129049419980
57
+ version_requirements: *70304744679180
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: pry
60
- requirement: &70129049417400 !ruby/object:Gem::Requirement
60
+ requirement: &70304744678760 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70129049417400
68
+ version_requirements: *70304744678760
69
69
  description: Guideline.gem checks if your code observes your coding guidelines
70
70
  email:
71
71
  - r7kamura@gmail.com
@@ -80,25 +80,26 @@ files:
80
80
  - README.md
81
81
  - Rakefile
82
82
  - bin/guideline
83
- - examples/hard_tab_indent_checker.rb
84
- - examples/long_line_checker.rb
85
- - examples/long_method_checker.rb
86
83
  - guideline.gemspec
87
84
  - guideline.yml
88
85
  - lib/guideline.rb
89
86
  - lib/guideline/checker_factory.rb
90
87
  - lib/guideline/checkers/checker.rb
91
88
  - lib/guideline/checkers/hard_tab_indent_checker.rb
89
+ - lib/guideline/checkers/hash_comma_checker.rb
92
90
  - lib/guideline/checkers/long_line_checker.rb
93
91
  - lib/guideline/checkers/long_method_checker.rb
94
92
  - lib/guideline/error.rb
93
+ - lib/guideline/runner.rb
95
94
  - lib/guideline/version.rb
96
95
  - lib/guideline/visitor.rb
97
96
  - spec/guideline/checker_factory_spec.rb
98
97
  - spec/guideline/checkers/checker_spec.rb
99
98
  - spec/guideline/checkers/hard_tab_indent_checker_spec.rb
99
+ - spec/guideline/checkers/hash_comma_checker_spec.rb
100
100
  - spec/guideline/checkers/long_line_checker_spec.rb
101
101
  - spec/guideline/checkers/long_method_checker_spec.rb
102
+ - spec/guideline/runner_spec.rb
102
103
  - spec/guideline/visitor_spec.rb
103
104
  - spec/guideline_spec.rb
104
105
  - spec/spec_helper.rb
@@ -130,8 +131,10 @@ test_files:
130
131
  - spec/guideline/checker_factory_spec.rb
131
132
  - spec/guideline/checkers/checker_spec.rb
132
133
  - spec/guideline/checkers/hard_tab_indent_checker_spec.rb
134
+ - spec/guideline/checkers/hash_comma_checker_spec.rb
133
135
  - spec/guideline/checkers/long_line_checker_spec.rb
134
136
  - spec/guideline/checkers/long_method_checker_spec.rb
137
+ - spec/guideline/runner_spec.rb
135
138
  - spec/guideline/visitor_spec.rb
136
139
  - spec/guideline_spec.rb
137
140
  - spec/spec_helper.rb
@@ -1,9 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
- require "guideline"
5
-
6
- checker = Guideline::HardTabIndentChecker.new
7
- visitor = Guideline::Visitor.new(:checker => checker)
8
- visitor.check
9
- visitor.render
@@ -1,9 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
- require "guideline"
5
-
6
- checker = Guideline::LongLineChecker.new(:max => 80)
7
- visitor = Guideline::Visitor.new(:checker => checker)
8
- visitor.check
9
- visitor.render
@@ -1,9 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
- require "guideline"
5
-
6
- checker = Guideline::LongMethodChecker.new(:max => 5)
7
- visitor = Guideline::Visitor.new(:checker => checker)
8
- visitor.check
9
- visitor.render