@@ -226,14 +226,12 @@ let rec emitTypeImpl (flags: EmitTypeFlags) (overrideFunc: OverrideFunc) (ctx: C
226
226
else
227
227
let maxArity = List.length typrms
228
228
let tyName = Naming.createTypeNameOfArity arity ( Some maxArity) " t"
229
- let simple () =
230
- Naming.structured Naming.moduleName i. name + " ." + tyName |> str |> withTyargs
231
- if fn.source <> ctx.currentSourceFile then simple ()
229
+ let simple name =
230
+ Naming.structured Naming.moduleName name + " ." + tyName |> str |> withTyargs
231
+ if fn.source <> ctx.currentSourceFile then simple fn.name
232
232
else
233
233
match ctx |> Context.getRelativeNameTo fn.name with
234
- | Ok relativeName ->
235
- assert ( relativeName = fn.name)
236
- simple ()
234
+ | Ok relativeName -> simple relativeName
237
235
| Error [] -> // the type is the current namespace
238
236
tyName |> str |> withTyargs
239
237
| Error diff ->
@@ -1233,12 +1231,19 @@ let createStructuredText (rootCtx: Context) (stmts: Statement list) : Structured
1233
1231
| SymbolIndexer _ | UnknownMember None -> () ]
1234
1232
1235
1233
let rec folder ctx ( current : StructuredText ) ( s : Statement ) : StructuredText =
1236
- let getModule name =
1237
- match current |> Trie.getSubTrie [ name] with
1238
- | Some t -> t
1239
- | None -> Trie.empty
1240
- let setModule name trie = current |> Trie.setSubTrie [ name] trie
1241
- let setNode node = current |> Trie.setOrUpdate node StructuredTextNode.union
1234
+ let getTrie name current =
1235
+ current |> Trie.getSubTrie name |> Option.defaultValue Trie.empty
1236
+ let setTrie name trie current =
1237
+ current |> Trie.setSubTrie name trie
1238
+ let inTrie name f current =
1239
+ let m =
1240
+ current
1241
+ |> Trie.getSubTrie name
1242
+ |> Option.defaultValue Trie.empty
1243
+ |> f
1244
+ current |> Trie.setSubTrie name m
1245
+ let set node current = current |> Trie.setOrUpdate node StructuredTextNode.union
1246
+ let add name node current = current |> Trie.addOrUpdate name node StructuredTextNode.union
1242
1247
1243
1248
let comments =
1244
1249
match ( s :> ICommented<_>) .getComments() with
@@ -1249,7 +1254,7 @@ let createStructuredText (rootCtx: Context) (stmts: Statement list) : Structured
1249
1254
let addExport name kind kindString current =
1250
1255
match getExportFromStatement ctx name kind kindString s with
1251
1256
| None -> current
1252
- | Some e -> current |> Trie.setOrUpdate {| StructuredTextNode.empty with exports = [ e] |} StructuredTextNode.union
1257
+ | Some e -> current |> set {| StructuredTextNode.empty with exports = [ e] |}
1253
1258
let addAnonymousInterfaceExcluding ais current =
1254
1259
knownTypes ()
1255
1260
|> Seq.choose ( function KnownType.AnonymousInterface ( a, info) -> Some ( a, info) | _ -> None)
@@ -1263,53 +1268,54 @@ let createStructuredText (rootCtx: Context) (stmts: Statement list) : Structured
1263
1268
if shouldSkip then current
1264
1269
else
1265
1270
emitClass emitTypeFlags OverrideFunc.noOverride ctx current ( a.MapName Choice2Of2) (( fun _ _ _ -> []), Set.empty, None)
1266
- |> Trie.setOrUpdate {| StructuredTextNode.empty with anonymousInterfaces = Set.singleton a |} StructuredTextNode.union
1271
+ |> set {| StructuredTextNode.empty with anonymousInterfaces = Set.singleton a |}
1267
1272
) current
1268
1273
let addAnonymousInterface current = addAnonymousInterfaceExcluding [] current
1269
1274
1270
1275
match s with
1271
1276
| Module m ->
1272
1277
let module ' =
1273
1278
let node = {| StructuredTextNode.empty with docCommentLines = comments; knownTypes = knownTypes () |}
1274
- let module ' = getModule m.name |> Trie.setOrUpdate node StructuredTextNode.union
1279
+ let module ' = current |> getTrie [ m.name] |> set node
1275
1280
let ctx = ctx |> Context.ofChildNamespace m.name
1276
1281
m.statements |> List.fold ( folder ctx) module'
1277
- let result = setModule m.name module'
1282
+ let current =
1283
+ current |> setTrie [ m.name] module'
1278
1284
match module'.value with
1279
- | None -> result
1285
+ | None -> current
1280
1286
| Some v ->
1281
1287
let kind =
1282
1288
if v.scoped <> Scoped.No then Kind.OfModule
1283
1289
else Kind.OfNamespace
1284
- result |> addExport m.name kind ( if m.isNamespace then " namespace" else " module" )
1290
+ current |> addExport m.name kind ( if m.isNamespace then " namespace" else " module" )
1285
1291
| Global m -> m.statements |> List.fold ( folder ctx) current
1286
1292
| Class c ->
1287
1293
emitClass emitTypeFlags OverrideFunc.noOverride ctx current ( c.MapName Choice1Of2) (( fun _ _ _ -> []), Set.empty, None)
1288
1294
|> addAnonymousInterface
1289
1295
| Enum e ->
1290
- let module ' =
1296
+ current
1297
+ |> inTrie [ e.name] ( fun module' ->
1291
1298
let ctx = ctx |> Context.ofChildNamespace e.name
1292
1299
let items = emitTypeAliases emitTypeFlags OverrideFunc.noOverride ctx [] ( GetSelfTyText.enumCases e e.cases)
1293
- let node = {| StructuredTextNode.empty with items = items; docCommentLines = comments; knownTypes = knownTypes () |}
1294
1300
let module ' =
1295
- getModule e.name |> Trie.setOrUpdate node StructuredTextNode.union
1301
+ let node = {| StructuredTextNode.empty with items = items; docCommentLines = comments; knownTypes = knownTypes () |}
1302
+ module' |> set node
1296
1303
e.cases |> List.fold ( fun state c ->
1297
1304
let ctx = ctx |> Context.ofChildNamespace c.name
1298
1305
let comments = List.map emitCommentBody c.comments
1299
1306
let items =
1300
1307
emitTypeAliases emitTypeFlags OverrideFunc.noOverride ctx [] ( GetSelfTyText.enumCases e [ c])
1301
1308
let node = {| StructuredTextNode.empty with items = items; docCommentLines = comments; knownTypes = knownTypes () |}
1302
- state |> Trie.addOrUpdate [ c.name] node StructuredTextNode.union
1303
- ) module'
1304
- setModule e.name module' |> addExport e.name Kind.OfEnum " enum"
1309
+ state |> add [ c.name] node
1310
+ ) module')
1311
+ |> addExport e.name Kind.OfEnum " enum"
1305
1312
| TypeAlias ta ->
1306
1313
let ctx = ctx |> Context.ofChildNamespace ta.name
1307
1314
let items =
1308
1315
emitTypeAliases emitTypeFlags OverrideFunc.noOverride ctx ta.typeParams ( emitSelfType ctx ta.target)
1309
1316
let node = {| StructuredTextNode.empty with items = items; docCommentLines = comments; knownTypes = knownTypes () |}
1310
- let module ' =
1311
- getModule ta.name |> Trie.setOrUpdate node StructuredTextNode.union
1312
- setModule ta.name module'
1317
+ current
1318
+ |> inTrie [ ta.name] ( set node)
1313
1319
|> addExport ta.name Kind.OfTypeAlias " type"
1314
1320
|> addAnonymousInterface
1315
1321
| Pattern p ->
@@ -1326,10 +1332,9 @@ let createStructuredText (rootCtx: Context) (stmts: Statement list) : Structured
1326
1332
if knownTypesInMembers |> Set.contains ( KnownType.Ident ( ctx |> Context.getFullName [ intfName])) then
1327
1333
fallback current
1328
1334
else
1329
- getModule value.name
1330
- |> Trie.setOrUpdate ( createModule ()) StructuredTextNode.union
1331
- |> setModule value.name
1332
- |> Trie.setOrUpdate {| StructuredTextNode.empty with scoped = Scoped.Yes |} StructuredTextNode.union
1335
+ current
1336
+ |> inTrie [ value.name] ( set ( createModule ()))
1337
+ |> set {| StructuredTextNode.empty with scoped = Scoped.Yes |}
1333
1338
|> addExport value.name Kind.OfClass " interface"
1334
1339
|> addAnonymousInterface
1335
1340
| ImmediateConstructor ( baseIntf, ctorIntf, ctorValue) when Simplify.Has( ctx.options.simplify, Simplify.ImmediateConstructor) ->
@@ -1343,7 +1348,7 @@ let createStructuredText (rootCtx: Context) (stmts: Statement list) : Structured
1343
1348
knownTypes = knownTypes ()
1344
1349
scoped = Scoped.Yes |}
1345
1350
current
1346
- |> Trie.setOrUpdate node StructuredTextNode.union
1351
+ |> set node
1347
1352
|> addExport func.name Kind.OfValue " function"
1348
1353
|> addAnonymousInterface
1349
1354
| Variable value ->
@@ -1354,22 +1359,24 @@ let createStructuredText (rootCtx: Context) (stmts: Statement list) : Structured
1354
1359
knownTypes = knownTypes ()
1355
1360
scoped = Scoped.Yes |}
1356
1361
current
1357
- |> Trie.setOrUpdate node StructuredTextNode.union
1362
+ |> set node
1358
1363
|> addExport value.name Kind.OfValue ( if value.isConst then " const" else " let" )
1359
1364
|> addAnonymousInterface
1360
1365
let inline (| Dummy |) _ = []
1361
1366
match value.typ with
1362
1367
| AnonymousInterface intf when Simplify.Has( ctx.options.simplify, Simplify.AnonymousInterfaceValue) ->
1363
1368
let knownTypes = knownTypes ()
1364
1369
let items = intfToStmts intf ( ctx |> Context.ofChildNamespace value.name) emitTypeFlags overrideFunc
1365
- getModule value.name
1366
- |> Trie.setOrUpdate {| StructuredTextNode.empty with items = items; knownTypes = knownTypes; scoped = Scoped.Force value.name |} StructuredTextNode.union
1367
- |> setModule value.name
1368
- |> Trie.setOrUpdate
1370
+ current
1371
+ |> inTrie [ value.name]
1372
+ ( set
1373
+ {| StructuredTextNode.empty with
1374
+ items = items; knownTypes = knownTypes; scoped = Scoped.Force value.name |})
1375
+ |> set
1369
1376
{| StructuredTextNode.empty with
1370
1377
items = emitVariable emitTypeFlags overrideFunc ctx value
1371
1378
knownTypes = knownTypes
1372
- scoped = Scoped.Yes |} StructuredTextNode.union
1379
+ scoped = Scoped.Yes |}
1373
1380
|> addExport value.name Kind.OfValue ( if value.isConst then " const" else " let" )
1374
1381
|> addAnonymousInterface
1375
1382
| Ident ( i & { loc = loc }) & Dummy tyargs
@@ -1386,29 +1393,31 @@ let createStructuredText (rootCtx: Context) (stmts: Statement list) : Structured
1386
1393
let createModule () =
1387
1394
let items = intfToStmts intf ctx emitTypeFlags overrideFunc
1388
1395
{| StructuredTextNode.empty with items = items; knownTypes = knownTypesInMembers; scoped = Scoped.Force value.name |}
1389
- getModule name
1390
- |> Trie.setOrUpdate ( createModule ()) StructuredTextNode.union
1391
- |> setModule name
1392
- |> Trie.setOrUpdate {| StructuredTextNode.empty with scoped = Scoped.Yes |} StructuredTextNode.union
1396
+ current
1397
+ |> inTrie [ name] ( set ( createModule ()))
1398
+ |> set {| StructuredTextNode.empty with scoped = Scoped.Yes |}
1393
1399
|> addExport name Kind.OfValue ( if value.isConst then " const" else " let" )
1394
1400
|> fallback
1395
1401
| _ -> fallback current
1396
1402
| Import i ->
1397
- setNode {| StructuredTextNode.empty with items = emitImport ctx i |}
1403
+ current |> set {| StructuredTextNode.empty with items = emitImport ctx i |}
1398
1404
| Export e ->
1399
1405
let getKind = function
1400
1406
| CommonJsExport i | ES6DefaultExport i -> i |> Ident.getKind ctx
1401
1407
| ES6Export x -> x.target |> Ident.getKind ctx
1402
1408
| NamespaceExport _ -> Set.empty
1403
- setNode {| StructuredTextNode.empty with exports = [ ExportItem.Statement {| e with clauses = e.clauses |> List.map ( fun c -> c, getKind c) |}] |}
1409
+ current
1410
+ |> set
1411
+ {| StructuredTextNode.empty with
1412
+ exports = [ ExportItem.Statement {| e with clauses = e.clauses |> List.map ( fun c -> c, getKind c) |}] |}
1404
1413
| UnknownStatement u ->
1405
1414
let cmt =
1406
1415
match u.origText with
1407
1416
| Some s -> commentStr s | None -> commentStr " unknown statement"
1408
- setNode {| StructuredTextNode.empty with items = [ ScopeIndependent cmt] |}
1417
+ current |> set {| StructuredTextNode.empty with items = [ ScopeIndependent cmt] |}
1409
1418
| FloatingComment c ->
1410
1419
let cmt = ScopeIndependent empty :: ( c.comments |> List.map ( emitCommentBody >> comment >> ScopeIndependent))
1411
- setNode {| StructuredTextNode.empty with items = ScopeIndependent empty :: cmt |}
1420
+ current |> set {| StructuredTextNode.empty with items = ScopeIndependent empty :: cmt |}
1412
1421
and folder ' ctx stmt node = folder ctx node stmt
1413
1422
1414
1423
stmts |> List.fold ( folder rootCtx) Trie.empty
0 commit comments