Skip to content

Commit 6eeb240

Browse files
Dart: Add ResizeList
1 parent c0735aa commit 6eeb240

26 files changed

+2548
-607
lines changed

.vscode/settings.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,13 @@
1414
},
1515
"files.trimTrailingWhitespace": true,
1616
"files.trimFinalNewlines": true,
17-
"gitlens.codeLens.enabled": false
17+
"gitlens.codeLens.enabled": false,
18+
"FSharp.excludeProjectDirectories": [
19+
".git",
20+
"paket-files",
21+
"fable_modules",
22+
"packages",
23+
"node_modules",
24+
"src/fcs-fable"
25+
]
1826
}

build.fsx

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ let BUILD_ARGS_LOWER =
2525
module Util =
2626
let cleanDirs dirs =
2727
for dir in dirs do
28+
printfn $"Clean {dir}"
2829
removeDirRecursive dir
2930

3031
let resolveDir dir =
@@ -175,6 +176,7 @@ let buildLibraryTs() =
175176

176177
// TODO: move to PublishUtils.fs ?
177178
let copyFiles sourceDir searchPattern destDir =
179+
printfn $"Copy {sourceDir </> searchPattern} to {destDir}"
178180
for source in IO.Directory.GetFiles(sourceDir, searchPattern) do
179181
let fileName = IO.Path.GetFileName(source)
180182
let target = destDir </> fileName
@@ -236,12 +238,16 @@ let buildLibraryRust() =
236238
// if not (pathExists (baseDir </> "build/fable-library-rust")) then
237239
// buildLibraryRust()
238240

239-
let buildLibraryDart() =
241+
let buildLibraryDart(clean: bool) =
240242
let sourceDir = resolveDir "src/fable-library-dart"
241243
let buildDir = resolveDir "build/fable-library-dart"
242-
cleanDirs [buildDir]
244+
245+
if clean then
246+
cleanDirs [buildDir]
247+
makeDirRecursive buildDir
248+
copyFiles sourceDir "pubspec.*" buildDir
249+
copyFiles sourceDir "analysis_options.yaml" buildDir
243250

244-
makeDirRecursive buildDir
245251
copyFiles sourceDir "*.dart" buildDir
246252

