Skip to content

Commit

Permalink
Enable some Misc Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsogarciacaro committed Mar 27, 2023
1 parent 41a612d commit 459a7ce
Show file tree
Hide file tree
Showing 16 changed files with 964 additions and 904 deletions.
Binary file modified lib/fcs/FSharp.Compiler.Service.dll
Binary file not shown.
5 changes: 5 additions & 0 deletions lib/fcs/FSharp.Compiler.Service.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30347,6 +30347,11 @@ Get the signature for the value's XML documentation
Indicates if the type is a struct tuple type. The GenericArguments property returns the elements of the tuple type.
</summary>
</member>
<member name="P:FSharp.Compiler.Symbols.FSharpType.IsMeasureType">
<summary>
Indicates if the type is a measure type.
</summary>
</member>
<member name="P:FSharp.Compiler.Symbols.FSharpType.IsGenericParameter">
<summary>
Indicates if the type is a variable type, whether declared, generalized or an inference type parameter
Expand Down
Binary file modified lib/fcs/FSharp.Core.dll
Binary file not shown.
Binary file modified lib/fcs/FSharp.DependencyManager.Nuget.dll
Binary file not shown.
4 changes: 2 additions & 2 deletions lib/fcs/FSharp.DependencyManager.Nuget.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 15 additions & 10 deletions src/Fable.Transforms/BabelPrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,11 @@ module PrinterExtensions =
member printer.PrintObjectMethod(kind, key, parameters, body, isComputed, returnType, typeParameters, loc) =
printer.AddLocation(loc)

match kind with
| ObjectGetter -> printer.Print("get ")
| ObjectSetter -> printer.Print("set ")
| ObjectMeth -> ()
let isSetter =
match kind with
| ObjectGetter -> printer.Print("get "); false
| ObjectSetter -> printer.Print("set "); true
| ObjectMeth -> false

if isComputed then
printer.Print("[")
Expand All @@ -813,7 +814,8 @@ module PrinterExtensions =
printer.Print("(")
printer.PrintParameters(parameters)
printer.Print(")")
printer.PrintOptional(returnType, ": ")
if not isSetter then
printer.PrintOptional(returnType, ": ")
printer.Print(" ")

printer.PrintBlock(body.Body, skipNewLineAtEnd=true)
Expand Down Expand Up @@ -904,10 +906,12 @@ module PrinterExtensions =

if isStatic then printer.Print("static ")
if isAbstract then printer.Print("abstract ")
match kind with
| ClassSetter _ -> printer.Print("set ")
| ClassGetter _ -> printer.Print("get ")
| ClassPrimaryConstructor _ | ClassFunction _ -> ()

let isSetter =
match kind with
| ClassGetter _ -> printer.Print("get "); false
| ClassSetter _ -> printer.Print("set "); true
| ClassPrimaryConstructor _ | ClassFunction _ -> false

let key, isComputed, accessModifiers =
match kind with
Expand All @@ -928,7 +932,8 @@ module PrinterExtensions =
printer.Print("(")
printer.PrintParameters(parameters, accessModifiers)
printer.Print(")")
printer.PrintOptional(returnType, ": ")
if not isSetter then
printer.PrintOptional(returnType, ": ")
printer.Print(" ")

printer.Print(body)
Expand Down
53 changes: 22 additions & 31 deletions src/Fable.Transforms/Dart/Fable2Dart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -172,44 +172,36 @@ module Util =
let genArg = transformType com ctx genArg
Type.reference(libValue com ctx Fable.MetaType "Types" "Some", [genArg]) |> Nullable

let transformDeclaredTypeIgnoreMeasure ignoreMeasure (com: IDartCompiler) ctx (entRef: Fable.EntityRef) genArgs =
let transformDeclaredType (com: IDartCompiler) ctx (entRef: Fable.EntityRef) genArgs =
let genArgs = transformGenArgs com ctx genArgs
let makeIterator genArg = Type.reference(makeImmutableIdent MetaType "Iterator", [genArg])
let makeMapEntry key value = Type.reference(makeImmutableIdent MetaType "MapEntry", [key; value])

