rbs_heuristic_prototype 0.1.0 → 0.2.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: a07c6fce554f459391e7d81cfee78e42cd54a78704d1ccefa27f6b289cdce67e
4
- data.tar.gz: 78846ef4bcd829e0cb74de64feedc83314599fac49f9174919f6f72fd8e8c180
3
+ metadata.gz: 2ee5a4a5f00977d9e0dbc066729d092cec68269d386873fc3f5f54a916c307c9
4
+ data.tar.gz: 2418855e5847a4acaba0a3f03f9154126ab26026168a70764a3e30bec74d38fa
5
5
  SHA512:
6
- metadata.gz: a4ad899d1c92ff8ebfa26dab2d041357440724637b1c28094d9748b4024b97214bd9a2328ddbc524e35cc0c468d0658113d31484ee34b93cae6d2d545e1e0aa2
7
- data.tar.gz: ff09d89ee6476e0951e31fad137b172130cb82820072a8e6a86f85718c9afa9e6a36dcc384e66e12d774d71a38609bb1287b6326bb063c97e1cf94537e5dc046
6
+ metadata.gz: b50cbc5fdbef39926001a9a6ca9c4844a62985a4f1827d69bc03f4963059875ce48a1c235fc15d5f9d3479475903225dbf74f63bd4448d024ad69c007e1ff68e
7
+ data.tar.gz: 8d7c8d7b4a71c9574cd31b65359548fdad6e7782d00e675352a4ff77ec268bcf8ff864dd47f900783a4f575b8745148cc412916b793413f326822f1d57b92902
data/.rubocop.yml CHANGED
@@ -15,5 +15,9 @@ Style/StringLiteralsInInterpolation:
15
15
  Layout/LineLength:
16
16
  Max: 120
17
17
 
18
+ Metrics/BlockLength:
19
+ Exclude:
20
+ - "spec/**/*"
21
+
18
22
  Metrics/MethodLength:
19
23
  Max: 20
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rbs_heuristic_prototype (0.1.0)
4
+ rbs_heuristic_prototype (0.2.0)
5
5
  rbs
6
6
 
7
7
  GEM
@@ -35,7 +35,7 @@ GEM
35
35
  coderay (1.1.3)
36
36
  concurrent-ruby (1.2.2)
37
37
  crass (1.0.6)
38
- csv (3.2.6)
38
+ csv (3.2.7)
39
39
  diff-lcs (1.5.0)
40
40
  erubi (1.12.0)
41
41
  ffi (1.15.5)
@@ -106,7 +106,7 @@ GEM
106
106
  diff-lcs (>= 1.2.0, < 2.0)
107
107
  rspec-support (~> 3.12.0)
108
108
  rspec-support (3.12.0)
109
- rubocop (1.54.1)
109
+ rubocop (1.54.2)
110
110
  json (~> 2.3)
111
111
  language_server-protocol (>= 3.17.0)
112
112
  parallel (~> 1.10)
@@ -121,9 +121,9 @@ GEM
121
121
  parser (>= 3.2.1.0)
122
122
  ruby-progressbar (1.13.0)
123
123
  securerandom (0.2.2)
124
- steep (1.4.0)
124
+ steep (1.5.0)
125
125
  activesupport (>= 5.1)
126
- concurrent-ruby (>= 1.2.2)
126
+ concurrent-ruby (>= 1.1.10)
127
127
  csv (>= 3.0.9)
128
128
  fileutils (>= 1.1.0)
129
129
  json (>= 2.1.0)
@@ -132,7 +132,7 @@ GEM
132
132
  logger (>= 1.3.0)
133
133
  parser (>= 3.1)
134
134
  rainbow (>= 2.2.2, < 4.0)
135
- rbs (>= 2.8.0)
135
+ rbs (>= 3.1.0)
136
136
  securerandom (>= 0.1)
137
137
  strscan (>= 1.0.0)
138
138
  terminal-table (>= 2, < 4)
data/README.md CHANGED
@@ -29,7 +29,7 @@ gems:
29
29
  ## Usage
30
30
 
31
31
  1. Run `rbs prototype rb` or `rbs prototype runtime` first