247253
runFableWithArgsInDir sourceDir [
@@ -524,7 +530,7 @@ let testRust() =
524530

525531
let testDart(isWatch) =
526532
if not (pathExists "build/fable-library-dart") then
527-
buildLibraryDart()
533+
buildLibraryDart(true)
528534

529535
let runDir = "tests/Dart"
530536
let projectDir = runDir + "/src"
@@ -675,8 +681,8 @@ let publishPackages restArgs =
675681
else
676682
pushNpm ("src" </> pkg) buildAction
677683

678-
let minify<'T> =
679-
BUILD_ARGS_LOWER |> List.contains "--no-minify" |> not
684+
let hasFlag flag =
685+
BUILD_ARGS_LOWER |> List.contains flag
680686

681687
match BUILD_ARGS_LOWER with
682688
// | "check-sourcemaps"::_ ->
@@ -688,7 +694,9 @@ match BUILD_ARGS_LOWER with
688694
| "test-mocha"::_ -> compileAndRunTestsWithMocha true "Main"
689695
| "test-mocha-fast"::_ -> compileAndRunTestsWithMocha false "Main"
690696
| "test-react"::_ -> testReact()
691-
| "test-standalone"::_ -> testStandalone(minify)
697+
| "test-standalone"::_ ->
698+
let minify = hasFlag "--no-minify" |> not
699+
testStandalone(minify)
692700
| "test-standalone-fast"::_ -> testStandaloneFast()
693701
| "test-configs"::_ -> testProjectConfigs()
694702
| "test-integration"::_ -> testIntegration()
@@ -723,9 +731,16 @@ match BUILD_ARGS_LOWER with
723731
| ("fable-library-ts"|"library-ts")::_ -> buildLibraryTs()
724732
| ("fable-library-py"|"library-py")::_ -> buildLibraryPy()
725733
| ("fable-library-rust" | "library-rust")::_ -> buildLibraryRust()
726-
| ("fable-library-dart" | "library-dart")::_ -> buildLibraryDart()
727-
| ("fable-compiler-js"|"compiler-js")::_ -> buildCompilerJs(minify)
728-
| ("fable-standalone"|"standalone")::_ -> buildStandalone {|minify=minify; watch=false|}
734+
| ("fable-library-dart" | "library-dart")::_ ->
735+
let clean = hasFlag "--no-clean" |> not
736+
buildLibraryDart(clean)
737+
738+
| ("fable-compiler-js"|"compiler-js")::_ ->
739+
let minify = hasFlag "--no-minify" |> not
740+
buildCompilerJs(minify)
741+
| ("fable-standalone"|"standalone")::_ ->
742+
let minify = hasFlag "--no-minify" |> not
743+
buildStandalone {|minify=minify; watch=false|}
729744
| "watch-standalone"::_ -> buildStandalone {|minify=false; watch=true|}
730745
| "publish"::restArgs -> publishPackages restArgs
731746
| "github-release"::_ ->

src/Fable.AST/Fable.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ type MemberDecl = {
191191
/// for a declaration in the root scope
192192
ExportDefault: bool
193193
DeclaringEntity: EntityRef option
194+
XmlDoc: string option
194195
} with
195196
member this.ArgIdents = this.Args |> List.map (fun a -> a.Ident)
196197
member this.ArgTypes = this.Args |> List.map (fun a -> a.Ident.Type)
@@ -201,6 +202,7 @@ type ClassDecl = {
201202
Constructor: MemberDecl option
202203
BaseCall: Expr option
203204
AttachedMembers: MemberDecl list
205+
XmlDoc: string option
204206
}
205207

206208
type ModuleDecl = {
@@ -462,7 +464,7 @@ type UnresolvedExpr =
462464
// TODO: Add also MemberKind from the flags?
463465
| UnresolvedTraitCall of sourceTypes: Type list * traitName: string * isInstance: bool * argTypes: Type list * argExprs: Expr list
464466
| UnresolvedReplaceCall of thisArg: Expr option * args: Expr list * info: ReplaceCallInfo * attachedCall: Expr option
465-
| UnresolvedInlineCall of memberUniqueName: string * genArgs: Type list * witnesses: Witness list * callee: Expr option * info: CallInfo
467+
| UnresolvedInlineCall of memberUniqueName: string * witnesses: Witness list * callee: Expr option * info: CallInfo
466468

467469
type Expr =
468470
/// Identifiers that reference another expression

src/Fable.Cli/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"profiles": {
33
"Fable.Cli": {
44
"commandName": "Project",
5-
"commandLineArgs": "watch src/Quicktest --noCache --exclude Fable.Core",
6-
"workingDirectory": "C:\\Users\\alfon\\repos\\Fable"
5+
"commandLineArgs": "src/quicktest-dart --lang dart --noCache --exclude Fable.Core",
6+
"workingDirectory": "../../../../.."
77
}
88
}
99
}

src/Fable.Transforms/Dart/Dart.fs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,12 @@ type Statement =
224224
static member switchStatement(discriminant, cases, ?defaultCase) =
225225
SwitchStatement(discriminant, cases, defaultCase)
226226

227-
type FunctionArg(ident: Ident, ?isOptional: bool, ?isNamed: bool) =
227+
type FunctionArg(ident: Ident, ?isOptional: bool, ?isNamed: bool, ?isConsThisArg: bool) =
228228
member _.Ident = ident
229229
member _.IsOptional = defaultArg isOptional false
230230
member _.IsNamed = defaultArg isNamed false
231+
member _.IsConsThisArg = defaultArg isConsThisArg false
232+
member _.AsConsThisArg(name) = FunctionArg({ ident with Name = name }, ?isOptional=isOptional, ?isNamed=isNamed, isConsThisArg=true)
231233

232234
type FunctionDecl =
233235
{
@@ -238,12 +240,8 @@ type FunctionDecl =
238240
ReturnType: Type
239241
}
240242

241-
type ConsArg =
242-
| ConsArg of Ident
243-
| ConsThisArg of name: string
244-
245243
type Constructor(?args, ?body, ?superArgs, ?isConst, ?isFactory) =
246-
member _.Args: ConsArg list = defaultArg args []
244+
member _.Args: FunctionArg list = defaultArg args []
247245
member _.Body: Statement list = defaultArg body []
248246
member _.SuperArgs: CallArg list = defaultArg superArgs []
249247
member _.IsConst = defaultArg isConst false

src/Fable.Transforms/Dart/DartPrinter.fs

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ module PrinterExtensions =
7474
let subSegment =
7575
// Remove whitespace in front of new lines,
7676
// indent will be automatically applied
77-
if printer.Column = 0 then subSegments.[i - 1].TrimStart()
78-
else subSegments.[i - 1]
77+
if printer.Column = 0 then subSegments[i - 1].TrimStart()
78+
else subSegments[i - 1]
7979
if subSegment.Length > 0 then
8080
printer.Print(subSegment)
8181
if i < subSegments.Length then
@@ -87,53 +87,53 @@ module PrinterExtensions =
8787
value
8888
|> replace @"\$(\d+)\.\.\." (fun m ->
8989
let rep = ResizeArray()
90-
let i = int m.Groups.[1].Value
90+
let i = int m.Groups[1].Value
9191
for j = i to args.Length - 1 do
9292
rep.Add("$" + string j)
9393
String.concat ", " rep)
9494

9595
|> replace @"\{\{\s*\$(\d+)\s*\?(.*?)\:(.*?)\}\}" (fun m ->
96-
let i = int m.Groups.[1].Value
97-
match args.[i] with
98-
| Literal(BooleanLiteral(value=value)) when value -> m.Groups.[2].Value
99-
| _ -> m.Groups.[3].Value)
96+
let i = int m.Groups[1].Value
97+
match args[i] with
98+
| Literal(BooleanLiteral(value=value)) when value -> m.Groups[2].Value
99+
| _ -> m.Groups[3].Value)
100100

