Skip to content

Commit b470255

Browse files
committed
Add --readable-names
Close #68.
1 parent d75d016 commit b470255

File tree

5 files changed

+55
-34
lines changed

5 files changed

+55
-34
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
- Add option `--human-readable-anonymous-interface-names` (or `--readable-names`) to try to use more readable names instead of `AnonymousInterfaceN`.
9+
710
## [1.4.0-beta.2] - 2022-01-17
811
- Fix a bug which prevented ts2ocaml from generating anonymous interfaces when used with --simplify=named-interface-value
912

build.fsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ module Test =
136136
"safe", !! "node_modules/@types/scheduler/tracing.d.ts", [];
137137
"full", !! "node_modules/csstype/index.d.ts", [];
138138
"safe", !! "node_modules/@types/prop-types/index.d.ts", ["--rec-module=off"];
139-
"full", !! "node_modules/@types/react/index.d.ts" ++ "node_modules/@types/react/global.d.ts", [];
140-
"full", !! "node_modules/@types/react-modal/index.d.ts", [];
139+
"full", !! "node_modules/@types/react/index.d.ts" ++ "node_modules/@types/react/global.d.ts", ["--readable-names"];
140+
"full", !! "node_modules/@types/react-modal/index.d.ts", ["--readable-names"];
141141

142142
// "safe" package which depends on another "safe" package
143143
"safe", !! "node_modules/@types/yargs-parser/index.d.ts", [];

lib/Typer.fs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,15 +1071,39 @@ module Statement =
10711071
and typeFinder (state: {| origin: AnonymousInterfaceOrigin; namespace_: string list |}) ty =
10721072
let inline resultMany xs = Some [], state, xs
10731073
match ty with
1074-
| App (AAnonymousInterface i, _, _) ->
1075-
None, {| state with origin = AnonymousInterfaceOrigin.Empty |}, Seq.singleton (i, state)
1076-
| AnonymousInterface i ->
1077-
None, {| state with origin = AnonymousInterfaceOrigin.Empty |}, Seq.singleton (i, state)
1074+
| App (AAnonymousInterface i, _, _) | AnonymousInterface i ->
1075+
let inner =
1076+
let state = {| state with origin = AnonymousInterfaceOrigin.Empty |}
1077+
treatClassLike state (i.MapName(ignore))
1078+
None, {| state with origin = AnonymousInterfaceOrigin.Empty |}, Seq.append [i, state] inner
10781079
| Func (ft, tps, _) | NewableFunc (ft, tps, _) ->
10791080
treatFuncType state ft tps |> resultMany
10801081
| Union { types = types } | Intersection { types = types } ->
10811082
Some types, state, Seq.empty
10821083
| _ -> None, {| state with origin = AnonymousInterfaceOrigin.Empty |}, Seq.empty
1084+
and treatClassLike (state: {| origin: AnonymousInterfaceOrigin; namespace_: string list |}) (c: Class<unit>) =
1085+
seq {
1086+
for _, m in c.members do
1087+
match m with
1088+
| Method (name, ft, tps) ->
1089+
yield! treatFuncType {| state with origin = { state.origin with valueName = Some name } |} ft tps
1090+
| Newable (ft, tps) | Callable (ft, tps) -> yield! treatFuncType state ft tps
1091+
| Field (fl, _) | Getter fl | Setter fl -> yield! treatNamed state fl.name fl.value
1092+
| Indexer (ft, _) -> yield! treatFuncType state ft []
1093+
| SymbolIndexer (name, ft, _) ->
1094+
yield! treatFuncType {| state with origin = { state.origin with valueName = Some name } |} ft []
1095+
| Constructor ft ->
1096+
for arg in ft.args do
1097+
let ty, origin =
1098+
match arg with
1099+
| Choice1Of2 fl -> fl.value, { state.origin with argName = Some fl.name }
1100+
| Choice2Of2 t -> t, state.origin
1101+
yield! findTypes typeFinder {| state with origin = origin |} ty
1102+
| UnknownMember _ -> ()
1103+
for t in c.implements do
1104+
yield! findTypes typeFinder state t
1105+
yield! treatTypeParameters state c.typeParams
1106+
}
10831107

10841108
findStatements (fun currentNamespace state stmt ->
10851109
let inline result_ x = Some [], state, x
@@ -1099,28 +1123,7 @@ module Statement =
10991123
let typeName =
11001124
match c.name with Name n -> Some n | _ -> None
11011125
let state = {| state with namespace_ = currentNamespace; origin = { state.origin with typeName = typeName } |}
1102-
seq {
1103-
for _, m in c.members do
1104-
match m with
1105-
| Method (name, ft, tps) ->
1106-
yield! treatFuncType {| state with origin = { state.origin with valueName = Some name } |} ft tps
1107-
| Newable (ft, tps) | Callable (ft, tps) -> yield! treatFuncType state ft tps
1108-
| Field (fl, _) | Getter fl | Setter fl -> yield! treatNamed state fl.name fl.value
1109-
| Indexer (ft, _) -> yield! treatFuncType state ft []
1110-
| SymbolIndexer (name, ft, _) ->
1111-
yield! treatFuncType {| state with origin = { state.origin with valueName = Some name } |} ft []
1112-
| Constructor ft ->
1113-
for arg in ft.args do
1114-
let ty, origin =
1115-
match arg with
1116-
| Choice1Of2 fl -> fl.value, { state.origin with argName = Some fl.name }
1117-
| Choice2Of2 t -> t, state.origin
1118-
yield! findTypes typeFinder {| state with origin = origin |} ty
1119-
| UnknownMember _ -> ()
1120-
for t in c.implements do
1121-
yield! findTypes typeFinder state t
1122-
yield! treatTypeParameters state c.typeParams
1123-
} |> result_
1126+
treatClassLike state (c.MapName(ignore)) |> result_
11241127
| _ -> None, state.origin, Seq.empty
11251128
) AnonymousInterfaceOrigin.Empty stmts |> Set.ofSeq
11261129

