Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Fix rubocop part1
Browse files Browse the repository at this point in the history
  • Loading branch information
euglena1215 committed Jun 17, 2024
1 parent 24c315e commit a07644e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
10 changes: 9 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 3.0
TargetRubyVersion: 3.3

Style/StringLiterals:
EnforcedStyle: double_quotes
Expand All @@ -9,3 +9,11 @@ Style/StringLiteralsInInterpolation:

Layout/LeadingCommentSpace:
Enabled: false

Metrics/ClassLength:
Exclude:
- 'test/**/*'

Metrics/MethodLength:
Exclude:
- 'test/**/*'
1 change: 1 addition & 0 deletions lib/rbs_inline_data/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "rbs_inline_data/writer"

module RbsInlineData
# Process executed when running the rbs-inline-data command.
class CLI
#:: (Array[String]) -> void
def run(args)
Expand Down
12 changes: 7 additions & 5 deletions lib/rbs_inline_data/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class Parser < Prism::Visitor
# @rbs @definitions: Array[RbsInlineData::Parser::TypedDefinition]
# @rbs @surronding_class_or_module: Array[Symbol]

# rubocop:disable Lint/MissingSuper
#:: (Array[RbsInlineData::Parser::TypedDefinition]) -> void
def initialize(definitions)
@definitions = definitions
@surronding_class_or_module = []
end
# rubocop:enable Lint/MissingSuper

#:: (Prism::ParseResult) -> Array[RbsInlineData::Parser::TypedDefinition]
def self.parse(result)
Expand Down Expand Up @@ -86,21 +88,21 @@ def extract_definition(node)

fields = field_text.split("\n").map(&:strip).reject(&:empty?).map do |str|
case str
when /:(\w+),? #:: ([\w\:\[\], ]+)/
when /:(\w+),? #:: ([\w:\[\], ]+)/
[::Regexp.last_match(1), ::Regexp.last_match(2)]
when /:(\w+),?/
[::Regexp.last_match(1), "untyped"]
end
end.compact.map do |field_name, type|
TypedField.new(
field_name: field_name,
type: type
field_name:,
type:
)
end

TypedDefinition.new(
class_name: class_name,
fields: fields
class_name:,
fields:
)
end
end
Expand Down
47 changes: 29 additions & 18 deletions test/rbs_inline_data/parser_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "test_helper"
require "rbs_inline_data/parser"

Expand All @@ -22,13 +24,15 @@ class Foo
assert_equal definitions[0], Parser::TypedDefinition.new(
class_name: "Foo::Bar",
fields: [
Parser::TypedField.new(field_name: "value", type: "String"),
])
Parser::TypedField.new(field_name: "value", type: "String")
]
)
assert_equal definitions[1], Parser::TypedDefinition.new(
class_name: "Foo::Bar2",
fields: [
Parser::TypedField.new(field_name: "value", type: "Integer"),
])
Parser::TypedField.new(field_name: "value", type: "Integer")
]
)
end

def test_multiple_definition
Expand All @@ -46,13 +50,15 @@ class Foo
assert_equal definitions[0], Parser::TypedDefinition.new(
class_name: "Foo::Bar",
fields: [
Parser::TypedField.new(field_name: "string_value", type: "String"),
])
Parser::TypedField.new(field_name: "string_value", type: "String")
]
)
assert_equal definitions[1], Parser::TypedDefinition.new(
class_name: "Foo::Bar2",
fields: [
Parser::TypedField.new(field_name: "string_value", type: "String"),
])
Parser::TypedField.new(field_name: "string_value", type: "String")
]
)
end

def test_untyped
Expand All @@ -71,18 +77,21 @@ class Foo
assert_equal definitions[0], Parser::TypedDefinition.new(
class_name: "Foo::Bar",
fields: [
Parser::TypedField.new(field_name: "value", type: "untyped"),
])
Parser::TypedField.new(field_name: "value", type: "untyped")
]
)
assert_equal definitions[1], Parser::TypedDefinition.new(
class_name: "Foo::Bar2",
fields: [
Parser::TypedField.new(field_name: "value", type: "untyped"),
])
Parser::TypedField.new(field_name: "value", type: "untyped")
]
)
assert_equal definitions[2], Parser::TypedDefinition.new(
class_name: "Foo::Bar3",
fields: [
Parser::TypedField.new(field_name: "value", type: "untyped"),
])
Parser::TypedField.new(field_name: "value", type: "untyped")
]
)
end

def test_nested_type
Expand All @@ -102,8 +111,9 @@ class C; end
class_name: "A::B::D",
fields: [
Parser::TypedField.new(field_name: "x", type: "A::B::C"),
Parser::TypedField.new(field_name: "y", type: "Array[A::B::C]"),
])
Parser::TypedField.new(field_name: "y", type: "Array[A::B::C]")
]
)
end

def test_hash_type
Expand All @@ -118,8 +128,9 @@ class A
assert_equal definitions[0], Parser::TypedDefinition.new(
class_name: "A::B",
fields: [
Parser::TypedField.new(field_name: "x", type: "Hash[Symbol, String]"),
])
Parser::TypedField.new(field_name: "x", type: "Hash[Symbol, String]")
]
)
end
end
end

0 comments on commit a07644e

Please sign in to comment.