ast_utils 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23ff9e3ff8b73a951d4e8b806cfe365efeb05b0c2eba54fbace486ffc1da08b9
4
- data.tar.gz: 0c8f922226a0b684731bf1a847a8537cf815ae627faa8d6e2bd742e5b39ffc67
3
+ metadata.gz: 2dcd270ef11c2c8f91cc821dcc7cff592bfd719169c7c1096f7d06f1d3cfac5b
4
+ data.tar.gz: db15a5577fc9229b477ee9fd7a660278a31efbb48e4e2bcd6f230da756883f9d
5
5
  SHA512:
6
- metadata.gz: fa21e113ab51dd2e37e9074130ddbe3715c55b524deeef575f158ed642b88db1d66616a268ea79b5d15e916ad889a3b1c5853a3872b5175eb1b3c7481e84a3f9
7
- data.tar.gz: 30195cf9284d14db8233cde89dcb522cb9b790f2bdc4c4184cd9cdc9a76e483460df5d406d054daad0ac2064114daab45af5e55acc7b555515c56700cdae064a
6
+ metadata.gz: ab497bea22d86a24d481950b369550783ffc13aac4f55fa94d48bd6d71ec99fd18a2e2d1e1af63c62d01ce6821bafef4a9ecc1790dd29bbbe620c97bb0ee9301
7
+ data.tar.gz: 4746706461519e9cb7217f71f5ee4dd4454141ea42edc960979ff53f37d8e90920d58b254db9fbc82677ff7ac5115462e2b31c897c9d42d21fd0f066873a80b5
@@ -0,0 +1,27 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request: {}
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: "ubuntu-latest"
12
+ strategy:
13
+ matrix:
14
+ container_tag:
15
+ - 3.0-focal
16
+ - 2.6-bionic
17
+ - 2.7-bionic
18
+ container:
19
+ image: rubylang/ruby:${{ matrix.container_tag }}
20
+ steps:
21
+ - uses: actions/checkout@v1
22
+ - name: Run test
23
+ run: |
24
+ ruby -v
25
+ gem install bundler
26
+ bundle install --jobs 4 --retry 3
27
+ bundle exec rake
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in ast_utils.gemspec
4
3
  gemspec
4
+
5
+ gem "bundler"
6
+ gem "rake"
7
+ gem "minitest"
8
+ gem "thor"
@@ -20,10 +20,5 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.13"
24
- spec.add_development_dependency "rake", "~> 10.0"
25
- spec.add_development_dependency "minitest", "~> 5.0"
26
-
27
- spec.add_runtime_dependency "parser", "~> 2.4"
28
- spec.add_runtime_dependency "thor", ">= 0.19"
23
+ spec.add_runtime_dependency "parser", ">= 2.7.0"
29
24
  end
@@ -6,6 +6,14 @@ Parser::Builders::Default.emit_lambda = true
6
6
  Parser::Builders::Default.emit_procarg0 = true
7
7
 
8
8
  require "ast_utils"
9
- require "ast_utils/cli"
10
9
 
10
+ begin
11
+ require "thor"
12
+ rescue LoadError
13
+ puts "ast_utils command requires an optional `thor` gem:"
14
+ puts "Running `gem install thor` or having `gem 'thor'` in your Gemfile will solve the problem."
15
+ exit 1
16
+ end
17
+
18
+ require "ast_utils/cli"
11
19
  ASTUtils::CLI.start(ARGV)
@@ -3,7 +3,6 @@ require "ast_utils/version"
3
3
  require "pathname"
4
4
  require "set"
5
5
 
6
- require "ast_utils/node_set"
7
6
  require "ast_utils/node_helper"
8
7
  require "ast_utils/partial_map"
9
8
  require "ast_utils/labeling"
@@ -1,5 +1,3 @@
1
- require "thor"
2
-
3
1
  module ASTUtils
4
2
  class CLI < Thor
5
3
  desc "label SCRIPTS...", "labeling Ruby scripts given as SCRIPTS..."
@@ -8,8 +8,8 @@ module ASTUtils
8
8
 
9
9
  def initialize(node:)
10
10
  @root = node
11
- @nodes = NodeSet.new
12
- @parents = {}
11
+ @nodes = Set[].compare_by_identity
12
+ @parents = {}.compare_by_identity
13
13
  end
14
14
 
15
15
  def construct
@@ -20,13 +20,13 @@ module ASTUtils
20
20
  nodes << node
21
21
 
22
22
  each_child_node(node) do |child|
23
- parents[child.__id__] = node
23
+ parents[child] = node
24
24
  set_parent(child)
25
25
  end
26
26
  end
27
27
 
28
28
  def parent(node)
29
- parents[node.__id__]
29
+ parents[node]
30
30
  end
31
31
 
32
32
  def self.from(node:)
