orthoses-yard 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: b2f6b90fd20646f14534289bd4b876eb0faf8cb4a2d422d442bcbcefd07b2069
4
- data.tar.gz: e7283f8f85defc5ac290310d35a8a248c47fd87ef09f54c2dd65e352373bc881
3
+ metadata.gz: 6417c32f741a5dfc134166c945908278cbd986d2a2004a8fa4cdede7155e63d0
4
+ data.tar.gz: 45d9288f5ddfe277dd5592cc65103d3961aa195df8af80a3707f1309ee0d8344
5
5
  SHA512:
6
- metadata.gz: 1796d238fbfa72d260fce28cf167ca6b0d595bc6b65faa0295e5416bd17aeec384303f8395b5aa5e3c9f8c9587ffb48cbb0a7654301ceb004be457a349a5d34b
7
- data.tar.gz: 27a5c5c9dcfdd9d51834aa57c875c1e3600ed1a3841a930b199286624e843054a98653ff9f6e8af397fb46792136735283df49f5ed4aed4411feb14cad3b5f38
6
+ metadata.gz: c929d6d2754ba40898f13213289da72cf66a4977a04626c3670a3dbb85717007f93c254654fbd50d86f0b91c1a179ed47c180496b5210b22182ad6008fb8f358
7
+ data.tar.gz: e918f5642b21d48b95c3b47c774c71537481310829206a73ab6e1f4844ed4a66cec81b1243bed6734993d5cecddd46fb9a9777053fbf18cd8e416f8ca3920d1b
data/README.md CHANGED
@@ -50,7 +50,14 @@ end
50
50
 
51
51
  ## Usage
52
52
 
53
- Please see https://github.com/ksss/orthoses-yard/blob/main/examples/yard/Rakefile
53
+ ```rb
54
+ use Orthoses::YARD,
55
+ parse: ['{lib,app}/**/*.rb'], # Target files (require)
56
+ use_cache: true, # Use cache .yardoc database (optional default=true)
57
+ log_level: :debug # Set log level for YARD (optional default=nil)
58
+ ```
59
+
60
+ Please see also https://github.com/ksss/orthoses-yard/blob/main/examples/yard/generate.rb
54
61
 
55
62
  ## Development
56
63
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Orthoses
4
4
  class YARD
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
@@ -48,6 +48,32 @@ module Orthoses
48
48
  generate_for_classvariable
49
49
  end
50
50
 
51
+ # @return [RBS::Types::t]
52
+ def tag_types_to_rbs_type(tag_types)
53
+ return untyped if tag_types.nil?
54
+
55
+ tag_types = tag_types.compact.uniq
56
+ return untyped if tag_types.empty?
57
+
58
+ begin
59
+ types_explainers = ::YARD::Tags::TypesExplainer::Parser.parse(tag_types.join(", "))
60
+ rescue SyntaxError => e
61
+ Orthoses.logger.warn("#{tag_types} in #{yardoc.inspect} cannot parse as tags. use untyped instead")
62
+ Orthoses.logger.warn(" => exception message=`#{e.message}`")
63
+ return untyped
64
+ end
65
+
66
+ Utils::TypeList.new(recursive_resolve(types_explainers)).inject.tap do |rbs|
67
+ Orthoses.logger.debug("#{yardoc.inspect} tag #{tag_types} => #{rbs}")
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def class_of(mod)
74
+ Kernel.instance_method(:class).bind_call(mod)
75
+ end
76
+
51
77
  # @return [void]
52
78
  def generate_for_attributes
53
79
  yardoc.attributes.each do |kind, attributes|
@@ -88,13 +114,13 @@ module Orthoses
88
114
 
89
115
  begin
90
116
  mod = Object.const_get(namespace.to_s)
91
- case meth.scope
92
- when :class
93
- prefix = 'self.'
94
- method_object = mod.method(method_name)
95
- when :instance
117
+ case
118
+ when meth.scope == :instance || class_of(meth) == ::YARD::CodeObjects::ExtendedMethodObject
96
119
  prefix = ''
97
120
  method_object = mod.instance_method(method_name)
121
+ when meth.scope == :class
122
+ prefix = 'self.'
123
+ method_object = mod.method(method_name)
98
124
  else
99
125
  raise "bug"
100
126
  end
@@ -222,65 +248,6 @@ module Orthoses
222
248
  end
223
249
  end
224
250
 
225
- # @return [RBS::Types::t]
226
- def tag_types_to_rbs_type(tag_types)
227
- return untyped if tag_types.nil?
228
- return untyped if tag_types.empty?
229
-
230
- begin
231
- types_explainers = ::YARD::Tags::TypesExplainer::Parser.parse(tag_types.uniq.join(", "))
232
- rescue SyntaxError
233
- Orthoses.logger.warn("#{tag_types} in #{yardoc.inspect} cannot parse as tags. use untyped instead")
234
- return untyped
235
- end
236
-
237
- wrap(recursive_resolve(types_explainers)).tap do |rbs|
238
- Orthoses.logger.debug("#{yardoc.inspect} tag #{tag_types} => #{rbs}")
239
- end
240
- end
241
-
242
- # @return [RBS::Types::t]
243
- def wrap(types)
244
- if types.nil? || types.empty? || types == [untyped]
245
- return untyped
246
- end
247
-
248
- if 1 < types.length
249
- if index = types.find_index { |t| t.to_s == "nil" }
250
- types.delete_at(index)
251
- is_optional = true
252
- if types == [untyped]
253
- return untyped
254
- end
255
- end
256
- end
257
- is_union = 1 < types.length
258
-
259
- if is_union
260
- if is_optional
261
- ::RBS::Types::Optional.new(
262
- type: ::RBS::Types::Union.new(
263
- types: types,
264
- location: nil,
265
- ),
266
- location: nil,
267
- )
268
- else
269
- ::RBS::Types::Union.new(
270
- types: types,
271
- location: nil,
272
- )
273
- end
274
- elsif is_optional
275
- ::RBS::Types::Optional.new(
276
- type: types.first,
277
- location: nil,
278
- )
279
- else
280
- types.first
281
- end
282
- end
283
-
284
251
  # @return [Array<RBS::Types::t>]
285
252
  def recursive_resolve(types_explainer_types)
286
253
  types_explainer_types.map do |types_explainer_type|
@@ -291,7 +258,7 @@ module Orthoses
291
258
  location: nil
292
259
  )
293
260
  when ::YARD::Tags::TypesExplainer::CollectionType
294
- type = wrap(recursive_resolve(types_explainer_type.types))
261
+ type = Utils::TypeList.new(recursive_resolve(types_explainer_type.types)).inject
295
262
  if types_explainer_type.name == "Class"
296
263
  if type.to_s == "untyped"
297
264
  untyped
@@ -312,8 +279,8 @@ module Orthoses
312
279
  ::RBS::Types::ClassInstance.new(
313
280
  name: TypeName(types_explainer_type.name),
314
281
  args: [
315
- wrap(recursive_resolve(types_explainer_type.key_types)),
316
- wrap(recursive_resolve(types_explainer_type.value_types)),
282
+ Utils::TypeList.new(recursive_resolve(types_explainer_type.key_types)).inject,
283
+ Utils::TypeList.new(recursive_resolve(types_explainer_type.value_types)).inject,
317
284
  ],
318
285
  location: nil
319
286
  )
@@ -347,8 +314,14 @@ module Orthoses
347
314
  # end
348
315
  else
349
316
  case types_explainer_type.name
350
- when "Object" then next untyped
351
- when "Boolean" then next bool
317
+ when "Object"
318
+ next untyped
319
+ when "Boolean", "TrueClass", "FalseClass", "true", "false"
320
+ next bool
321
+ when "NilClass", "nil"
322
+ next ::RBS::Types::Bases::Nil.new(location: nil)
323
+ when "Fixnum"
324
+ next ::RBS::Types::ClassInstance.new(name: TypeName("Integer"), args: [], location: nil)
352
325
  end
353
326
 
354
327
  begin
@@ -359,6 +332,7 @@ module Orthoses
359
332
  when ::RBS::Types::Alias
360
333
  end
361
334
  rescue ::RBS::ParsingError
335
+ # will be unresolve type
362
336
  end
363
337
 
364
338
  if Utils.rbs_defined_class?(types_explainer_type.name, collection: true)
@@ -368,22 +342,13 @@ module Orthoses
368
342
  location: nil
369
343
  )
370
344
  else
371
- name =
372
- case types_explainer_type.name
373
- when "Fixnum"
374
- "Integer"
375
- else
376
- resolved = ::YARD::Registry.resolve(yardoc.namespace, types_explainer_type.name, true, false)
377
- if resolved
378
- resolved.to_s
379
- else
380
- Orthoses.logger.warn("#{types_explainer_type.name} in #{yardoc.namespace} set `untyped` because it cannot resolved type")
381
- next untyped
382
- end
383
- end
384
-
345
+ resolved = ::YARD::Registry.resolve(yardoc, types_explainer_type.name, true, false)
346
+ unless resolved
347
+ Orthoses.logger.warn("yardoc type=[#{types_explainer_type.name}] in #{yardoc.path} set `untyped` because it cannot resolved type by `::YARD::Registry.resolve`")
348
+ next untyped
349
+ end
385
350
  ::RBS::Types::ClassInstance.new(
386
- name: TypeName(name),
351
+ name: TypeName(resolved.to_s),
387
352
  args: [],
388
353
  location: nil
389
354
  )
data/lib/orthoses/yard.rb CHANGED
@@ -6,22 +6,39 @@ require_relative "yard/yard2rbs"
6
6
  module Orthoses
7
7
  # use Orthoses::YARD, parse: "lib/**/*.rb"
8
8
  class YARD
9
- def initialize(loader, parse:)
9
+ # @param loader
10
+ # @param [<String>, String] parse Target files
11
+ # @param [Boolean] use_cache Use cache .yardoc
12
+ # @param [Symbol, nil] log_level Set YARD log level
13
+ def initialize(loader, parse:, use_cache: true, log_level: nil)
10
14
  @loader = loader
11
15
  @parse = Array(parse)
16
+ @use_cache = use_cache
17
+ @log_level = log_level
12
18
  end
13
19
 
20
+ # @return [void]
14
21
  def call
15
22
  @loader.call.tap do |store|
16
23
  require 'yard'
17
24
 
25
+ log.level = @log_level if @log_level
26
+
27
+ ::YARD::Registry.load if @use_cache
18
28
  ::YARD.parse(@parse)
29
+ ::YARD::Registry.save(true) if @use_cache
19
30
  ::YARD::Registry.root.children.each do |yardoc|
31
+ # Skip anonymous yardoc
32
+ next unless yardoc.file
33
+
34
+ # Skip external doc (e.g. pry-doc)
35
+ next unless @parse.any? { |pattern| File.fnmatch(pattern, yardoc.file, File::FNM_EXTGLOB | File::FNM_PATHNAME) }
36
+
20
37
  case yardoc.type
21
38
  when :class, :module
22
39
  YARD2RBS.run(yardoc: yardoc) do |namespace, docstring, rbs|
23
40
  comment = docstring.each_line.map { |line| "# #{line}" }.join
24
- if rbs.nil?
41
+ if rbs.nil? && comment && !store.has_key?(namespace)
25
42
  store[namespace].comment = comment
26
43
  else
27
44
  Orthoses.logger.debug("#{namespace} << #{rbs}")
data/sig/orthoses.rbs ADDED
@@ -0,0 +1,43 @@
1
+ # GENERATED FILE
2
+
3
+ # use Orthoses::YARD, parse: "lib/**/*.rb"
4
+ class Orthoses::YARD
5
+ # @param loader
6
+ # @param [<String>, String] parse Target files
7
+ # @param [Boolean] use_cache Use cache .yardoc
8
+ # @param [Symbol, nil] log_level Set YARD log level
9
+ def initialize: (untyped loader, parse: Array[String] | String, ?use_cache: bool, ?log_level: Symbol?) -> void
10
+ # @return [void]
11
+ def call: () -> void
12
+ VERSION: untyped
13
+ end
14
+
15
+ class Orthoses::YARD::YARD2RBS
16
+ # @return [YARD::CodeObjects::t]
17
+ attr_reader yardoc: untyped
18
+ # @return [Proc]
19
+ attr_reader block: Proc
20
+ # @return [RBS::Types::Bases::Any]
21
+ attr_reader untyped: RBS::Types::Bases::Any
22
+ # @return [RBS::Types::Bases::Void]
23
+ attr_reader void: RBS::Types::Bases::Void
24
+ # @return [RBS::Types::Bases::Bool]
25
+ attr_reader bool: RBS::Types::Bases::Bool
26
+ def initialize: (yardoc: untyped, block: untyped) -> void
27
+ # @return [void]
28
+ def run: () -> void
29
+ # @return [RBS::Types::t]
30
+ def tag_types_to_rbs_type: (untyped tag_types) -> untyped
31
+ # @return [void]
32
+ private def generate_for_attributes: () -> void
33
+ # @return [void]
34
+ private def generate_for_methods: () -> void
35
+ # @return [void]
36
+ private def generate_for_constants: () -> void
37
+ # @return [void]
38
+ private def generate_for_classvariable: () -> void
39
+ # @return [Array<RBS::Types::t>]
40
+ private def recursive_resolve: (untyped types_explainer_types) -> untyped
41
+ # @return [Array<RBS::Types::Bases::Any>]
42
+ private def temporary_type_params: (untyped name) -> Array[RBS::Types::Bases::Any]
43
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orthoses-yard
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
  - ksss
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-15 00:00:00.000000000 Z
11
+ date: 2023-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: orthoses
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.3.0
19
+ version: 1.5.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '2.0'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 1.3.0
29
+ version: 1.5.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '2.0'
@@ -52,18 +52,13 @@ extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
54
  - CODE_OF_CONDUCT.md
55
- - Gemfile
56
- - Gemfile.lock
57
55
  - LICENSE.txt
58
56
  - README.md
59
57
  - lib/orthoses-yard.rb
60
58
  - lib/orthoses/yard.rb
61
59
  - lib/orthoses/yard/version.rb
62
60
  - lib/orthoses/yard/yard2rbs.rb
63
- - orthoses-yard.gemspec
64
- - sig/orthoses/yard.rbs
65
- - sig/orthoses/yard/yard2_rbs.rbs
66
- - sig_patch/yard.rbs
61
+ - sig/orthoses.rbs
67
62
  homepage: https://github.com/ksss/orthoses-yard
68
63
  licenses:
69
64
  - MIT
@@ -86,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
81
  - !ruby/object:Gem::Version
87
82
  version: '0'
88
83
  requirements: []
89
- rubygems_version: 3.4.8
84
+ rubygems_version: 3.4.10
90
85
  signing_key:
91
86
  specification_version: 4
92
87
  summary: Orthoses extention for YARD.
data/Gemfile DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
-
5
- # Specify your gem's dependencies in orthoses-yard.gemspec
6
- gemspec
7
-
8
- gem "rake", "~> 13.0"
9
- gem "rgot", "~> 1.1"
10
- gem "rbs"
11
- gem "steep"
12
- gem "debug"
data/Gemfile.lock DELETED
@@ -1,89 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- orthoses-yard (0.3.0)
5
- orthoses (>= 1.3.0, < 2.0)
6
- yard
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- activesupport (7.0.4.3)
12
- concurrent-ruby (~> 1.0, >= 1.0.2)
13
- i18n (>= 1.6, < 2)
14
- minitest (>= 5.1)
15
- tzinfo (~> 2.0)
16
- ast (2.4.2)
17
- concurrent-ruby (1.2.2)
18
- csv (3.2.6)
19
- debug (1.7.1)
20
- irb (>= 1.5.0)
21
- reline (>= 0.3.1)
22
- ffi (1.15.5)
23
- fileutils (1.7.0)
24
- i18n (1.12.0)
25
- concurrent-ruby (~> 1.0)
26
- io-console (0.6.0)
27
- irb (1.6.3)
28
- reline (>= 0.3.0)
29
- json (2.6.3)
30
- language_server-protocol (3.17.0.3)
31
- listen (3.8.0)
32
- rb-fsevent (~> 0.10, >= 0.10.3)
33
- rb-inotify (~> 0.9, >= 0.9.10)
34
- logger (1.5.3)
35
- minitest (5.18.0)
36
- orthoses (1.6.0)
37
- rbs (~> 3.0)
38
- parallel (1.22.1)
39
- parser (3.2.1.1)
40
- ast (~> 2.4.1)
41
- rainbow (3.1.1)
42
- rake (13.0.6)
43
- rb-fsevent (0.11.2)
44
- rb-inotify (0.10.1)
45
- ffi (~> 1.0)
46
- rbs (3.0.4)
47
- reline (0.3.2)
48
- io-console (~> 0.5)
49
- rgot (1.3.0)
50
- securerandom (0.2.2)
51
- steep (1.3.0)
52
- activesupport (>= 5.1)
53
- csv (>= 3.0.9)
54
- fileutils (>= 1.1.0)
55
- json (>= 2.1.0)
56
- language_server-protocol (>= 3.15, < 4.0)
57
- listen (~> 3.0)
58
- logger (>= 1.3.0)
59
- parallel (>= 1.0.0)
60
- parser (>= 3.1)
61
- rainbow (>= 2.2.2, < 4.0)
62
- rbs (>= 2.8.0)
63
- securerandom (>= 0.1)
64
- strscan (>= 1.0.0)
65
- terminal-table (>= 2, < 4)
66
- strscan (3.0.6)
67
- terminal-table (3.0.2)
68
- unicode-display_width (>= 1.1.1, < 3)
69
- tzinfo (2.0.6)
70
- concurrent-ruby (~> 1.0)
71
- unicode-display_width (2.4.2)
72
- webrick (1.7.0)
73
- yard (0.9.28)
74
- webrick (~> 1.7.0)
75
-
76
- PLATFORMS
77
- arm64-darwin-21
78
- x86_64-linux
79
-
80
- DEPENDENCIES
81
- debug
82
- orthoses-yard!
83
- rake (~> 13.0)
84
- rbs
85
- rgot (~> 1.1)
86
- steep
87
-
88
- BUNDLED WITH
89
- 2.3.16
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/orthoses/yard/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "orthoses-yard"
7
- spec.version = Orthoses::YARD::VERSION
8
- spec.authors = ["ksss"]
9
- spec.email = ["co000ri@gmail.com"]
10
-
11
- spec.summary = "Orthoses extention for YARD."
12
- spec.description = "Orthoses extention for YARD."
13
- spec.homepage = "https://github.com/ksss/orthoses-yard"
14
- spec.license = "MIT"
15
- spec.required_ruby_version = ">= 2.6.0"
16
-
17
- spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = spec.homepage
19
- spec.metadata["changelog_uri"] = spec.homepage
20
-
21
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
- `git ls-files -z`.split("\x0").reject do |f|
23
- next true if (f == __FILE__)
24
- next true if f.match?(%r{\A(?:bin|known_sig|examples)/}) # dir
25
- next true if f.match?(%r{\A\.(?:git)}) # git
26
- next true if f.match?(%r{\A(?:rbs_collection|Steepfile|Rakefile)}) # top file
27
- next true if f.match?(%r{_test\.rb\z}) # test
28
- false
29
- end
30
- end
31
- spec.bindir = "exe"
32
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
- spec.require_paths = ["lib"]
34
-
35
- spec.add_dependency "orthoses", ">= 1.3.0", "< 2.0"
36
- spec.add_dependency "yard"
37
- end
@@ -1,31 +0,0 @@
1
- # GENERATED FILE
2
-
3
- class Orthoses::YARD::YARD2RBS
4
- # @return [YARD::CodeObjects::t]
5
- attr_reader yardoc: untyped
6
- # @return [Proc]
7
- attr_reader block: Proc
8
- # @return [RBS::Types::Bases::Any]
9
- attr_reader untyped: RBS::Types::Bases::Any
10
- # @return [RBS::Types::Bases::Void]
11
- attr_reader void: RBS::Types::Bases::Void
12
- # @return [RBS::Types::Bases::Bool]
13
- attr_reader bool: RBS::Types::Bases::Bool
14
- def initialize: (yardoc: untyped, block: untyped) -> void
15
- # @return [void]
16
- def run: () -> void
17
- # @return [void]
18
- def generate_for_attributes: () -> void
19
- # @return [void]
20
- def generate_for_methods: () -> void
21
- # @return [void]
22
- def generate_for_constants: () -> void
23
- # @return [RBS::Types::t]
24
- def tag_types_to_rbs_type: (untyped tag_types) -> untyped
25
- # @return [RBS::Types::t]
26
- def wrap: (untyped types) -> untyped
27
- # @return [Array<RBS::Types::t>]
28
- def recursive_resolve: (untyped types_explainer_types) -> untyped
29
- # @return [Array<RBS::Types::Bases::Any>]
30
- def temporary_type_params: (untyped name) -> Array[RBS::Types::Bases::Any]
31
- end
@@ -1,6 +0,0 @@
1
- # GENERATED FILE
2
-
3
- class Orthoses::YARD
4
- def initialize: (untyped loader, parse: untyped) -> void
5
- VERSION: untyped
6
- end
data/sig_patch/yard.rbs DELETED
@@ -1,38 +0,0 @@
1
- module YARD
2
- module Registry
3
- end
4
-
5
- module Tags
6
- module TypesExplainer
7
- class Parser
8
- end
9
-
10
- class Type
11
- end
12
-
13
- class CollectionType < Type
14
- end
15
-
16
- class FixedCollectionType < CollectionType
17
- end
18
-
19
- class HashCollectionType < Type
20
- end
21
- end
22
- end
23
-
24
- module CodeObjects
25
- type t = ModuleObject | ClassObject | MethodObject
26
- class Base
27
- def attributes: () -> { class: Hash[Symbol, untyped], instance: Hash[Symbol, untyped] }
28
- end
29
- class NamespaceObject < Base
30
- end
31
- class ModuleObject < NamespaceObject
32
- end
33
- class ClassObject < NamespaceObject
34
- end
35
- class MethodObject < Base
36
- end
37
- end
38
- end