Skip to content

Commit 44eb828

Browse files
committed
Use impossible and |?
1 parent 86f1df7 commit 44eb828

File tree

7 files changed

+113
-1061
lines changed

7 files changed

+113
-1061
lines changed

lib/Extensions.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
[<AutoOpen>]
22
module Ts2Ml.Extensions
33

4+
let inline (|?) (xo: 'a option) (y: 'a) : 'a = Option.defaultValue y xo
5+
6+
/// Use when a certain code path is impossible or unreachable.
7+
let impossible fmt =
8+
Printf.ksprintf (fun msg -> failwith ("impossible: " + msg)) fmt
9+
410
open System
511

612
module Enum =

lib/JsHelper.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let getPackageJsonPath (exampleFilePath: string) =
2121
match rest with
2222
| userName :: packageName :: _ when userName.StartsWith("@") -> [userName; packageName]
2323
| packageName :: _ -> [packageName]
24-
| _ -> failwith "impossible_getPackageJsonPath_root"
24+
| _ -> impossible "getPackageJsonPath_root"
2525
let path =
2626
prefix @ packageName @ ["package.json"] |> String.concat Path.separator
2727

@@ -163,7 +163,7 @@ let getJsModuleName (info: Syntax.PackageInfo option) (sourceFile: Path.Absolute
163163
match List.rev rest with
164164
| "index.d.ts" :: name :: _ -> name
165165
| name :: _ -> stripExtension name
166-
| [] -> failwith "impossible"
166+
| [] -> impossible "getJsModuleName_getSubmodule"
167167
match info with
168168
| Some info ->
169169
if info.indexFile |> Option.exists ((=) sourceFile) then

lib/Parser.fs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ module private ParserImpl =
251251
|> Array.toList
252252

253253
let readJSDocTag (tag: Ts.JSDocTag) : Comment =
254-
let text = tag.comment |> Option.map readCommentText |> Option.defaultValue []
254+
let text = tag.comment |> Option.map readCommentText |? []
255255
match tag.kind with
256256
| Kind.JSDocParameterTag ->
257257
let tag = tag :?> Ts.JSDocParameterTag
@@ -376,7 +376,7 @@ module private ParserImpl =
376376
match t.kind with
377377
| Kind.TypeReference -> !!(t :?> Ts.TypeReferenceNode).typeName
378378
| Kind.ExpressionWithTypeArguments -> !!(t :?> Ts.ExpressionWithTypeArguments).expression
379-
| _ -> failwith "impossible"
379+
| _ -> impossible "readTypeNode_TypeReference"
380380
match readIdentOrTypeVar ctx typrm lhs with
381381
| Choice1Of2 lt ->
382382
match t.typeArguments with
@@ -508,7 +508,7 @@ module private ParserImpl =
508508
{ args = args; isVariadic = isVariadic; returnType = retType; loc = Node.location parent }
509509

510510
and readMemberAttribute (ctx: ParserContext) (nd: Ts.NamedDeclaration) : MemberAttribute =
511-
let accessibility = getAccessibility nd.modifiers |> Option.defaultValue Public
511+
let accessibility = getAccessibility nd.modifiers |? Public
512512
let isStatic = hasModifier Kind.StaticKeyword nd.modifiers
513513
let comments = readCommentsForNamedDeclaration ctx nd
514514
{ accessibility = accessibility; isStatic = isStatic; comments = comments; loc = Node.location nd }
@@ -675,7 +675,7 @@ module private ParserImpl =
675675
{
676676
comments = readCommentsForNamedDeclaration ctx i
677677
name = Name name
678-
accessibility = getAccessibility i.modifiers |> Option.defaultValue Public
678+
accessibility = getAccessibility i.modifiers |? Public
679679
typeParams = typrms
680680
implements = readInherits typrmsSet ctx i.heritageClauses
681681
isInterface = true
@@ -689,8 +689,8 @@ module private ParserImpl =
689689
let typrmsSet = typrms |> List.map (fun tp -> tp.name) |> Set.ofList
690690
{
691691
comments = readCommentsForNamedDeclaration ctx i
692-
name = i.name |> Option.map (fun id -> Name id.text) |> Option.defaultValue ExportDefaultUnnamedClass
693-
accessibility = getAccessibility i.modifiers |> Option.defaultValue Public
692+
name = i.name |> Option.map (fun id -> Name id.text) |? ExportDefaultUnnamedClass
693+
accessibility = getAccessibility i.modifiers |? Public
694694
typeParams = typrms
695695
implements = readInherits typrmsSet ctx i.heritageClauses
696696
isInterface = false
@@ -925,11 +925,11 @@ module private ParserImpl =
925925
let desc =
926926
doc.comment
927927
|> Option.map (readCommentText >> Description >> List.singleton)
928-
|> Option.defaultValue []
928+
|? []
929929
let tags =
930930
doc.tags
931931
|> Option.map (Array.map readJSDocTag >> List.ofArray)
932-
|> Option.defaultValue []
932+
|? []
933933
desc @ tags
934934

935935
let readJSDoc (ctx: ParserContext) (doc: Ts.JSDoc) : Statement option =

lib/Typer.fs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ module FullName =
222222
| Some info ->
223223
info.definitionsMap
224224
|> Trie.tryFind fullName.name
225-
|> Option.defaultValue []
225+
|? []
226226

227227
let getExportType (ctx: TyperContext<_, _>) (fullName: FullName) : ExportType option =
228228
match ctx.info |> Map.tryFind fullName.source with
@@ -236,7 +236,7 @@ module FullName =
236236
| Definition.EnumCase _ -> Kind.OfEnumCase
237237
| Definition.Module m -> if m.isNamespace then Kind.OfNamespace else Kind.OfModule
238238
| Definition.Variable _ | Definition.Function _ -> Kind.OfValue
239-
| Definition.Import (c, _) -> c.kind |> Option.map Set.toList |> Option.defaultValue []
239+
| Definition.Import (c, _) -> c.kind |> Option.map Set.toList |? []
240240
| Definition.Member _ -> Kind.OfMember
241241

242242
let hasKind (ctx: TyperContext<_, _>) (kind: Kind) (fullName: FullName) =
@@ -422,8 +422,8 @@ module Type =
422422
let rec findTypesInFieldLike pred s (fl: FieldLike) = findTypes pred s fl.value
423423
and findTypesInTypeParam pred s (tp: TypeParam) =
424424
seq {
425-
yield! tp.extends |> Option.map (findTypes pred s) |> Option.defaultValue Seq.empty
426-
yield! tp.defaultType |> Option.map (findTypes pred s) |> Option.defaultValue Seq.empty
425+
yield! tp.extends |> Option.map (findTypes pred s) |? Seq.empty
426+
yield! tp.defaultType |> Option.map (findTypes pred s) |? Seq.empty
427427
}
428428
and findTypesInFuncType pred s (ft: FuncType<Type>) =
429429
seq {
@@ -1080,8 +1080,8 @@ module Statement =
10801080
and treatTypeParameters (state: {| origin: AnonymousInterfaceOrigin; namespace_: string list |}) (tps: TypeParam list) =
10811081
seq {
10821082
for tp in tps do
1083-
yield! tp.extends |> Option.map (findTypes typeFinder state) |> Option.defaultValue Seq.empty
1084-
yield! tp.defaultType |> Option.map (findTypes typeFinder state) |> Option.defaultValue Seq.empty
1083+
yield! tp.extends |> Option.map (findTypes typeFinder state) |? Seq.empty
1084+
yield! tp.defaultType |> Option.map (findTypes typeFinder state) |? Seq.empty
10851085
}
10861086
and treatNamed (state: {| origin: AnonymousInterfaceOrigin; namespace_: string list |}) name value =
10871087
findTypes typeFinder {| state with origin = { state.origin with valueName = Some name } |} value
@@ -1442,7 +1442,7 @@ module ResolvedUnion =
14421442
let inline getLiteralFieldsFromClass c = getLiteralFieldsFromClass getLiteralFieldsFromType c
14431443
match ty with
14441444
| Intrinsic | PolymorphicThis | TypeVar _ | Prim _ | TypeLiteral _ | Tuple _ | Func _ | NewableFunc _ -> Map.empty
1445-
| Erased _ -> failwith "impossible_getDiscriminatedFromUnion_getLiteralFieldsFromType_Erased"
1445+
| Erased _ -> impossible "getDiscriminatedFromUnion_getLiteralFieldsFromType_Erased"
14461446
| Union u ->
14471447
let result = u.types |> List.map getLiteralFieldsFromType
14481448
result |> List.fold (fun state fields ->
@@ -1708,7 +1708,7 @@ let mergeSources newFileName (srcs: SourceFile list) =
17081708
srcs |> List.map (fun src -> src.fileName, newFileName) |> Map.ofList
17091709
let f (i: Ident) =
17101710
i |> Ident.mapSource (fun path ->
1711-
sourceMapping |> Map.tryFind path |> Option.defaultValue path
1711+
sourceMapping |> Map.tryFind path |? path
17121712
)
17131713
let statements =
17141714
srcs

src/Extensions.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type Argv<'T> with
5959
:> Argv<'U>
6060
member this.addFlag (key: string, f: 'U -> bool, ?descr, ?defaultValue, ?defaultDescr, ?alias) =
6161
this.boolean(!^key)
62-
.addImpl<bool>(key, descr, dv=(defaultValue |> Option.defaultValue false), ?dd=defaultDescr, ?alias=alias)
62+
.addImpl<bool>(key, descr, dv=(defaultValue |? false), ?dd=defaultDescr, ?alias=alias)
6363
:> Argv<'U>
6464
member this.addCounter (key: string, f: 'U -> int, ?descr, ?alias) =
6565
this.count(!^key)

0 commit comments

Comments
 (0)