32
- 2. Run `rbs:prototype:heuristic`
32
+ 2. Run `rbs:prototype:heuristic:apply`
33
33
 
34
34
  Then rbs_heuristic_prototype will update the prototype signature files generated
35
35
  by `rbs prototype` according to the heuristic rules.
@@ -40,6 +40,9 @@ by `rbs prototype` according to the heuristic rules.
40
40
  * Convert a constant annotated with Array of Union of symbols (ex. `Array[:sym1 | :sym2 | ...]`) to a constant annotated with `Array[Symbol]`
41
41
  * This is useful to avoid `Ruby::IncompatibleAssignment` warning when right hand value uses `#freeze` method
42
42
  * ref: https://github.com/soutaro/steep/issues/363
43
+ * Rule 3:
44
+ * Convert a scoped module/class definition (ex. `Foo::Bar::Baz`) to the nested definitions
45
+ * This is useful to define intermediate modules automatically like what Rails and zeitwerk does.
43
46
 
44
47
  ## Development
45
48
 
@@ -64,7 +67,5 @@ The gem is available as open source under the terms of the [MIT License](https:/
64
67
 
65
68
  ## Code of Conduct
66
69
 
67
-
68
-
69
70
  Everyone interacting in the RbsHeuristicPrototype project's codebases, issue trackers is
70
71
  expected to follow the [code of conduct](https://github.com/tk0miya/rbs_heuristic_prototype/blob/main/CODE_OF_CONDUCT.md).
data/Steepfile CHANGED
@@ -5,8 +5,7 @@ D = Steep::Diagnostic
5
5
  target :lib do
6
6
  signature "sig"
7
7
 
8
- check "lib" # Directory name
9
- check "Gemfile" # File name
8
+ check "lib"
10
9
 
11
- configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
10
+ configure_code_diagnostics(D::Ruby.lenient)
12
11
  end
@@ -8,11 +8,16 @@ module RbsHeuristicPrototype
8
8
  attr_reader :klass
9
9
 
10
10
  def process_class(decl)
11
- @klass = const_get(decl)
11
+ klass = const_get(decl)
12
12
 
13
- return decl unless klass&.ancestors&.include?(ActiveModel::Validations)
14
-
15
- super
13
+ if klass&.ancestors&.include?(ActiveModel::Validations)
14
+ @klass = klass
15
+ super
16
+ else
17
+ decl
18
+ end
19
+ ensure
20
+ @klass = nil
16
21
  end
17
22
 
18
23
  def process_member(member)
@@ -84,8 +84,9 @@ module RbsHeuristicPrototype
84
84
  end
85
85
 
86
86
  def const_get(decl)
87
- decl.name.to_namespace.path.inject(Kernel) do |const, mod|
88
- const.const_get(mod)
87
+ init = Kernel # : singleton(Class)?
88
+ decl.name.to_namespace.path.inject(init) do |const, mod|
89
+ const&.const_get(mod)
89
90
  end
90
91
  rescue StandardError
91
92
  nil
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module RbsHeuristicPrototype
6
+ module Filters
7
+ class DeepModuleFilter < Base
8
+ attr_reader :stack
9
+
10
+ def initialize(env)
11
+ super
12
+ @stack = [::Kernel]
13
+ end
14
+
15
+ def process_module(decl) # rubocop:disable Metrics/AbcSize
16
+ if decl.name.namespace.empty?
17
+ namespaces = []
18
+ stack << const_get(decl)
19
+ super
20
+ else
21
+ namespaces = namespace_to_decl!(decl.name.namespace)
22
+ decl = decl.dup
23
+ decl.instance_eval { @name = RBS::TypeName.new(name: name.name, namespace: RBS::Namespace.empty) }
24
+ (_ = namespaces.last).members << super
25
+ namespaces.first or raise
26
+ end
27
+ ensure
28
+ namespaces.size.succ.times { stack.pop }
29
+ end
30
+
31
+ def process_class(decl) # rubocop:disable Metrics/AbcSize
32
+ if decl.name.namespace.empty?
33
+ namespaces = []
34
+ stack << const_get(decl)
35
+ super
36
+ else
37
+ namespaces = namespace_to_decl!(decl.name.namespace)
38
+ decl = decl.dup
39
+ decl.instance_eval { @name = RBS::TypeName.new(name: name.name, namespace: RBS::Namespace.empty) }
40
+ stack << stack.last&.const_get(decl.name.name)
41
+ (_ = namespaces.last).members << super
42
+ namespaces.first or raise
43
+ end
44
+ ensure
45
+ namespaces.size.succ.times { stack.pop }
46
+ end
47
+
48
+ def namespace_to_decl!(namespace)
49
+ namespaces = []
50
+ namespace.path.each do |path|
51
+ obj = stack.last&.const_get(path)
52
+ stack << obj
53
+ decl = obj_to_decl(obj)
54
+ namespaces.last.members << decl unless namespaces.empty?
55
+ namespaces << decl
56
+ end
57
+ namespaces
58
+ end
59
+
60
+ def obj_to_decl(obj) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
61
+ if obj.is_a?(Class)
62
+ if (superclass = obj.superclass)
63
+ super_class = RBS::AST::Declarations::Class::Super.new(name: TypeName(superclass.name.to_s),
64
+ args: [],
65
+ location: nil)
66
+ end
67
+ RBS::AST::Declarations::Class.new(
68
+ name: TypeName(obj.name.to_s.split("::").last.to_s),
69
+ type_params: [],
70
+ super_class:,
71
+ members: [],
72
+ annotations: [],
73
+ location: nil,
74
+ comment: nil
75
+ )
76
+ else
77
+ RBS::AST::Declarations::Module.new(
78
+ name: TypeName(obj.name.to_s.split("::").last.to_s),
79
+ type_params: [],
80
+ members: [],
81
+ self_types: [],
82
+ annotations: [],
83
+ location: nil,
84
+ comment: nil
85
+ )
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -19,11 +19,9 @@ module RbsHeuristicPrototype
19
19
  end
20
20
 
21
21
  def process_constant_type(type)
22
- # @type var location: untyped
23
22
  name = RBS::TypeName.new(namespace: RBS::Namespace.root, name: :Symbol)
24
- location = type.location
25
- symbol = RBS::Types::Alias.new(name:, args: [], location:)
26
- RBS::Types::ClassInstance.new(name: type.name, args: [symbol], location:)
23
+ symbol = RBS::Types::Alias.new(name:, args: [], location: type.location)
24
+ RBS::Types::ClassInstance.new(name: type.name, args: [symbol], location: type.location)
27
25
  end
28
26
 
29
27
  def symbol_array?(type)
@@ -9,6 +9,7 @@ module RbsHeuristicPrototype
9
9
  FILTERS = {
10
10
  active_model_validations: Filters::ActiveModelValidationsFilter,
11
11
  boolean_methods: Filters::BooleanMethodsFilter,
12
+ deep_module: Filters::DeepModuleFilter,
12
13
  symbol_array_constants: Filters::SymbolArrayConstantsFilter
13
14
  }.freeze
14
15
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RbsHeuristicPrototype
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "rbs_heuristic_prototype/filters/active_model_validations_filter"
4
4
  require_relative "rbs_heuristic_prototype/filters/boolean_methods_filter"
5
+ require_relative "rbs_heuristic_prototype/filters/deep_module_filter"
5
6
  require_relative "rbs_heuristic_prototype/filters/symbol_array_constants_filter"
6
7
  require_relative "rbs_heuristic_prototype/rake_task"
7
8
  require_relative "rbs_heuristic_prototype/version"
@@ -1,7 +1,9 @@
1
1
  module RbsHeuristicPrototype
2
2
  module Filters
3
3
  class ActiveModelValidationsFilter < Base
4
- @klass: Class?
4
+ attr_reader klass: singleton(ActiveModel::Validations)?
5
+
6
+ @klass: singleton(ActiveModel::Validations)?
5
7
 
6
8
  def process_class: (RBS::AST::Declarations::Class decl) -> RBS::AST::Declarations::Class
7
9
  def process_member: (RBS::AST::Members::t member) -> RBS::AST::Members::t
@@ -10,7 +10,7 @@ module RbsHeuristicPrototype
10
10
  def process_class: (RBS::AST::Declarations::Class decl) -> RBS::AST::Declarations::Class
11
11
  def process_constant: (RBS::AST::Declarations::Constant member) -> RBS::AST::Declarations::Constant
12
12
  def process_member: (RBS::AST::Members::t member) -> RBS::AST::Members::t
13
- def const_get: (RBS::AST::Declarations::t decl) -> Class?
13
+ def const_get: (RBS::AST::Declarations::Class | RBS::AST::Declarations::Module decl) -> singleton(Class)?
14
14
  end
15
15
  end
16
16
  end
@@ -0,0 +1,15 @@
1
+ module RbsHeuristicPrototype
2
+ module Filters
3
+ class DeepModuleFilter < Base
4
+ attr_reader stack: Array[Module | Class | nil]
5
+
6
+ @stack: Array[Module | Class | nil]
7
+
8
+ def initialize: (RBS::Environment env) -> void
9
+ def process_module: (RBS::AST::Declarations::Module decl) -> RBS::AST::Declarations::t
10
+ def process_class: (RBS::AST::Declarations::Class decl) -> RBS::AST::Declarations::t
11
+ def namespace_to_decl!: (RBS::Namespace namespace) -> Array[RBS::AST::Declarations::Module | RBS::AST::Declarations::Class]
12
+ def obj_to_decl: (Module | Class obj) -> (RBS::AST::Declarations::Module | RBS::AST::Declarations::Class)
13
+ end
14
+ end
15
+ end
@@ -2,11 +2,11 @@ module RbsHeuristicPrototype
2
2
  module Filters
3
3
  class SymbolArrayConstantsFilter < Base
4
4
  def process_constant: (RBS::AST::Declarations::Constant constant) -> RBS::AST::Declarations::Constant
5
- def process_constant_type: (RBS::Types::t type) -> RBS::Types::ClassInstance
6
- def symbol_array?: (RBS::Types::t type) -> bool
7
- def array?: (RBS::Types::t type) -> bool
8
- def symbol_union?: (RBS::Types::t type) -> bool
9
- def symbol?: (RBS::Types::t type) -> bool
5
+ def process_constant_type: (untyped type) -> RBS::Types::ClassInstance
6
+ def symbol_array?: (untyped type) -> bool
7
+ def array?: (untyped type) -> bool
8
+ def symbol_union?: (untyped type) -> bool
9
+ def symbol?: (untyped type) -> bool
10
10
  end
11
11
  end
12
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs_heuristic_prototype
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takeshi KOMIYA
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-15 00:00:00.000000000 Z
11
+ date: 2023-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rbs
@@ -47,6 +47,7 @@ files:
47
47
  - lib/rbs_heuristic_prototype/filters/active_model_validations_filter.rb
48
48
  - lib/rbs_heuristic_prototype/filters/base.rb
49
49
  - lib/rbs_heuristic_prototype/filters/boolean_methods_filter.rb
50
+ - lib/rbs_heuristic_prototype/filters/deep_module_filter.rb
50
51
  - lib/rbs_heuristic_prototype/filters/symbol_array_constants_filter.rb
51
52
  - lib/rbs_heuristic_prototype/rake_task.rb
52
53
  - lib/rbs_heuristic_prototype/version.rb
@@ -59,6 +60,7 @@ files:
59
60
  - sig/rbs_heuristic_prototype/filters/active_model_validations_filter.rbs
60
61
  - sig/rbs_heuristic_prototype/filters/base.rbs
61
62
  - sig/rbs_heuristic_prototype/filters/boolean_methods_filter.rbs
63
+ - sig/rbs_heuristic_prototype/filters/deep_module_filter.rbs
62
64
  - sig/rbs_heuristic_prototype/filters/symbol_array_constants_filter.rbs
63
65
  - sig/rbs_heuristic_prototype/rake_task.rbs
64
66
  homepage: https://github.com/tk0miya/rbs_heuristic_prototype