Skip to content

Commit

Permalink
[TypeScript] Some interface annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave committed Sep 17, 2022
1 parent a43885f commit 2986355
Show file tree
Hide file tree
Showing 23 changed files with 212 additions and 168 deletions.
6 changes: 3 additions & 3 deletions src/Fable.Transforms/Dart/Replacements.fs
Expand Up @@ -554,9 +554,9 @@ let injectArg (com: ICompiler) (ctx: Context) r moduleName methName (genArgs: Ty
List.tryItem injectGenArgIndex genArgs
|> Option.bind (fun genArg ->
match injectType with
| Types.comparer ->
| Types.icomparerGeneric ->
args @ [makeComparer com ctx genArg] |> Some
| Types.equalityComparerGeneric ->
| Types.iequalityComparerGeneric ->
args @ [makeEqualityComparer com ctx genArg] |> Some
| Types.adder ->
args @ [makeGenericAdder com ctx genArg] |> Some
Expand Down Expand Up @@ -604,7 +604,7 @@ let tryReplacedEntityRef (com: Compiler) entFullName =
| Naming.EndsWith "Enumerator" _
-> makeIdentExpr "Iterator" |> Some
| Types.icomparable | Types.icomparableGeneric -> makeIdentExpr "Comparable" |> Some
| Types.idisposable | Types.adder | Types.averager | Types.comparer | Types.equalityComparerGeneric ->
| Types.idisposable | Types.adder | Types.averager | Types.icomparerGeneric | Types.iequalityComparerGeneric ->
let entFullName = entFullName[entFullName.LastIndexOf(".") + 1..]
let entFullName =
match entFullName.IndexOf("`") with
Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/FSharp2Fable.Util.fs
Expand Up @@ -2001,8 +2001,8 @@ module Util =
| "System.IObserver`1"
| Types.ienumerableGeneric
// These are used for injections
| Types.comparer
| Types.equalityComparerGeneric -> false
| Types.icomparerGeneric
| Types.iequalityComparerGeneric -> false
| Types.icomparable -> false
| Types.icomparableGeneric -> com.Options.Language <> Dart
| _ -> true
Expand Down
74 changes: 57 additions & 17 deletions src/Fable.Transforms/Fable2Babel.fs
Expand Up @@ -409,13 +409,13 @@ module Annotation =
| Fable.Tuple(genArgs,_) -> makeTupleTypeAnnotation com ctx genArgs
| Fable.Array(genArg, kind) -> makeArrayTypeAnnotation com ctx genArg kind
| Fable.List genArg -> makeListTypeAnnotation com ctx genArg
| Replacements.Util.Builtin kind -> makeBuiltinTypeAnnotation com ctx kind
| Fable.GenericParam(name=name) -> makeSimpleTypeAnnotation com ctx name
| Fable.LambdaType(argType, returnType) ->
([argType], returnType)
||> FableTransforms.uncurryLambdaType
||> makeFunctionTypeAnnotation com ctx typ
| Fable.DelegateType(argTypes, returnType) -> makeFunctionTypeAnnotation com ctx typ argTypes returnType
| Fable.GenericParam(name=name) -> makeSimpleTypeAnnotation com ctx name
| Replacements.Util.Builtin kind -> makeBuiltinTypeAnnotation com ctx kind
| Fable.DeclaredType(ent, genArgs) ->
makeEntityTypeAnnotation com ctx ent genArgs
| Fable.AnonymousRecordType(fieldNames, genArgs, _isStruct) ->
Expand Down Expand Up @@ -490,29 +490,67 @@ module Annotation =

let makeFunctionTypeAnnotation com ctx _typ argTypes returnType =
let funcTypeParams =
argTypes
match argTypes with
| [Fable.Unit] -> []
| _ -> argTypes
|> List.mapi (fun i argType ->
FunctionTypeParam.functionTypeParam(
Identifier.identifier("arg" + (string i)),
typeAnnotation com ctx argType))
|> List.toArray
let ctx = { ctx with IsParamType = true };
let genParams = Util.getGenericTypeParams ctx (argTypes @ [returnType])
let returnType = typeAnnotation com ctx returnType
let typeParamDecl = makeTypeParamDecl com ctx genParams
TypeAnnotationInfo.functionTypeAnnotation(funcTypeParams, returnType, ?typeParameters=typeParamDecl)

let makeEntityTypeAnnotation com ctx (ent: Fable.EntityRef) genArgs =
match ent.FullName with
| Types.ienumerableGeneric ->
makeNativeTypeAnnotation com ctx genArgs "IterableIterator"
let makeInterfaceTypeAnnotation com ctx (entRef: Fable.EntityRef) genArgs =
match entRef.FullName with
| Types.icollection
-> makeImportTypeAnnotation com ctx [Fable.Any] "Util" "ICollection"
| Types.icollectionGeneric
-> makeImportTypeAnnotation com ctx genArgs "Util" "ICollection"
// | Types.idictionary
// | Types.ireadonlydictionary
| Types.idisposable
-> makeImportTypeAnnotation com ctx genArgs "Util" "IDisposable"
| Types.ienumerable
-> makeNativeTypeAnnotation com ctx [Fable.Any] "Iterable"
| Types.ienumerableGeneric
-> makeNativeTypeAnnotation com ctx genArgs "Iterable"
// -> makeImportTypeAnnotation com ctx genArgs "Util" "IEnumerable"
| Types.ienumerator
-> makeImportTypeAnnotation com ctx [Fable.Any] "Util" "IEnumerator"
| Types.ienumeratorGeneric
-> makeImportTypeAnnotation com ctx genArgs "Util" "IEnumerator"
| Types.icomparable
-> makeImportTypeAnnotation com ctx [Fable.Any] "Util" "IComparable"
| Types.icomparableGeneric
| Types.iStructuralComparable
-> makeImportTypeAnnotation com ctx genArgs "Util" "IComparable"
| Types.iequatableGeneric
| Types.iStructuralEquatable
-> makeImportTypeAnnotation com ctx genArgs "Util" "IEquatable"
| Types.icomparer
-> makeImportTypeAnnotation com ctx [Fable.Any] "Util" "IComparer"
| Types.icomparerGeneric
-> makeImportTypeAnnotation com ctx genArgs "Util" "IComparer"
| Types.iequalityComparerGeneric
-> makeImportTypeAnnotation com ctx genArgs "Util" "IEqualityComparer"
| _ ->
// TODO: add more interfaces
AnyTypeAnnotation

let makeEntityTypeAnnotation com ctx (entRef: Fable.EntityRef) genArgs =
match entRef.FullName with
| Types.result ->
makeUnionTypeAnnotation com ctx genArgs
| entName when entName.StartsWith(Types.choiceNonGeneric) ->
makeUnionTypeAnnotation com ctx genArgs
| _ ->
let ent = com.GetEntity(ent)
let ent = com.GetEntity(entRef)
if ent.IsInterface then
AnyTypeAnnotation // TODO:
makeInterfaceTypeAnnotation com ctx entRef genArgs
else
match Lib.tryJsConstructor com ctx ent with
| Some entRef ->
Expand Down Expand Up @@ -594,7 +632,7 @@ module Util =
type NamedTailCallOpportunity(_com: Compiler, ctx, name, args: Fable.Ident list) =
// Capture the current argument values to prevent delayed references from getting corrupted,
// for that we use block-scoped ES2015 variable declarations. See #681, #1859
let argIds = discardUnitArg args |> List.map (fun arg ->
let argIds = args |> discardUnitArg |> List.map (fun arg ->
getUniqueNameInDeclarationScope ctx (arg.Name + "_mut"))
interface ITailCallOpportunity with
member _.Label = name
Expand Down Expand Up @@ -2098,23 +2136,25 @@ module Util =
let transformAttachedProperty (com: IBabelCompiler) ctx (info: Fable.MemberFunctionOrValue) (memb: Fable.MemberDecl) =
let isStatic = not info.IsInstance
let kind = if info.IsGetter then ClassGetter else ClassSetter
let args, body, _returnType, _typeParamDecl =
let args, body, returnType, _typeParamDecl =
getMemberArgsAndBody com ctx (Attached isStatic) false memb.Args memb.Body
let key, computed = memberFromName memb.Name
ClassMember.classMethod(kind, key, args, body, computed_=computed, ``static``=isStatic)
ClassMember.classMethod(kind, key, args, body, computed_=computed, ``static``=isStatic,
?returnType=returnType) //, ?typeParameters=typeParamDecl)
|> Array.singleton

let transformAttachedMethod (com: IBabelCompiler) ctx (info: Fable.MemberFunctionOrValue) (memb: Fable.MemberDecl) =
let isStatic = not info.IsInstance
let makeMethod name args body =
let makeMethod name args body returnType _typeParamDecl =
let key, computed = memberFromName name
ClassMember.classMethod(ClassFunction, key, args, body, computed_=computed, ``static``=isStatic)
let args, body, _returnType, _typeParamDecl =
ClassMember.classMethod(ClassFunction, key, args, body, computed_=computed, ``static``=isStatic,
?returnType=returnType) //, ?typeParameters=typeParamDecl)
let args, body, returnType, typeParamDecl =
getMemberArgsAndBody com ctx (Attached isStatic) info.HasSpread memb.Args memb.Body
[|
yield makeMethod memb.Name args body
yield makeMethod memb.Name args body returnType typeParamDecl
if info.FullName = "System.Collections.Generic.IEnumerable.GetEnumerator" then
yield makeMethod "Symbol.iterator" [||] (enumerator2iterator com ctx)
yield makeMethod "Symbol.iterator" [||] (enumerator2iterator com ctx) None None
|]

let transformUnion (com: IBabelCompiler) ctx (ent: Fable.Entity) (entName: string) classMembers =
Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Python/Fable2Python.fs
Expand Up @@ -851,12 +851,12 @@ module Annotation =
| Types.icomparable, _ -> libValue com ctx "util" "IComparable", []
| Types.iStructuralEquatable, _ -> libValue com ctx "util" "IStructuralEquatable", []
| Types.iStructuralComparable, _ -> libValue com ctx "util" "IStructuralComparable", []
| Types.comparer, _ ->
| Types.icomparerGeneric, _ ->
let resolved, stmts = resolveGenerics com ctx genArgs repeatedGenerics
fableModuleAnnotation com ctx "util" "IComparer_1" resolved, stmts
| Types.equalityComparer, _ ->
libValue com ctx "util" "IEqualityComparer", []
| Types.equalityComparerGeneric, _ ->
| Types.iequalityComparerGeneric, _ ->
let resolved, stmts = stdlibModuleTypeHint com ctx "typing" "Any" []
fableModuleAnnotation com ctx "util" "IEqualityComparer_1" [ resolved ], stmts
| Types.ienumerator, _ ->
Expand Down
5 changes: 2 additions & 3 deletions src/Fable.Transforms/Python/Replacements.fs
Expand Up @@ -486,7 +486,6 @@ let structuralHash (com: ICompiler) r (arg: Expr) =
| BclDateTimeOffset) -> "dateHash"
| DeclaredType (ent, _) ->
let ent = com.GetEntity(ent)

if not ent.IsInterface then
"safeHash"
else
Expand Down Expand Up @@ -769,8 +768,8 @@ let injectArg (com: ICompiler) (ctx: Context) r moduleName methName (genArgs: Ty
| None -> fail ()
| Some genArg ->
match injectType with
| Types.comparer -> args @ [ makeComparer com ctx genArg ]
| Types.equalityComparerGeneric -> args @ [ makeEqualityComparer com ctx genArg ]
| Types.icomparerGeneric -> args @ [ makeComparer com ctx genArg ]
| Types.iequalityComparerGeneric -> args @ [ makeEqualityComparer com ctx genArg ]
| Types.arrayCons ->
match genArg with
// We don't have a module for ResizeArray so let's assume the kind is MutableArray
Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Replacements.Util.fs
Expand Up @@ -414,15 +414,15 @@ let (|IDictionary|IEqualityComparer|Other|) = function
| DeclaredType(ent,_) ->
match ent.FullName with
| Types.idictionary -> IDictionary
| Types.equalityComparerGeneric -> IEqualityComparer
| Types.iequalityComparerGeneric -> IEqualityComparer
| _ -> Other
| _ -> Other

let (|IEnumerable|IEqualityComparer|Other|) = function
| DeclaredType(ent,_) ->
match ent.FullName with
| Types.ienumerableGeneric -> IEnumerable
| Types.equalityComparerGeneric -> IEqualityComparer
| Types.iequalityComparerGeneric -> IEqualityComparer
| _ -> Other
| _ -> Other

Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Replacements.fs
Expand Up @@ -689,9 +689,9 @@ let injectArg (com: ICompiler) (ctx: Context) r moduleName methName (genArgs: Ty
| None -> fail()
| Some genArg ->
match injectType with
| Types.comparer ->
| Types.icomparerGeneric ->
args @ [makeComparer com ctx genArg]
| Types.equalityComparerGeneric ->
| Types.iequalityComparerGeneric ->
args @ [makeEqualityComparer com ctx genArg]
| Types.arrayCons ->
match genArg with
Expand Down
96 changes: 48 additions & 48 deletions src/Fable.Transforms/ReplacementsInject.fs
Expand Up @@ -15,8 +15,8 @@ let fableReplacementsModules =
"mapFoldBack", (Types.arrayCons, 2)
"concat", (Types.arrayCons, 0)
"collect", (Types.arrayCons, 1)
"indexOf", (Types.equalityComparerGeneric, 0)
"contains", (Types.equalityComparerGeneric, 0)
"indexOf", (Types.iequalityComparerGeneric, 0)
"contains", (Types.iequalityComparerGeneric, 0)
"singleton", (Types.arrayCons, 0)
"initialize", (Types.arrayCons, 0)
"replicate", (Types.arrayCons, 0)
Expand All @@ -26,21 +26,21 @@ let fableReplacementsModules =
"skipWhile", (Types.arrayCons, 0)
"take", (Types.arrayCons, 0)
"takeWhile", (Types.arrayCons, 0)
"removeInPlace", (Types.equalityComparerGeneric, 0)
"removeInPlace", (Types.iequalityComparerGeneric, 0)
"partition", (Types.arrayCons, 0)
"choose", (Types.arrayCons, 1)
"sortInPlaceBy", (Types.comparer, 1)
"sortInPlace", (Types.comparer, 0)
"sort", (Types.comparer, 0)
"sortBy", (Types.comparer, 1)
"sortDescending", (Types.comparer, 0)
"sortByDescending", (Types.comparer, 1)
"sortInPlaceBy", (Types.icomparerGeneric, 1)
"sortInPlace", (Types.icomparerGeneric, 0)
"sort", (Types.icomparerGeneric, 0)
"sortBy", (Types.icomparerGeneric, 1)
"sortDescending", (Types.icomparerGeneric, 0)
"sortByDescending", (Types.icomparerGeneric, 1)
"sum", ("Fable.Core.IGenericAdder`1", 0)
"sumBy", ("Fable.Core.IGenericAdder`1", 1)
"maxBy", (Types.comparer, 1)
"max", (Types.comparer, 0)
"minBy", (Types.comparer, 1)
"min", (Types.comparer, 0)
"maxBy", (Types.icomparerGeneric, 1)
"max", (Types.icomparerGeneric, 0)
"minBy", (Types.icomparerGeneric, 1)
"min", (Types.icomparerGeneric, 0)
"average", ("Fable.Core.IGenericAverager`1", 0)
"averageBy", ("Fable.Core.IGenericAverager`1", 1)
"transpose", (Types.arrayCons, 0)
Expand All @@ -49,57 +49,57 @@ let fableReplacementsModules =
"updateAt", (Types.arrayCons, 0)
]
"List", Map [
"contains", (Types.equalityComparerGeneric, 0)
"sort", (Types.comparer, 0)
"sortBy", (Types.comparer, 1)
"sortDescending", (Types.comparer, 0)
"sortByDescending", (Types.comparer, 1)
"contains", (Types.iequalityComparerGeneric, 0)
"sort", (Types.icomparerGeneric, 0)
"sortBy", (Types.icomparerGeneric, 1)
"sortDescending", (Types.icomparerGeneric, 0)
"sortByDescending", (Types.icomparerGeneric, 1)
"sum", ("Fable.Core.IGenericAdder`1", 0)
"sumBy", ("Fable.Core.IGenericAdder`1", 1)
"maxBy", (Types.comparer, 1)
"max", (Types.comparer, 0)
"minBy", (Types.comparer, 1)
"min", (Types.comparer, 0)
"maxBy", (Types.icomparerGeneric, 1)
"max", (Types.icomparerGeneric, 0)
"minBy", (Types.icomparerGeneric, 1)
"min", (Types.icomparerGeneric, 0)
"average", ("Fable.Core.IGenericAverager`1", 0)
"averageBy", ("Fable.Core.IGenericAverager`1", 1)
]
"Seq", Map [
"contains", (Types.equalityComparerGeneric, 0)
"sort", (Types.comparer, 0)
"sortBy", (Types.comparer, 1)
"sortDescending", (Types.comparer, 0)
"sortByDescending", (Types.comparer, 1)
"contains", (Types.iequalityComparerGeneric, 0)
"sort", (Types.icomparerGeneric, 0)
"sortBy", (Types.icomparerGeneric, 1)
"sortDescending", (Types.icomparerGeneric, 0)
"sortByDescending", (Types.icomparerGeneric, 1)
"sum", ("Fable.Core.IGenericAdder`1", 0)
"sumBy", ("Fable.Core.IGenericAdder`1", 1)
"maxBy", (Types.comparer, 1)
"max", (Types.comparer, 0)
"minBy", (Types.comparer, 1)
"min", (Types.comparer, 0)
"maxBy", (Types.icomparerGeneric, 1)
"max", (Types.icomparerGeneric, 0)
"minBy", (Types.icomparerGeneric, 1)
"min", (Types.icomparerGeneric, 0)
"average", ("Fable.Core.IGenericAverager`1", 0)
"averageBy", ("Fable.Core.IGenericAverager`1", 1)
]
"Seq2", Map [
"distinct", (Types.equalityComparerGeneric, 0)
"distinctBy", (Types.equalityComparerGeneric, 1)
"except", (Types.equalityComparerGeneric, 0)
"countBy", (Types.equalityComparerGeneric, 1)
"groupBy", (Types.equalityComparerGeneric, 1)
"distinct", (Types.iequalityComparerGeneric, 0)
"distinctBy", (Types.iequalityComparerGeneric, 1)
"except", (Types.iequalityComparerGeneric, 0)
"countBy", (Types.iequalityComparerGeneric, 1)
"groupBy", (Types.iequalityComparerGeneric, 1)
]
"Set", Map [
"FSharpSet__Map", (Types.comparer, 1)
"singleton", (Types.comparer, 0)
"unionMany", (Types.comparer, 0)
"empty", (Types.comparer, 0)
"map", (Types.comparer, 1)
"ofList", (Types.comparer, 0)
"ofArray", (Types.comparer, 0)
"ofSeq", (Types.comparer, 0)
"FSharpSet__Map", (Types.icomparerGeneric, 1)
"singleton", (Types.icomparerGeneric, 0)
"unionMany", (Types.icomparerGeneric, 0)
"empty", (Types.icomparerGeneric, 0)
"map", (Types.icomparerGeneric, 1)
"ofList", (Types.icomparerGeneric, 0)
"ofArray", (Types.icomparerGeneric, 0)
"ofSeq", (Types.icomparerGeneric, 0)
]
"Map", Map [
"ofList", (Types.comparer, 0)
"ofSeq", (Types.comparer, 0)
"ofArray", (Types.comparer, 0)
"empty", (Types.comparer, 0)
"ofList", (Types.icomparerGeneric, 0)
"ofSeq", (Types.icomparerGeneric, 0)
"ofArray", (Types.icomparerGeneric, 0)
"empty", (Types.icomparerGeneric, 0)
]
]

0 comments on commit 2986355

Please sign in to comment.