101101
|> replace @"\{\{([^\}]*\$(\d+).*?)\}\}" (fun m ->
102-
let i = int m.Groups.[2].Value
102+
let i = int m.Groups[2].Value
103103
match List.tryItem i args with
104-
| Some _ -> m.Groups.[1].Value
104+
| Some _ -> m.Groups[1].Value
105105
| None -> "")
106106

107107
// If placeholder is followed by !, emit string literals as native code: "let $0! = $1"
108108
|> replace @"\$(\d+)!" (fun m ->
109-
let i = int m.Groups.[1].Value
109+
let i = int m.Groups[1].Value
110110
match List.tryItem i args with
111111
| Some(Literal(StringLiteral value)) -> value
112112
| _ -> "")
113113

114114
let matches = Regex.Matches(value, @"\$\d+")
115115
if matches.Count > 0 then
116116
for i = 0 to matches.Count - 1 do
117-
let m = matches.[i]
117+
let m = matches[i]
118118
let isSurroundedWithParens =
119119
m.Index > 0
120120
&& m.Index + m.Length < value.Length
121-
&& value.[m.Index - 1] = '('
122-
&& value.[m.Index + m.Length] = ')'
121+
&& value[m.Index - 1] = '('
122+
&& value[m.Index + m.Length] = ')'
123123

124124
let segmentStart =
125-
if i > 0 then matches.[i-1].Index + matches.[i-1].Length
125+
if i > 0 then matches[i-1].Index + matches[i-1].Length
126126
else 0
127127

128128
printSegment printer value segmentStart m.Index
129129

130-
let argIndex = int m.Value.[1..]
130+
let argIndex = int m.Value[1..]
131131
match List.tryItem argIndex args with
132132
| Some e when isSurroundedWithParens -> printer.Print(e)
133133
| Some e -> printer.PrintWithParensIfComplex(e)
134134
| None -> ()
135135

136-
let lastMatch = matches.[matches.Count - 1]
136+
let lastMatch = matches[matches.Count - 1]
137137
printSegment printer value (lastMatch.Index + lastMatch.Length) value.Length
138138
else
139139
printSegment printer value 0 value.Length
@@ -496,14 +496,17 @@ module PrinterExtensions =
496496
p.PushIndentation()
497497
for s in c.Body do
498498
p.Print(s)
499-
p.Print(";")
500-
p.PrintNewLine()
499+
p.PrintStatementSeparator()
500+
501+
let rec needsBreak statements =
502+
match List.tryLast statements with
503+
| Some(ContinueStatement _)
504+
| Some(BreakStatement _)
505+
| Some(ReturnStatement _) -> false
506+
| Some(IfStatement(_, consequent, alternate)) -> needsBreak consequent || needsBreak alternate
507+
| _ -> true
501508

502-
match List.tryLast c.Body with
503-
| Some(ContinueStatement _)
504-
| Some(BreakStatement _)
505-
| Some(ReturnStatement _) -> ()
506-
| _ ->
509+
if needsBreak c.Body then
507510
p.Print("break;")
508511
p.PrintNewLine()
509512