match entRef.FullName, genArgs with
| Types.enum_, _ -> Integer |> Some
| Types.enum_, _ -> Integer
// List without generics is same as List<dynamic>
| Types.array, _ -> List Dynamic |> Some
| "System.Tuple`1", _ -> transformTupleType com ctx genArgs |> Some
| Types.valueType, _ -> Some Object
| Types.array, _ -> List Dynamic
| "System.Tuple`1", _ -> transformTupleType com ctx genArgs
| Types.valueType, _ -> Object
| Types.nullable, [genArg]
| "Fable.Core.Dart.DartNullable`1", [genArg] -> Nullable genArg |> Some
| "System.Text.RegularExpressions.Group", _ -> Nullable String |> Some
| "Fable.Core.Dart.DartNullable`1", [genArg] -> Nullable genArg
| "System.Text.RegularExpressions.Group", _ -> Nullable String
| "System.Text.RegularExpressions.Match", _ ->
makeTypeRefFromName "Match" [] |> Some
| Types.measureOne, _ when ignoreMeasure -> None
| Types.measureProduct2, _ when ignoreMeasure -> None
makeTypeRefFromName "Match" []
// We use `dynamic` for now because there doesn't seem to be a type that catches all errors in Dart
| Naming.EndsWith "Exception" _, _ -> Dynamic |> Some
| "System.Collections.Generic.Dictionary`2.Enumerator", [key; value] -> makeMapEntry key value |> makeIterator |> Some
| "System.Collections.Generic.Dictionary`2.KeyCollection.Enumerator", [key; _] -> makeIterator key |> Some
| "System.Collections.Generic.Dictionary`2.ValueCollection.Enumerator", [_; value] -> makeIterator value |> Some
| Naming.EndsWith "Exception" _, _ -> Dynamic
| "System.Collections.Generic.Dictionary`2.Enumerator", [key; value] -> makeMapEntry key value |> makeIterator
| "System.Collections.Generic.Dictionary`2.KeyCollection.Enumerator", [key; _] -> makeIterator key
| "System.Collections.Generic.Dictionary`2.ValueCollection.Enumerator", [_; value] -> makeIterator value
| _ ->
let ent = com.GetEntity(entRef)
if ignoreMeasure && ent.IsMeasure then
None
else
let ident, genArgs =
match getEntityIdent com ctx ent with
// If Iterator has more than one genArg assume we need to use MapEntry
| { Name = "Iterator"; ImportModule = None } as ident when List.isMultiple genArgs ->
ident, [Type.reference(makeImmutableIdent MetaType "MapEntry", genArgs)]
| ident -> ident, genArgs
Type.reference(ident, genArgs, isRecord=ent.IsFSharpRecord, isUnion=ent.IsFSharpUnion) |> Some

let transformDeclaredType (com: IDartCompiler) ctx (entRef: Fable.EntityRef) genArgs =
transformDeclaredTypeIgnoreMeasure false com ctx entRef genArgs |> Option.get
let ident, genArgs =
match getEntityIdent com ctx ent with
// If Iterator has more than one genArg assume we need to use MapEntry
| { Name = "Iterator"; ImportModule = None } as ident when List.isMultiple genArgs ->
ident, [Type.reference(makeImmutableIdent MetaType "MapEntry", genArgs)]
| ident -> ident, genArgs
Type.reference(ident, genArgs, isRecord=ent.IsFSharpRecord, isUnion=ent.IsFSharpUnion)

let get t left memberName =
PropertyAccess(left, memberName, t, isConst=false)
Expand Down Expand Up @@ -488,10 +480,9 @@ module Util =

/// Discards Measure generic arguments
let transformGenArgs com ctx (genArgs: Fable.Type list) =
genArgs |> List.choose (function
| Fable.GenericParam(isMeasure=true) -> None
| Fable.DeclaredType(entRef, genArgs) -> transformDeclaredTypeIgnoreMeasure true com ctx entRef genArgs
| t -> transformType com ctx t |> Some)
genArgs |> List.choose (fun t ->
if isUnitOfMeasure t then None
else transformType com ctx t |> Some)

let transformType (com: IDartCompiler) (ctx: Context) (t: Fable.Type) =
match t with
Expand Down
11 changes: 7 additions & 4 deletions src/Fable.Transforms/FSharp2Fable.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,11 +1047,10 @@ module TypeHelpers =
// Currently for Dart we're doing it in the Fable2Dart step
let makeTypeGenArgsWithConstraints withConstraints ctxTypeArgs (genArgs: IList<FSharpType>) =
genArgs
|> Seq.map (fun genArg ->
|> Seq.mapToList (fun genArg ->
if genArg.IsGenericParameter
then resolveGenParam withConstraints ctxTypeArgs genArg.GenericParameter
else makeTypeWithConstraints withConstraints ctxTypeArgs genArg)
|> Seq.toList

let makeTypeGenArgs ctxTypeArgs (genArgs: IList<FSharpType>) =
makeTypeGenArgsWithConstraints true ctxTypeArgs genArgs
Expand Down Expand Up @@ -1131,8 +1130,10 @@ module TypeHelpers =
"FSharp.UMX.string`1", Choice1Of2 Fable.String
"FSharp.UMX.Guid`1", Choice2Of2 Types.guid
"FSharp.UMX.TimeSpan`1", Choice2Of2 Types.timespan
"FSharp.UMX.TimeOnly`1", Choice2Of2 Types.timeOnly
"FSharp.UMX.DateTime`1", Choice2Of2 Types.datetime
"FSharp.UMX.DateTimeOffset`1", Choice2Of2 Types.datetimeOffset
"FSharp.UMX.DateOnly`1", Choice2Of2 Types.dateOnly
]

let private getMeasureFullName (genArgs: IList<FSharpType>) =
Expand Down Expand Up @@ -1193,7 +1194,6 @@ module TypeHelpers =
| DicContains numbersWithMeasure kind ->
let info = getMeasureFullName genArgs |> Fable.NumberInfo.IsMeasure
Fable.Number(kind, info)
// | Types.measureProduct2 as fullName -> makeFSharpCoreType [] fullName
| DicContains runtimeTypesWithMeasure choice ->
match choice with
| Choice1Of2 t -> t
Expand Down Expand Up @@ -1245,7 +1245,10 @@ module TypeHelpers =
else
#endif
makeTypeFromDef withConstraints ctxTypeArgs t.GenericArguments t.TypeDefinition
else Fable.Any // failwithf "Unexpected non-declared F# type: %A" t
elif t.IsMeasureType then
Fable.Measure ""
else
Fable.Any // failwithf "Unexpected non-declared F# type: %A" t

let makeType (ctxTypeArgs: Map<string, Fable.Type>) t =
makeTypeWithConstraints true ctxTypeArgs t
Expand Down
55 changes: 29 additions & 26 deletions src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ module Lib =
| consExpr -> consExpr |> Option.map (fun e -> com.TransformAsExpr(ctx, e))

let tryJsConstructorForAnnotation forAnnotation (com: IBabelCompiler) ctx (ent: Fable.Entity) =
// TODO: Check this is not an StringEnum or Erase union
let suffix = if not forAnnotation && com.Options.Language = TypeScript && ent.IsFSharpUnion then "_Cons" else ""
let suffix =
if not forAnnotation
&& Annotation.isTypeScript com
&& ent.IsFSharpUnion
then "_" + Util.UnionHelpers.CONS
else ""
tryJsConstructorWithSuffix com ctx ent suffix

/// Cannot be used for annotations (use `tryJsConstructorForAnnotation true` instead)
Expand Down Expand Up @@ -393,16 +397,14 @@ module Annotation =
| _ -> None)

let makeTypeParamInstantiation (com: IBabelCompiler) ctx genArgs =
genArgs |> List.chooseToArray (function
| Fable.DeclaredType(entRef, genArgs) ->
let ent = com.GetEntity(entRef)
if ent.IsMeasure then None
else makeEntityTypeAnnotation com ctx ent genArgs |> Some
| t -> makeTypeAnnotation com ctx t |> Some)
if List.isEmpty genArgs then [||]
else
genArgs |> List.chooseToArray (fun t ->
if isUnitOfMeasure t then None
else makeTypeAnnotation com ctx t |> Some)

let makeTypeParamInstantiationIfTypeScript (com: IBabelCompiler) ctx genArgs =
if com.Options.Language = TypeScript then
makeTypeParamInstantiation com ctx genArgs |> Some
if isTypeScript com then makeTypeParamInstantiation com ctx genArgs |> Some
else None

let getGenericTypeAnnotation com ctx name genArgs =
Expand Down Expand Up @@ -443,7 +445,7 @@ module Annotation =
makeEntityTypeAnnotation com ctx ent genArgs

let makeTypeAnnotationIfTypeScript (com: IBabelCompiler) ctx typ expr =
if com.Options.Language = TypeScript then
if isTypeScript com then
match typ, expr with
| Fable.Option _, _ -> makeTypeAnnotation com ctx typ |> Some
// Use type annotation for NullLiteral and enum cases
Expand All @@ -463,7 +465,7 @@ module Annotation =
FableTransforms.uncurryType fieldType |> makeTypeAnnotation com ctx

let makeFieldAnnotationIfTypeScript (com: IBabelCompiler) ctx (fieldType: Fable.Type) =
if com.Options.Language = TypeScript
if isTypeScript com
then makeFieldAnnotation com ctx fieldType |> Some
else None

Expand Down Expand Up @@ -637,7 +639,7 @@ module Annotation =
AnyTypeAnnotation // TODO:

let transformFunctionWithAnnotations (com: IBabelCompiler) ctx name typeParams (args: Fable.Ident list) (body: Fable.Expr) =
if com.Options.Language = TypeScript then
if isTypeScript com then
let argTypes = args |> List.map (fun id -> id.Type)
let scopedTypeParams, genParams =
match typeParams with
Expand Down Expand Up @@ -1100,7 +1102,7 @@ module Util =
let genMap = if List.contains "allow-generics" tags then None else Some Map.empty
transformTypeInfo com ctx r genMap t
| Fable.Null _t ->
// if com.Options.Language = TypeScript
// if isTypeScript com
// let ta = makeTypeAnnotation com ctx t |> TypeAnnotation |> Some
// upcast Identifier("null", ?typeAnnotation=ta, ?loc=r)
// else
Expand Down Expand Up @@ -1139,7 +1141,7 @@ module Util =
| Fable.ArrayFrom expr -> makeArrayFrom com ctx typ kind expr
| Fable.NewTuple(vals,_) ->
let tup = makeArray com ctx vals
if com.Options.Language = TypeScript
if isTypeScript com
then AsExpression(tup, makeTypeAnnotation com ctx value.Type)
else tup
// | Fable.NewList (headAndTail, _) when List.contains "FABLE_LIBRARY" com.Options.Define ->
Expand Down Expand Up @@ -1175,7 +1177,7 @@ module Util =
let values = List.mapToArray (fun x -> com.TransformAsExpr(ctx, x)) values
let consRef = ent |> jsConstructor com ctx
let typeParamInst =
if com.Options.Language = TypeScript && (ent.FullName = Types.refCell)
if isTypeScript com && (ent.FullName = Types.refCell)
then makeTypeParamInstantiation com ctx genArgs |> Some
else None
Expression.newExpression(consRef, values, ?typeArguments=typeParamInst, ?loc=r)
Expand All @@ -1184,7 +1186,7 @@ module Util =
Array.zip fieldNames values |> makeJsObject
| Fable.NewUnion(values, tag, ent, genArgs) ->
let ent = com.GetEntity(ent)
if com.Options.Language = TypeScript then
if isTypeScript com then
let case = ent.UnionCases[tag]
match tryJsConstructorWithSuffix com ctx ent ("_" + case.Name) with
| Some helperRef when not(UnionHelpers.caseNameClashes case.Name)->
Expand Down Expand Up @@ -1590,7 +1592,7 @@ module Util =

| Fable.OptionValue ->
let expr = com.TransformAsExpr(ctx, fableExpr)
if mustWrapOption typ || com.Options.Language = TypeScript
if mustWrapOption typ || isTypeScript com
then libCall com ctx None "Option" "value" [] [expr]
else expr

