88 IntersectionTypeNode ,
99 LiteralTypeNode ,
1010 Node ,
11+ ParameterDeclaration ,
1112 PropertyDeclaration ,
1213 PropertySignature ,
1314 Signature ,
@@ -772,6 +773,44 @@ export class Converter {
772773 return this . otherTypeToIR ( typeNode ) ;
773774 }
774775
776+ paramToIR (
777+ param : ParameterDeclaration ,
778+ idx : number ,
779+ isLast : boolean ,
780+ ) : ParamIR | undefined {
781+ const optional = ! ! param . hasQuestionToken ( ) ;
782+ let name = param . getName ( ) ;
783+ let isIdentifier = isValidPythonIdentifier ( name ) ;
784+ const oldNameContext = this . nameContext ?. slice ( ) ;
785+ const paramType = param . getTypeNode ( ) ! ;
786+ const destructureOnly = isLast && Node . isTypeLiteral ( paramType ) ;
787+ if ( ! destructureOnly && ! isIdentifier ) {
788+ // Replace name with args${idx}. This is an unfortunate case so we log it.
789+ console . log ( "Encountered argument with non identifier name" ) ;
790+ console . log ( param . print ( ) ) ;
791+ console . log ( getNodeLocation ( param ) ) ;
792+ name = `args${ idx } ` ;
793+ isIdentifier = true ;
794+ }
795+ if ( destructureOnly ) {
796+ // If it's the last argument and the type is a type literal, we'll
797+ // destructure it so don't make a type.
798+ this . nameContext = undefined ;
799+ } else if ( isIdentifier ) {
800+ this . pushNameContext ( name ) ;
801+ } else {
802+ this . nameContext = undefined ;
803+ }
804+ const type = this . typeToIR ( paramType , optional ) ;
805+ this . nameContext = oldNameContext ;
806+ const pyParam : ParamIR = {
807+ name,
808+ type,
809+ isOptional : optional ,
810+ } ;
811+ return pyParam ;
812+ }
813+
775814 sigToIR ( sig : Signature ) : SigIR {
776815 const decl1 = sig . getDeclaration ( ) ;
777816 const decl =
@@ -790,51 +829,28 @@ export class Converter {
790829 const params = decl . getParameters ( ) ;
791830 for ( let idx = 0 ; idx < params . length ; idx ++ ) {
792831 const param = params [ idx ] ;
793- const spread = ! ! param . getDotDotDotToken ( ) ;
794- const optional = ! ! param . hasQuestionToken ( ) ;
795- let name = param . getName ( ) ;
796- let isIdentifier = isValidPythonIdentifier ( name ) ;
797- const oldNameContext = this . nameContext ?. slice ( ) ;
798- const paramType = param . getTypeNode ( ) ! ;
799832 const isLast = idx === params . length - 1 ;
800- const destructureOnly = isLast && Node . isTypeLiteral ( paramType ) ;
801- if ( ! destructureOnly && ! isIdentifier ) {
802- // Replace name with args${idx}. This is an unfortunate case so we log it.
803- console . log ( "Encountered argument with non identifier name" ) ;
804- console . log ( params [ idx ] . print ( ) ) ;
805- console . log ( getNodeLocation ( params [ idx ] ) ) ;
806- name = `args${ idx } ` ;
807- isIdentifier = true ;
808- }
809- if ( destructureOnly ) {
810- // If it's the last argument and the type is a type literal, we'll
811- // destructure it so don't make a type.
812- this . nameContext = undefined ;
813- } else if ( isIdentifier ) {
814- this . pushNameContext ( name ) ;
815- } else {
816- this . nameContext = undefined ;
833+ const spread = ! ! param . getDotDotDotToken ( ) ;
834+ const pyParam = this . paramToIR ( param , idx , isLast ) ;
835+ if ( ! pyParam ) {
836+ continue ;
817837 }
818- const type = this . typeToIR ( paramType , optional ) ;
819- this . nameContext = oldNameContext ;
820- const pyParam : ParamIR = {
821- name,
822- type,
823- isOptional : optional ,
824- } ;
825838 if ( spread ) {
826839 spreadParam = pyParam ;
827- if ( type . kind === "array" ) {
828- pyParam . type = type . type ;
829- } else {
830- console . warn (
831- `expected type array for spread param, got ${ type . kind } ` ,
832- ) ;
833- pyParam . type = type ;
834- }
835- continue ;
840+ } else {
841+ pyParams . push ( pyParam ) ;
842+ }
843+ }
844+ if ( spreadParam ) {
845+ const type = spreadParam . type ;
846+ if ( type . kind === "array" ) {
847+ spreadParam . type = type . type ;
848+ } else {
849+ console . warn (
850+ `expected type array for spread param, got ${ type . kind } ` ,
851+ ) ;
852+ spreadParam . type = type ;
836853 }
837- pyParams . push ( pyParam ) ;
838854 }
839855 const retNode = decl . getReturnTypeNode ( ) ;
840856 let returns : TypeIR ;
0 commit comments