Skip to content

Commit 419db57

Browse files
committed
Fix a bug when adding default constructor to class
1 parent d42db05 commit 419db57

File tree

4 files changed

+83
-19
lines changed

4 files changed

+83
-19
lines changed

lib/Extensions.fs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,32 @@ type JSRecord<'TKey, 'TValue> =
126126
[<Emit("$1 in $0")>]
127127
abstract member hasKey: 'TKey -> bool
128128

129+
module JSRecord =
130+
[<Emit("{}")>]
131+
let empty<'TKey, 'TValue> : JSRecord<'TKey, 'TValue> = jsNative
132+
133+
type JSObj =
134+
[<EmitIndexer>]
135+
abstract member Item: string -> JSObj option with get
136+
[<EmitIndexer>]
137+
abstract member Item: string -> JSObj with set
138+
[<Emit("Object.keys($0)")>]
139+
abstract member keys: string[]
140+
[<Emit("Object.values($0)")>]
141+
abstract member values: JSObj[]
142+
[<Emit("Object.entries($0)")>]
143+
abstract member entries: (string * JSObj)[]
144+
[<Emit("$1 in $0")>]
145+
abstract member hasKey: string -> bool
146+
147+
module JSObj =
148+
[<Emit("{}")>]
149+
let empty : JSObj = jsNative
150+
[<Emit("$0")>]
151+
let box _ : JSObj = jsNative
152+
[<Emit("$0")>]
153+
let unbox (_: JSObj) : 'a = jsNative
154+
129155
module JS =
130156
[<Emit("typeof $0")>]
131157
let typeof (_: 'a) : string = jsNative

lib/Syntax.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ with
6868
match yo with
6969
| :? Location as y -> true
7070
| _ -> false
71-
override x.GetHashCode() = 0
71+
override x.GetHashCode() = x.AsComparable.GetHashCode()
7272
interface System.IComparable with
7373
member x.CompareTo(yo) =
7474
match yo with

lib/Typer.fs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,8 @@ module Statement =
908908

909909
let createDefinitionsMap (stmts: Statement list) : Trie<string, Definition list> =
910910
let add ns name x trie =
911-
trie |> Trie.addOrUpdate (List.rev (name :: ns)) [x] List.append
911+
let key = List.rev (name :: ns)
912+
trie |> Trie.addOrUpdate key [x] List.append
912913
let rec go (ns: string list) trie s =
913914
match s with
914915
| Export _
@@ -1415,14 +1416,16 @@ let inferEnumCaseValue (stmts: Statement list) : Statement list =
14151416
{ c with value = v }, v
14161417
Enum { e with cases = e.cases |> List.mapFold f None |> fst }
14171418
| Module m -> Module { m with statements = m.statements |> List.map go }
1419+
| Global m -> Global { m with statements = m.statements |> List.map go }
14181420
| s -> s
14191421
stmts |> List.map go
14201422

14211423
let rec mergeStatements (stmts: Statement list) =
1422-
let mutable result : Choice<Statement, Class ref, Module ref> list = []
1424+
let mutable result : Choice<Statement, Class ref, Module ref, Global ref> list = []
14231425

14241426
let mutable intfMap = Map.empty
14251427
let mutable nsMap = Map.empty
1428+
let mutable globalM = None
14261429
let mutable otherStmtSet = Set.empty
14271430
let mergeTypeParams tps1 tps2 =
14281431
let rec go acc = function
@@ -1456,7 +1459,7 @@ let rec mergeStatements (stmts: Statement list) =
14561459
| None ->
14571460
let iref = ref i
14581461
intfMap <- (intfMap |> Map.add i.name iref)
1459-
result <- Choice2Of3 iref :: result
1462+
result <- Choice2Of4 iref :: result
14601463
| Some iref' ->
14611464
let i' = iref'.Value
14621465
assert (i.accessibility = i'.accessibility)
@@ -1474,25 +1477,38 @@ let rec mergeStatements (stmts: Statement list) =
14741477
| None ->
14751478
let nref = ref n
14761479
nsMap <- (nsMap |> Map.add n.name nref)
1477-
result <- Choice3Of3 nref :: result
1480+
result <- Choice3Of4 nref :: result
14781481
| Some nref' ->
14791482
let n' = nref'.Value
14801483
nref'.Value <-
14811484
{ n with
14821485
loc = n.loc ++ n'.loc
14831486
comments = n.comments @ n'.comments |> List.distinct
14841487
statements = n'.statements @ n.statements }
1488+
| Global n ->
1489+
match globalM with
1490+
| None ->
1491+
let nref = ref n
1492+
globalM <- Some nref
1493+
result <- Choice4Of4 nref :: result
1494+
| Some nref ->
1495+
let n' = nref.Value
1496+
nref.Value <-
1497+
{ n with
1498+
loc = n.loc ++ n'.loc
1499+
comments = n.comments @ n'.comments |> List.distinct
1500+
statements = n'.statements @ n.statements }
14851501
| stmt ->
14861502
if otherStmtSet |> Set.contains stmt |> not then
14871503
otherStmtSet <- otherStmtSet |> Set.add stmt
1488-
result <- Choice1Of3 stmt :: result
1504+
result <- Choice1Of4 stmt :: result
14891505
result
14901506
|> List.rev
14911507
|> List.map (function
1492-
| Choice1Of3 s -> s
1493-
| Choice2Of3 i -> Class i.Value
1494-
| Choice3Of3 n ->
1495-
Module { n.Value with statements = mergeStatements n.Value.statements }
1508+
| Choice1Of4 s -> s
1509+
| Choice2Of4 i -> Class i.Value
1510+
| Choice3Of4 n -> Module { n.Value with statements = mergeStatements n.Value.statements }
1511+
| Choice4Of4 n -> Global { n.Value with statements = mergeStatements n.Value.statements }
14961512
)
14971513

14981514
let mergeSources newFileName (srcs: SourceFile list) =
@@ -1527,7 +1543,7 @@ let addDefaultConstructorToClass (ctx: TyperContext<_, _>) (stmts: Statement lis
15271543
else
15281544
match m.TryGetValue(c.loc) with
15291545
| true, (c, cs) -> c, cs
1530-
| false, _ ->
1546+
| _, _ ->
15311547
let parentConstructors =
15321548
let (|Dummy|) _ = []
15331549
let rec picker = function
@@ -1563,9 +1579,9 @@ let addDefaultConstructorToClass (ctx: TyperContext<_, _>) (stmts: Statement lis
15631579
(c, cs)
15641580
let rec go stmts =
15651581
stmts |> List.map (function
1566-
| Class c when not c.isInterface ->
1567-
Class (addConstructors c |> fst)
1582+
| Class c when not c.isInterface -> Class (addConstructors c |> fst)
15681583
| Module m -> Module { m with statements = go m.statements }
1584+
| Global m -> Global { m with statements = go m.statements }
15691585
| x -> x)
15701586
go stmts
15711587

@@ -1629,6 +1645,7 @@ let introduceAdditionalInheritance (ctx: IContext<#TyperOptions>) (stmts: Statem
16291645

16301646
Class { c with implements = List.ofSeq inherits |> List.distinct }
16311647
| Module m -> Module { m with statements = go m.statements }
1648+
| Global m -> Global { m with statements = go m.statements }
16321649
| x -> x
16331650
)
16341651
go stmts
@@ -1691,14 +1708,15 @@ let detectPatterns (stmts: Statement list) : Statement list =
16911708
else Some (Class intf)
16921709
else Some (Class intf)
16931710
| Module m -> Some (Module { m with statements = go m.statements })
1711+
| Global m -> Global { m with statements = go m.statements } |> Some
16941712
| x -> Some x
16951713
)
16961714
go stmts
16971715

16981716
let replaceAliasToFunction (ctx: #IContext<#TyperOptions>) stmts =
16991717
let rec go = function
1700-
| Module m ->
1701-
Module { m with statements = List.map go m.statements }
1718+
| Module m -> Module { m with statements = List.map go m.statements }
1719+
| Global m -> Global { m with statements = List.map go m.statements }
17021720
| TypeAlias ta ->
17031721
match ta.target with
17041722
| Func (f, typrms, loc) ->
@@ -1852,6 +1870,7 @@ let mergeESLibDefinitions (srcs: SourceFile list) =
18521870
let vo, s = map None s.loc s
18531871
match s with
18541872
| Module m -> Module { m with statements = List.map mapStmt m.statements }
1873+
| Global m -> Global { m with statements = List.map mapStmt m.statements }
18551874
| Enum e -> Enum { e with cases = e.cases |> List.map (fun c -> map vo c.loc c |> snd) }
18561875
| Class c -> Class { c with members = c.members |> List.map (fun (a, m) -> map vo a.loc a |> snd, m) }
18571876
| _ -> s

src/Targets/ParserTest.fs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Targets.ParserTest
33
open Ts2Ml
44
open Syntax
55
open Target
6+
open DataTypes
67

78
type Options =
89
inherit GlobalOptions
@@ -44,12 +45,15 @@ let private run (input: Input) (baseCtx: IContext<Options>) =
4445
baseCtx.options.replaceAliasToFunction <- true
4546
baseCtx.options.replaceNewableFunction <- true
4647
baseCtx.options.replaceRankNFunction <- true
48+
baseCtx.options.inheritArraylike <- true
49+
baseCtx.options.inheritIterable <- true
50+
baseCtx.options.inheritPromiselike <- true
4751

48-
let srcs =
52+
let ctx, srcs =
4953
if baseCtx.options.typing then
50-
Typer.runAll input.sources baseCtx |> snd
54+
Typer.runAll input.sources baseCtx
5155
else
52-
input.sources
56+
Typer.createRootContext input.sources baseCtx, input.sources
5357
let moduleName =
5458
JsHelper.deriveModuleName input.info (srcs |> List.map (fun src -> src.fileName))
5559
printfn "package info: %A" (JS.stringify input.info)
@@ -58,12 +62,27 @@ let private run (input: Input) (baseCtx: IContext<Options>) =
5862
match baseCtx.options.dumpAst with
5963
| None -> ()
6064
| Some output ->
61-
let o = [|
65+
let sources = [|
6266
for src in input.sources do
6367
yield
6468
{| file = src.fileName;
6569
statements = src.statements |> List.toArray |}
6670
|]
71+
let info = [|
72+
for KeyValue(src, info) in ctx.info do
73+
let trie = JSObj.empty
74+
for key in info.definitionsMap |> Trie.keys do
75+
key |> List.fold (fun (o: JSObj) k ->
76+
match o.[k] with
77+
| Some o -> o
78+
| None ->
79+
let o' = JSObj.empty
80+
o.[k] <- o'
81+
o'
82+
) trie |> ignore
83+
yield {| file = src; trie = trie |}
84+
|]
85+
let o = {| sources = sources; info = info |}
6786
Node.Api.fs.writeFileSync(output, stringify o)
6887

6988
let target =

0 commit comments

Comments
 (0)