# Types.ts

`

1<comment>/**</comment>

2<comment> * Node type declarations</comment>

3<comment> */</comment>

4<comment>/** */</comment>

5

6import { Log } from <quote>'hsutil'</quote>; const log = new Log(<quote>'Types'</quote>);

7import { DocsNode } from <quote>'./Nodes'</quote>;

8import { m, Vnode} from <quote>'hslayout'</quote>;

9import { libLinkByPath } from <quote>'./NodesDisplay'</quote>;

10import { titleArr } from <quote>'./NodesDisplay'</quote>;

11

12interface DocsGenericType {

13 type: string;

14 [field:string]: any;

15}

16

17export interface DocsNamedType extends DocsGenericType {

18 type: string;

19 name: string;

20}

21

22export interface DocsReferenceIdType extends DocsNamedType {

23 id: number;

24}

25

26export interface DocsConstrainedType extends DocsNamedType {

27 constraint: {

28 operator: string;

29 type: string;

30 target: DocsNamedType;

31 };

32}

33

34

35<comment>//------------- Type class definitions -------------------------------------------</comment>

36<comment></comment>

37export class DocsType implements DocsReferenceIdType {

38 static makeType(mdlType:DocsGenericType, node:DocsNode) {

39 switch (mdlType.type) {

40 case <quote>'intrinsic'</quote>: return new DocsIntrinsicType(mdlType, node);

41 case <quote>'array'</quote>: return new DocsArrayType(mdlType, node);

42 case <quote>'reflection'</quote>: return new DocsReflectionType(mdlType, node);

43 case <quote>'stringLiteral'</quote>: return new DocsStringLiteralType(mdlType, node);

44 case <quote>'reference'</quote>: return new DocsReferenceType(mdlType, node);

45 case <quote>'tuple'</quote>: return new DocsTupleType(mdlType, node);

46 case <quote>'union'</quote>: return new DocsUnionType(mdlType, node);

47 case <quote>'typeParameter'</quote>: return new DocsTypeParameterType(mdlType, node);

48 case <quote>'typeOperator'</quote>: return new DocsTypeOperatorType(mdlType, node);

49 case <quote>'query'</quote>: return new DocsQueryType(mdlType, node);

50 case <quote>'indexedAccess'</quote>: return new DocsIndexedAccessType(mdlType, node);

51 case <quote>'unknown'</quote>: return new DocsUnknownType(mdlType, node);

52 default: log.warn(`unknown type <quote>'${mdlType.type}'</quote> for ${node.fullPath} in makeType`);

53 }

54 }

55 type: string;

56 name: string;

57 node: DocsNode;

58 id: number;

59 declaration: DocsNode;

60 constructor(mdlType:DocsGenericType, node:DocsNode) {

61 this.type = mdlType.type;

62 this.id = mdlType.id;

63 this.name = mdlType.name;

64 this.node = node;

65 }

66

67 mType(parent:DocsNode) { return m(<quote>'span.hsdocs_type_unknown'</quote>, <quote>'default type'</quote>); }

68}

69

70class DocsIntrinsicType extends DocsType {

71 name: string;

72 constructor(mdlType:DocsGenericType, node:DocsNode) {

73 super(mdlType, node);

74 this.name = mdlType.name;

75 }

76 mType(parent:DocsNode) {

77 <comment>// return m(<quote>'span.hsdocs_type_intrinsic'</quote>, this.id? libLinkByPath(this.node.lib, <quote>''</quote>+this.id, this.name) : parent.getName()); </comment>

78<comment></comment> return m(<quote>'span.hsdocs_type_intrinsic'</quote>, this.id? libLinkByPath(this.node.lib, <quote>''</quote>+this.id, this.name) : this.name);

79 }

80}

81

82class DocsReferenceType extends DocsType {

83 typeArguments: DocsType[];

84 constructor(mdlType:DocsGenericType, node:DocsNode) {

85 super(mdlType, node);

86 this.typeArguments = !mdlType.typeArguments? [] : mdlType.typeArguments.map((t:DocsNamedType) => DocsType.makeType(t, node));

87 }

88

89 mType(parent:DocsNode) {

90 let refRes = [this.id? libLinkByPath(this.node.lib, <quote>''</quote>+this.id, this.name) : this.name];

91 if (this.typeArguments.length) {

92 refRes.push(<quote>'<'</quote>);

93 refRes.push(...this.typeArguments.map(t => t.mType(parent)));

94 refRes.push(<quote>'>'</quote>);

95 }

96 return m(<quote>'span.hsdocs_type_reference'</quote>, refRes);

97 }

98}

99

100class DocsUnknownType extends DocsType {

101 name: string;

102 constructor(mdlType:DocsGenericType, node:DocsNode) {

103 super(mdlType, node);

104 this.name = mdlType.name;

105 }

106 mType() { return m(<quote>'span.hsdocs_type_unknown'</quote>, <quote>'unknown type'</quote>); }

107}

108

109class DocsStringLiteralType extends DocsType {

110 value: string;

111 constructor(mdlType:DocsGenericType, node:DocsNode) {

112 super(mdlType, node);

113 this.value = mdlType.value;

114 }

115 mType() {

116 return m(<quote>'span.hsdocs_type_string_literal'</quote>, this.type);

117 }

118}

119

120class DocsArrayType extends DocsType {

121 elementType: DocsType;

122 constructor(mdlType:DocsGenericType, node:DocsNode) {

123 super(mdlType, node);

124 this.elementType = DocsType.makeType(mdlType.elementType, node);

125 }

126 mType(parent:DocsNode) {

127 return m(<quote>'span.hsdocs_type_array'</quote>, [<quote>'Array<'</quote>, this.elementType.mType(parent), <quote>'>'</quote>]);

128 }

129}

130

131class DocsTypeParameterType extends DocsType {

132 constraint: DocsNamedType|DocsTypeOperatorType;

133 constructor(mdlType:DocsGenericType, node:DocsNode) {

134 super(mdlType, node);

135 this.constraint = mdlType.constraint;

136 }

137 mType() {

138 return !this.constraint?

139 m(<quote>'span.hsdocs_type_typeparameter'</quote>, this.name) : this.constraint.name?

140 m(<quote>'span.hsdocs_type_typeparameter'</quote>, `${this.constraint.name}`)

141 : m(<quote>'span.hsdocs_type_typeparameter'</quote>, `${this.constraint.operator} ${this.constraint.target.name}`);

142 }

143}

144

145class DocsTypeOperatorType extends DocsType {

146 operator: string; <comment>// <quote>"keyof"</quote>;</comment>

147<comment></comment> target: DocsNamedType;

148 constructor(mdlType:DocsGenericType, node:DocsNode) {

149 super(mdlType, node);

150 this.operator = mdlType.operator;

151 this.target = mdlType.target;

152 }

153 mType() {

154 return m(<quote>'span.hsdocs_type_typeoperator'</quote>, `${this.operator} ${this.target.name}`);

155 }

156}

157

158class DocsReflectionType extends DocsType {

159 constructor(mdlType:DocsGenericType, node:DocsNode) {

160 super(mdlType, node);

161 if (mdlType.declaration) {

162 const decl:DocsNode = this.declaration = DocsNode.traverse(mdlType.declaration, node.fullPath);

163 <comment>// if (decl.children) {</comment>

164<comment></comment> <comment>// this.declaration.children = decl.children.map((c:json) => DocsNode.traverse(c, node.fullPath));</comment>

165<comment></comment> <comment>// }</comment>

166<comment></comment> <comment>// if (decl.signatures) {</comment>

167<comment></comment> <comment>// this.declaration.signatures = decl.signatures.map((c:json) => DocsNode.traverse(c, node.fullPath)));</comment>

168<comment></comment> <comment>// }</comment>

169<comment></comment> <comment>// if (decl.indexSignature) {</comment>

170<comment></comment> <comment>// decl.indexSignature = decl.indexSignature.map(c => <DocsIndexSignature>DocsNode.traverse(c, node.fullPath));</comment>

171<comment></comment> <comment>// }</comment>

172<comment></comment> }

173 }

174

175 mType(parent:DocsNode) {

176 const dec = this.declaration;

177 const rflRes = !dec? <quote>'UNKNOWN'</quote> :

178 dec.children? [

179 <quote>'{ '</quote>,

180 ...dec.children.map((c:any, i:number) => `${c.name}: ${c.type.mType(parent)}`).join(<quote>', '</quote>),

181 <quote>' }'</quote>

182 ] : dec.getSignatures()? [

183 ...dec.getSignatures().map(s => titleArr(s))

184 ] : dec.kindString

185 ;

186 return m(<quote>'span.hsdocs_type_reflection'</quote>, rflRes);

187 }

188}

189

190class DocsTupleType extends DocsType {

191 elements = <DocsType[]>[];

192 constructor(mdlType:DocsGenericType, node:DocsNode) {

193 super(mdlType, node);

194 mdlType.elements.forEach((e:DocsType) => this.elements.push(DocsType.makeType(e, node)));

195 }

196 mType(parent:DocsNode) {

197 return m(<quote>'span.hsdocs_type_tuple'</quote>, [

198 <quote>'[ '</quote>,

199 ...this.elements.map((e:any, i:number) => [i>0?<quote>', '</quote>:undefined, e.mType(parent)]),

200 <quote>' ]'</quote>

201 ]);

202 }

203}

204

205class DocsUnionType extends DocsType {

206 types = <DocsReferenceIdType[]>[];

207 constructor(mdlType:DocsGenericType, node:DocsNode) {

208 super(mdlType, node);

209 mdlType.types.forEach((t:DocsReferenceIdType) => this.types.push(DocsType.makeType(t, node)));

210 }

211

212 mType(parent:DocsNode):Vnode {

213 return m(<quote>'span.hsdocs_type_union'</quote>, [...this.types.map((t:any, i:number) => m(<quote>'span'</quote>, [i>0?<quote>' | '</quote>:<quote>''</quote>, t.mType(parent)]))]);

214 }

215}

216

217class DocsQueryType extends DocsType {

218 queryType:DocsReferenceIdType;

219 constructor(mdlType:DocsGenericType, node:DocsNode) {

220 super(mdlType, node);

221 this.queryType = mdlType.queryType;

222 }

223 mType() {

224 return m(<quote>'span.hsdocs_type_query'</quote>, `typeof ${this.queryType.name}`);

225 }

226}

227

228class DocsIndexedAccessType extends DocsType {

229 indexType: DocsConstrainedType;

230 objectType:DocsNamedType;

231 constructor(mdlType:DocsGenericType, node:DocsNode) {

232 super(mdlType, node);

233 this.indexType = mdlType.indexType;

234 this.objectType = mdlType.objectType;

235 }

236 mType() {

237 return m(<quote>'span.hsdocs_type_indexed_access'</quote>, this.objectType.name);

238 }

239}

240

`