Skip to content

Commit bf4906e

Browse files
authored
Merge pull request #624 from Shopify/at-fix-anonymous-params
Fix anonymous parameter modeling and merging
2 parents c671f81 + 290fc6f commit bf4906e

13 files changed

Lines changed: 451 additions & 143 deletions

lib/rbi/model.rb

Lines changed: 148 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,36 @@ def to_s
599599

600600
# @abstract
601601
class Param < NodeWithComments
602+
#: (?loc: Loc?, ?comments: Array[Comment]?) -> void
603+
def initialize(loc: nil, comments: nil)
604+
super(loc: loc, comments: comments)
605+
end
606+
607+
#: -> String?
608+
def name
609+
case self
610+
when ReqParam, OptParam, KwParam, KwOptParam, RestParam, KwRestParam, BlockParam
611+
name
612+
end
613+
end
614+
615+
# @abstract
616+
#: -> bool
617+
def anonymous?
618+
raise NotImplementedError, "Abstract method called"
619+
end
620+
end
621+
622+
class ReqParam < Param
623+
# @override
602624
#: String
603625
attr_reader :name
604626

605-
#: (String name, ?loc: Loc?, ?comments: Array[Comment]?) -> void
606-
def initialize(name, loc: nil, comments: nil)
627+
#: (String name, ?loc: Loc?, ?comments: Array[Comment]?) ?{ (ReqParam node) -> void } -> void
628+
def initialize(name, loc: nil, comments: nil, &block)
607629
super(loc: loc, comments: comments)
608630
@name = name.to_s #: String
609-
@anonymous = name.start_with?("_") #: bool
631+
block&.call(self)
610632
end
611633

612634
# @override
@@ -615,42 +637,61 @@ def to_s
615637
name
616638
end
617639

640+
# @override
618641
#: -> bool
619642
def anonymous?
620-
@anonymous
643+
false
621644
end
622645

623646
#: (Object? other) -> bool
624647
def ==(other)
625-
self.class === other &&
626-
(name == other.name || anonymous? || other.anonymous?)
627-
end
628-
end
629-
630-
class ReqParam < Param
631-
#: (String name, ?loc: Loc?, ?comments: Array[Comment]?) ?{ (ReqParam node) -> void } -> void
632-
def initialize(name, loc: nil, comments: nil, &block)
633-
super(name, loc: loc, comments: comments)
634-
block&.call(self)
648+
ReqParam === other && (name == other.name)
635649
end
636650
end
637651

638652
class OptParam < Param
653+
# @override
654+
#: String
655+
attr_reader :name
656+
639657
#: String
640658
attr_reader :value
641659

642660
#: (String name, String value, ?loc: Loc?, ?comments: Array[Comment]?) ?{ (OptParam node) -> void } -> void
643661
def initialize(name, value, loc: nil, comments: nil, &block)
644-
super(name, loc: loc, comments: comments)
662+
super(loc: loc, comments: comments)
663+
@name = name.to_s #: String
645664
@value = value
646665
block&.call(self)
647666
end
667+
668+
# @override
669+
#: -> String
670+
def to_s
671+
"#{name} = #{value}"
672+
end
673+
674+
# @override
675+
#: -> bool
676+
def anonymous?
677+
false
678+
end
679+
680+
#: (Object? other) -> bool
681+
def ==(other)
682+
OptParam === other && (name == other.name)
683+
end
648684
end
649685

650686
class RestParam < Param
651-
#: (String name, ?loc: Loc?, ?comments: Array[Comment]?) ?{ (RestParam node) -> void } -> void
687+
# @override
688+
#: String?
689+
attr_reader :name
690+
691+
#: (String? name, ?loc: Loc?, ?comments: Array[Comment]?) ?{ (RestParam node) -> void } -> void
652692
def initialize(name, loc: nil, comments: nil, &block)
653-
super(name, loc: loc, comments: comments)
693+
super(loc: loc, comments: comments)
694+
@name = name&.to_s #: String?
654695
block&.call(self)
655696
end
656697

@@ -659,12 +700,28 @@ def initialize(name, loc: nil, comments: nil, &block)
659700
def to_s
660701
"*#{name}"
661702
end
703+
704+
# @override
705+
#: -> bool
706+
def anonymous?
707+
name.nil?
708+
end
709+
710+
#: (Object? other) -> bool
711+
def ==(other)
712+
RestParam === other && (name == other.name || anonymous? || other.anonymous?)
713+
end
662714
end
663715

664716
class KwParam < Param
717+
# @override
718+
#: String
719+
attr_reader :name
720+
665721
#: (String name, ?loc: Loc?, ?comments: Array[Comment]?) ?{ (KwParam node) -> void } -> void
666722
def initialize(name, loc: nil, comments: nil, &block)
667-
super(name, loc: loc, comments: comments)
723+
super(loc: loc, comments: comments)
724+
@name = name.to_s #: String
668725
block&.call(self)
669726
end
670727

@@ -673,44 +730,92 @@ def initialize(name, loc: nil, comments: nil, &block)
673730
def to_s
674731
"#{name}:"
675732
end
733+
734+
# @override
735+
#: -> bool
736+
def anonymous?
737+
false
738+
end
739+
740+
#: (Object? other) -> bool
741+
def ==(other)
742+
KwParam === other && (name == other.name)
743+
end
676744
end
677745

