Skip to content

Commit f39aa96

Browse files
committed
Fix everything
1 parent 8a6805f commit f39aa96

File tree

4 files changed

+132
-79
lines changed

4 files changed

+132
-79
lines changed

lib/Parser.fs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,26 @@ module private ParserImpl =
148148
] |> Some
149149
go s
150150

151+
let normalizeQualifiedName (fileNames: string list) (s: string) =
152+
s
153+
|> String.split "."
154+
|> List.ofArray
155+
|> function
156+
| x :: xs when x.StartsWith("\"") ->
157+
let basenames = fileNames |> List.map JsHelper.stripExtension
158+
if basenames |> List.exists (fun basename -> x.EndsWith(basename + "\"")) then xs
159+
else x.Trim('"') :: xs
160+
| xs -> xs
161+
162+
let getFullName (ctx: ParserContext) (nd: Node) =
163+
match ctx.checker.getSymbolAtLocation nd with
164+
| None -> None
165+
| Some s ->
166+
let source = ctx.currentSource.fileName
167+
let fullName =
168+
ctx.checker.getFullyQualifiedName s |> normalizeQualifiedName [source]
169+
Some { source = source; name = fullName }
170+
151171
let getFullNames (ctx: ParserContext) (nd: Node) =
152172
let getSources (s: Ts.Symbol) =
153173
s.declarations
@@ -167,17 +187,6 @@ module private ParserImpl =
167187
_ -> ()
168188
roots.ToArray()
169189

170-
let normalizeQualifiedName (fileNames: string list) (s: string) =
171-
s
172-
|> String.split "."
173-
|> List.ofArray
174-
|> function
175-
| x :: xs when x.StartsWith("\"") ->
176-
let basenames = fileNames |> List.map JsHelper.stripExtension
177-
if basenames |> List.exists (fun basename -> x.EndsWith(basename + "\"")) then xs
178-
else x.Trim('"') :: xs
179-
| xs -> xs
180-
181190
match ctx.checker.getSymbolAtLocation nd with
182191
| None -> []
183192
| Some s ->
@@ -921,11 +930,15 @@ module private ParserImpl =
921930
| [] -> None
922931
| xs -> FloatingComment {| comments = xs; loc = Node.location doc |} |> Some
923932

924-
let rec readModule (ctx: ParserContext) (md: Ts.ModuleDeclaration) : Module =
933+
let rec readModule (ctx: ParserContext) (md: Ts.ModuleDeclaration) : Statement =
925934
let name =
926935
match (!!md.name : Ts.Node).kind with
927-
| Kind.Identifier -> (!!md.name : Ts.Identifier).text
928-
| Kind.StringLiteral -> (!!md.name : Ts.StringLiteral).text
936+
| Kind.GlobalKeyword -> None
937+
| Kind.Identifier ->
938+
match (!!md.name : Ts.Identifier).text with
939+
| "global" -> None
940+
| name -> Some name
941+
| Kind.StringLiteral -> (!!md.name : Ts.StringLiteral).text |> Some
929942
| _ -> nodeError ctx !!md.name "unsupported module name '%s'" (getText md.name)
930943
let check kind =
931944
md.getChildren() |> Seq.exists (fun nd -> nd.kind = kind)
@@ -943,7 +956,7 @@ module private ParserImpl =
943956
| Kind.DeclareKeyword | Kind.StringLiteral | Kind.DotToken | Kind.SyntaxList | Kind.ModuleKeyword -> []
944957
| Kind.JSDocComment -> []
945958
| Kind.ModuleDeclaration ->
946-
[ Module (readModule ctx (nd :?> Ts.ModuleDeclaration)) ]
959+
[ readModule ctx (nd :?> Ts.ModuleDeclaration) ]
947960
| _ ->
948961
nodeWarn ctx nd "unknown kind in ModuleDeclaration: %s" (Enum.pp nd.kind)
949962
[])
@@ -952,7 +965,11 @@ module private ParserImpl =
952965
|> Seq.filter (fun nd -> nd.kind = Kind.JSDocComment)
953966
|> List.ofSeq
954967
|> List.collect (fun nd -> nd :?> Ts.JSDoc |> readJSDocImpl ctx)
955-
{ isExported = isExported; isNamespace = isNamespace; name = name; statements = statements; comments = comments; loc = Node.location md }
968+
match name with
969+
| Some name ->
970+
Module { isExported = isExported; isNamespace = isNamespace; name = name; statements = statements; comments = comments; loc = Node.location md }
971+
| None ->
972+
Global { isExported = isExported; isNamespace = isNamespace; name = (); statements = statements; comments = comments; loc = Node.location md }
956973

