From 4b14e37f15bf6fc800670fff4caeb465aa3f0d5c Mon Sep 17 00:00:00 2001 From: ksss Date: Tue, 14 Nov 2023 14:54:10 +0900 Subject: [PATCH 1/2] Should escape if param name include not simple-word --- lib/rbs/types.rb | 6 +++++- test/rbs/runtime_prototype_test.rb | 32 +++++++++++++++++------------- test/rbs/writer_test.rb | 2 ++ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/rbs/types.rb b/lib/rbs/types.rb index 316fb0cf7..a40b52c9a 100644 --- a/lib/rbs/types.rb +++ b/lib/rbs/types.rb @@ -883,7 +883,11 @@ def to_json(state = _ = nil) def to_s if name - "#{type} #{name}" + if name.match?(/\A[a-zA-Z0-9_]+\z/) + "#{type} #{name}" + else + "#{type} `#{name}`" + end else "#{type}" end diff --git a/test/rbs/runtime_prototype_test.rb b/test/rbs/runtime_prototype_test.rb index 9f6dec611..b1efa24d7 100644 --- a/test/rbs/runtime_prototype_test.rb +++ b/test/rbs/runtime_prototype_test.rb @@ -696,7 +696,7 @@ def public_todo: () -> untyped end end - class StructInheritWithNil < Struct.new(:foo, :bar, keyword_init: nil) + class StructInheritWithNil < Struct.new(:foo, :bar, :baz?, keyword_init: nil) end StructKeywordInitTrue = Struct.new(:foo, :bar, keyword_init: true) StructKeywordInitFalse = Struct.new(:foo, :bar, keyword_init: false) @@ -712,21 +712,23 @@ def test_struct module RBS class RuntimePrototypeTest < ::Test::Unit::TestCase class StructInheritWithNil < ::Struct[untyped] - def self.new: (?untyped foo, ?untyped bar) -> instance - | (?foo: untyped, ?bar: untyped) -> instance + def self.new: (?untyped foo, ?untyped bar, ?untyped `baz?`) -> instance + | (?foo: untyped, ?bar: untyped, ?baz?: untyped) -> instance - def self.[]: (?untyped foo, ?untyped bar) -> instance - | (?foo: untyped, ?bar: untyped) -> instance + def self.[]: (?untyped foo, ?untyped bar, ?untyped `baz?`) -> instance + | (?foo: untyped, ?bar: untyped, ?baz?: untyped) -> instance def self.keyword_init?: () -> nil - def self.members: () -> [ :foo, :bar ] + def self.members: () -> [ :foo, :bar, :baz? ] - def members: () -> [ :foo, :bar ] + def members: () -> [ :foo, :bar, :baz? ] attr_accessor foo: untyped attr_accessor bar: untyped + + attr_accessor baz?: untyped end end end @@ -863,7 +865,7 @@ class StructDirectInherited < ::Struct[untyped] end if RUBY_VERSION >= '3.2' - class DataInherit < Data.define(:foo, :bar) + class DataInherit < Data.define(:foo, :bar, :baz?) end DataConst = Data.define(:foo, :bar) class DataDirectInherit < Data @@ -877,19 +879,21 @@ def test_data module RBS class RuntimePrototypeTest < ::Test::Unit::TestCase class DataInherit < ::Data - def self.new: (untyped foo, untyped bar) -> instance - | (foo: untyped, bar: untyped) -> instance + def self.new: (untyped foo, untyped bar, untyped `baz?`) -> instance + | (foo: untyped, bar: untyped, baz?: untyped) -> instance - def self.[]: (untyped foo, untyped bar) -> instance - | (foo: untyped, bar: untyped) -> instance + def self.[]: (untyped foo, untyped bar, untyped `baz?`) -> instance + | (foo: untyped, bar: untyped, baz?: untyped) -> instance - def self.members: () -> [ :foo, :bar ] + def self.members: () -> [ :foo, :bar, :baz? ] - def members: () -> [ :foo, :bar ] + def members: () -> [ :foo, :bar, :baz? ] attr_reader foo: untyped attr_reader bar: untyped + + attr_reader baz?: untyped end end end diff --git a/test/rbs/writer_test.rb b/test/rbs/writer_test.rb index ec306e06d..b5ddbc32b 100644 --- a/test/rbs/writer_test.rb +++ b/test/rbs/writer_test.rb @@ -115,6 +115,8 @@ def __id__: () -> Integer def def: () -> Symbol + def foo: (untyped `include?`) -> void + def self: () -> void def self?: () -> void From 88c248ced3d0269d293fb533f965ad03ac5e77d9 Mon Sep 17 00:00:00 2001 From: ksss Date: Tue, 14 Nov 2023 15:20:11 +0900 Subject: [PATCH 2/2] Fix test for ruby 3.0 --- test/rbs/runtime_prototype_test.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/rbs/runtime_prototype_test.rb b/test/rbs/runtime_prototype_test.rb index b1efa24d7..2be2f7985 100644 --- a/test/rbs/runtime_prototype_test.rb +++ b/test/rbs/runtime_prototype_test.rb @@ -738,19 +738,21 @@ def members: () -> [ :foo, :bar, :baz? ] module RBS class RuntimePrototypeTest < ::Test::Unit::TestCase class StructInheritWithNil < ::Struct[untyped] - def self.new: (?untyped foo, ?untyped bar) -> instance - | (?foo: untyped, ?bar: untyped) -> instance + def self.new: (?untyped foo, ?untyped bar, ?untyped `baz?`) -> instance + | (?foo: untyped, ?bar: untyped, ?baz?: untyped) -> instance - def self.[]: (?untyped foo, ?untyped bar) -> instance - | (?foo: untyped, ?bar: untyped) -> instance + def self.[]: (?untyped foo, ?untyped bar, ?untyped `baz?`) -> instance + | (?foo: untyped, ?bar: untyped, ?baz?: untyped) -> instance - def self.members: () -> [ :foo, :bar ] + def self.members: () -> [ :foo, :bar, :baz? ] - def members: () -> [ :foo, :bar ] + def members: () -> [ :foo, :bar, :baz? ] attr_accessor foo: untyped attr_accessor bar: untyped + + attr_accessor baz?: untyped end end end