courgette 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b29ee38ee6dfcfc6cc22cbc3caf3f00b08c0cc02
4
- data.tar.gz: 5cdd93f711eec0daeaba7057f251be5478fe52bd
3
+ metadata.gz: f615e4ef69593e21d1de8c8e0f5a1f4137777bd6
4
+ data.tar.gz: 9972586e2328530973db1e9234d6fd6978a214e8
5
5
  SHA512:
6
- metadata.gz: 8d94271ccd57572f4313b620f87e57212821bae2660071e50433122176d00a34387ef544586def5673b9b52d1ea2cb5bb82ed103bc62a3cc676bca387ce2efa1
7
- data.tar.gz: 49432ae8c095a31cd30f88e3387c5054ad1a74fc0dbb4c9039f2164c6058d42e017bc1d4730270be4b67a0463d257c8557810189473e1adff5cf4c428353599f
6
+ metadata.gz: 31414ebd38fb6d5a3328e0c4e46be2d6bdc7fd2b97b66beabcc7f081140c2fe2ea31ca489168cf538fee32e025f88d9a7268b08f46b3f1720c56b1b5314ca3c5
7
+ data.tar.gz: 86c6a07660d04acffb78c4909b1a90fd67548f0952b7e0876b0a9de511608b3b144134ada8869de57b8f5992f287d68272709ac05b83ef21219cc205630b5890
data/courgette.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "ruby_parser"
21
+ spec.add_dependency "parser"
22
22
  spec.add_dependency "commander"
23
23
  spec.add_dependency "graph"
24
24
 
@@ -12,6 +12,7 @@ module Courgette
12
12
  end
13
13
 
14
14
  def add_reference reference, context
15
+ context = [Kernel] if context.empty?
15
16
  @references << Reference.new(reference, context)
16
17
  end
17
18
 
@@ -74,11 +75,13 @@ module Courgette
74
75
  end
75
76
 
76
77
  def iterate_definition sexpr, context
77
- s = scope sexpr[1]
78
+ s = scope sexpr.children[0]
78
79
  new_context = context + [s]
80
+
79
81
  add_definition new_context
80
82
 
81
- iterate_many sexpr[2..-1], new_context
83
+
84
+ iterate_many sexpr.children[1..-1], new_context
82
85
  end
83
86
 
84
87
  def iterate_call sexpr, context
@@ -87,23 +90,17 @@ module Courgette
87
90
  end
88
91
 
89
92
  def iterate sexpr, context
90
- return unless sexpr.is_a? Enumerable
93
+ return unless sexpr.is_a? Parser::AST::Node
91
94
 
92
- case sexpr[0]
95
+ case sexpr.type
93
96
  when :const, :colon2
94
97
  add_reference context, scope(sexpr)
95
98
  when :module, :class
96
99
  iterate_definition sexpr, context
97
- when :cdecl
98
- iterate_cdecl sexpr, context
99
- when :defn
100
- iterate_many sexpr[3..-1], context
101
-
102
- when :call
103
- iterate_call sexpr, context
104
- when :nil, :lit
100
+ when :casgn
101
+ add_definition context + [sexpr.children[1]]
105
102
  else
106
- iterate_many sexpr[1..-1], context
103
+ iterate_many sexpr.children, context
107
104
  end
108
105
  end
109
106
  end
@@ -1,17 +1,21 @@
1
- require 'ruby_parser'
1
+ require 'parser/current'
2
2
 
3
3
  module Courgette
4
4
  class FileToSexpr
5
- def initialize
6
- @parser = RubyParser.new
5
+ def initialize p=nil
6
+ @parser = p || default_parser
7
+ end
8
+
9
+ def default_parser
10
+ Parser::CurrentRuby
7
11
  end
8
12
 
9
13
  def convert filename
10
14
  contents = File.read filename
11
15
 
12
16
  begin
13
- x = @parser.parse contents
14
- rescue Racc::ParseError, RubyParser::SyntaxError => e
17
+ @parser.parse contents
18
+ rescue Parser::SyntaxError => e
15
19
  $stderr.puts "Error parsing #{filename}: #{e} (file ignored)"
16
20
  end
17
21
  end
@@ -1,13 +1,10 @@
1
1
  module Courgette
2
2
  class Scope
3
3
  def scope sexpr
4
- case sexpr[0]
5
- when :colon2
6
- scope(sexpr[1]) + scope(sexpr[2])
7
- when :const
8
- scope sexpr[1]
4
+ if sexpr.respond_to? :type
5
+ sexpr.children.flat_map { |c| scope c }
9
6
  else
10
- [sexpr]
7
+ [sexpr].compact
11
8
  end
12
9
  end
13
10
  end
@@ -1,4 +1,3 @@
1
1
  module Courgette
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
4
-
@@ -2,6 +2,7 @@ require 'simplecov'
2
2
  require 'minitest/autorun'
3
3
  require 'courgette/capturer'
4
4
  require 'mocha/setup'
5
+ require 'parser/current'
5
6
 