678746
class KwOptParam < Param
747+
# @override
748+
#: String
749+
attr_reader :name
750+
679751
#: String
680752
attr_reader :value
681753

682754
#: (String name, String value, ?loc: Loc?, ?comments: Array[Comment]?) ?{ (KwOptParam node) -> void } -> void
683755
def initialize(name, value, loc: nil, comments: nil, &block)
684-
super(name, loc: loc, comments: comments)
756+
super(loc: loc, comments: comments)
757+
@name = name.to_s #: String
685758
@value = value
686759
block&.call(self)
687760
end
688761

689762
# @override
690763
#: -> String
691764
def to_s
692-
"#{name}:"
765+
"#{name}: #{value}"
766+
end
767+
768+
# @override
769+
#: -> bool
770+
def anonymous?
771+
false
772+
end
773+
774+
#: (Object? other) -> bool
775+
def ==(other)
776+
KwOptParam === other && (name == other.name)
693777
end
694778
end
695779

696780
class KwRestParam < Param
697-
#: (String name, ?loc: Loc?, ?comments: Array[Comment]?) ?{ (KwRestParam node) -> void } -> void
781+
# @override
782+
#: String?
783+
attr_reader :name
784+
785+
#: (String? name, ?loc: Loc?, ?comments: Array[Comment]?) ?{ (KwRestParam node) -> void } -> void
698786
def initialize(name, loc: nil, comments: nil, &block)
699-
super(name, loc: loc, comments: comments)
787+
super(loc: loc, comments: comments)
788+
@name = name&.to_s #: String?
700789
block&.call(self)
701790
end
702791

703792
# @override
704793
#: -> String
705794
def to_s
706-
"**#{name}:"
795+
"**#{name}"
796+
end
797+
798+
# @override
799+
#: -> bool
800+
def anonymous?
801+
name.nil?
802+
end
803+
804+
#: (Object? other) -> bool
805+
def ==(other)
806+
KwRestParam === other && (name == other.name || anonymous? || other.anonymous?)
707807
end
708808
end
709809

710810
class BlockParam < Param
711-
#: (String name, ?loc: Loc?, ?comments: Array[Comment]?) ?{ (BlockParam node) -> void } -> void
811+
# @override
812+
#: String?
813+
attr_reader :name
814+
815+
#: (String? name, ?loc: Loc?, ?comments: Array[Comment]?) ?{ (BlockParam node) -> void } -> void
712816
def initialize(name, loc: nil, comments: nil, &block)
713-
super(name, loc: loc, comments: comments)
817+
super(loc: loc, comments: comments)
818+
@name = name
714819
block&.call(self)
715820
end
716821

@@ -719,6 +824,17 @@ def initialize(name, loc: nil, comments: nil, &block)
719824
def to_s
720825
"&#{name}"
721826
end
827+
828+
# @override
829+
#: -> bool
830+
def anonymous?
831+
name.nil?
832+
end
833+
834+
#: (Object? other) -> bool
835+
def ==(other)
836+
BlockParam === other && (name == other.name || anonymous? || other.anonymous?)
837+
end
722838
end
723839

724840
# Mixins
@@ -1023,11 +1139,17 @@ class SigParam < NodeWithComments
10231139
def initialize(name, type, loc: nil, comments: nil, &block)
10241140
super(loc: loc, comments: comments)
10251141
@name = name.to_s #: String
1026-
@anonymous = name.start_with?("_") #: bool
1142+
@anonymous = name == "*" || name == "**" || name == "&" #: bool
10271143
@type = type
10281144
block&.call(self)
10291145
end
10301146

1147+
# @override
1148+
#: -> String
1149+
def to_s
1150+
name
1151+
end
1152+
10311153
#: -> bool
10321154
def anonymous?
10331155
@anonymous

lib/rbi/parser.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ def parse_params(node)
726726
)
727727
when Prism::RestParameterNode
728728
RestParam.new(
729-
param.name&.to_s || "*args",
729+
param.name&.to_s,
730730
loc: node_loc(param),
731731
comments: node_comments(param),
732732
)
@@ -745,13 +745,13 @@ def parse_params(node)
745745
)
746746
when Prism::KeywordRestParameterNode
747747
KwRestParam.new(
748-
param.name&.to_s || "**kwargs",
748+
param.name&.to_s,
749749
loc: node_loc(param),
750750
comments: node_comments(param),
751751
)
752752
when Prism::BlockParameterNode
753753
BlockParam.new(
754-
param.name&.to_s || "",
754+
param.name&.to_s,
755755
loc: node_loc(param),
756756
comments: node_comments(param),
757757
)
@@ -977,11 +977,21 @@ def visit_call_node(node)
977977
#: (Prism::AssocNode node) -> void
978978
def visit_assoc_node(node)
979979
@current.params << SigParam.new(
980-
node_string!(node.key).delete_suffix(":"),
980+
sig_param_name(node.key),
981981
node_string!(node.value),
982982
)
983983
end
984984

985+
#: (Prism::Node node) -> String
986+
def sig_param_name(node)
987+
case node
988+
when Prism::SymbolNode
989+
node.value.to_s
990+
else
991+
node_string!(node).delete_suffix(":")
992+
end
993+
end
994+
985995
#: (Prism::CallNode node, String value) -> bool
986996
def allow_incompatible_override?(node, value)
987997
args = node.arguments&.arguments

0 commit comments

Comments
 (0)