Skip to content

Commit 813eaf4

Browse files
authored
Merge pull request #40 from ocsigen/separate-core
Separation & major refactoring of core modules
2 parents 24dbca9 + 9ecc077 commit 813eaf4

33 files changed

+4872
-2990
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ _opam/
300300
# Outputs
301301
output
302302
*.fs.js*
303+
output*.txt
303304

304305
# Test artifacts
305306
test/jsoo/src/*.mli

build.fsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,29 @@ Target.create "YarnInstall" <| fun _ ->
7777

7878
Target.create "Prepare" ignore
7979

80-
Target.create "BuildOnly" <| fun _ ->
80+
Target.create "BuildForPublish" <| fun _ ->
8181
dotnetExec "fable" $"{srcDir} --sourceMaps --run webpack --mode=production"
8282

83+
Target.create "BuildForTest" <| fun _ ->
84+
dotnetExec "fable" $"{srcDir} --sourceMaps --define DEBUG --run webpack --mode=development"
85+
8386
Target.create "Build" ignore
8487

8588
Target.create "Watch" <| fun _ ->
8689
dotnetExec "fable" $"watch {srcDir} --sourceMaps --define DEBUG --run webpack -w --mode=development"
8790

91+
Target.create "TestComplete" ignore
92+
8893
"Clean"
8994
==> "YarnInstall"
9095
==> "Restore"
9196
==> "Prepare"
9297
==> "Build"
9398

9499
"Prepare"
95-
?=> "BuildOnly"
100+
?=> "BuildForTest"
101+
?=> "TestComplete"
102+
?=> "BuildForPublish"
96103
==> "Build"
97104

98105
"Prepare"
@@ -128,13 +135,13 @@ module Test =
128135
// "full" packages involving a lot of dependencies (which includes some "safe" packages)
129136
"safe", !! "node_modules/@types/scheduler/tracing.d.ts", [];
130137
"full", !! "node_modules/csstype/index.d.ts", [];
131-
"safe", !! "node_modules/@types/prop-types/index.d.ts", [];
138+
"safe", !! "node_modules/@types/prop-types/index.d.ts", ["--rec-module=off"];
132139
"full", !! "node_modules/@types/react/index.d.ts" ++ "node_modules/@types/react/global.d.ts", [];
133140
"full", !! "node_modules/@types/react-modal/index.d.ts", [];
134141

135142
// "safe" package which depends on another "safe" package
136143
"safe", !! "node_modules/@types/yargs-parser/index.d.ts", [];
137-
"safe", !! "node_modules/@types/yargs/index.d.ts", [];
144+
"safe", !! "node_modules/@types/yargs/index.d.ts", ["--rec-module=off"];
138145
]
139146

140147
for preset, package, additionalOptions in packages do
@@ -153,7 +160,7 @@ Target.create "TestJsooGenerateBindings" <| fun _ -> Test.Jsoo.generateBindings
153160
Target.create "TestJsooBuild" <| fun _ -> Test.Jsoo.build ()
154161
Target.create "TestJsoo" ignore
155162

156-
"BuildOnly"
163+
"BuildForTest"
157164
==> "TestJsooClean"
158165
==> "TestJsooGenerateBindings"
159166
==> "TestJsooBuild"
@@ -164,10 +171,9 @@ Target.create "TestOnly" ignore
164171

165172
"TestJsoo"
166173
==> "TestOnly"
174+
==> "TestComplete"
167175
==> "Test"
168176

169-
"Build" ==> "Test"
170-
171177
// Publish targets
172178

173179
module Publish =
@@ -225,15 +231,15 @@ Target.create "PublishJsoo" <| fun _ ->
225231
Publish.Jsoo.updateVersion ()
226232
Publish.Jsoo.testBuild ()
227233

228-
"BuildOnly"
234+
"BuildForPublish"
229235
==> "PublishNpm"
230236
==> "PublishJsoo"
231237
==> "PublishOnly"
232238
==> "Publish"
233239

234240
"TestJsoo" ==> "PublishJsoo"
235241

236-
"Build" ==> "Publish"
242+
"Build" ?=> "Test" ?=> "Publish"
237243

238244
Target.create "All" ignore
239245

docs/development.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ Overview for Developers
55

66
Modules with **\[\<AutoOpen\>\]** does not require `open` to use.
77

8-
- `src/`
9-
- `Bindings/` ... bindings to JS libraries
8+
- `lib/` ... target-agnostic part of the tool (will be separated to a different repo in near future)
9+
- `Bindings/` ... bindings to JS libraries (typescript, browser-or-node)
1010
- `Extensions.fs` ... **\[\<AutoOpen\>\]** extensions for standard library and JS libraries
1111
- `DataTypes/` ... common data structures and algorithm
1212
- `Text.fs` ... efficient rope with `O(1)` concat & `O(n)` stringify
1313
- `Trie.fs` ... trie based on immutable map
1414
- `Graph.fs` ... graph based on immutable map & graph algorithms
15-
- `Common.fs` ... **\[\<AutoOpen\>\]** global command line options, types, and modules
15+
- `Common.fs` ... global interfaces
1616
- `Syntax.fs` ... AST for parsed TypeScAript code
1717
- `Naming.fs` ... naming helpers
1818
- `JsHelper.fs` ... helper functions for JavaScript-related things e.g. NPM packages and ES6 module names.
19+
- `TypeScriptHelper.fs` ... helper functions for using TypeScript Compiler API
1920
- `Typer.fs` ... functions for resolving and manipulating AST
2021
- `Parser.fs` ... functions for converting TS syntax tree to our AST
22+
- `src/` ... target-dependent part of the tool
23+
- `Bindings/` ... bindings to JS libraries (yargs)
24+
- `Extensions.fs` ... **\[\<AutoOpen\>\]** extensions for standard library and JS libraries
25+
- `Common.fs` ... **\[\<AutoOpen\>\]** global command line options, types, and modules
2126
- `Target.fs` ... generic definitions for each targets (`ITarget<_>`)
2227
- `Targets/` ... targets should be placed into here
2328
- `ParserTest.fs` ... debug target to test parser and typer

lib/Bindings/BrowserOrNode.fs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ts2fable 0.7.1
2+
module rec BrowserOrNode
3+
open System
4+
open Fable.Core
5+
open Fable.Core.JS
6+
7+
let [<Import("isBrowser","browser-or-node")>] isBrowser: bool = jsNative
8+
let [<Import("isWebWorker","browser-or-node")>] isWebWorker: bool = jsNative
9+
let [<Import("isNode","browser-or-node")>] isNode: bool = jsNative
10+
let [<Import("isJsDom","browser-or-node")>] isJsDom: bool = jsNative
11+
let [<Import("isDeno","browser-or-node")>] isDeno: bool = jsNative
File renamed without changes.

lib/Common.fs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Ts2Ml.Common
2+
3+
type ILogger =
4+
abstract tracef: fmt:Printf.StringFormat<'T, unit> -> 'T
5+
abstract warnf: fmt:Printf.StringFormat<'T, unit> -> 'T
6+
abstract errorf: fmt:Printf.StringFormat<'T, 'Result> -> 'T
7+
8+
type IOptions =
9+
abstract followRelativeReferences: bool with get, set
10+
11+
type IContext<'Options when 'Options :> IOptions> =
12+
abstract options: 'Options
13+
abstract logger: ILogger
14+
15+
type IContext =
16+
inherit IContext<IOptions>
17+
18+
/// Stateful class to rename overloaded identifiers.
19+
type OverloadRenamer(?rename: string -> int -> string, ?used: Set<string * string>) =
20+
let rename =
21+
match rename with
22+
| Some f -> f
23+
| None -> (fun s i -> s + (String.replicate i "'"))
24+
let m = new MutableMap<string * string, int>()
25+
do
26+
match used with
27+
| None -> ()
28+
| Some used ->
29+
for name in used |> Set.toSeq do
30+
m.[name] <- 0
31+
32+
/// If the `name` is already used in the same `category`, returns the new renamed name.
33+
///
34+
/// Otherwise, (even if it is used in a different `category`), returns the original name.
35+
///
36+
/// `category` can be arbitrary, but it is intended for something like `value`, `type`, `module`, etc.
37+
member __.Rename (category: string) (name: string) =
38+
match m.TryGetValue((category, name)) with
39+
| true, i ->
40+
m.[(category, name)] <- i + 1
41+
let name' = rename name (i+1)
42+
m.[(category, name')] <- 0
43+
name'
44+
| false, _ ->
45+
m.[(category, name)] <- 0
46+
name

src/DataTypes/Graph.fs renamed to lib/DataTypes/Graph.fs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ namespace DataTypes
33
type Graph<'node when 'node: comparison> = Map<'node, 'node list>
44

55
module Graph =
6+
let empty : Graph<_> = Map.empty
7+
8+
let add origin target (graph: Graph<_>) : Graph<_> =
9+
match graph |> Map.tryFind origin with
10+
| None -> graph |> Map.add origin [target]
11+
| Some targets -> graph |> Map.add origin (target :: targets |> List.distinct)
12+
13+
let addEdge (origin, target) (graph: Graph<_>) : Graph<_> = add origin target graph
14+
15+
let remove origin target (graph: Graph<_>) : Graph<_> =
16+
match graph |> Map.tryFind origin with
17+
| Some targets -> graph |> Map.add origin (targets |> List.except [target])
18+
| None -> graph
19+
20+
let removeEdge (origin, target) (graph: Graph<_>) : Graph<_> = remove origin target graph
21+
622
let rec private dfsImpl' g (used, ordRev) v =
723
let used = used |> Set.add v
824
let used, ordRev =
@@ -19,11 +35,13 @@ module Graph =
1935

2036
let ofEdges (edges: ('a * 'a) list) : Graph<_> =
2137
edges
38+
|> List.distinct
2239
|> List.groupBy fst
2340
|> List.fold (fun state (k, xs) -> state |> Map.add k (xs |> List.map snd)) Map.empty
2441

2542
let ofEdgesRev (edges: ('a * 'a) list) : Graph<_> =
2643
edges
44+
|> List.distinct
2745
|> List.groupBy snd
2846
|> List.fold (fun state (k, xs) -> state |> Map.add k (xs |> List.map fst)) Map.empty
2947

src/DataTypes/Text.fs renamed to lib/DataTypes/Text.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace DataTypes
22

3+
open Ts2Ml.Extensions
4+
35
[<StructuredFormatDisplay("{AsString}")>]
46
type text =
57
private

src/DataTypes/Trie.fs renamed to lib/DataTypes/Trie.fs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ module Trie =
9191
| Some child -> { t with children = t.children |> Map.add k (setSubTrie ks subTrie child) }
9292
| None -> { t with children = t.children |> Map.add k (setSubTrie ks subTrie empty) }
9393

94+
let rec getLongestMatch (ks: 'k list) (trie: Trie<'k, 'v>) : {| value: 'v option; rest: 'k list |} =
95+
match ks with
96+
| [] -> {| value = trie.value; rest = [] |}
97+
| k :: ks ->
98+
match Map.tryFind k trie.children with
99+
| Some child -> getLongestMatch ks child
100+
| None -> {| value = trie.value; rest = k :: ks |}
101+
102+
let collectPath (ks: 'k list) (collector: 'k list -> 'v option -> 'a option) (trie: Trie<'k, 'v>) : 'a list =
103+
let rec go acc ks trie =
104+
let acc =
105+
match collector ks trie.value with
106+
| Some a -> a :: acc
107+
| None -> acc
108+
match ks with
109+
| [] -> List.rev acc
110+
| k :: ks ->
111+
match Map.tryFind k trie.children with
112+
| Some child -> go acc ks child
113+
| None -> List.rev acc
114+
go [] ks trie
115+
94116
let fold (f: 'state -> 'k list -> 'v -> 'state) (s: 'state) (t: Trie<'k, 'v>) =
95117
let rec go ksRev state t =
96118
let state =

0 commit comments

Comments
 (0)