diff --git a/scenario/known-issues/absolute-constant-path.rb b/scenario/known-issues/absolute-constant-path.rb new file mode 100644 index 00000000..bef283f1 --- /dev/null +++ b/scenario/known-issues/absolute-constant-path.rb @@ -0,0 +1,38 @@ +## update +class ::Foo + def foo = 1 +end + +## assert +class Foo + def foo: -> Integer +end + +## update +module M + class ::Bar + def bar = 1 + end +end + +## assert +module M + class ::Bar + def bar: -> Integer + end +end + +## update +module M +end + +class ::M::Baz + def baz = 1 +end + +## assert +module M +end +class M::Baz + def baz: -> Integer +end diff --git a/scenario/known-issues/array-aref-range.rb b/scenario/known-issues/array-aref-range.rb new file mode 100644 index 00000000..19c49751 --- /dev/null +++ b/scenario/known-issues/array-aref-range.rb @@ -0,0 +1,16 @@ +## update +def foo + [1, 2, 3][1..] +end + +def bar(a) + a[0..1] +end + +bar([1, "str"]) + +## assert +class Object + def foo: -> Array[Integer]? + def bar: ([Integer, String]) -> Array[Integer | String]? +end diff --git a/scenario/known-issues/attr-with-visibility.rb b/scenario/known-issues/attr-with-visibility.rb new file mode 100644 index 00000000..dca43312 --- /dev/null +++ b/scenario/known-issues/attr-with-visibility.rb @@ -0,0 +1,38 @@ +## update +class Foo + def initialize(name) + @name = name + end + + private attr_reader :name + + def label = name +end + +Foo.new("foo").label + +## assert +class Foo + def initialize: (String) -> void + def name: -> String + def label: -> String +end + +## update +class Foo + def initialize(count) + @count = count + end + + public attr_accessor :count +end + +foo = Foo.new(1) +foo.count = 2 + +## assert +class Foo + def initialize: (Integer) -> void + def count: -> Integer + def count=: (Integer) -> Integer +end diff --git a/scenario/known-issues/attrasgn-value.rb b/scenario/known-issues/attrasgn-value.rb new file mode 100644 index 00000000..257dc1db --- /dev/null +++ b/scenario/known-issues/attrasgn-value.rb @@ -0,0 +1,30 @@ +## update +class C + def foo=(x) + :ret + end + + def []=(k, v) + :ret + end +end + +def check_attrasgn + c = C.new + c.foo = 1 +end + +def check_index_asgn + c = C.new + c[:a] = 1 +end + +## assert +class C + def foo=: (Integer) -> :ret + def []=: (:a, Integer) -> :ret +end +class Object + def check_attrasgn: -> Integer + def check_index_asgn: -> Integer +end diff --git a/scenario/known-issues/block-autosplat-non-array.rb b/scenario/known-issues/block-autosplat-non-array.rb new file mode 100644 index 00000000..53aab81b --- /dev/null +++ b/scenario/known-issues/block-autosplat-non-array.rb @@ -0,0 +1,17 @@ +## update +# A sole non-Array value is bound to the first parameter, not splatted. +def yield_one + yield 1 +end + +def check1 = yield_one {|x, y| [x, y] } +def check2 = [1, "str"].map {|x, y| x } +def check3 = [[1, 2], :sym].map {|x, y| x } + +## assert +class Object + def yield_one: { (Integer) -> [Integer, nil] } -> [Integer, nil] + def check1: -> [Integer, nil] + def check2: -> Array[Integer | String] + def check3: -> Array[:sym | Integer] +end diff --git a/scenario/known-issues/block-rest-param.rb b/scenario/known-issues/block-rest-param.rb new file mode 100644 index 00000000..1d79d3a9 --- /dev/null +++ b/scenario/known-issues/block-rest-param.rb @@ -0,0 +1,11 @@ +## update +def check1 = [1].map {|*b| b } +def check2 = [[1, "str"]].map {|a, *b| b } +def check3 = [[1, "str", :sym]].map {|a, *b, c| c } + +## assert +class Object + def check1: -> Array[Array[Integer]] + def check2: -> Array[Array[String]] + def check3: -> Array[:sym] +end diff --git a/scenario/known-issues/case-in-local-assignment.rb b/scenario/known-issues/case-in-local-assignment.rb new file mode 100644 index 00000000..9224b8cf --- /dev/null +++ b/scenario/known-issues/case-in-local-assignment.rb @@ -0,0 +1,20 @@ +## update +def foo(value) + label = :none + case value + in Integer + label = :integer + in String + label = :string + else + end + label +end + +foo(1) +foo("str") + +## assert +class Object + def foo: (Integer | String) -> (:integer | :none | :string) +end diff --git a/scenario/known-issues/constant-alias.rb b/scenario/known-issues/constant-alias.rb new file mode 100644 index 00000000..f32ec881 --- /dev/null +++ b/scenario/known-issues/constant-alias.rb @@ -0,0 +1,40 @@ +## update +module M + CONST = 1 +end + +Alias = M + +def test = Alias::CONST + +## assert +module M + CONST: Integer +end +Alias: singleton(M) +class Object + def test: -> Integer +end + +## update +class Base + def hello = :hi +end + +Alias = Base + +class Foo < Alias +end + +def test = Foo.new.hello + +## assert +class Base + def hello: -> :hi +end +Alias: singleton(Base) +class Foo < Base +end +class Object + def test: -> :hi +end diff --git a/scenario/known-issues/cyclic-include.rb b/scenario/known-issues/cyclic-include.rb new file mode 100644 index 00000000..3bf265ef --- /dev/null +++ b/scenario/known-issues/cyclic-include.rb @@ -0,0 +1,28 @@ +## update +module A +end + +module B + include A +end + +module A + include B +end + +class C + include A + def foo = bar +end + +## assert +module A +end +module B +end +module A +end +class C + include A + def foo: -> untyped +end diff --git a/scenario/known-issues/defined.rb b/scenario/known-issues/defined.rb new file mode 100644 index 00000000..3351edfa --- /dev/null +++ b/scenario/known-issues/defined.rb @@ -0,0 +1,15 @@ +## update +def foo + x = 1 + defined?(x) +end + +def bar + defined?(@unknown) +end + +## assert +class Object + def foo: -> String? + def bar: -> String? +end diff --git a/scenario/known-issues/destructuring-param-rest.rb b/scenario/known-issues/destructuring-param-rest.rb new file mode 100644 index 00000000..4e93cbeb --- /dev/null +++ b/scenario/known-issues/destructuring-param-rest.rb @@ -0,0 +1,35 @@ +## update +def check1((a, *b)) + b +end + +check1([1, 2, 3]) + +def check2((a, *b, c)) + c +end + +check2([1, 2, :sym]) + +def check3((a, (b, c))) + c +end + +check3([1, [2, "str"]]) + +## assert +class Object + def check1: ([Integer, Integer, Integer]) -> Array[Integer] + def check2: ([Integer, Integer, :sym]) -> :sym + def check3: ([Integer, [Integer, String]]) -> String +end + +## update +def check + [[1, "str", :sym]].map {|(a, *b)| b } +end + +## assert +class Object + def check: -> Array[Array[:sym | String]] +end diff --git a/scenario/known-issues/dynamic-constant-scope.rb b/scenario/known-issues/dynamic-constant-scope.rb new file mode 100644 index 00000000..09e61b35 --- /dev/null +++ b/scenario/known-issues/dynamic-constant-scope.rb @@ -0,0 +1,27 @@ +## update +class Foo + X = 1 + + def self.foo = self::X +end + +## assert +class Foo + X: Integer + def self.foo: -> Integer +end + +## update +class Foo + X = 1 + + def foo = self.class::X +end + +Foo.new.foo + +## assert +class Foo + X: Integer + def foo: -> Integer +end diff --git a/scenario/known-issues/for-loop-index-variable.rb b/scenario/known-issues/for-loop-index-variable.rb new file mode 100644 index 00000000..75de7dcb --- /dev/null +++ b/scenario/known-issues/for-loop-index-variable.rb @@ -0,0 +1,19 @@ +## update +def foo + for i in [1, 2, 3] + return i + end + nil +end + +def bar + for s in ["a", "b"] + end + s +end + +## assert +class Object + def foo: -> Integer? + def bar: -> String? +end diff --git a/scenario/known-issues/hash-pattern-capture.rb b/scenario/known-issues/hash-pattern-capture.rb new file mode 100644 index 00000000..52e5c042 --- /dev/null +++ b/scenario/known-issues/hash-pattern-capture.rb @@ -0,0 +1,23 @@ +## update +def foo(h) + case h + in { v: x } + x + end +end + +def bar(h) + case h + in { v: } + v + end +end + +foo({ v: 1 }) +bar({ v: "str" }) + +## assert +class Object + def foo: ({ v: Integer }) -> Integer + def bar: ({ v: String }) -> String +end diff --git a/scenario/known-issues/include-in-singleton-class.rb b/scenario/known-issues/include-in-singleton-class.rb new file mode 100644 index 00000000..cbcb966b --- /dev/null +++ b/scenario/known-issues/include-in-singleton-class.rb @@ -0,0 +1,21 @@ +## update +module Helpers + def helper = :ok +end + +class Foo + class << self + include Helpers + + def bar = helper + end +end + +## assert +module Helpers + def helper: -> :ok +end +class Foo + extend Helpers + def self.bar: -> :ok +end diff --git a/scenario/known-issues/inline-rbs-keywords.rb b/scenario/known-issues/inline-rbs-keywords.rb new file mode 100644 index 00000000..d62f3c95 --- /dev/null +++ b/scenario/known-issues/inline-rbs-keywords.rb @@ -0,0 +1,16 @@ +## update +#: (name: String, ?age: Integer) -> String +def greet(name:, age: 0) + "#{name}: #{age}" +end + +#: (**Integer) -> Integer +def total(**opts) + 1 +end + +## assert +class Object + def greet: (name: String, ?age: Integer) -> String + def total: (**Integer) -> Integer +end diff --git a/scenario/known-issues/inline-rbs-type-args-mismatch.rb b/scenario/known-issues/inline-rbs-type-args-mismatch.rb new file mode 100644 index 00000000..f3887164 --- /dev/null +++ b/scenario/known-issues/inline-rbs-type-args-mismatch.rb @@ -0,0 +1,15 @@ +## update +class Foo +end + +class Bar + #: -> Foo[Integer] + def foo = Foo.new +end + +## assert +class Foo +end +class Bar + def foo: -> Foo[Integer] +end diff --git a/scenario/known-issues/inline-rbs-type-params.rb b/scenario/known-issues/inline-rbs-type-params.rb new file mode 100644 index 00000000..4253044d --- /dev/null +++ b/scenario/known-issues/inline-rbs-type-params.rb @@ -0,0 +1,10 @@ +## update +class C + #: [T] (T) -> T + def id(x) = x +end + +## assert +class C + def id: [T] (T) -> T +end diff --git a/scenario/known-issues/keyword-args-to-rbs-hash-param.rb b/scenario/known-issues/keyword-args-to-rbs-hash-param.rb new file mode 100644 index 00000000..61821999 --- /dev/null +++ b/scenario/known-issues/keyword-args-to-rbs-hash-param.rb @@ -0,0 +1,22 @@ +## update: test.rbs +class C + def foo: () -> Integer + | (Hash[Symbol, Integer]) -> String + def bar: (Hash[Symbol, Integer]) -> String +end + +## update: test.rb +def check1 = C.new.foo(a: 1) +def check2 = C.new.bar(a: 1) +def check3 = { a: 1 }.merge(b: "str") +def check4 = "abc".sub(/a/, "a" => "X") + +## assert: test.rb +class Object + def check1: -> String + def check2: -> String + def check3: -> Hash[:a | :b, Integer | String] + def check4: -> String +end + +## diagnostics: test.rb diff --git a/scenario/known-issues/module-attr.rb b/scenario/known-issues/module-attr.rb new file mode 100644 index 00000000..ff9f2d02 --- /dev/null +++ b/scenario/known-issues/module-attr.rb @@ -0,0 +1,16 @@ +## update +class Foo + attr :name + + def initialize + @name = "foo" + end +end + +Foo.new.name + +## assert +class Foo + def name: -> String + def initialize: -> void +end diff --git a/scenario/known-issues/next-in-branch.rb b/scenario/known-issues/next-in-branch.rb new file mode 100644 index 00000000..ae02ef73 --- /dev/null +++ b/scenario/known-issues/next-in-branch.rb @@ -0,0 +1,36 @@ +## update +def foo(n) + found = nil + while n < 3 + n += 1 + if n == 1 + found = "a" + next + end + end + found +end + +foo(0) + +## assert +class Object + def foo: (Integer) -> String? +end + +## update +def foo + found = nil + [1, 2].each do |item| + if item == 1 + found = :hit + next + end + end + found +end + +## assert +class Object + def foo: -> :hit? +end diff --git a/scenario/known-issues/prepend.rb b/scenario/known-issues/prepend.rb new file mode 100644 index 00000000..813e7334 --- /dev/null +++ b/scenario/known-issues/prepend.rb @@ -0,0 +1,46 @@ +## update +module M + def hello = :module +end + +class C + prepend M + + def hello = :class +end + +def test = C.new.hello + +## assert +module M + def hello: -> :module +end +class C + prepend M + def hello: -> :class +end +class Object + def test: -> :module +end + +## update +module M + def hello = :module +end + +class C + prepend M +end + +def test = C.new.hello + +## assert +module M + def hello: -> :module +end +class C + prepend M +end +class Object + def test: -> :module +end diff --git a/scenario/known-issues/rbs-overload-missing-required-keyword.rb b/scenario/known-issues/rbs-overload-missing-required-keyword.rb new file mode 100644 index 00000000..ac6f21da --- /dev/null +++ b/scenario/known-issues/rbs-overload-missing-required-keyword.rb @@ -0,0 +1,13 @@ +## update: test.rbs +class Object + def foo: (String) -> Integer + | (String, exception: bool) -> String +end + +## update: test.rb +def check = foo("str") + +## assert: test.rb +class Object + def check: -> Integer +end diff --git a/scenario/known-issues/rbs-record-string-key.rb b/scenario/known-issues/rbs-record-string-key.rb new file mode 100644 index 00000000..f825bd77 --- /dev/null +++ b/scenario/known-issues/rbs-record-string-key.rb @@ -0,0 +1,10 @@ +## update +#: ({ "name" => String }) -> String +def foo(x) + "str" +end + +## assert +class Object + def foo: ({ "name" => String }) -> String +end diff --git a/scenario/known-issues/record-key-quoting.rb b/scenario/known-issues/record-key-quoting.rb new file mode 100644 index 00000000..055f9d4a --- /dev/null +++ b/scenario/known-issues/record-key-quoting.rb @@ -0,0 +1,7 @@ +## update +def foo = { "@context": "x", "data-id": 1 } + +## assert +class Object + def foo: -> { :@context => String, :"data-id" => Integer } +end diff --git a/scenario/known-issues/rescue-local-assignment.rb b/scenario/known-issues/rescue-local-assignment.rb new file mode 100644 index 00000000..365235ec --- /dev/null +++ b/scenario/known-issues/rescue-local-assignment.rb @@ -0,0 +1,49 @@ +## update +def foo + x = :start + begin + 1 + rescue + x = :rescue + end + x +end + +## assert +class Object + def foo: -> (:rescue | :start) +end + +## update +def foo + x = :start + begin + 1 + rescue + x = :rescue + else + x = :else + end + x +end + +## assert +class Object + def foo: -> (:else | :rescue) +end + +## update +def foo + e = :before + begin + 1 + rescue ArgumentError => e + 2 + end + e +end + +## assert +class Object + def foo: -> (:before | ArgumentError) +end diff --git a/scenario/known-issues/rescue-reference.rb b/scenario/known-issues/rescue-reference.rb new file mode 100644 index 00000000..ae7eeafb --- /dev/null +++ b/scenario/known-issues/rescue-reference.rb @@ -0,0 +1,26 @@ +## update +def foo + :normal +rescue => e + e +end + +## assert +class Object + def foo: -> (:normal | StandardError) +end + +## update +ERRORS = [ArgumentError, TypeError] + +def foo + :normal +rescue *ERRORS => e + e +end + +## assert +ERRORS: [singleton(ArgumentError), singleton(TypeError)] +class Object + def foo: -> (:normal | ArgumentError | TypeError) +end diff --git a/scenario/known-issues/splat-arg-order.rb b/scenario/known-issues/splat-arg-order.rb new file mode 100644 index 00000000..dbac5c13 --- /dev/null +++ b/scenario/known-issues/splat-arg-order.rb @@ -0,0 +1,13 @@ +## update +class C + def check = take(*gen) + def gen = [1, "str"] + def take(a, b) = a +end + +## assert +class C + def check: -> (Integer | String) + def gen: -> [Integer, String] + def take: (Integer | String, Integer | String) -> (Integer | String) +end diff --git a/scenario/known-issues/struct-member-name.rb b/scenario/known-issues/struct-member-name.rb new file mode 100644 index 00000000..8204bced --- /dev/null +++ b/scenario/known-issues/struct-member-name.rb @@ -0,0 +1,11 @@ +## update +Foo = Struct.new(:bar?) +Foo.new(true) + +## assert +class Foo + def bar?: -> true + def `bar?=`: (untyped) -> untyped + def initialize: (?true) -> void + def self.[]: (?true) -> Foo +end diff --git a/scenario/known-issues/truthiness-narrowing-false.rb b/scenario/known-issues/truthiness-narrowing-false.rb new file mode 100644 index 00000000..0133669b --- /dev/null +++ b/scenario/known-issues/truthiness-narrowing-false.rb @@ -0,0 +1,29 @@ +## update +def foo(v) + if v + [v] + else + v + end +end + +foo(true) +foo(false) + +## assert +class Object + def foo: (bool) -> ([true] | false) +end + +## update +def foo(v) + v ? 1 : v +end + +foo(true) +foo(false) + +## assert +class Object + def foo: (bool) -> (Integer | false) +end diff --git a/scenario/known-issues/union-false-block-return.rb b/scenario/known-issues/union-false-block-return.rb new file mode 100644 index 00000000..33bede92 --- /dev/null +++ b/scenario/known-issues/union-false-block-return.rb @@ -0,0 +1,12 @@ +## update +# Enumerable#filter_map: [U] () { (E elem) -> (nil | false | U) } -> Array[U] +# A block that can return `false` makes U lose every other type, because the +# `false` member of the union accepts the whole return vertex. +def check1 = [1, 2].filter_map {|n| n.even? ? n : false } +def check2 = [1, 2].filter_map {|n| n.even? ? n.to_s : false } + +## assert +class Object + def check1: -> Array[Integer] + def check2: -> Array[String] +end diff --git a/scenario/known-issues/while-assign-narrowing.rb b/scenario/known-issues/while-assign-narrowing.rb new file mode 100644 index 00000000..221c147b --- /dev/null +++ b/scenario/known-issues/while-assign-narrowing.rb @@ -0,0 +1,29 @@ +## update +# `if (y = x)` narrows y (scenario/flow/if_assign.rb) and `while x` narrows x +# (scenario/flow/while.rb), but an assignment in a while condition does not. +def check(ary) + while (x = ary.pop) + return x + end + :none +end + +check(["str"]) + +## assert +class Object + def check: ([String]) -> (:none | String) +end + +## update +def each_entry + entries = ["str"] + while entry = entries.pop + yield entry + end +end + +## assert +class Object + def each_entry: { (String) -> untyped } -> nil +end