@@ -13,7 +13,7 @@ module ASTUtils
13
13
  end
14
14
 
15
15
  def eql?(other)
16
- other.is_a?(Assignment) && other.node.__id__ == node.__id__ && other.variable == variable
16
+ other.is_a?(Assignment) && other.node.equal?(node) && other.variable == variable
17
17
  end
18
18
 
19
19
  def ==(other)
@@ -34,48 +34,48 @@ module ASTUtils
34
34
 
35
35
  def initialize(root:)
36
36
  @root = root
37
- @all_scopes = NodeSet.new
38
- @child_scopes = {}
39
- @parent_scopes = {}
40
- @sub_scopes = {}
41
- @super_scopes = {}
42
- @assignment_nodes = {}
43
- @reference_nodes = {}
37
+ @all_scopes = Set.new.compare_by_identity
38
+ @child_scopes = {}.compare_by_identity
39
+ @parent_scopes = {}.compare_by_identity
40
+ @sub_scopes = {}.compare_by_identity
41
+ @super_scopes = {}.compare_by_identity
42
+ @assignment_nodes = {}.compare_by_identity
43
+ @reference_nodes = {}.compare_by_identity
44
44
  end
45
45
 
46
46
  def children(scope)
47
- child_scopes[scope.__id__]
47
+ child_scopes[scope]
48
48
  end
49
49
 
50
50
  def parent(scope)
51
- parent_scopes[scope.__id__]
51
+ parent_scopes[scope]
52
52
  end
53
53
 
54
54
  def subs(scope)
55
- sub_scopes[scope.__id__]
55
+ sub_scopes[scope]
56
56
  end
57
57
 
58
58
  def sup(scope)
59
- super_scopes[scope.__id__]
59
+ super_scopes[scope]
60
60
  end
61
61
 
62
62
  def assignments(scope, include_subs: false)
63
63
  if include_subs
64
- subs(scope).inject(assignment_nodes[scope.__id__]) {|assignments, scope_|
64
+ subs(scope).inject(assignment_nodes[scope]) {|assignments, scope_|
65
65
  assignments + assignments(scope_, include_subst: true)
66
66
  }
67
67
  else
68
- assignment_nodes[scope.__id__]
68
+ assignment_nodes[scope]
69
69
  end
70
70
  end
71
71
 
72
72
  def references(scope, include_subs: false)
73
73
  if include_subs
74
- subs(scope).inject(reference_nodes[scope.__id__]) {|references, scope_|
74
+ subs(scope).inject(reference_nodes[scope]) {|references, scope_|
75
75
  references + references(scope_, include_subs: true)
76
76
  }
77
77
  else
78
- reference_nodes[scope.__id__]
78
+ reference_nodes[scope]
79
79
  end
80
80
 
81
81
  end
@@ -95,18 +95,18 @@ module ASTUtils
95
95
 
96
96
  def add_scope(scope)
97
97
  all_scopes << scope
98
- child_scopes[scope.__id__] = NodeSet.new
99
- sub_scopes[scope.__id__] = NodeSet.new
100
- assignment_nodes[scope.__id__] = Set.new
101
- reference_nodes[scope.__id__] = Set.new
98
+ child_scopes[scope] = Set.new.compare_by_identity
99
+ sub_scopes[scope] = Set.new.compare_by_identity
100
+ assignment_nodes[scope] = Set.new.compare_by_identity
101
+ reference_nodes[scope] = Set.new.compare_by_identity
102
102
  end
103
103
 
104
104
  def child_scope!(scope, parent_scope)
105
105
  add_scope(scope)
106
106
 
107
107
  if parent_scope
108
- parent_scopes[scope.__id__] = parent_scope
109
- child_scopes[parent_scope.__id__] << scope
108
+ parent_scopes[scope] = parent_scope
109
+ child_scopes[parent_scope] << scope
110
110
  end
111
111
 
112
112
  each_child_node(scope) do |child|
@@ -118,10 +118,10 @@ module ASTUtils
118
118
  add_scope(scope)
119
119
 
120
120
  if super_scope
121
- parent_scopes[scope.__id__] = super_scope
122
- child_scopes[super_scope.__id__] << scope
123
- super_scopes[scope.__id__] = super_scope
124
- sub_scopes[super_scope.__id__] << scope
121
+ parent_scopes[scope] = super_scope
122
+ child_scopes[super_scope] << scope
123
+ super_scopes[scope] = super_scope
124
+ sub_scopes[super_scope] << scope
125
125
  end
126
126
 
127
127
  each_child_node(scope) do |child|
@@ -136,21 +136,21 @@ module ASTUtils
136
136
  when :block
137
137
  nested_scope!(node, current_scope)
138
138
  when :lvar
139
- reference_nodes[current_scope.__id__] << node
139
+ reference_nodes[current_scope] << node
140
140
  when :lvasgn, :arg, :optarg, :restarg, :kwarg, :kwoptarg, :kwrestarg, :blockarg
141
- assignment_nodes[current_scope.__id__] << Assignment.new(node: node, variable: node.children[0])
141
+ assignment_nodes[current_scope] << Assignment.new(node: node, variable: node.children[0])
142
142
  construct_children(node, current_scope)
143
143
  when :procarg0
144
144
  case node.children[0]
145
145
  when AST::Node
146
146
  construct_children(node, current_scope)
147
147
  else
148
- assignment_nodes[current_scope.__id__] << Assignment.new(node: node, variable: node.children[0])
148
+ assignment_nodes[current_scope] << Assignment.new(node: node, variable: node.children[0])
149
149
  construct_children(node, current_scope)
150
150
  end
151
151
  when :match_with_lvasgn
152
152
  node.children[2].each do |var|
153
- assignment_nodes[current_scope.__id__] << Assignment.new(node: node, variable: var)
153
+ assignment_nodes[current_scope] << Assignment.new(node: node, variable: var)
154
154
  end
155
155
 
156
156
  construct_children(node, current_scope)
@@ -1,3 +1,3 @@
1
1
  module ASTUtils
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,85 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ast_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-22 00:00:00.000000000 Z
11
+ date: 2020-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.13'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.13'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '10.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '10.0'
41
- - !ruby/object:Gem::Dependency
42
- name: minitest
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '5.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '5.0'
55
13
  - !ruby/object:Gem::Dependency
56
14
  name: parser
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '2.4'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '2.4'
69
- - !ruby/object:Gem::Dependency
70
- name: thor
71
15
  requirement: !ruby/object:Gem::Requirement
72
16
  requirements:
73
17
  - - ">="
74
18
  - !ruby/object:Gem::Version
75
- version: '0.19'
19
+ version: 2.7.0
76
20
  type: :runtime
77
21
  prerelease: false
78
22
  version_requirements: !ruby/object:Gem::Requirement
79
23
  requirements:
80
24
  - - ">="
81
25
  - !ruby/object:Gem::Version
82
- version: '0.19'
26
+ version: 2.7.0
83
27
  description: Ruby AST Utility
84
28
  email:
85
29
  - matsumoto@soutaro.com
@@ -88,6 +32,7 @@ executables:
88
32
  extensions: []
89
33
  extra_rdoc_files: []
90
34
  files:
35
+ - ".github/workflows/test.yml"
91
36
  - ".gitignore"
92
37
  - ".travis.yml"
93
38
  - Gemfile
@@ -102,14 +47,13 @@ files:
102
47
  - lib/ast_utils/labeling.rb
103
48
  - lib/ast_utils/navigation.rb
104
49
  - lib/ast_utils/node_helper.rb
105
- - lib/ast_utils/node_set.rb
106
50
  - lib/ast_utils/partial_map.rb
107
51
  - lib/ast_utils/scope.rb
108
52
  - lib/ast_utils/version.rb
109
53
  homepage: https://github.com/soutaro/ast_utils
110
54
  licenses: []
111
55
  metadata: {}
112
- post_install_message:
56
+ post_install_message:
113
57
  rdoc_options: []
114
58
  require_paths:
115
59
  - lib
@@ -124,9 +68,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
68
  - !ruby/object:Gem::Version
125
69
  version: '0'
126
70
  requirements: []
127
- rubyforge_project:
128
- rubygems_version: 2.7.3
129
- signing_key:
71
+ rubygems_version: 3.0.6
72
+ signing_key:
130
73
  specification_version: 4
131
74
  summary: Ruby AST Utility
132
75
  test_files: []
@@ -1,55 +0,0 @@
1
- module ASTUtils
2
- class NodeSet
3
- class Item
4
- attr_reader :object
5
-
6
- def initialize(object)
7
- @object = object
8
- end
9
-
10
- def hash
11
- object.__id__
12
- end
13
-
14
- def eql?(other)
15
- other.is_a?(Item) && other.object.__id__ == object.__id__
16
- end
17
-
18
- def ==(other)
19
- eql?(other)
20
- end
21
- end
22
-
23
- attr_reader :set
24
-
25
- def initialize(objects = [])
26
- @set = Set.new(objects.map {|object| Item.new(object) })
27
- end
28
-
29
- def <<(node)
30
- set << Item.new(node)
31
- end
32
-
33
- def delete(node)
34
- set.delete Item.new(node)
35
- end
36
-
37
- def each(&block)
38
- set.map(&:object).each(&block)
39
- end
40
-
41
- def empty?
42
- size == 0
43
- end
44
-
45
- def size
46
- set.size
47
- end
48
-
49
- include Enumerable
50
-
51
- def +(other)
52
- self.class.new(self.set + other.set)
53
- end
54
- end
55
- end