@@ -806,23 +806,80 @@ def return_type(self, converter: Converter) -> list[ir.Return]:
806
806
)
807
807
]
808
808
809
+ def _fix_type_suffix (self ) -> None :
810
+ if self .path [- 1 ] == "__type" :
811
+ self .path = self .path [:- 1 ]
812
+ self .path [- 1 ] = self .path [- 1 ].removesuffix ("." )
813
+ self .name = self .path [- 1 ]
814
+
815
+ def _destructure_param (self , param : Param ) -> list [Param ]:
816
+ """We want to document a destructured argument as if it were several
817
+ separate arguments. This finds complex inline object types in the arguments
818
+ list of a function and "destructures" them into separately documented arguments.
819
+
820
+ E.g., a function
821
+
822
+ /**
823
+ * @param options
824
+ * @destructure options
825
+ */
826
+ function f({x , y } : {
827
+ /** The x value */
828
+ x : number,
829
+ /** The y value */
830
+ y : string
831
+ }){ ... }
832
+
833
+ should be documented like:
834
+
835
+ options.x (number) The x value
836
+ options.y (number) The y value
837
+ """
838
+ type = param .type
839
+ assert isinstance (type , ReflectionType )
840
+ decl = type .declaration
841
+ result = []
842
+ for child in decl .children :
843
+ assert isinstance (child , Member )
844
+ child_param = Param (
845
+ name = param .name + "." + child .name , flags = Flags (), type = child .type
846
+ )
847
+ result .append (child_param )
848
+ return result
849
+
850
+ def _destructure_params (self ) -> list [Param ]:
851
+ destructure_targets = []
852
+ for tag in self .comment .blockTags :
853
+ if tag .tag == "@destructure" :
854
+ destructure_targets = tag .content [0 ].text .split (" " )
855
+ break
856
+
857
+ if not destructure_targets :
858
+ return self .parameters
859
+
860
+ params = []
861
+ for p in self .parameters :
862
+ if p .name in destructure_targets :
863
+ params .extend (self ._destructure_param (p ))
864
+ return params
865
+
809
866
def to_ir (
810
867
self , converter : Converter
811
868
) -> tuple [ir .Function | None , Sequence ["Node" ]]:
812
869
if self .inheritedFrom is not None :
813
870
if self .comment == Comment ():
814
871
return None , []
815
- if self . path [ - 1 ] == "__type" :
816
- self .path = self . path [: - 1 ]
817
- self . path [ - 1 ] = self .path [ - 1 ]. removesuffix ( "." )
818
- self . name = self . path [ - 1 ]
872
+
873
+ self ._fix_type_suffix ()
874
+ params = self ._destructure_params ( )
875
+
819
876
# This is the real meat of a function, method, or constructor.
820
877
#
821
878
# Constructors' .name attrs end up being like 'new Foo'. They
822
879
# should probably be called "constructor", but I'm not bothering
823
880
# with that yet because nobody uses that attr on constructors atm.
824
881
result = ir .Function (
825
- params = [p .to_ir (converter ) for p in self . parameters ],
882
+ params = [p .to_ir (converter ) for p in params ],
826
883
# Exceptions are discouraged in TS as being unrepresentable in its
827
884
# type system. More importantly, TypeDoc does not support them.
828
885
exceptions = [],
0 commit comments