src/Targets/JsOfOCaml/Common.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type Options =
103103
abstract recModule: RecModule with get, set
104104
abstract safeArity: FeatureFlag with get, set
105105
abstract simplify: Simplify list with get, set
106+
abstract humanReadableAnonymousInterfaceNames: bool with get, set
106107

107108
module Options =
108109
open Fable.Core.JsInterop
@@ -223,6 +224,7 @@ module Options =
223224
"safe-arity";
224225
"rec-module";
225226
"simplify";
227+
"human-readable-anonymous-interface-names"
226228
],
227229
"Code Generator Options:")
228230
.addChoice(
@@ -244,6 +246,13 @@ module Options =
244246
descr=
245247
sprintf "Turn on simplification features. Available features: %s"
246248
(Simplify.Values |> Array.map string |> String.concat ", "))
249+
.addFlag(
250+
"human-readable-anonymous-interface-names",
251+
(fun (o: Options) -> o.humanReadableAnonymousInterfaceNames),
252+
descr="Try to use more readable names instead of AnonymousInterfaceN.",
253+
defaultValue = false,
254+
alias="readable-names"
255+
)
247256

248257
.middleware(!^validate)
249258

src/Targets/JsOfOCaml/Writer.fs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,18 @@ let literalToIdentifier (ctx: Context) (l: Literal) : text =
8686
| LFloat l -> tprintf "n_%s" (formatNumber l)
8787
| LBool true -> str "b_true" | LBool false -> str "b_false"
8888

89-
let anonymousInterfaceModuleName (info: AnonymousInterfaceInfo) =
90-
sprintf "AnonymousInterface%d" info.id
89+
let anonymousInterfaceModuleName (ctx: Context) (info: AnonymousInterfaceInfo) =
90+
match info.origin.valueName, info.origin.argName with
91+
| _, Some s | Some s, None when ctx.options.humanReadableAnonymousInterfaceNames ->
92+
sprintf "%s%d" (Naming.toCase Naming.PascalCase s) info.id
93+
| _, _ ->
94+
sprintf "AnonymousInterface%d" info.id
9195

9296
let anonymousInterfaceToIdentifier (ctx: Context) (a: AnonymousInterface) : text =
9397
match ctx |> Context.bindCurrentSourceInfo (fun i -> i.anonymousInterfacesMap |> Map.tryFind a) with
9498
| Some i ->
9599
if not ctx.options.recModule.IsOffOrDefault then
96-
tprintf "%s.t" (anonymousInterfaceModuleName i)
100+
tprintf "%s.t" (anonymousInterfaceModuleName ctx i)
97101
else
98102
tprintf "anonymous_interface_%d" i.id
99103
| None -> failwithf "impossible_anonymousInterfaceToIdentifier(%s)" a.loc.AsString
@@ -576,7 +580,7 @@ module StructuredText =
576580
|> Set.fold (fun state -> function
577581
| KnownType.Ident fn when fn.source = ctx.currentSourceFile -> state |> WeakTrie.add fn.name
578582
| KnownType.AnonymousInterface (_, i) ->
579-
state |> WeakTrie.add (i.namespace_ @ [anonymousInterfaceModuleName i])
583+
state |> WeakTrie.add (i.namespace_ @ [anonymousInterfaceModuleName ctx i])
580584
| _ -> state
581585
) WeakTrie.empty)
582586
|> Option.defaultValue WeakTrie.empty
@@ -701,7 +705,9 @@ let rec emitMembers (emitType_: TypeEmitter) ctx (selfTy: Type) (ma: MemberAttri
701705
| Indexer (ft, Mutable) ->
702706
yield! emitMembers emitType_ ctx selfTy ma (Indexer (ft, ReadOnly))
703707
yield! emitMembers emitType_ ctx selfTy ma (Indexer (ft, WriteOnly))
704-
| SymbolIndexer _ -> ()
708+
| SymbolIndexer (symbol, ft, _) ->
709+
let ft = func ft |> emitType_ ctx
710+
yield comment (tprintf "[Symbol.%s]: " symbol + ft) |> ScopeIndependent
705711
| UnknownMember msgo ->
706712
yield! comments ()
707713
match msgo with
@@ -908,7 +914,7 @@ let rec emitClass flags overrideFunc (ctx: Context) (current: StructuredText) (c
908914
Some (Type.appOpt (str "t") (ts |> List.map (_emitType _ctx)))
909915
| _ -> None
910916
ClassKind.AnonymousInterface {|
911-
name = anonymousInterfaceModuleName i
917+
name = anonymousInterfaceModuleName ctx i
912918
orig = c.MapName(fun _ -> Anonymous)
913919
|},
914920
selfTy,

0 commit comments

Comments
 (0)