957974
and readStatement (ctx: ParserContext) (stmt: Ts.Statement) : Statement list =
958975
let onError () =
@@ -964,7 +981,7 @@ module private ParserImpl =
964981
| Kind.InterfaceDeclaration -> [readInterface ctx (stmt :?> _) |> Class]
965982
| Kind.ClassDeclaration -> [readClass ctx (stmt :?> _) |> Class]
966983
| Kind.EnumDeclaration -> [readEnum ctx (stmt :?> _) |> Enum]
967-
| Kind.ModuleDeclaration -> [readModule ctx (stmt :?> _) |> Module]
984+
| Kind.ModuleDeclaration -> [readModule ctx (stmt :?> _)]
968985
| Kind.VariableStatement -> readVariable ctx (stmt :?> _)
969986
| Kind.FunctionDeclaration -> [readFunction ctx (stmt :?> _) |> Option.map Function |> Option.defaultWith onError]
970987
| Kind.ExportAssignment -> [readExportAssignment ctx (stmt :?> _) |> Option.defaultWith onError]

lib/Syntax.fs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ and Statement =
365365
/// ```
366366
| Module of Module
367367
/// ```ts
368+
/// namespace global { ... }
369+
/// ```
370+
| Global of Global
371+
/// ```ts
368372
/// var name: Type
369373
/// ```
370374
/// or
@@ -394,7 +398,7 @@ and Statement =
394398
member this.loc =
395399
match this with
396400
| TypeAlias ta -> ta.loc | Class c -> c.loc | Enum e -> e.loc
397-
| Module m -> m.loc | Variable v -> v.loc | Function f -> f.loc
401+
| Module m -> m.loc | Global m -> m.loc | Variable v -> v.loc | Function f -> f.loc
398402
| Import i -> i.loc | Export e -> e.loc
399403
| Pattern p -> p.loc
400404
| UnknownStatement u -> u.loc | FloatingComment c -> c.loc
@@ -405,12 +409,12 @@ and Statement =
405409
| Variable { isExported = i } | Function { isExported = i }
406410
| Import { isExported = i } -> i
407411
| Pattern p -> p.isExported
408-
| Export _ | UnknownStatement _ | FloatingComment _ -> Exported.No
412+
| Export _ | UnknownStatement _ | FloatingComment _ | Global _ -> Exported.No
409413
interface ICommented<Statement> with
410414
member this.getComments() =
411415
match this with
412416
| TypeAlias ta -> ta.comments | Class c -> c.comments
413-
| Enum e -> e.comments | Module m -> m.comments
417+
| Enum e -> e.comments | Module m -> m.comments | Global m -> m.comments
414418
| Variable v -> v.comments | Function f -> f.comments
415419
| Import i -> i.comments
416420
| Export e -> e.comments
@@ -424,6 +428,7 @@ and Statement =
424428
| Class c -> Class (map f c)
425429
| Enum e -> Enum (map f e)
426430
| Module m -> Module (map f m)
431+
| Global m -> Global (map f m)
427432
| Variable v -> Variable (map f v)
428433
| Function g -> Function (map f g)
429434
| Import i -> Import (map f i)
@@ -476,18 +481,21 @@ and Pattern =
476481
| ImmediateConstructor (bi, ci, v) ->
477482
ImmediateConstructor ((bi :> ICommented<_>).mapComments f, (ci :> ICommented<_>).mapComments f, (v :> ICommented<_>).mapComments f)
478483

479-
and Module = {
480-
name: string
484+
and Module<'name> = {
485+
name: 'name
481486
isExported: Exported
482487
isNamespace: bool
483488
statements: Statement list
484489
comments: Comment list
485490
loc: Location
486491
} with
487-
interface ICommented<Module> with
492+
interface ICommented<Module<'name>> with
488493
member this.getComments() = this.comments
489494
member this.mapComments f = { this with comments = f this.comments }
490495

