@teambit/typescript 0.0.738 → 0.0.739
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/dist/exceptions/transformer-not-found.d.ts +2 -1
- package/dist/exceptions/transformer-not-found.js +3 -2
- package/dist/exceptions/transformer-not-found.js.map +1 -1
- package/dist/schema-extractor-context.d.ts +4 -1
- package/dist/schema-extractor-context.js +20 -2
- package/dist/schema-extractor-context.js.map +1 -1
- package/dist/transformers/class-deceleration.js +9 -1
- package/dist/transformers/class-deceleration.js.map +1 -1
- package/dist/transformers/enum-declaration.d.ts +10 -0
- package/dist/transformers/enum-declaration.js +60 -0
- package/dist/transformers/enum-declaration.js.map +1 -0
- package/dist/transformers/index.d.ts +1 -0
- package/dist/transformers/index.js +16 -0
- package/dist/transformers/index.js.map +1 -1
- package/dist/transformers/utils/get-params.js +53 -7
- package/dist/transformers/utils/get-params.js.map +1 -1
- package/dist/transformers/utils/parse-type-from-quick-info.d.ts +1 -0
- package/dist/transformers/utils/parse-type-from-quick-info.js +12 -3
- package/dist/transformers/utils/parse-type-from-quick-info.js.map +1 -1
- package/dist/transformers/utils/type-node-to-schema.js +34 -4
- package/dist/transformers/utils/type-node-to-schema.js.map +1 -1
- package/dist/typescript.extractor.d.ts +7 -1
- package/dist/typescript.extractor.js +30 -9
- package/dist/typescript.extractor.js.map +1 -1
- package/dist/typescript.main.runtime.d.ts +11 -2
- package/dist/typescript.main.runtime.js +17 -6
- package/dist/typescript.main.runtime.js.map +1 -1
- package/exceptions/transformer-not-found.ts +4 -2
- package/package-tar/teambit-typescript-0.0.739.tgz +0 -0
- package/package.json +12 -11
- package/{preview-1653227849497.js → preview-1653362849981.js} +2 -2
- package/transformers/class-deceleration.ts +4 -1
- package/transformers/enum-declaration.ts +20 -0
- package/transformers/index.ts +1 -0
- package/transformers/utils/get-params.ts +53 -9
- package/transformers/utils/parse-type-from-quick-info.ts +9 -1
- package/transformers/utils/type-node-to-schema.ts +37 -4
- package/package-tar/teambit-typescript-0.0.738.tgz +0 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import ts, { Node, EnumDeclaration } from 'typescript';
|
|
2
|
+
import { EnumSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
3
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
4
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
5
|
+
import { ExportIdentifier } from '../export-identifier';
|
|
6
|
+
|
|
7
|
+
export class EnumDeclarationTransformer implements SchemaTransformer {
|
|
8
|
+
predicate(node: Node) {
|
|
9
|
+
return node.kind === ts.SyntaxKind.EnumDeclaration;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async getIdentifiers(node: EnumDeclaration): Promise<ExportIdentifier[]> {
|
|
13
|
+
return [new ExportIdentifier(node.name.getText(), node.getSourceFile().fileName)];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async transform(enumDec: EnumDeclaration, context: SchemaExtractorContext) {
|
|
17
|
+
const members = enumDec.members.map((member) => member.name.getText());
|
|
18
|
+
return new EnumSchema(context.getLocation(enumDec), enumDec.name.getText(), members);
|
|
19
|
+
}
|
|
20
|
+
}
|
package/transformers/index.ts
CHANGED
|
@@ -13,3 +13,4 @@ export { LiteralTypeTransformer } from './literal-type';
|
|
|
13
13
|
export { IndexSignature } from './index-signature';
|
|
14
14
|
export { InterfaceDeclarationTransformer } from './interface-declaration';
|
|
15
15
|
export { MethodSignatureTransformer } from './method-signature';
|
|
16
|
+
export { EnumDeclarationTransformer } from './enum-declaration';
|
|
@@ -1,6 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
InferenceTypeSchema,
|
|
3
|
+
ParameterSchema,
|
|
4
|
+
TupleTypeSchema,
|
|
5
|
+
TypeLiteralSchema,
|
|
6
|
+
SchemaNode,
|
|
7
|
+
} from '@teambit/semantics.entities.semantic-schema';
|
|
2
8
|
import pMapSeries from 'p-map-series';
|
|
3
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
SyntaxKind,
|
|
11
|
+
ParameterDeclaration,
|
|
12
|
+
NodeArray,
|
|
13
|
+
isIdentifier,
|
|
14
|
+
BindingElement,
|
|
15
|
+
ArrayBindingElement,
|
|
16
|
+
} from 'typescript';
|
|
4
17
|
import { SchemaExtractorContext } from '../../schema-extractor-context';
|
|
5
18
|
import { parseTypeFromQuickInfo } from './parse-type-from-quick-info';
|
|
6
19
|
import { typeNodeToSchema } from './type-node-to-schema';
|
|
@@ -12,22 +25,53 @@ export async function getParams(
|
|
|
12
25
|
return pMapSeries(parameterNodes, async (param) => {
|
|
13
26
|
return new ParameterSchema(
|
|
14
27
|
context.getLocation(param),
|
|
15
|
-
param
|
|
28
|
+
getParamName(param),
|
|
16
29
|
await getParamType(param, context),
|
|
17
30
|
param.initializer ? param.initializer.getText() : undefined
|
|
18
31
|
);
|
|
19
32
|
});
|
|
20
33
|
}
|
|
21
34
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
35
|
+
function getParamName(param: ParameterDeclaration): string {
|
|
36
|
+
if (isIdentifier(param.name)) {
|
|
37
|
+
return param.name.getText();
|
|
38
|
+
}
|
|
39
|
+
// it's binding pattern, either an array or an object
|
|
40
|
+
const elements = param.name.elements.map((elem) => elem.getText());
|
|
41
|
+
const elementsStr = elements.join(', ');
|
|
42
|
+
if (param.name.kind === SyntaxKind.ArrayBindingPattern) {
|
|
43
|
+
return `[ ${elementsStr} ]`;
|
|
44
|
+
}
|
|
45
|
+
// it's an object binding
|
|
46
|
+
return `{ ${elementsStr} }`;
|
|
47
|
+
}
|
|
48
|
+
|
|
25
49
|
async function getParamType(param: ParameterDeclaration, context: SchemaExtractorContext): Promise<SchemaNode> {
|
|
26
50
|
if (param.type) {
|
|
27
51
|
const type = param.type;
|
|
28
52
|
return typeNodeToSchema(type, context);
|
|
29
53
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
54
|
+
if (isIdentifier(param.name)) {
|
|
55
|
+
const info = await context.getQuickInfo(param.name);
|
|
56
|
+
const parsed = parseTypeFromQuickInfo(info);
|
|
57
|
+
return new InferenceTypeSchema(context.getLocation(param), parsed);
|
|
58
|
+
}
|
|
59
|
+
// it's binding pattern, either an array or an object
|
|
60
|
+
if (param.name.kind === SyntaxKind.ArrayBindingPattern) {
|
|
61
|
+
const elements = await pMapSeries(param.name.elements, async (elem: ArrayBindingElement) => {
|
|
62
|
+
const info = await context.getQuickInfo(elem);
|
|
63
|
+
const parsed = parseTypeFromQuickInfo(info);
|
|
64
|
+
return new InferenceTypeSchema(context.getLocation(param), parsed);
|
|
65
|
+
});
|
|
66
|
+
return new TupleTypeSchema(context.getLocation(param), elements);
|
|
67
|
+
}
|
|
68
|
+
if (param.name.kind === SyntaxKind.ObjectBindingPattern) {
|
|
69
|
+
const elements = await pMapSeries(param.name.elements, async (elem: BindingElement) => {
|
|
70
|
+
const info = await context.getQuickInfo(elem.name);
|
|
71
|
+
const parsed = parseTypeFromQuickInfo(info);
|
|
72
|
+
return new InferenceTypeSchema(context.getLocation(param), parsed, elem.name.getText());
|
|
73
|
+
});
|
|
74
|
+
return new TypeLiteralSchema(context.getLocation(param), elements);
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`unknown param type`);
|
|
33
77
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// eslint-disable-next-line import/no-unresolved
|
|
2
2
|
import protocol from 'typescript/lib/protocol';
|
|
3
3
|
|
|
4
|
+
export const UNRESOLVED = '<<unresolved>>';
|
|
5
|
+
|
|
4
6
|
/**
|
|
5
7
|
* try to parse the type from the quickinfo.
|
|
6
8
|
* this is an error-prone process, we do our best here.
|
|
@@ -49,7 +51,13 @@ export function parseTypeFromQuickInfo(quickInfo: protocol.QuickInfoResponse | u
|
|
|
49
51
|
case 'function': {
|
|
50
52
|
const split = displayString.split('): ');
|
|
51
53
|
if (split.length !== 2) {
|
|
52
|
-
|
|
54
|
+
// it's hard to determine where the return-type is. so it's better to show unresolved.
|
|
55
|
+
// maybe, in the UI, in this case, it's best to show the signature.
|
|
56
|
+
// e.g.
|
|
57
|
+
// (method) IssuesList.getIssue<T extends ComponentIssue>(IssueClass: {
|
|
58
|
+
// new (): T;
|
|
59
|
+
// }): T | undefined
|
|
60
|
+
return UNRESOLVED;
|
|
53
61
|
}
|
|
54
62
|
return split[1].trim();
|
|
55
63
|
}
|
|
@@ -11,9 +11,14 @@ import {
|
|
|
11
11
|
IntersectionTypeNode,
|
|
12
12
|
UnionTypeNode,
|
|
13
13
|
TypeLiteralNode,
|
|
14
|
+
ParenthesizedTypeNode,
|
|
15
|
+
TypePredicateNode,
|
|
16
|
+
isIdentifier,
|
|
17
|
+
IndexedAccessTypeNode,
|
|
14
18
|
} from 'typescript';
|
|
15
19
|
import {
|
|
16
20
|
SchemaNode,
|
|
21
|
+
TypeRefSchema,
|
|
17
22
|
TypeIntersectionSchema,
|
|
18
23
|
TypeUnionSchema,
|
|
19
24
|
TypeLiteralSchema,
|
|
@@ -24,6 +29,9 @@ import {
|
|
|
24
29
|
TypeOperatorSchema,
|
|
25
30
|
TupleTypeSchema,
|
|
26
31
|
FunctionLikeSchema,
|
|
32
|
+
ParenthesizedTypeSchema,
|
|
33
|
+
TypePredicateSchema,
|
|
34
|
+
IndexedAccessSchema,
|
|
27
35
|
} from '@teambit/semantics.entities.semantic-schema';
|
|
28
36
|
import pMapSeries from 'p-map-series';
|
|
29
37
|
import { SchemaExtractorContext } from '../../schema-extractor-context';
|
|
@@ -56,16 +64,19 @@ export async function typeNodeToSchema(node: TypeNode, context: SchemaExtractorC
|
|
|
56
64
|
return typeOperator(node as TypeOperatorNode, context);
|
|
57
65
|
case SyntaxKind.TupleType:
|
|
58
66
|
return tupleType(node as TupleTypeNode, context);
|
|
67
|
+
case SyntaxKind.ParenthesizedType:
|
|
68
|
+
return parenthesizedType(node as ParenthesizedTypeNode, context);
|
|
59
69
|
case SyntaxKind.TypePredicate:
|
|
70
|
+
return typePredicate(node as TypePredicateNode, context);
|
|
71
|
+
case SyntaxKind.IndexedAccessType:
|
|
72
|
+
return indexedAccessType(node as IndexedAccessTypeNode, context);
|
|
60
73
|
case SyntaxKind.ConstructorType:
|
|
61
74
|
case SyntaxKind.NamedTupleMember:
|
|
62
75
|
case SyntaxKind.OptionalType:
|
|
63
76
|
case SyntaxKind.RestType:
|
|
64
77
|
case SyntaxKind.ConditionalType:
|
|
65
78
|
case SyntaxKind.InferType:
|
|
66
|
-
case SyntaxKind.ParenthesizedType:
|
|
67
79
|
case SyntaxKind.ThisType:
|
|
68
|
-
case SyntaxKind.IndexedAccessType:
|
|
69
80
|
case SyntaxKind.MappedType:
|
|
70
81
|
case SyntaxKind.TemplateLiteralType:
|
|
71
82
|
case SyntaxKind.TemplateLiteralTypeSpan:
|
|
@@ -82,10 +93,10 @@ export async function typeNodeToSchema(node: TypeNode, context: SchemaExtractorC
|
|
|
82
93
|
case SyntaxKind.JSDocNamepathType:
|
|
83
94
|
case SyntaxKind.JSDocSignature:
|
|
84
95
|
case SyntaxKind.JSDocTypeLiteral:
|
|
85
|
-
throw new Error(`TypeNode
|
|
96
|
+
throw new Error(`TypeNode ${node.kind} (probably ${SyntaxKind[node.kind]}) was not implemented yet.
|
|
86
97
|
context: ${node.getText()}`);
|
|
87
98
|
default:
|
|
88
|
-
throw new Error(`Node
|
|
99
|
+
throw new Error(`Node ${node.kind} (probably ${SyntaxKind[node.kind]}) is not a TypeNode.
|
|
89
100
|
context: ${node.getText()}`);
|
|
90
101
|
}
|
|
91
102
|
}
|
|
@@ -154,6 +165,10 @@ async function typeLiteral(node: TypeLiteralNode, context: SchemaExtractorContex
|
|
|
154
165
|
async function typeReference(node: TypeReferenceNode, context: SchemaExtractorContext) {
|
|
155
166
|
const name = node.typeName.getText();
|
|
156
167
|
const type = await context.resolveType(node, name, false);
|
|
168
|
+
if (node.typeArguments && type instanceof TypeRefSchema) {
|
|
169
|
+
const args = await pMapSeries(node.typeArguments, (arg) => typeNodeToSchema(arg, context));
|
|
170
|
+
type.typeArgs = args;
|
|
171
|
+
}
|
|
157
172
|
return type;
|
|
158
173
|
}
|
|
159
174
|
|
|
@@ -210,3 +225,21 @@ async function tupleType(node: TupleTypeNode, context: SchemaExtractorContext) {
|
|
|
210
225
|
});
|
|
211
226
|
return new TupleTypeSchema(context.getLocation(node), elements);
|
|
212
227
|
}
|
|
228
|
+
|
|
229
|
+
async function parenthesizedType(node: ParenthesizedTypeNode, context: SchemaExtractorContext) {
|
|
230
|
+
const type = await typeNodeToSchema(node.type, context);
|
|
231
|
+
return new ParenthesizedTypeSchema(context.getLocation(node), type);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function typePredicate(node: TypePredicateNode, context: SchemaExtractorContext) {
|
|
235
|
+
const parameterName = isIdentifier(node.parameterName) ? node.parameterName.getText() : 'this';
|
|
236
|
+
const type = node.type ? await typeNodeToSchema(node.type, context) : undefined;
|
|
237
|
+
const hasAssertsModifier = Boolean(node.assertsModifier);
|
|
238
|
+
return new TypePredicateSchema(context.getLocation(node), parameterName, type, hasAssertsModifier);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function indexedAccessType(node: IndexedAccessTypeNode, context: SchemaExtractorContext) {
|
|
242
|
+
const objectType = await typeNodeToSchema(node.objectType, context);
|
|
243
|
+
const indexType = await typeNodeToSchema(node.indexType, context);
|
|
244
|
+
return new IndexedAccessSchema(context.getLocation(node), objectType, indexType);
|
|
245
|
+
}
|
|
Binary file
|