code_analyzer 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/code_analyzer.rb +1 -0
- data/lib/code_analyzer/analyzer_exception.rb +1 -0
- data/lib/code_analyzer/checker.rb +1 -0
- data/lib/code_analyzer/checking_visitor.rb +1 -0
- data/lib/code_analyzer/checking_visitor/base.rb +15 -0
- data/lib/code_analyzer/checking_visitor/default.rb +16 -5
- data/lib/code_analyzer/checking_visitor/plain.rb +6 -8
- data/lib/code_analyzer/version.rb +2 -1
- data/spec/code_analyzer/checking_visitor/base_spec.rb +16 -0
- metadata +9 -6
data/README.md
CHANGED
data/lib/code_analyzer.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module CodeAnalyzer::CheckingVisitor
|
3
|
+
# Base class for checking visitor.
|
4
|
+
class Base
|
5
|
+
def initialize(options={})
|
6
|
+
@checkers = options[:checkers]
|
7
|
+
end
|
8
|
+
|
9
|
+
def after_check; end
|
10
|
+
|
11
|
+
def warnings
|
12
|
+
@warnings ||= @checkers.map(&:warnings).flatten
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,9 +1,10 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module CodeAnalyzer::CheckingVisitor
|
3
|
-
|
3
|
+
# This is the default checking visitor to check ruby sexp nodes.
|
4
|
+
class Default < Base
|
4
5
|
def initialize(options={})
|
6
|
+
super
|
5
7
|
@checks = {}
|
6
|
-
@checkers = options[:checkers]
|
7
8
|
@checkers.each do |checker|
|
8
9
|
checker.interesting_nodes.each do |node|
|
9
10
|
@checks[node] ||= []
|
@@ -11,14 +12,19 @@ module CodeAnalyzer::CheckingVisitor
|
|
11
12
|
@checks[node].uniq!
|
12
13
|
end
|
13
14
|
end
|
14
|
-
|
15
|
+
end
|
15
16
|
|
17
|
+
# check the ruby sexp nodes for the ruby file.
|
18
|
+
#
|
19
|
+
# @param [String] filename is the filename of ruby code.
|
20
|
+
# @param [String] content is the content of ruby file.
|
16
21
|
def check(filename, content)
|
17
22
|
node = parse(filename, content)
|
18
23
|
node.file = filename
|
19
24
|
check_node(node)
|
20
25
|
end
|
21
26
|
|
27
|
+
# trigger all after_check callbacks defined in all checkers.
|
22
28
|
def after_check
|
23
29
|
@checkers.each do |checker|
|
24
30
|
after_check_callbacks = checker.class.get_callbacks(:after_check)
|
@@ -30,14 +36,19 @@ module CodeAnalyzer::CheckingVisitor
|
|
30
36
|
|
31
37
|
# parse ruby code.
|
32
38
|
#
|
33
|
-
# @param [String] filename is the filename of ruby
|
34
|
-
# @param [String] content is the
|
39
|
+
# @param [String] filename is the filename of ruby code.
|
40
|
+
# @param [String] content is the content of ruby file.
|
35
41
|
def parse(filename, content)
|
36
42
|
Sexp.from_array(Ripper::SexpBuilder.new(content).parse)
|
37
43
|
rescue Exception
|
38
44
|
raise AnalyzerException.new("#{filename} looks like it's not a valid Ruby file. Skipping...")
|
39
45
|
end
|
40
46
|
|
47
|
+
# recursively check ruby sexp node.
|
48
|
+
#
|
49
|
+
# 1. it triggers the interesting checkers' start callbacks.
|
50
|
+
# 2. recursively check the sexp children.
|
51
|
+
# 3. it triggers the interesting checkers' end callbacks.
|
41
52
|
def check_node(node)
|
42
53
|
checkers = @checks[node.sexp_type]
|
43
54
|
if checkers
|
@@ -1,17 +1,15 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module CodeAnalyzer::CheckingVisitor
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
# This is the checking visitor to check ruby plain code.
|
4
|
+
class Plain < Base
|
5
|
+
# check the ruby plain code.
|
6
|
+
#
|
7
|
+
# @param [String] filename is the filename of ruby code.
|
8
|
+
# @param [String] content is the content of ruby file.
|
8
9
|
def check(filename, content)
|
9
10
|
@checkers.each do |checker|
|
10
11
|
checker.check(filename, content)
|
11
12
|
end
|
12
13
|
end
|
13
|
-
|
14
|
-
def after_check
|
15
|
-
end
|
16
14
|
end
|
17
15
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module CodeAnalyzer
|
4
|
+
module CheckingVisitor
|
5
|
+
describe Base do
|
6
|
+
it "should return all checkers' warnings" do
|
7
|
+
warning1 = Warning.new
|
8
|
+
checker1 = mock(:checker, warnings: [warning1])
|
9
|
+
warning2 = Warning.new
|
10
|
+
checker2 = mock(:checker, warnings: [warning2])
|
11
|
+
visitor = Base.new(checkers: [checker1, checker2])
|
12
|
+
visitor.warnings.should == [warning1, warning2]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code_analyzer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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-09-
|
12
|
+
date: 2012-09-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sexp_processor
|
16
|
-
requirement: &
|
16
|
+
requirement: &70129984050860 !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: *
|
24
|
+
version_requirements: *70129984050860
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70129984046400 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70129984046400
|
36
36
|
description: a code analyzer tool which extracted from rails_best_practices, it helps
|
37
37
|
you easily build your own code analyzer tool.
|
38
38
|
email:
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- lib/code_analyzer/analyzer_exception.rb
|
54
54
|
- lib/code_analyzer/checker.rb
|
55
55
|
- lib/code_analyzer/checking_visitor.rb
|
56
|
+
- lib/code_analyzer/checking_visitor/base.rb
|
56
57
|
- lib/code_analyzer/checking_visitor/default.rb
|
57
58
|
- lib/code_analyzer/checking_visitor/plain.rb
|
58
59
|
- lib/code_analyzer/nil.rb
|
@@ -60,6 +61,7 @@ files:
|
|
60
61
|
- lib/code_analyzer/version.rb
|
61
62
|
- lib/code_analyzer/warning.rb
|
62
63
|
- spec/code_analyzer/checker_spec.rb
|
64
|
+
- spec/code_analyzer/checking_visitor/base_spec.rb
|
63
65
|
- spec/code_analyzer/checking_visitor/default_spec.rb
|
64
66
|
- spec/code_analyzer/checking_visitor/plain_spec.rb
|
65
67
|
- spec/code_analyzer/nil_spec.rb
|
@@ -92,6 +94,7 @@ specification_version: 3
|
|
92
94
|
summary: a code analyzer helps you build your own code analyzer tool.
|
93
95
|
test_files:
|
94
96
|
- spec/code_analyzer/checker_spec.rb
|
97
|
+
- spec/code_analyzer/checking_visitor/base_spec.rb
|
95
98
|
- spec/code_analyzer/checking_visitor/default_spec.rb
|
96
99
|
- spec/code_analyzer/checking_visitor/plain_spec.rb
|
97
100
|
- spec/code_analyzer/nil_spec.rb
|