|
| 1 | +module Glutinum.Converter.Reader.TypeQueryNode |
| 2 | + |
| 3 | +open Glutinum.Converter.GlueAST |
| 4 | +open Glutinum.Converter.Reader.Types |
| 5 | +open TypeScript |
| 6 | +open Fable.Core.JsInterop |
| 7 | +open Glutinum.Converter.Reader.Utils |
| 8 | +open FsToolkit.ErrorHandling |
| 9 | + |
| 10 | +let readTypeQueryNode |
| 11 | + (reader: ITypeScriptReader) |
| 12 | + (typeQueryNode: Ts.TypeQueryNode) |
| 13 | + = |
| 14 | + |
| 15 | + let checker = reader.checker |
| 16 | + let typ = checker.getTypeAtLocation !!typeQueryNode.exprName |
| 17 | + |
| 18 | + match typ.flags with |
| 19 | + | HasTypeFlags Ts.TypeFlags.Object -> |
| 20 | + // This is safe as both cases have a `kind` field |
| 21 | + let exprNameKind: Ts.SyntaxKind = typeQueryNode.exprName?kind |
| 22 | + |
| 23 | + match typ.getSymbol (), exprNameKind with |
| 24 | + | None, Ts.SyntaxKind.Identifier -> |
| 25 | + |
| 26 | + let exprName: Ts.Identifier = !!typeQueryNode.exprName |
| 27 | + |
| 28 | + result { |
| 29 | + let! aliasSymbol = |
| 30 | + checker.getSymbolAtLocation exprName |
| 31 | + |> Result.requireSome ( |
| 32 | + Report.readerError ( |
| 33 | + "type node (TypeQuery)", |
| 34 | + "Missing symbol", |
| 35 | + typeQueryNode |
| 36 | + ) |
| 37 | + ) |
| 38 | + |
| 39 | + let! declarations = |
| 40 | + aliasSymbol.declarations |
| 41 | + |> Result.requireSome ( |
| 42 | + Report.readerError ( |
| 43 | + "type node (TypeQuery)", |
| 44 | + "Missing declarations", |
| 45 | + typeQueryNode |
| 46 | + ) |
| 47 | + ) |
| 48 | + |
| 49 | + let! declaration = |
| 50 | + if declarations.Count <> 1 then |
| 51 | + Report.readerError ( |
| 52 | + |
| 53 | + "type node (TypeQuery)", |
| 54 | + "Expected exactly one declaration", |
| 55 | + typeQueryNode |
| 56 | + ) |
| 57 | + |> Error |
| 58 | + |
| 59 | + else |
| 60 | + Ok(declarations.[0]) |
| 61 | + |
| 62 | + let! variableDeclaration = |
| 63 | + match declaration.kind with |
| 64 | + | Ts.SyntaxKind.VariableDeclaration -> |
| 65 | + Ok(declaration :?> Ts.VariableDeclaration) |
| 66 | + |
| 67 | + | unsupported -> |
| 68 | + Report.readerError ( |
| 69 | + "type node (TypeQuery)", |
| 70 | + $"Unsupported declaration kind {unsupported.Name}", |
| 71 | + typeQueryNode |
| 72 | + ) |
| 73 | + |> Error |
| 74 | + |
| 75 | + let! typeNode = |
| 76 | + variableDeclaration.``type`` |
| 77 | + |> Result.requireSome ( |
| 78 | + Report.readerError ( |
| 79 | + "type node (TypeQuery)", |
| 80 | + "Missing type", |
| 81 | + typeQueryNode |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + match typeNode.kind with |
| 86 | + | Ts.SyntaxKind.TypeOperator -> |
| 87 | + let typeOperatorNode = typeNode :?> Ts.TypeOperatorNode |
| 88 | + return reader.ReadTypeOperatorNode typeOperatorNode |
| 89 | + |
| 90 | + | unsupported -> |
| 91 | + return! |
| 92 | + Report.readerError ( |
| 93 | + "type node (TypeQuery)", |
| 94 | + $"Unsupported declaration kind {unsupported.Name}", |
| 95 | + typeQueryNode |
| 96 | + ) |
| 97 | + |> Error |
| 98 | + |
| 99 | + } |
| 100 | + |> function |
| 101 | + | Ok glueType -> glueType |
| 102 | + | Error warning -> |
| 103 | + reader.Warnings.Add warning |
| 104 | + GlueType.Discard |
| 105 | + |
| 106 | + | None, _ -> |
| 107 | + let warning = |
| 108 | + Report.readerError ( |
| 109 | + "type node (TypeQuery)", |
| 110 | + "Expected an Identifier", |
| 111 | + typeQueryNode |
| 112 | + ) |
| 113 | + |
| 114 | + reader.Warnings.Add warning |
| 115 | + GlueType.Primitive GluePrimitive.Any |
| 116 | + |
| 117 | + | Some symbol, _ -> |
| 118 | + // Try to find the declaration of the type, to get more information about it |
| 119 | + match symbol.declarations with |
| 120 | + | Some declarations -> |
| 121 | + let declaration = declarations.[0] |
| 122 | + |
| 123 | + match declaration.kind with |
| 124 | + | Ts.SyntaxKind.ClassDeclaration -> |
| 125 | + { |
| 126 | + Name = symbol.name |
| 127 | + Constructors = [] |
| 128 | + Members = [] |
| 129 | + TypeParameters = [] |
| 130 | + HeritageClauses = [] |
| 131 | + } |
| 132 | + |> GlueType.ClassDeclaration |
| 133 | + |
| 134 | + // We don't support TypeQuery for ModuleDeclaration yet |
| 135 | + // See https://github.com/glutinum-org/cli/issues/70 for a possible solution |
| 136 | + | Ts.SyntaxKind.ModuleDeclaration -> GlueType.Discard |
| 137 | + | _ -> reader.ReadNode declaration |
| 138 | + |
| 139 | + | None -> GlueType.Primitive GluePrimitive.Any |
| 140 | + |
| 141 | + | HasTypeFlags Ts.TypeFlags.String -> |
| 142 | + GlueType.Primitive GluePrimitive.String |
| 143 | + |
| 144 | + | HasTypeFlags Ts.TypeFlags.Number -> |
| 145 | + GlueType.Primitive GluePrimitive.Number |
| 146 | + |
| 147 | + | HasTypeFlags Ts.TypeFlags.Boolean -> GlueType.Primitive GluePrimitive.Bool |
| 148 | + |
| 149 | + | HasTypeFlags Ts.TypeFlags.Any -> GlueType.Primitive GluePrimitive.Any |
| 150 | + |
| 151 | + | HasTypeFlags Ts.TypeFlags.Void -> GlueType.Primitive GluePrimitive.Unit |
| 152 | + |
| 153 | + | _ -> GlueType.Primitive GluePrimitive.Any |
0 commit comments