Expand Down Expand Up @@ -1651,7 +1653,7 @@ module Util =
if nonEmpty then Expression.unaryExpression(UnaryNot, expr, ?loc=range) else expr
| Fable.UnionCaseTest tag ->
let expected =
if com.Options.Language = TypeScript then
if isTypeScript com then
match expr.Type with
| Fable.DeclaredType(ent, _) ->
let ent = com.GetEntity(ent)
Expand Down Expand Up @@ -1827,7 +1829,7 @@ module Util =
/// and another to execute the actual target
let transformDecisionTreeWithTwoSwitches (com: IBabelCompiler) ctx returnStrategy (targets: (Fable.Ident list * Fable.Expr) list) treeExpr =
// Most of the time, TypeScript will complain the variable declared on top are not initialized
if com.Options.Language = TypeScript then
if isTypeScript com then
let ctx = { ctx with DecisionTargets = targets }
com.TransformAsStatements(ctx, returnStrategy, treeExpr)
else
Expand Down Expand Up @@ -2221,7 +2223,7 @@ module Util =

let declareClassWithParams (com: IBabelCompiler) ctx (ent: Fable.Entity) entName (consArgs: Parameter[]) (consArgsModifiers: AccessModifier[]) (consBody: BlockStatement) (superClass: SuperClass option) classMembers typeParamDecl =
let implements =
if com.Options.Language = TypeScript then
if isTypeScript com then
let implements = Util.getClassImplements com ctx ent |> Seq.toArray
if Array.isEmpty implements then None else Some implements
else None
Expand All @@ -2230,7 +2232,7 @@ module Util =
ClassMember.classMethod(ClassPrimaryConstructor consArgsModifiers, consArgs, consBody)

let classFields =
if com.Options.Language = TypeScript && not ent.IsFSharpUnion then
if isTypeScript com && not ent.IsFSharpUnion then
ent.FSharpFields |> List.mapToArray (fun field ->
let prop, isComputed = memberFromName field.Name
let ta = makeFieldAnnotation com ctx field.FieldType
Expand All @@ -2251,14 +2253,14 @@ module Util =
|> declareModuleMember com ctx classExpr

let declareClass (com: IBabelCompiler) ctx ent entName consArgs consBody superClass classMembers =
if com.Options.Language = TypeScript
if isTypeScript com
then FSharp2Fable.Util.getEntityGenArgs ent |> makeTypeParamDecl com ctx |> Some
else None
|> declareClassWithParams com ctx ent entName consArgs [||] consBody superClass classMembers

let declareTypeReflection (com: IBabelCompiler) ctx (ent: Fable.Entity) entName: ModuleDeclaration =
let ta =
if com.Options.Language = TypeScript then
if isTypeScript com then
makeImportTypeAnnotation com ctx [] "Reflection" "TypeInfo" |> Some
else None
let genArgs = Array.init (ent.GenericParameters.Length) (fun i -> "gen" + string i |> makeIdent)
Expand Down Expand Up @@ -2351,7 +2353,8 @@ module Util =
|> BlockStatement
ClassMember.classMethod(ClassFunction(Expression.identifier("cases"), false), [||], body)

if com.Options.Language = TypeScript then
// TODO: Don't emit helpers for unions with just one case
if isTypeScript com then
// Merge this with makeTypeParamDecl/makeTypeParamInstantiation?
let entParams = ent.GenericParameters |> List.chooseToArray (fun p ->
if not p.IsMeasure then Some p.Name else None)
Expand Down Expand Up @@ -2454,7 +2457,7 @@ module Util =

let returnType, typeParamDecl =
// change constructor's return type from void to entity type
if com.Options.Language = TypeScript then
if isTypeScript com then
let genArgs = FSharp2Fable.Util.getEntityGenArgs classEnt
let returnType = getGenericTypeAnnotation com ctx classDecl.Name genArgs
let typeParamDecl = makeTypeParamDecl com ctx genArgs |> Some
Expand Down
Loading

0 comments on commit 459a7ce

Please sign in to comment.