@@ -552,8 +555,8 @@ module PrinterExtensions =
552555
else "'"
553556
printer.Print(quotes)
554557
for i = 0 to parts.Length - 2 do
555-
printer.Print(escape parts.[i])
556-
match values.[i] with
558+
printer.Print(escape parts[i])
559+
match values[i] with
557560
| IdentExpression i ->
558561
printer.Print("$")
559562
printer.PrintIdent(i)
@@ -579,7 +582,11 @@ module PrinterExtensions =
579582
| test, Literal(BooleanLiteral(false)), Literal(BooleanLiteral(true)) ->
580583
printer.Print("!")
581584
printer.PrintWithParensIfComplex(test)
582-
| test, _, Literal(BooleanLiteral(false)) ->
585+
| test, Literal(BooleanLiteral(true)), alternate ->
586+
printer.PrintWithParensIfComplex(test)
587+
printer.Print(" || ")
588+
printer.PrintWithParensIfComplex(alternate)
589+
| test, consequent, Literal(BooleanLiteral(false)) ->
583590
printer.PrintWithParensIfComplex(test)
584591
printer.Print(" && ")
585592
printer.PrintWithParensIfComplex(consequent)
@@ -709,9 +716,8 @@ module PrinterExtensions =
709716
if v.IsOverride then
710717
p.Print("@override")
711718
p.PrintNewLine()
712-
match v.Kind with
713-
| Final when v.IsLate -> p.Print("late ")
714-
| _ -> ()
719+
if v.IsLate then
720+
p.Print("late ")
715721
p.PrintVariableDeclaration(v.Ident, v.Kind, ?value=v.Value)
716722
p.Print(";")
717723

@@ -722,13 +728,7 @@ module PrinterExtensions =
722728
if c.IsFactory then
723729
p.Print("factory ")
724730
p.Print(decl.Name)
725-
printer.PrintList("(", ", ", ")", c.Args, function
726-
| ConsThisArg name ->
727-
printer.Print("this.")
728-
printer.Print(name)
729-
| ConsArg i ->
730-
printer.PrintIdent(i, printType=true)
731-
)
731+
printer.PrintFunctionArgs(c.Args)
732732

733733
if callSuper then
734734
p.Print(": super")
@@ -776,12 +776,7 @@ module PrinterExtensions =
776776
printer.Print(" ")
777777
printer.PrintBlock(body, skipNewLineAtEnd=(isExpression || isModuleOrClassMember))
778778

779-
member printer.PrintFunctionDeclaration(returnType: Type, name: string, genParams: GenericParam list, args: FunctionArg list, ?body: Statement list, ?isModuleOrClassMember) =
780-
printer.PrintType(returnType)
781-
printer.Print(" ")
782-
printer.Print(name)
783-
printer.PrintGenericParams(genParams)
784-
779+
member printer.PrintFunctionArgs(args: FunctionArg list) =
785780
let mutable prevArg: FunctionArg option = None
786781
printer.PrintList("(", ")", args, fun pos arg ->
787782
if arg.IsNamed then
@@ -797,7 +792,10 @@ module PrinterExtensions =
797792
else
798793
()
799794

800-
printer.PrintIdent(arg.Ident, printType=true)
795+
if arg.IsConsThisArg then
796+
printer.Print("this." + arg.Ident.Name)
797+
else
798+
printer.PrintIdent(arg.Ident, printType=true)
801799

802800
match pos with
803801
| IsSingle | IsLast ->
@@ -811,6 +809,13 @@ module PrinterExtensions =
811809

812810
prevArg <- Some arg
813811
)
812+
813+
member printer.PrintFunctionDeclaration(returnType: Type, name: string, genParams: GenericParam list, args: FunctionArg list, ?body: Statement list, ?isModuleOrClassMember) =
814+
printer.PrintType(returnType)
815+
printer.Print(" ")
816+
printer.Print(name)
817+
printer.PrintGenericParams(genParams)
818+
printer.PrintFunctionArgs(args)
814819
printer.PrintFunctionBody(?body=body, ?isModuleOrClassMember=isModuleOrClassMember)
815820

816821
member printer.PrintVariableDeclaration(ident: Ident, kind: VariableDeclarationKind, ?value: Expression) =
@@ -871,7 +876,7 @@ let run (writer: Writer) (file: File): Async<unit> =
871876
let printer = printerImpl :> Printer
872877

873878
// If we manage to master null assertions maybe we can remove unnecessary_non_null_assertion
874-
printer.Print("// ignore_for_file: non_constant_identifier_names, camel_case_types, constant_identifier_names, unnecessary_non_null_assertion")
879+
printer.Print("// ignore_for_file: camel_case_types, constant_identifier_names, non_constant_identifier_names, unnecessary_non_null_assertion, unnecessary_this")
875880
printer.PrintNewLine()
876881

877882
file.Imports

0 commit comments

Comments
 (0)