6
7
  class TestCourgetteCaptured < MiniTest::Unit::TestCase
7
8
  def setup
@@ -40,6 +41,11 @@ class TestCourgetteCapturer < MiniTest::Unit::TestCase
40
41
  def setup
41
42
  @observer = mock()
42
43
  @object = Courgette::Capturer.new @observer
44
+ @parser = Parser::CurrentRuby
45
+ end
46
+
47
+ def capture code
48
+ @object.capture @parser.parse(code)
43
49
  end
44
50
 
45
51
  def test_references_delegation
@@ -57,53 +63,53 @@ class TestCourgetteCapturer < MiniTest::Unit::TestCase
57
63
  def test_class_definition
58
64
  @observer.expects(:add_definition).with([:X]).once
59
65
 
60
- @object.capture [:class, :X]
66
+ capture "class X; end"
61
67
  end
62
68
 
63
69
  def test_module_definition
64
70
  @observer.expects(:add_definition).with([:M]).once
65
71
 
66
- @object.capture [:module, :M]
72
+ capture "module M; end"
67
73
  end
68
74
 
69
75
  def test_class_inside_module
70
76
  @observer.expects(:add_definition).with([:M]).once
71
77
  @observer.expects(:add_definition).with([:M, :C]).once
72
78
 
73
- @object.capture [:module, :M, [:class, :C]]
79
+ capture "module M; class C; end; end"
74
80
  end
75
81
 
76
82
  def test_constant_declaration
77
83
  @observer.expects(:add_definition).with([:C]).once
78
84
 
79
- @object.capture [:cdecl, :C, [:lit, 1]]
85
+ capture "C = 1"
80
86
  end
81
87
 
82
88
  def test_scoped_definition
83
89
  @observer.expects(:add_definition).with([:M, :C]).once
84
90
 
85
- @object.capture [:module, [:colon2, [:const, :M], :C]]
91
+ capture "module M::C; end"
86
92
  end
87
93
 
88
94
  def test_reference
89
95
  @observer.expects(:add_reference).with([:C], []).once
90
96
 
91
- @object.capture [:const, :C]
97
+ capture "C"
92
98
  end
93
99
 
94
100
  def test_class_call
95
101
  @observer.expects(:add_reference).with([:C], []).once
96
102
 
97
- @object.capture [:call, [:const, :C], :new]
103
+ capture "C.new"
98
104
  end
99
105
 
100
106
  def test_reference_in_method
101
107
  @observer.expects(:add_reference).with([:C], []).once
102
108
 
103
- @object.capture [:defn, :bananas, [], [:const, :C]]
109
+ capture "def bananas; C; end"
104
110
  end
105
111
 
106
112
  def test_others
107
- @object.capture [:if, [:lit, 42]]
113
+ capture "if 42; end"
108
114
  end
109
115
  end
@@ -7,8 +7,14 @@ class TestCourgetteScope < MiniTest::Unit::TestCase
7
7
  @scope = Courgette::Scope.new
8
8
  end
9
9
 
10
+ class ConstNode < Struct.new(:children)
11
+ def type
12
+ :const
13
+ end
14
+ end
15
+
10
16
  def test_const
11
- assert_equal [:Name], @scope.scope([:const, :Name])
17
+ assert_equal [:Name], @scope.scope(ConstNode.new([nil, :Name]))
12
18
  end
13
19
 
14
20
  def test_other
@@ -17,16 +23,16 @@ class TestCourgetteScope < MiniTest::Unit::TestCase
17
23
 
18
24
  def test_colon2
19
25
  assert_equal [:Scoped, :Name],
20
- @scope.scope([:colon2, :Scoped, :Name])
26
+ @scope.scope(ConstNode.new([ConstNode.new([nil, :Scoped]), :Name]))
21
27
  end
22
28
 
23
29
  def test_compose
24
30
  assert_equal [:Really, :Scoped, :Name],
25
31
  @scope.scope(
26
- [:colon2,
27
- [:colon2,
28
- [:const, :Really],
29
- [:const, :Scoped]],
30
- [:const, :Name]])
32
+ ConstNode.new([
33
+ ConstNode.new([
34
+ ConstNode.new([nil, :Really]),
35
+ :Scoped]),
36
+ :Name]))
31
37
  end
32
38
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: courgette
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hugo Peixoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-20 00:00:00.000000000 Z
11
+ date: 2015-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: ruby_parser
14
+ name: parser
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
171
  version: '0'
172
172
  requirements: []
173
173
  rubyforge_project:
174
- rubygems_version: 2.2.0.preview.1
174
+ rubygems_version: 2.2.2
175
175
  signing_key:
176
176
  specification_version: 4
177
177
  summary: Courgette analyses a set of files, and calculates a dependency graph
@@ -180,3 +180,4 @@ test_files:
180
180
  - test/courgette/test_name_resolution.rb
181
181
  - test/courgette/test_reference_to_dependency.rb
182
182
  - test/courgette/test_scope.rb
183
+ has_rdoc: