rbs_inline_data 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: 5b590372f180f2c8115503f7d93a8eee62b4a67098a278b9487a24e3f715710f
4
- data.tar.gz: 9b17bc5d5b477670ea88acb82f6123f6e7c361a370a0589190e02015a417f4e3
3
+ metadata.gz: e626cd6506b96f771d5b5e178354e6fcca38b1ee8f9eb1e8c6eaf9191ddb9074
4
+ data.tar.gz: 79cd75b8df23c3066617a5b0a6186b9dc8617b444c5c6318816714bcfc285cfe
5
5
  SHA512:
6
- metadata.gz: 4696211d602da66ccbf6c07a2ce2ad6f66e7e42c1f503c560a5c31b5b64bda3c0d46e357106f73ecc584788e09a15fee1fd5756c7a5bf0edac91d7616104a058
7
- data.tar.gz: dff46a22e6c68c0d6624e8bead20ca70779752546859cd2fd2086ea02fdee028d10251095c43c89e42c566db62d42a8e6f5bd443197c02ec52320b7efd00c203
6
+ metadata.gz: 8a648312df3817a4e65e810f12ba3a63dd63746c00db2f289b037883563c61da31fc9d1b208aaa3d7a4149ed651d07171eb62134775b257c7e15e889e95ed3ff
7
+ data.tar.gz: 31eaf6f6f76f5995b5e9c3ce2e878a756601dfb8a04b16e6699b45e2465d7d879ca4b8ebd2cebf1cf530809ec5aca0ace9abf40c5a390e0266c20a22b0f68910
data/.rubocop.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  AllCops:
2
- TargetRubyVersion: 3.0
2
+ TargetRubyVersion: 3.3
3
+ NewCops: enable
3
4
 
4
5
  Style/StringLiterals:
5
6
  EnforcedStyle: double_quotes
@@ -9,3 +10,16 @@ Style/StringLiteralsInInterpolation:
9
10
 
10
11
  Layout/LeadingCommentSpace:
11
12
  Enabled: false
13
+
14
+ Metrics/ClassLength:
15
+ Exclude:
16
+ - 'test/**/*'
17
+
18
+ Metrics/MethodLength:
19
+ Exclude:
20
+ - 'test/**/*'
21
+ Max: 20
22
+
23
+ Style/Style/Documentation:
24
+ Enabled: false
25
+
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2024-06-18
4
+
5
+ - Support Hash type https://github.com/euglena1215/rbs_inline_data/pull/1
6
+
3
7
  ## [0.1.0] - 2024-06-09
4
8
 
5
9
  - Initial release
@@ -6,6 +6,7 @@ require "rbs_inline_data/parser"
6
6
  require "rbs_inline_data/writer"
7
7
 
8
8
  module RbsInlineData
9
+ # Process executed when running the rbs-inline-data command.
9
10
  class CLI
10
11
  #:: (Array[String]) -> void
11
12
  def run(args)
@@ -18,22 +19,29 @@ module RbsInlineData
18
19
  end
19
20
  end.parse!(args)
20
21
 
21
- targets = Pathname.glob(args[0]).flat_map do |path|
22
- if path.directory?
23
- Pathname.glob(path.join("**/*.rb").to_s)
22
+ get_targets(args[0]).each do |target|
23
+ result = Prism.parse_file(target.to_s)
24
+ definitions = Parser.parse(result)
25
+ Writer.write(definitions, output_path ? (output_path + target).sub_ext(".rbs") : nil)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ #:: (String) -> Array[Pathname]
32
+ def get_targets(path)
33
+ targets = Pathname.glob(path).flat_map do |pathname|
34
+ if pathname.directory?
35
+ Pathname.glob(pathname.join("**/*.rb").to_s)
24
36
  else
25
- path
37
+ pathname
26
38
  end
27
39
  end
28
40
 
29
41
  targets.sort!
30
42
  targets.uniq!
31
43
 
32
- targets.each do |target|
33
- result = Prism.parse_file(target.to_s)
34
- definitions = Parser.parse(result)
35
- Writer.write(definitions, output_path ? (output_path + target).sub_ext(".rbs") : nil)
36
- end
44
+ targets
37
45
  end
