Skip to content

Commit 86f1df7

Browse files
committed
Reverse-merge common files from rescript-support
1 parent c2ce6e2 commit 86f1df7

File tree

9 files changed

+358
-120
lines changed

9 files changed

+358
-120
lines changed

lib/DataTypes/Text.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,11 @@ module Text =
103103
| Indent x -> isMultiLine x
104104
| Concat (x, y) -> isMultiLine x || isMultiLine y
105105
| Empty | Str _ -> false
106+
107+
/// Get the length of a text. Indents are ignored.
108+
let rec length = function
109+
| Empty -> 0
110+
| Newline -> 1
111+
| Str s -> s.Length
112+
| Concat (x, y) -> length x + length y
113+
| Indent x -> length x

lib/DataTypes/Trie.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ type Trie<'k, 'v when 'k: comparison> = {
77

88
type Trie<'k when 'k: comparison> = Trie<'k, unit>
99

10-
type WeakTrie<'k when 'k: comparison> = {
11-
children: Map<'k, WeakTrie<'k>>
12-
}
13-
1410
module Trie =
1511
let empty<'k, 'v when 'k: comparison> : Trie<'k, 'v> =
1612
{ value = None; children = Map.empty }
@@ -165,6 +161,10 @@ module Trie =
165161
let toMap (t: Trie<'k, 'v>) : Map<'k list, 'v> = t |> fold (fun state ks v -> Map.add ks v state) Map.empty
166162
let ofMap (xs: Map<'k list, 'v>) : Trie<'k, 'v> = xs |> Map.fold (fun state ks v -> add ks v state) empty
167163

164+
type WeakTrie<'k when 'k: comparison> = {
165+
children: Map<'k, WeakTrie<'k>>
166+
}
167+
168168
module WeakTrie =
169169
let empty<'k when 'k: comparison> : WeakTrie<'k> = { children = Map.empty }
170170

lib/Extensions.fs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module String =
4242
let escape (s: string) =
4343
s
4444
.Replace("\\", "\\\\")
45-
.Replace("'", "\\'").Replace("\"", "\\\"")
45+
.Replace("\"", "\\\"")
4646
.Replace("\b", "\\b").Replace("\n", "\\n").Replace("\r", "\\r")
4747
.Replace("\t", "\\t")
4848

@@ -51,6 +51,10 @@ module String =
5151
state.Replace(e, "\\" + e)
5252
) s
5353

54+
open Fable.Core
55+
[<Emit("$0.normalize()")>]
56+
let normalize (_: string) : string = jsNative
57+
5458
module Option =
5559
let iterNone f = function
5660
| Some x -> Some x
@@ -71,6 +75,14 @@ module List =
7175
) ([], [])
7276
List.rev xs1, List.rev xs2
7377

78+
let splitChoice3 (xs: Choice<'t1, 't2, 't3> list) : 't1 list * 't2 list * 't3 list =
79+
let xs1, xs2, xs3 =
80+
xs |> List.fold (fun (xs1, xs2, xs3) -> function
81+
| Choice1Of3 x -> x :: xs1, xs2, xs3
82+
| Choice2Of3 x -> xs1, x :: xs2, xs3
83+
| Choice3Of3 x -> xs1, xs2, x :: xs3) ([], [], [])
84+
List.rev xs1, List.rev xs2, List.rev xs3
85+
7486
module Map =
7587
let addNoOverwrite k v m =
7688
m |> Map.change k (function None -> Some v | Some v -> Some v)

lib/Naming.fs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,33 +127,27 @@ let toCase (case: Case) (str: string) =
127127
| _, _ -> wordsToCase case words
128128

129129
module Keywords =
130-
let keywords =
131-
Set.ofList [
132-
"and"; "as"; "assert"; "asr"; "begin"; "class";
133-
"constraint"; "do"; "done"; "downto"; "else"; "end";
134-
"exception"; "external"; "false"; "for"; "fun"; "function";
135-
"functor"; "if"; "in"; "include"; "inherit"; "initializer";
136-
"land"; "lazy"; "let"; "lor"; "lsl"; "lsr";
137-
"lxor"; "match"; "method"; "mod"; "module"; "mutable";
138-
"new"; "nonrec"; "object"; "of"; "open"; "or";
139-
"private"; "rec"; "sig"; "struct"; "then"; "to";
140-
"true"; "try"; "type"; "val"; "virtual"; "when";
141-
"while"; "with"
142-
]
143-
144130
let esKeywords =
145131
Set.ofList [
146132
"Readonly"; "Partial"; "Pick";
147133
"HTMLDialogElement"; "HTMLWebViewElement"
148134
]
149135

150-
let primitives = Set.ofList [ "string"; "obj"; "unit"; "float"; "bool"; "int" ]
151-
let basicTypes = Set.ofList [ "list"; "array"; "option" ]
152-
153136
let removeQuotesAndTrim (s: string) =
154137
if String.IsNullOrEmpty s then ""
155138
else
156139
let c = s.[0]
157140
if (c = '\"' || c = ''')
158141
then s.Trim(c).Trim()
159142
else s.Trim()
143+
144+
let isValidJSIdentifier (s: string) =
145+
if String.IsNullOrEmpty s then false
146+
else
147+
let cs = s.ToCharArray()
148+
if cs[0] |> Char.IsDigit then false
149+
else
150+
cs |> Array.forall (fun c ->
151+
Char.IsLetterOrDigit(c)
152+
|| c = '_' || c = '$'
153+
)

0 commit comments

Comments
 (0)