496+
and Module = Module<string>
497+
and Global = Module<unit>
498+
491499
and Export = {
492500
comments: Comment list
493501
clauses: ExportClause list

lib/Typer.fs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ module TyperContext =
125125
let ofSourceFileRoot source (ctx: TyperContext<'a, 's>) : TyperContext<'a, 's> =
126126
{ ctx with _currentSourceFile = source; _currentNamespace = [] }
127127

128+
let ofRoot (ctx: TyperContext<'a, 's>) : TyperContext<'a, 's> =
129+
{ ctx with _currentNamespace = [] }
130+
128131
let ofLocation (fullName: FullName) (ctx: TyperContext<'a, 's>) : TyperContext<'a, 's> =
129132
{ ctx with _currentSourceFile = fullName.source; _currentNamespace = List.rev fullName.name }
130133

@@ -948,6 +951,8 @@ module Statement =
948951
m.statements
949952
|> List.fold (go (m.name :: ns)) trie
950953
|> add ns m.name (Definition.Module m)
954+
| Global m ->
955+
m.statements |> List.fold (go []) trie
951956
stmts |> List.fold (go []) Trie.empty
952957

953958
let findTypesInStatements pred (stmts: Statement list) : 'a seq =
@@ -967,8 +972,8 @@ module Statement =
967972
for _, m in c.members do
968973
yield! findTypesInClassMember (pred (List.rev ns)) m
969974
}
970-
| Module m ->
971-
m.statements |> Seq.collect (go (m.name :: ns))
975+
| Module m -> m.statements |> Seq.collect (go (m.name :: ns))
976+
| Global m -> m.statements |> Seq.collect (go [])
972977
| Variable v -> findTypes (pred (List.rev ns)) v.typ
973978
| Function f ->
974979
seq {
@@ -1011,8 +1016,6 @@ module Statement =
10111016
let getKnownTypes (ctx: TyperContext<_, _>) stmts =
10121017
let (|Dummy|) _ = []
10131018
findTypesInStatements (fun _ -> function
1014-
| App (AIdent { fullName = fns }, ts, _) ->
1015-
Choice2Of2 ts, Some (fns |> List.map KnownType.Ident)
10161019
| Ident { fullName = fns } ->
10171020
Choice1Of2 true, Some (fns |> List.map KnownType.Ident)
10181021
| AnonymousInterface a ->
@@ -1026,7 +1029,7 @@ module Statement =
10261029
Choice1Of2 true, None
10271030
) stmts |> Seq.concat |> Set.ofSeq
10281031

1029-
let rec mapTypeWith overrideFunc mapping ctxOfChildNamespace ctx stmts =
1032+
let rec mapTypeWith overrideFunc mapping ctxOfChildNamespace ctxOfRoot ctx stmts =
10301033
let mapVariable (v: Variable) = { v with typ = mapping ctx v.typ }
10311034
let mapFunction f =
10321035
{ f with
@@ -1058,9 +1061,22 @@ module Statement =
10581061
overrideFunc
10591062
mapping
10601063
ctxOfChildNamespace
1064+
ctxOfRoot
10611065
(ctx |> ctxOfChildNamespace m.name)
10621066
m.statements
10631067
}
1068+
| Global m ->
1069+
Global {
1070+
m with
1071+
statements =
1072+
mapTypeWith
1073+
overrideFunc
1074+
mapping
1075+
ctxOfChildNamespace
1076+
ctxOfRoot
1077+
(ctx |> ctxOfRoot)
1078+
m.statements
1079+
}
10641080
| UnknownStatement u -> UnknownStatement u
10651081
| FloatingComment c -> FloatingComment c
10661082
| Pattern (ImmediateInstance (i, v)) -> Pattern (ImmediateInstance (mapInClass mapping ctx i, mapVariable v))
@@ -1077,7 +1093,7 @@ module Statement =
10771093
mapTypeWith (fun _ _ -> None) mapping ctxOfChildNamespace ctx stmts
10781094

10791095
let resolveErasedTypes (ctx: TyperContext<#TyperOptions, _>) (stmts: Statement list) =
1080-
mapType resolveErasedType TyperContext.ofChildNamespace ctx stmts
1096+
mapType resolveErasedType TyperContext.ofChildNamespace TyperContext.ofRoot ctx stmts
10811097

10821098
let mapIdent f stmts =
10831099
let orf _ = function
@@ -1094,7 +1110,7 @@ module Statement =
10941110
| x -> x
10951111
Import { i with clauses = i.clauses |> List.map g } |> Some
10961112
| _ -> None
1097-
mapTypeWith orf (fun () -> mapIdent f) (fun _ -> id) () stmts
1113+
mapTypeWith orf (fun () -> mapIdent f) (fun _ -> id) id () stmts
10981114

10991115
type [<RequireQualifiedAccess>] Typeofable = Number | String | Boolean | Symbol | BigInt
11001116
module TypeofableType =
@@ -1690,7 +1706,7 @@ let replaceFunctions (ctx: #IContext<#TyperOptions>) (stmts: Statement list) =
16901706
Function { name = name; typ = typ; typeParams = typrms;
16911707
isExported = isExported; accessibility = accessibility; comments = comments; loc = loc } |> Some
16921708
| _ -> None
1693-
Statement.mapTypeWith goStatement goType (fun _ x -> x) ctx stmts
1709+
Statement.mapTypeWith goStatement goType (fun _ x -> x) id ctx stmts
16941710

16951711
let private createRootContextForTyper (srcs: SourceFile list) (baseCtx: IContext<'Options>) : TyperContext<'Options, unit> =
16961712
let info =

0 commit comments

Comments
 (0)