38
46
  end
39
47
  end
@@ -14,21 +14,47 @@ module RbsInlineData
14
14
  :field_name, #:: String
15
15
  :type #:: String
16
16
  )
17
+ # @rbs skip
18
+ Comments = Data.define(
19
+ :comment_lines #:: Hash[Integer, String]
20
+ )
21
+ class Comments
22
+ MARKER = "#::"
23
+
24
+ #:: (Array[Prism::Comment]) -> RbsInlineData::Parser::Comments
25
+ def self.from_prism_comments(comments)
26
+ # @type var comment_lines: Hash[Integer, String]
27
+ comment_lines = {}
28
+ comments.each do |comment|
29
+ sliced = comment.slice
30
+ next unless sliced.start_with?(MARKER)
31
+
32
+ comment_lines[comment.location.start_line] = sliced.sub(MARKER, "").strip
33
+ end
34
+
35
+ new(comment_lines:)
36
+ end
37
+ end
17
38
 
18
39
  # @rbs @definitions: Array[RbsInlineData::Parser::TypedDefinition]
19
40
  # @rbs @surronding_class_or_module: Array[Symbol]
41
+ # @rbs @comments: RbsInlineData::Parser::Comments
20
42
 
21
- #:: (Array[RbsInlineData::Parser::TypedDefinition]) -> void
22
- def initialize(definitions)
43
+ # rubocop:disable Lint/MissingSuper
44
+ #:: (Array[RbsInlineData::Parser::TypedDefinition], RbsInlineData::Parser::Comments) -> void
45
+ def initialize(definitions, comments)
23
46
  @definitions = definitions
47
+ @comments = comments
24
48
  @surronding_class_or_module = []
25
49
  end
50
+ # rubocop:enable Lint/MissingSuper
26
51
 
27
52
  #:: (Prism::ParseResult) -> Array[RbsInlineData::Parser::TypedDefinition]
28
53
  def self.parse(result)
29
54
  # @type var definitions: Array[RbsInlineData::Parser::TypedDefinition]
30
55
  definitions = []
31
- instance = new(definitions)
56
+ comments = Comments.from_prism_comments(result.comments)
57
+ instance = new(definitions, comments)
32
58
  result.value.accept(instance)
33
59
  definitions
34
60
  end
@@ -78,30 +104,27 @@ module RbsInlineData
78
104
 
79
105
  #:: (Prism::ConstantWriteNode) -> RbsInlineData::Parser::TypedDefinition?
80
106
  def extract_definition(node)
81
- source = node.slice
82
- _, class_name, field_text = source.match(/\A([a-zA-Z0-9]+) ?= ?Data\.define\(([\n\s\w\W]+)\)\z/).to_a
83
- return nil if field_text.nil? || class_name.nil?
84
-
85
- class_name = "#{@surronding_class_or_module.join("::")}::#{class_name}"
86
-
87
- fields = field_text.split("\n").map(&:strip).reject(&:empty?).map do |str|
88
- case str
89
- when /:(\w+),? #:: ([\w\:\[\]]+)/
90
- [::Regexp.last_match(1), ::Regexp.last_match(2)]
91
- when /:(\w+),?/
92
- [::Regexp.last_match(1), "untyped"]
93
- end
94
- end.compact.map do |field_name, type|
95
- TypedField.new(
96
- field_name: field_name,
97
- type: type
98
- )
107
+ arguments_node = node.value.arguments
108
+ if arguments_node
109
+ typed_fields = arguments_node.arguments.map do |sym_node|
110
+ return nil unless sym_node.is_a?(Prism::SymbolNode)
111
+
112
+ TypedField.new(
113
+ field_name: sym_node.unescaped,
114
+ type: type_of(sym_node)
115
+ )
116
+ end.compact
99
117
  end
100
118
 
101
119
  TypedDefinition.new(
102
- class_name: class_name,
103
- fields: fields
120
+ class_name: "#{@surronding_class_or_module.join("::")}::#{node.name}",
121
+ fields: typed_fields || []
104
122
  )
105
123
  end
124
+
125
+ #:: (Prism::SymbolNode) -> String
126
+ def type_of(node)
127
+ @comments.comment_lines[node.location.start_line] || "untyped"
128
+ end
106
129
  end
