Skip to content

Commit

Permalink
Use impossible and |?
Browse files Browse the repository at this point in the history
  • Loading branch information
cannorin committed May 9, 2022
1 parent 86f1df7 commit 44eb828
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 1,061 deletions.
6 changes: 6 additions & 0 deletions lib/Extensions.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[<AutoOpen>]
module Ts2Ml.Extensions

let inline (|?) (xo: 'a option) (y: 'a) : 'a = Option.defaultValue y xo

/// Use when a certain code path is impossible or unreachable.
let impossible fmt =
Printf.ksprintf (fun msg -> failwith ("impossible: " + msg)) fmt

open System

module Enum =
Expand Down
4 changes: 2 additions & 2 deletions lib/JsHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let getPackageJsonPath (exampleFilePath: string) =
match rest with
| userName :: packageName :: _ when userName.StartsWith("@") -> [userName; packageName]
| packageName :: _ -> [packageName]
| _ -> failwith "impossible_getPackageJsonPath_root"
| _ -> impossible "getPackageJsonPath_root"
let path =
prefix @ packageName @ ["package.json"] |> String.concat Path.separator

Expand Down Expand Up @@ -163,7 +163,7 @@ let getJsModuleName (info: Syntax.PackageInfo option) (sourceFile: Path.Absolute
match List.rev rest with
| "index.d.ts" :: name :: _ -> name
| name :: _ -> stripExtension name
| [] -> failwith "impossible"
| [] -> impossible "getJsModuleName_getSubmodule"
match info with
| Some info ->
if info.indexFile |> Option.exists ((=) sourceFile) then
Expand Down
16 changes: 8 additions & 8 deletions lib/Parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ module private ParserImpl =
|> Array.toList

let readJSDocTag (tag: Ts.JSDocTag) : Comment =
let text = tag.comment |> Option.map readCommentText |> Option.defaultValue []
let text = tag.comment |> Option.map readCommentText |? []
match tag.kind with
| Kind.JSDocParameterTag ->
let tag = tag :?> Ts.JSDocParameterTag
Expand Down Expand Up @@ -376,7 +376,7 @@ module private ParserImpl =
match t.kind with
| Kind.TypeReference -> !!(t :?> Ts.TypeReferenceNode).typeName
| Kind.ExpressionWithTypeArguments -> !!(t :?> Ts.ExpressionWithTypeArguments).expression
| _ -> failwith "impossible"
| _ -> impossible "readTypeNode_TypeReference"
match readIdentOrTypeVar ctx typrm lhs with
| Choice1Of2 lt ->
match t.typeArguments with
Expand Down Expand Up @@ -508,7 +508,7 @@ module private ParserImpl =
{ args = args; isVariadic = isVariadic; returnType = retType; loc = Node.location parent }

and readMemberAttribute (ctx: ParserContext) (nd: Ts.NamedDeclaration) : MemberAttribute =
let accessibility = getAccessibility nd.modifiers |> Option.defaultValue Public
let accessibility = getAccessibility nd.modifiers |? Public
let isStatic = hasModifier Kind.StaticKeyword nd.modifiers
let comments = readCommentsForNamedDeclaration ctx nd
{ accessibility = accessibility; isStatic = isStatic; comments = comments; loc = Node.location nd }
Expand Down Expand Up @@ -675,7 +675,7 @@ module private ParserImpl =
{
comments = readCommentsForNamedDeclaration ctx i
name = Name name
accessibility = getAccessibility i.modifiers |> Option.defaultValue Public
accessibility = getAccessibility i.modifiers |? Public
typeParams = typrms
implements = readInherits typrmsSet ctx i.heritageClauses
isInterface = true
Expand All @@ -689,8 +689,8 @@ module private ParserImpl =
let typrmsSet = typrms |> List.map (fun tp -> tp.name) |> Set.ofList
{
comments = readCommentsForNamedDeclaration ctx i
name = i.name |> Option.map (fun id -> Name id.text) |> Option.defaultValue ExportDefaultUnnamedClass
accessibility = getAccessibility i.modifiers |> Option.defaultValue Public
name = i.name |> Option.map (fun id -> Name id.text) |? ExportDefaultUnnamedClass
accessibility = getAccessibility i.modifiers |? Public
typeParams = typrms
implements = readInherits typrmsSet ctx i.heritageClauses
isInterface = false
Expand Down Expand Up @@ -925,11 +925,11 @@ module private ParserImpl =
let desc =
doc.comment
|> Option.map (readCommentText >> Description >> List.singleton)
|> Option.defaultValue []
|? []
let tags =
doc.tags
|> Option.map (Array.map readJSDocTag >> List.ofArray)
|> Option.defaultValue []
|? []
desc @ tags

let readJSDoc (ctx: ParserContext) (doc: Ts.JSDoc) : Statement option =
Expand Down
16 changes: 8 additions & 8 deletions lib/Typer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ module FullName =
| Some info ->
info.definitionsMap
|> Trie.tryFind fullName.name
|> Option.defaultValue []
|? []

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

let hasKind (ctx: TyperContext<_, _>) (kind: Kind) (fullName: FullName) =
Expand Down Expand Up @@ -422,8 +422,8 @@ module Type =
let rec findTypesInFieldLike pred s (fl: FieldLike) = findTypes pred s fl.value
and findTypesInTypeParam pred s (tp: TypeParam) =
seq {
yield! tp.extends |> Option.map (findTypes pred s) |> Option.defaultValue Seq.empty
yield! tp.defaultType |> Option.map (findTypes pred s) |> Option.defaultValue Seq.empty
yield! tp.extends |> Option.map (findTypes pred s) |? Seq.empty
yield! tp.defaultType |> Option.map (findTypes pred s) |? Seq.empty
}
and findTypesInFuncType pred s (ft: FuncType<Type>) =
seq {
Expand Down Expand Up @@ -1080,8 +1080,8 @@ module Statement =
and treatTypeParameters (state: {| origin: AnonymousInterfaceOrigin; namespace_: string list |}) (tps: TypeParam list) =
seq {
for tp in tps do
yield! tp.extends |> Option.map (findTypes typeFinder state) |> Option.defaultValue Seq.empty
yield! tp.defaultType |> Option.map (findTypes typeFinder state) |> Option.defaultValue Seq.empty
yield! tp.extends |> Option.map (findTypes typeFinder state) |? Seq.empty
yield! tp.defaultType |> Option.map (findTypes typeFinder state) |? Seq.empty
}
and treatNamed (state: {| origin: AnonymousInterfaceOrigin; namespace_: string list |}) name value =
findTypes typeFinder {| state with origin = { state.origin with valueName = Some name } |} value
Expand Down Expand Up @@ -1442,7 +1442,7 @@ module ResolvedUnion =
let inline getLiteralFieldsFromClass c = getLiteralFieldsFromClass getLiteralFieldsFromType c
match ty with
| Intrinsic | PolymorphicThis | TypeVar _ | Prim _ | TypeLiteral _ | Tuple _ | Func _ | NewableFunc _ -> Map.empty
| Erased _ -> failwith "impossible_getDiscriminatedFromUnion_getLiteralFieldsFromType_Erased"
| Erased _ -> impossible "getDiscriminatedFromUnion_getLiteralFieldsFromType_Erased"
| Union u ->
let result = u.types |> List.map getLiteralFieldsFromType
result |> List.fold (fun state fields ->
Expand Down Expand Up @@ -1708,7 +1708,7 @@ let mergeSources newFileName (srcs: SourceFile list) =
srcs |> List.map (fun src -> src.fileName, newFileName) |> Map.ofList
let f (i: Ident) =
i |> Ident.mapSource (fun path ->
sourceMapping |> Map.tryFind path |> Option.defaultValue path
sourceMapping |> Map.tryFind path |? path
)
let statements =
srcs
Expand Down
2 changes: 1 addition & 1 deletion src/Extensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Argv<'T> with
:> Argv<'U>
member this.addFlag (key: string, f: 'U -> bool, ?descr, ?defaultValue, ?defaultDescr, ?alias) =
this.boolean(!^key)
.addImpl<bool>(key, descr, dv=(defaultValue |> Option.defaultValue false), ?dd=defaultDescr, ?alias=alias)
.addImpl<bool>(key, descr, dv=(defaultValue |? false), ?dd=defaultDescr, ?alias=alias)
:> Argv<'U>
member this.addCounter (key: string, f: 'U -> int, ?descr, ?alias) =
this.count(!^key)
Expand Down
Loading

0 comments on commit 44eb828

Please sign in to comment.