Skip to content

Commit de89ab1

Browse files
Dart fixes
1 parent 7ba6ca2 commit de89ab1

File tree

20 files changed

+1504
-456
lines changed

20 files changed

+1504
-456
lines changed

src/Fable.Transforms/Dart/Dart.fs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,64 @@ type AssignmentOperator =
1919
| AssignXorBitwise
2020
| AssignAndBitwise
2121

22+
type TypeInfo =
23+
{
24+
IsRecord: bool
25+
IsUnion: bool
26+
}
27+
2228
type Type =
2329
// Built in
24-
| Integer
25-
| Double
26-
| Boolean
27-
| String
28-
2930
| Object
3031
| Dynamic
32+
3133
| Void
3234
| MetaType
3335

36+
| Integer
37+
| Double
38+
| Boolean
39+
| String
40+
3441
| List of Type
3542
| Nullable of Type
3643

3744
| Generic of name: string
38-
| TypeReference of Ident * generics: Type list
45+
| TypeReference of Ident * generics: Type list * info: TypeInfo
3946
| Function of argTypes: Type list * returnType: Type
4047

48+
static member reference(ident, ?generics, ?isRecord, ?isUnion) =
49+
let info: TypeInfo = { IsRecord = defaultArg isRecord false; IsUnion = defaultArg isUnion false }
50+
TypeReference(ident, defaultArg generics [], info)
51+
52+
static member needsCast (source: Type) (target: Type) =
53+
match source, target with
54+
| _, Object
55+
| _, Dynamic -> false
56+
| Void, Void
57+
| MetaType, MetaType
58+
| Integer, Integer
59+
| Double, Double
60+
| Boolean, Boolean
61+
| String, String -> false
62+
63+
| List source, List target -> Type.needsCast source target
64+
| Nullable source, Nullable target -> Type.needsCast source target
65+
| source, Nullable target -> Type.needsCast source target
66+
| Generic source, Generic target -> source <> target
67+
68+
// We should be able to detect class hierarchy here too
69+
| TypeReference(sourceIdent, sourceGen, _),
70+
TypeReference(targetIdent, targetGen, _) ->
71+
not(
72+
sourceIdent.Name = targetIdent.Name
73+
&& sourceIdent.ImportModule = targetIdent.ImportModule
74+
&& sourceGen.Length = targetGen.Length
75+
&& not(List.zip sourceGen targetGen |> List.exists (fun (s, t) -> Type.needsCast s t))
76+
)
77+
78+
| _ -> true
79+
4180
type Ident =
4281
{ ImportModule: string option
4382
Name: string
@@ -206,7 +245,7 @@ type Statement =
206245
static member whileStatement(test, body) =
207246
WhileStatement(test, body)
208247
static member breakStatement(?label) =
209-
BreakStatement(label)
248+
BreakStatement(label)
210249
static member continueStatement(?label) =
211250
ContinueStatement(label)
212251
static member tryStatement(body, ?handlers, ?finalizer) =
@@ -215,8 +254,9 @@ type Statement =
215254
addToScope ident.Name
216255
LocalVariableDeclaration(ident, kind, value)
217256
/// Variables that won't be added to scope
218-
static member tempVariableDeclaration(ident: Ident, ?value) =
219-
LocalVariableDeclaration(ident, Final, value)
257+
static member tempVariableDeclaration(ident: Ident, ?isMutable, ?value) =
258+
let isMutable = defaultArg isMutable false
259+
LocalVariableDeclaration(ident, (if isMutable then Var else Final), value)
220260
static member functionDeclaration(name: string, args: FunctionArg list, body: Statement list, returnType: Type, ?genParams: GenericParam list) =
221261
LocalFunctionDeclaration {
222262
Name = name

src/Fable.Transforms/Dart/DartPrinter.fs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ module PrinterExtensions =
230230
printer.Print("?")
231231
| Generic name ->
232232
printer.Print(name)
233-
| TypeReference(ref, gen) ->
233+
| TypeReference(ref, gen, _info) ->
234234
printer.PrintIdent(ref)
235235
printer.PrintList("<", ", ", ">", gen, printer.PrintType, skipIfEmpty=true)
236236
| Function(argTypes, returnType) ->
@@ -361,7 +361,7 @@ module PrinterExtensions =
361361
| Some p -> printer.Print(p + ".")
362362
printer.Print(ident.Name)
363363

364-
member printer.PrintIfStatment(test: Expression, consequent, alternate) =
364+
member printer.PrintIfStatement(test: Expression, consequent, alternate) =
365365
printer.Print("if (")
366366
printer.Print(test)
367367
printer.Print(") ")
@@ -372,7 +372,7 @@ module PrinterExtensions =
372372
match alternate with
373373
| [IfStatement(test, consequent, alternate)] ->
374374
printer.Print(" else ")
375-
printer.PrintIfStatment(test, consequent, alternate)
375+
printer.PrintIfStatement(test, consequent, alternate)
376376
| alternate ->
377377
// Get productive statements and skip `else` if they're empty
378378
alternate
@@ -393,7 +393,7 @@ module PrinterExtensions =
393393
printer.Print(statement)
394394

395395
| IfStatement(test, consequent, alternate) ->
396-
printer.PrintIfStatment(test, consequent, alternate)
396+
printer.PrintIfStatement(test, consequent, alternate)
397397

398398
| ForStatement(init, test, update, body) ->
399399
printer.Print("for (")
@@ -626,7 +626,10 @@ module PrinterExtensions =
626626
printer.PrintWithParensIfNotIdent(expr)
627627
match op with
628628
| UnaryMinus -> printUnaryOp "-" expr
629-
| UnaryNot -> printUnaryOp "!" expr
629+
| UnaryNot ->
630+
match expr with
631+
| UnaryExpression(UnaryNot, expr) -> printer.Print(expr)
632+
| _ -> printUnaryOp "!" expr
630633
| UnaryNotBitwise -> printUnaryOp "~" expr
631634
// TODO: I think Dart doesn't accept + prefix, check
632635
| UnaryPlus
@@ -850,9 +853,12 @@ module PrinterExtensions =
850853

851854
| Some value ->
852855
let printType =
853-
// Nullable types usually need to be typed explicitly
856+
// Nullable types and unions usually need to be typed explicitly
857+
// Print type also if ident and expression types are different?
858+
// (this usually happens when removing unnecessary casts)
854859
match ident.Type with
855860
| Nullable _ -> true
861+
| TypeReference(_, _, info) -> info.IsUnion
856862
| _ -> false
857863

858864
match kind, printType with

0 commit comments

Comments
 (0)