@next-core/build-next-bricks 1.10.0 → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next-core/build-next-bricks",
3
- "version": "1.10.0",
3
+ "version": "1.11.0",
4
4
  "description": "Build next bricks",
5
5
  "homepage": "https://github.com/easyops-cn/next-core/tree/v3/packages/build-next-bricks",
6
6
  "license": "GPL-3.0",
@@ -49,7 +49,7 @@
49
49
  "webpack": "^5.84.1"
50
50
  },
51
51
  "devDependencies": {
52
- "@next-core/brick-manifest": "^0.3.0"
52
+ "@next-core/brick-manifest": "^0.4.0"
53
53
  },
54
- "gitHead": "14b533269133db07014b416199d988b0025f2b3c"
54
+ "gitHead": "93ee03d986df5388568ed2ea424833718d6fc7eb"
55
55
  }
@@ -0,0 +1,377 @@
1
+ // @ts-check
2
+ import { parseTypeComment } from "./makeBrickManifest.js";
3
+
4
+ /** @typedef {import("@babel/types").Node} Node */
5
+ /** @typedef {import("@next-core/brick-manifest").Declaration} Declaration */
6
+ /** @typedef {import("@next-core/brick-manifest").Annotation} Annotation */
7
+ /** @typedef {import("@next-core/brick-manifest").AnnotationTypeParameterDeclaration} AnnotationTypeParameterDeclaration */
8
+ /** @typedef {import("@next-core/brick-manifest").AnnotationTypeParameterInstantiation} AnnotationTypeParameterInstantiation */
9
+ /** @typedef {import("@next-core/brick-manifest").AnnotationExpressionWithTypeArguments} AnnotationExpressionWithTypeArguments */
10
+ /** @typedef {import("@next-core/brick-manifest").AnnotationEnumMember} AnnotationEnumMember */
11
+ /** @typedef {import("@next-core/brick-manifest").AnnotationTypeParameter} AnnotationTypeParameter */
12
+ /** @typedef {import("@next-core/brick-manifest").AnnotationPropertySignature} AnnotationPropertySignature */
13
+
14
+ /**
15
+ * @param {Node | null | undefined} node
16
+ * @param {string} source
17
+ * @param {Set<string>} usedReferences
18
+ * @returns {Declaration | undefined}
19
+ */
20
+ export default function getTypeDeclaration(node, source, usedReferences) {
21
+ switch (node.type) {
22
+ case "TSInterfaceDeclaration":
23
+ // interface A { ... }
24
+ return {
25
+ type: "interface",
26
+ name: node.id.name,
27
+ typeParameters:
28
+ /** @type {AnnotationTypeParameterDeclaration | undefined} */
29
+ (getTypeAnnotation(node.typeParameters, source, usedReferences)),
30
+ body: node.body.body.map(
31
+ (item) =>
32
+ /** @type {AnnotationPropertySignature} */
33
+ (getTypeAnnotation(item, source, usedReferences))
34
+ ),
35
+ extends:
36
+ /** @type {AnnotationExpressionWithTypeArguments[] | undefined} */
37
+ (
38
+ node.extends?.map((item) =>
39
+ getTypeAnnotation(item, source, usedReferences)
40
+ )
41
+ ),
42
+ ...parseTypeComment(node, source),
43
+ };
44
+
45
+ case "TSTypeAliasDeclaration":
46
+ // type A = B
47
+ return {
48
+ type: "typeAlias",
49
+ name: node.id.name,
50
+ typeParameters:
51
+ /** @type {AnnotationTypeParameterDeclaration | undefined} */
52
+ (getTypeAnnotation(node.typeParameters, source, usedReferences)),
53
+ annotation: getTypeAnnotation(
54
+ node.typeAnnotation,
55
+ source,
56
+ usedReferences
57
+ ),
58
+ ...parseTypeComment(node, source),
59
+ };
60
+
61
+ case "TSEnumDeclaration":
62
+ // enum A { ... }
63
+ return {
64
+ type: "enum",
65
+ name: node.id.name,
66
+ members:
67
+ /** @type {AnnotationEnumMember[] | undefined} */
68
+ (
69
+ node.members.map((item) =>
70
+ getTypeAnnotation(item, source, usedReferences)
71
+ )
72
+ ),
73
+ ...parseTypeComment(node, source),
74
+ };
75
+ }
76
+ }
77
+
78
+ /**
79
+ * @param {Node | null | undefined} entryNode
80
+ * @param {string} source
81
+ * @param {Set<string>} usedReferences
82
+ * @returns {Annotation | undefined}
83
+ */
84
+ export function getTypeAnnotation(entryNode, source, usedReferences) {
85
+ /**
86
+ * @param {Node | null | undefined} node
87
+ * @param {Boolean =} isRef
88
+ * @returns {Annotation | undefined}
89
+ */
90
+ function get(node, isRef) {
91
+ if (!node) {
92
+ return;
93
+ }
94
+ switch (node.type) {
95
+ case "TSEnumMember":
96
+ // enum A { b = "B", c = "C" }
97
+ // ^^^^^^^
98
+ return {
99
+ type: "enumMember",
100
+ id: get(node.id),
101
+ initializer: get(node.initializer),
102
+ };
103
+
104
+ case "TSTypeAnnotation":
105
+ // let a: MyType
106
+ // ^^^^^^
107
+ return get(node.typeAnnotation);
108
+
109
+ case "TSTypeReference":
110
+ // type A = B
111
+ // ^
112
+ return {
113
+ type: "reference",
114
+ typeName: get(node.typeName, true),
115
+ typeParameters:
116
+ /** @type {AnnotationTypeParameterInstantiation | undefined} */
117
+ (get(node.typeParameters)),
118
+ };
119
+
120
+ case "TSQualifiedName":
121
+ // type A = B.C
122
+ // ^^^
123
+ return {
124
+ type: "qualifiedName",
125
+ left: get(node.left, true),
126
+ right: get(node.right),
127
+ };
128
+
129
+ case "TSUnionType":
130
+ // type A = string | number
131
+ // ^^^^^^^^^^^^^^^
132
+ return {
133
+ type: "union",
134
+ types: node.types.map((item) => get(item)),
135
+ };
136
+
137
+ case "TSArrayType":
138
+ // type A = string[]
139
+ // ^^^^^^^^
140
+ return {
141
+ type: "array",
142
+ elementType: get(node.elementType),
143
+ };
144
+
145
+ case "TSTupleType":
146
+ // type A = [string, number]
147
+ // ^^^^^^^^^^^^^^^^
148
+ return {
149
+ type: "tuple",
150
+ elementTypes: node.elementTypes.map((item) => get(item)),
151
+ };
152
+
153
+ case "TSNamedTupleMember":
154
+ // type A = [x: number, y: number]
155
+ // ^^^^^^^^^
156
+ return {
157
+ type: "namedTupleMember",
158
+ label: node.label.name,
159
+ optional: node.optional,
160
+ elementType: get(node.elementType),
161
+ };
162
+
163
+ case "TSIntersectionType":
164
+ // type A = B & C
165
+ // ^^^^^
166
+ return {
167
+ type: "intersection",
168
+ types: node.types.map((item) => get(item)),
169
+ };
170
+
171
+ case "TSTypeLiteral":
172
+ // type A = { prop: number }
173
+ // ^^^^^^^^^^^^^^^^
174
+ return {
175
+ type: "typeLiteral",
176
+ members: node.members.map((item) => get(item)),
177
+ };
178
+
179
+ case "TSPropertySignature":
180
+ // interface A { prop: number }
181
+ // ^^^^^^^^^^^^
182
+ return {
183
+ type: "propertySignature",
184
+ key: get(node.key),
185
+ annotation: get(node.typeAnnotation),
186
+ // initializer: get(node.initializer),
187
+ optional: node.optional,
188
+ computed: node.computed,
189
+ readonly: node.readonly,
190
+ kind: node.kind,
191
+ ...parseTypeComment(node, source),
192
+ };
193
+
194
+ case "TSMethodSignature":
195
+ // interface A { call(a: number): void }
196
+ // ^^^^^^^^^^^^^^^^^^^^^
197
+ return {
198
+ type: "methodSignature",
199
+ key: get(node.key),
200
+ typeParameters:
201
+ /** @type {AnnotationTypeParameterDeclaration | undefined} */
202
+ (get(node.typeParameters)),
203
+ parameters: node.parameters.map((item) => get(item)),
204
+ annotation: get(node.typeAnnotation),
205
+ optional: node.optional,
206
+ computed: node.computed,
207
+ kind: node.kind,
208
+ ...parseTypeComment(node, source),
209
+ };
210
+
211
+ case "TSIndexSignature":
212
+ // interface A { [k: string]: number }
213
+ // ^^^^^^^^^^^^^^^^^^^
214
+ return {
215
+ type: "indexSignature",
216
+ parameter: get(node.parameters[0]),
217
+ annotation: get(node.typeAnnotation),
218
+ ...parseTypeComment(node, source),
219
+ };
220
+
221
+ case "TSIndexedAccessType":
222
+ // type A = B["C"]
223
+ // ^^^^^^
224
+ return {
225
+ type: "indexedAccess",
226
+ objectType: get(node.objectType),
227
+ indexType: get(node.indexType),
228
+ };
229
+
230
+ case "TSTypeParameterDeclaration":
231
+ // interface A<T, P> {}
232
+ // ^^^^^^
233
+ return {
234
+ type: "typeParameterDeclaration",
235
+ params:
236
+ /** @type {AnnotationTypeParameter[]} */
237
+ (node.params.map((item) => get(item))),
238
+ };
239
+
240
+ case "TSTypeParameterInstantiation":
241
+ // type A = B<T, P>
242
+ // ^^^^^^
243
+ return {
244
+ type: "typeParameterInstantiation",
245
+ params: node.params.map((item) => get(item)),
246
+ };
247
+
248
+ case "TSTypeParameter":
249
+ // interface A<T extends string = "", P> {}
250
+ // ^^^^^^^^^^^^^^^^^^^^^
251
+ return {
252
+ type: "typeParameter",
253
+ name: node.name,
254
+ default: get(node.default),
255
+ constraint: get(node.constraint),
256
+ };
257
+
258
+ case "TSLiteralType":
259
+ return get(node.literal);
260
+
261
+ case "TSTypeOperator":
262
+ // type A = keyof B
263
+ // ^^^^^^^
264
+ return {
265
+ type: "typeOperator",
266
+ operator: node.operator,
267
+ annotation: get(node.typeAnnotation),
268
+ };
269
+
270
+ case "TSTypeQuery":
271
+ // type A = typeof B<T>
272
+ // ^^^^^^^^^^^
273
+ return {
274
+ type: "typeQuery",
275
+ exprName: get(node.exprName),
276
+ typeParameters:
277
+ /** @type {AnnotationTypeParameterInstantiation | undefined} */
278
+ (get(node.typeParameters)),
279
+ };
280
+
281
+ case "TSExpressionWithTypeArguments":
282
+ // interface A extends B<T>, C
283
+ // ^^^^
284
+ return {
285
+ type: "expressionWithTypeArguments",
286
+ expression: get(node.expression),
287
+ typeParameters:
288
+ /** @type {AnnotationTypeParameterInstantiation | undefined} */
289
+ (get(node.typeParameters)),
290
+ };
291
+
292
+ case "Identifier":
293
+ // interface A { call(a: number) => void }
294
+ // ^^^^^^^^^
295
+ if (isRef) {
296
+ usedReferences.add(node.name);
297
+ }
298
+ return {
299
+ type: "identifier",
300
+ name: node.name,
301
+ annotation: get(node.typeAnnotation),
302
+ };
303
+
304
+ case "RestElement":
305
+ // interface A { call(a, ...rest: unknown[]) => void }
306
+ // ^^^^^^^^^^^^^^^^^^
307
+ return {
308
+ type: "restElement",
309
+ argument: get(node.argument),
310
+ annotation: get(node.typeAnnotation),
311
+ };
312
+
313
+ case "TSFunctionType":
314
+ // interface A { call: (a: number) => void }
315
+ // ^^^^^^^^^^^^^^^^^^^
316
+ return {
317
+ type: "function",
318
+ typeParameters:
319
+ /** @type {AnnotationTypeParameterDeclaration | undefined} */
320
+ (get(node.typeParameters)),
321
+ parameters: node.parameters.map((item) => get(item)),
322
+ annotation: get(node.typeAnnotation),
323
+ };
324
+
325
+ case "StringLiteral":
326
+ case "NumericLiteral":
327
+ case "BooleanLiteral":
328
+ return {
329
+ type: "jsLiteral",
330
+ value: node.value,
331
+ };
332
+
333
+ case "NullLiteral":
334
+ return {
335
+ type: "jsLiteral",
336
+ value: null,
337
+ };
338
+
339
+ // <!-- Keywords start
340
+ case "TSAnyKeyword":
341
+ return { type: "keyword", value: "any" };
342
+ case "TSBooleanKeyword":
343
+ return { type: "keyword", value: "boolean" };
344
+ case "TSBigIntKeyword":
345
+ return { type: "keyword", value: "bigint" };
346
+ case "TSNullKeyword":
347
+ return { type: "keyword", value: "null" };
348
+ case "TSNumberKeyword":
349
+ return { type: "keyword", value: "number" };
350
+ case "TSObjectKeyword":
351
+ return { type: "keyword", value: "object" };
352
+ case "TSStringKeyword":
353
+ return { type: "keyword", value: "string" };
354
+ case "TSUndefinedKeyword":
355
+ return { type: "keyword", value: "undefined" };
356
+ case "TSUnknownKeyword":
357
+ return { type: "keyword", value: "unknown" };
358
+ case "TSVoidKeyword":
359
+ return { type: "keyword", value: "void" };
360
+ case "TSSymbolKeyword":
361
+ return { type: "keyword", value: "symbol" };
362
+ case "TSNeverKeyword":
363
+ return { type: "keyword", value: "never" };
364
+ // Keywords end -->
365
+
366
+ default:
367
+ return {
368
+ type: "unsupported",
369
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
370
+ // @ts-ignore
371
+ source: source.substring(node.start, node.end),
372
+ };
373
+ }
374
+ }
375
+
376
+ return get(entryNode);
377
+ }