107
130
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RbsInlineData
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -15,3 +15,11 @@ class RbsInlineData::Parser::TypedField
15
15
  | (**untyped) -> ::RbsInlineData::Parser::TypedField
16
16
  | ...
17
17
  end
18
+
19
+ class RbsInlineData::Parser::Comments
20
+ extend Data::_DataClass
21
+ attr_reader comment_lines: Hash[Integer, String]
22
+ def self.new: (*untyped) -> ::RbsInlineData::Parser::Comments
23
+ | (**untyped) -> ::RbsInlineData::Parser::Comments
24
+ | ...
25
+ end
@@ -1,8 +1,14 @@
1
1
  # Generated from lib/rbs_inline_data/cli.rb with RBS::Inline
2
2
 
3
3
  module RbsInlineData
4
+ # Process executed when running the rbs-inline-data command.
4
5
  class CLI
5
6
  # :: (Array[String]) -> void
6
7
  def run: (Array[String]) -> void
8
+
9
+ private
10
+
11
+ # :: (String) -> Array[Pathname]
12
+ def get_targets: (String) -> Array[Pathname]
7
13
  end
8
14
  end
@@ -2,12 +2,22 @@
2
2
 
3
3
  module RbsInlineData
4
4
  class Parser < Prism::Visitor
5
+ class Comments
6
+ MARKER: ::String
7
+
8
+ # :: (Array[Prism::Comment]) -> RbsInlineData::Parser::Comments
9
+ def self.from_prism_comments: (Array[Prism::Comment]) -> RbsInlineData::Parser::Comments
10
+ end
11
+
5
12
  @definitions: Array[RbsInlineData::Parser::TypedDefinition]
6
13
 
7
14
  @surronding_class_or_module: Array[Symbol]
8
15
 
9
- # :: (Array[RbsInlineData::Parser::TypedDefinition]) -> void
10
- def initialize: (Array[RbsInlineData::Parser::TypedDefinition]) -> void
16
+ @comments: RbsInlineData::Parser::Comments
17
+
18
+ # rubocop:disable Lint/MissingSuper
19
+ # :: (Array[RbsInlineData::Parser::TypedDefinition], RbsInlineData::Parser::Comments) -> void
20
+ def initialize: (Array[RbsInlineData::Parser::TypedDefinition], RbsInlineData::Parser::Comments) -> void
11
21
 
12
22
  # :: (Prism::ParseResult) -> Array[RbsInlineData::Parser::TypedDefinition]
13
23
  def self.parse: (Prism::ParseResult) -> Array[RbsInlineData::Parser::TypedDefinition]
@@ -31,5 +41,8 @@ module RbsInlineData
31
41
 
32
42
  # :: (Prism::ConstantWriteNode) -> RbsInlineData::Parser::TypedDefinition?
33
43
  def extract_definition: (Prism::ConstantWriteNode) -> RbsInlineData::Parser::TypedDefinition?
44
+
45
+ # :: (Prism::SymbolNode) -> String
46
+ def type_of: (Prism::SymbolNode) -> String
34
47
  end
35
48
  end
data/sig/patch.rbs CHANGED
@@ -8,4 +8,13 @@ module Prism
8
8
  def constant_path: () -> Prism::ConstantReadNode
9
9
  | ...
10
10
  end
11
+
12
+ class Comment
13
+ def slice: () -> String
14
+ end
15
+
16
+ class ConstantWriteNode
17
+ def value: () -> CallNode
18
+ | ...
19
+ end
11
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs_inline_data
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
  - Teppei Shintani
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-11 00:00:00.000000000 Z
11
+ date: 2024-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prism
@@ -73,6 +73,7 @@ metadata:
73
73
  homepage_uri: https://github.com/euglena1215/rbs_inline_data
74
74
  source_code_uri: https://github.com/euglena1215/rbs_inline_data
75
75
  changelog_uri: https://github.com/euglena1215/rbs_inline_data/blob/main/CHANGELOG.md
76
+ rubygems_mfa_required: 'true'
76
77
  post_install_message:
77
78
  rdoc_options: []
78
79
  require_paths: