@teambit/typescript 0.0.728 → 0.0.729
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.js +14 -1
- package/dist/exceptions/transformer-not-found.js.map +1 -1
- package/dist/schema-extractor-context.d.ts +33 -4
- package/dist/schema-extractor-context.js +163 -24
- package/dist/schema-extractor-context.js.map +1 -1
- package/dist/transformers/class-deceleration.d.ts +11 -0
- package/dist/transformers/class-deceleration.js +126 -0
- package/dist/transformers/class-deceleration.js.map +1 -0
- package/dist/transformers/constructor.d.ts +10 -0
- package/dist/transformers/constructor.js +70 -0
- package/dist/transformers/constructor.js.map +1 -0
- package/dist/transformers/export-declaration.d.ts +2 -1
- package/dist/transformers/export-declaration.js +64 -13
- package/dist/transformers/export-declaration.js.map +1 -1
- package/dist/transformers/function-declaration.d.ts +1 -3
- package/dist/transformers/function-declaration.js +25 -31
- package/dist/transformers/function-declaration.js.map +1 -1
- package/dist/transformers/index-signature.d.ts +9 -0
- package/dist/transformers/index-signature.js +61 -0
- package/dist/transformers/index-signature.js.map +1 -0
- package/dist/transformers/index.d.ts +14 -0
- package/dist/transformers/index.js +224 -0
- package/dist/transformers/index.js.map +1 -1
- package/dist/transformers/interface-declaration.d.ts +10 -0
- package/dist/transformers/interface-declaration.js +73 -0
- package/dist/transformers/interface-declaration.js.map +1 -0
- package/dist/transformers/literal-type.d.ts +8 -0
- package/dist/transformers/literal-type.js +50 -0
- package/dist/transformers/literal-type.js.map +1 -0
- package/dist/transformers/method-declaration.d.ts +11 -0
- package/dist/transformers/method-declaration.js +94 -0
- package/dist/transformers/method-declaration.js.map +1 -0
- package/dist/transformers/method-signature.d.ts +10 -0
- package/dist/transformers/method-signature.js +81 -0
- package/dist/transformers/method-signature.js.map +1 -0
- package/dist/transformers/property-declaration.d.ts +11 -0
- package/dist/transformers/property-declaration.js +81 -0
- package/dist/transformers/property-declaration.js.map +1 -0
- package/dist/transformers/property-signature.d.ts +10 -0
- package/dist/transformers/property-signature.js +91 -0
- package/dist/transformers/property-signature.js.map +1 -0
- package/dist/transformers/source-file-transformer.d.ts +2 -1
- package/dist/transformers/source-file-transformer.js +23 -3
- package/dist/transformers/source-file-transformer.js.map +1 -1
- package/dist/transformers/type-alias.d.ts +4 -1
- package/dist/transformers/type-alias.js +22 -2
- package/dist/transformers/type-alias.js.map +1 -1
- package/dist/transformers/type-intersection.d.ts +9 -0
- package/dist/transformers/type-intersection.js +63 -0
- package/dist/transformers/type-intersection.js.map +1 -0
- package/dist/transformers/type-literal.d.ts +14 -0
- package/dist/transformers/type-literal.js +67 -0
- package/dist/transformers/type-literal.js.map +1 -0
- package/dist/transformers/type-reference.d.ts +8 -0
- package/dist/transformers/type-reference.js +41 -0
- package/dist/transformers/type-reference.js.map +1 -0
- package/dist/transformers/type-union.d.ts +9 -0
- package/dist/transformers/type-union.js +63 -0
- package/dist/transformers/type-union.js.map +1 -0
- package/dist/transformers/utils/get-params.d.ts +4 -0
- package/dist/transformers/utils/get-params.js +67 -0
- package/dist/transformers/utils/get-params.js.map +1 -0
- package/dist/transformers/utils/parse-type-from-quick-info.d.ts +2 -0
- package/dist/transformers/utils/parse-type-from-quick-info.js +26 -0
- package/dist/transformers/utils/parse-type-from-quick-info.js.map +1 -0
- package/dist/transformers/variable-declaration.d.ts +11 -0
- package/dist/transformers/variable-declaration.js +98 -0
- package/dist/transformers/variable-declaration.js.map +1 -0
- package/dist/transformers/variable-statement.d.ts +7 -1
- package/dist/transformers/variable-statement.js +42 -2
- package/dist/transformers/variable-statement.js.map +1 -1
- package/dist/typescript.extractor.d.ts +3 -3
- package/dist/typescript.extractor.js +7 -4
- package/dist/typescript.extractor.js.map +1 -1
- package/dist/typescript.main.runtime.js +1 -1
- package/dist/typescript.main.runtime.js.map +1 -1
- package/exceptions/transformer-not-found.ts +5 -4
- package/package-tar/teambit-typescript-0.0.729.tgz +0 -0
- package/package.json +11 -11
- package/{preview-1652153159767.js → preview-1652239597845.js} +2 -2
- package/transformers/class-deceleration.ts +49 -0
- package/transformers/constructor.ts +22 -0
- package/transformers/export-declaration.ts +42 -13
- package/transformers/function-declaration.ts +7 -27
- package/transformers/index-signature.ts +21 -0
- package/transformers/index.ts +14 -0
- package/transformers/interface-declaration.ts +24 -0
- package/transformers/literal-type.ts +18 -0
- package/transformers/method-declaration.ts +32 -0
- package/transformers/method-signature.ts +30 -0
- package/transformers/property-declaration.ts +31 -0
- package/transformers/property-signature.ts +38 -0
- package/transformers/source-file-transformer.ts +6 -6
- package/transformers/type-alias.ts +12 -2
- package/transformers/type-intersection.ts +23 -0
- package/transformers/type-literal.ts +28 -0
- package/transformers/type-reference.ts +19 -0
- package/transformers/type-union.ts +23 -0
- package/transformers/utils/get-params.ts +30 -0
- package/transformers/utils/parse-type-from-quick-info.ts +12 -0
- package/transformers/variable-declaration.ts +36 -0
- package/transformers/variable-statement.ts +16 -2
- package/package-tar/teambit-typescript-0.0.728.tgz +0 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import pMapSeries from 'p-map-series';
|
|
2
|
+
import { ClassSchema, GetAccessorSchema, SetAccessorSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
3
|
+
import ts, { Node, ClassDeclaration } from 'typescript';
|
|
4
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
5
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
6
|
+
import { ExportIdentifier } from '../export-identifier';
|
|
7
|
+
import { parseTypeFromQuickInfo } from './utils/parse-type-from-quick-info';
|
|
8
|
+
import { getParams } from './utils/get-params';
|
|
9
|
+
|
|
10
|
+
export class ClassDecelerationTransformer implements SchemaTransformer {
|
|
11
|
+
predicate(node: Node) {
|
|
12
|
+
return node.kind === ts.SyntaxKind.ClassDeclaration;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// @todo: in case of `export default class` the class has no name.
|
|
16
|
+
private getName(node: ClassDeclaration) {
|
|
17
|
+
return node.name?.getText() || 'default';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async getIdentifiers(node: ClassDeclaration) {
|
|
21
|
+
return [new ExportIdentifier(this.getName(node), node.getSourceFile().fileName)];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async transform(node: ClassDeclaration, context: SchemaExtractorContext) {
|
|
25
|
+
const className = this.getName(node);
|
|
26
|
+
const members = await pMapSeries(node.members, async (member) => {
|
|
27
|
+
switch (member.kind) {
|
|
28
|
+
case ts.SyntaxKind.GetAccessor: {
|
|
29
|
+
const getter = member as ts.GetAccessorDeclaration;
|
|
30
|
+
const info = await context.getQuickInfo(getter.name);
|
|
31
|
+
const displaySig = info?.body?.displayString || '';
|
|
32
|
+
const typeStr = parseTypeFromQuickInfo(displaySig);
|
|
33
|
+
const type = await context.resolveType(getter, typeStr);
|
|
34
|
+
return new GetAccessorSchema(getter.name.getText(), type, displaySig);
|
|
35
|
+
}
|
|
36
|
+
case ts.SyntaxKind.SetAccessor: {
|
|
37
|
+
const setter = member as ts.SetAccessorDeclaration;
|
|
38
|
+
const params = await getParams(setter.parameters, context);
|
|
39
|
+
const info = await context.getQuickInfo(setter.name);
|
|
40
|
+
const displaySig = info?.body?.displayString || '';
|
|
41
|
+
return new SetAccessorSchema(setter.name.getText(), params[0], displaySig);
|
|
42
|
+
}
|
|
43
|
+
default:
|
|
44
|
+
return context.computeSchema(member);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return new ClassSchema(className, members);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { SchemaNode, ConstructorSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
2
|
+
import ts, { Node, ConstructorDeclaration } from 'typescript';
|
|
3
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
4
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
5
|
+
import { ExportIdentifier } from '../export-identifier';
|
|
6
|
+
import { getParams } from './utils/get-params';
|
|
7
|
+
|
|
8
|
+
export class Constructor implements SchemaTransformer {
|
|
9
|
+
predicate(node: Node) {
|
|
10
|
+
return node.kind === ts.SyntaxKind.Constructor;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async getIdentifiers(node: ConstructorDeclaration) {
|
|
14
|
+
return [new ExportIdentifier('constructor', node.getSourceFile().fileName)];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async transform(constructorDec: ConstructorDeclaration, context: SchemaExtractorContext): Promise<SchemaNode> {
|
|
18
|
+
const args = await getParams(constructorDec.parameters, context);
|
|
19
|
+
|
|
20
|
+
return new ConstructorSchema(args);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { SchemaNode, Module } from '@teambit/semantics.entities.semantic-schema';
|
|
2
|
+
import { compact } from 'lodash';
|
|
3
|
+
import ts, {
|
|
4
|
+
Node,
|
|
5
|
+
SyntaxKind,
|
|
6
|
+
ExportDeclaration as ExportDeclarationNode,
|
|
7
|
+
NamedExports,
|
|
8
|
+
NamespaceExport,
|
|
9
|
+
} from 'typescript';
|
|
2
10
|
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
3
11
|
import { SchemaTransformer } from '../schema-transformer';
|
|
4
12
|
import { ExportIdentifier } from '../export-identifier';
|
|
@@ -27,27 +35,48 @@ export class ExportDeclaration implements SchemaTransformer {
|
|
|
27
35
|
return [];
|
|
28
36
|
}
|
|
29
37
|
|
|
30
|
-
async transform(node: Node, context: SchemaExtractorContext) {
|
|
38
|
+
async transform(node: Node, context: SchemaExtractorContext): Promise<SchemaNode> {
|
|
31
39
|
const exportDec = node as ExportDeclarationNode;
|
|
32
|
-
// sourceFile.sear
|
|
33
40
|
const exportClause = exportDec.exportClause;
|
|
41
|
+
// e.g. `export { button1, button2 } as Composition from './button';
|
|
34
42
|
if (exportClause?.kind === SyntaxKind.NamedExports) {
|
|
35
43
|
exportClause as NamedExports;
|
|
36
|
-
|
|
37
|
-
const exports = await Promise.all(
|
|
44
|
+
const schemas = await Promise.all(
|
|
38
45
|
exportClause.elements.map(async (element) => {
|
|
39
|
-
|
|
40
|
-
await context.visitDefinition(element.name);
|
|
41
|
-
return element.name;
|
|
46
|
+
return context.visitDefinition(element.name);
|
|
42
47
|
})
|
|
43
48
|
);
|
|
44
49
|
|
|
45
|
-
return
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
return new Module(compact(schemas));
|
|
51
|
+
}
|
|
52
|
+
// e.g. `export * as Composition from './button';
|
|
53
|
+
if (exportClause?.kind === SyntaxKind.NamespaceExport) {
|
|
54
|
+
exportClause as NamespaceExport;
|
|
55
|
+
const namespace = exportClause.name.getText();
|
|
56
|
+
const sourceFile = await context.getSourceFileFromNode(exportClause.name);
|
|
57
|
+
if (!sourceFile) {
|
|
58
|
+
throw new Error(`unable to find the source-file for "${namespace}"`);
|
|
59
|
+
}
|
|
60
|
+
const result = await context.computeSchema(sourceFile);
|
|
61
|
+
if (!(result instanceof Module)) {
|
|
62
|
+
throw new Error(`expect result to be instance of Module`);
|
|
63
|
+
}
|
|
64
|
+
result.namespace = namespace;
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
// it's export-all, e.g. `export * from './button'`;
|
|
68
|
+
if (!exportClause) {
|
|
69
|
+
const specifier = exportDec.moduleSpecifier;
|
|
70
|
+
if (!specifier) {
|
|
71
|
+
throw new Error(`fatal: no specifier`);
|
|
72
|
+
}
|
|
73
|
+
const sourceFile = await context.getSourceFileFromNode(specifier);
|
|
74
|
+
if (!sourceFile) {
|
|
75
|
+
throw new Error(`unable to find the source-file`);
|
|
76
|
+
}
|
|
77
|
+
return context.computeSchema(sourceFile);
|
|
49
78
|
}
|
|
50
79
|
|
|
51
|
-
|
|
80
|
+
throw new Error('unrecognized export type');
|
|
52
81
|
}
|
|
53
82
|
}
|
|
@@ -3,6 +3,8 @@ import ts, { Node, FunctionDeclaration as FunctionDeclarationNode } from 'typesc
|
|
|
3
3
|
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
4
4
|
import { SchemaTransformer } from '../schema-transformer';
|
|
5
5
|
import { ExportIdentifier } from '../export-identifier';
|
|
6
|
+
import { getParams } from './utils/get-params';
|
|
7
|
+
import { parseTypeFromQuickInfo } from './utils/parse-type-from-quick-info';
|
|
6
8
|
|
|
7
9
|
export class FunctionDeclaration implements SchemaTransformer {
|
|
8
10
|
predicate(node: Node) {
|
|
@@ -18,37 +20,15 @@ export class FunctionDeclaration implements SchemaTransformer {
|
|
|
18
20
|
return funcDec.name?.getText() || '';
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
return Promise.all(
|
|
23
|
-
funcDec.parameters.map(async (param) => {
|
|
24
|
-
const type = param.type;
|
|
25
|
-
return {
|
|
26
|
-
name: param.name.getText(),
|
|
27
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
28
|
-
type: await context.resolveType(type!, type?.getText() || 'any'),
|
|
29
|
-
};
|
|
30
|
-
})
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
private parseReturnValue(displayString?: string) {
|
|
35
|
-
if (!displayString) return '';
|
|
36
|
-
const array = displayString.split(':');
|
|
37
|
-
return array[array.length - 1].trim();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async transform(node: Node, context: SchemaExtractorContext): Promise<SchemaNode> {
|
|
41
|
-
const funcDec = node as FunctionDeclarationNode;
|
|
23
|
+
async transform(funcDec: FunctionDeclarationNode, context: SchemaExtractorContext): Promise<SchemaNode> {
|
|
42
24
|
const name = this.getName(funcDec);
|
|
43
25
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
44
26
|
const info = await context.getQuickInfo(funcDec.name!);
|
|
45
27
|
const displaySig = info?.body?.displayString;
|
|
46
|
-
const returnTypeStr =
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
50
|
-
const returnType = await context.resolveType(funcDec.name!, returnTypeStr);
|
|
28
|
+
const returnTypeStr = parseTypeFromQuickInfo(displaySig);
|
|
29
|
+
const args = await getParams(funcDec.parameters, context);
|
|
30
|
+
const returnType = await context.resolveType(funcDec, returnTypeStr);
|
|
51
31
|
|
|
52
|
-
return new FunctionSchema(name || '',
|
|
32
|
+
return new FunctionSchema(name || '', args, returnType, displaySig || '');
|
|
53
33
|
}
|
|
54
34
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { SchemaNode, IndexSignatureSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
2
|
+
import ts, { Node, IndexSignatureDeclaration } from 'typescript';
|
|
3
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
4
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
5
|
+
import { getParams } from './utils/get-params';
|
|
6
|
+
|
|
7
|
+
export class IndexSignature implements SchemaTransformer {
|
|
8
|
+
predicate(node: Node) {
|
|
9
|
+
return node.kind === ts.SyntaxKind.IndexSignature;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async getIdentifiers() {
|
|
13
|
+
return [];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async transform(indexSig: IndexSignatureDeclaration, context: SchemaExtractorContext): Promise<SchemaNode> {
|
|
17
|
+
const params = await getParams(indexSig.parameters, context);
|
|
18
|
+
const type = await context.resolveType(indexSig, indexSig.type.getText());
|
|
19
|
+
return new IndexSignatureSchema(params, type);
|
|
20
|
+
}
|
|
21
|
+
}
|
package/transformers/index.ts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
export { ExportDeclaration } from './export-declaration';
|
|
2
2
|
export { FunctionDeclaration } from './function-declaration';
|
|
3
|
+
export { MethodDeclaration } from './method-declaration';
|
|
4
|
+
export { PropertyDeclaration } from './property-declaration';
|
|
3
5
|
export { VariableStatementTransformer } from './variable-statement';
|
|
6
|
+
export { VariableDeclaration } from './variable-declaration';
|
|
4
7
|
export { SourceFileTransformer } from './source-file-transformer';
|
|
5
8
|
export { TypeAliasTransformer } from './type-alias';
|
|
9
|
+
export { ClassDecelerationTransformer } from './class-deceleration';
|
|
10
|
+
export { TypeIntersectionTransformer } from './type-intersection';
|
|
11
|
+
export { Constructor } from './constructor';
|
|
12
|
+
export { TypeReferenceTransformer } from './type-reference';
|
|
13
|
+
export { TypeLiteralTransformer } from './type-literal';
|
|
14
|
+
export { PropertySignature } from './property-signature';
|
|
15
|
+
export { TypeUnionTransformer } from './type-union';
|
|
16
|
+
export { LiteralTypeTransformer } from './literal-type';
|
|
17
|
+
export { IndexSignature } from './index-signature';
|
|
18
|
+
export { InterfaceDeclarationTransformer } from './interface-declaration';
|
|
19
|
+
export { MethodSignatureTransformer } from './method-signature';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import ts, { Node, InterfaceDeclaration } from 'typescript';
|
|
2
|
+
import pMapSeries from 'p-map-series';
|
|
3
|
+
import { InterfaceSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
4
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
5
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
6
|
+
import { ExportIdentifier } from '../export-identifier';
|
|
7
|
+
|
|
8
|
+
export class InterfaceDeclarationTransformer implements SchemaTransformer {
|
|
9
|
+
predicate(node: Node) {
|
|
10
|
+
return node.kind === ts.SyntaxKind.InterfaceDeclaration;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async getIdentifiers(node: InterfaceDeclaration): Promise<ExportIdentifier[]> {
|
|
14
|
+
return [new ExportIdentifier(node.name.getText(), node.getSourceFile().fileName)];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async transform(interfaceDec: InterfaceDeclaration, context: SchemaExtractorContext) {
|
|
18
|
+
const members = await pMapSeries(interfaceDec.members, async (member) => {
|
|
19
|
+
const typeSchema = await context.computeSchema(member);
|
|
20
|
+
return typeSchema;
|
|
21
|
+
});
|
|
22
|
+
return new InterfaceSchema(interfaceDec.name.getText(), members);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import ts, { LiteralTypeNode, Node } from 'typescript';
|
|
2
|
+
import { TypeRefSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
3
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
4
|
+
|
|
5
|
+
export class LiteralTypeTransformer implements SchemaTransformer {
|
|
6
|
+
predicate(node: Node) {
|
|
7
|
+
return node.kind === ts.SyntaxKind.LiteralType;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async getIdentifiers() {
|
|
11
|
+
return [];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async transform(literalType: LiteralTypeNode) {
|
|
15
|
+
const type = literalType.literal.getText();
|
|
16
|
+
return new TypeRefSchema(type);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { SchemaNode, FunctionSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
2
|
+
import ts, { Node, MethodDeclaration as MethodDeclarationNode } from 'typescript';
|
|
3
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
4
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
5
|
+
import { ExportIdentifier } from '../export-identifier';
|
|
6
|
+
import { parseTypeFromQuickInfo } from './utils/parse-type-from-quick-info';
|
|
7
|
+
import { getParams } from './utils/get-params';
|
|
8
|
+
|
|
9
|
+
export class MethodDeclaration implements SchemaTransformer {
|
|
10
|
+
predicate(node: Node) {
|
|
11
|
+
return node.kind === ts.SyntaxKind.MethodDeclaration;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async getIdentifiers(funcDec: MethodDeclarationNode) {
|
|
15
|
+
return [new ExportIdentifier(this.getName(funcDec), funcDec.getSourceFile().fileName)];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private getName(funcDec: MethodDeclarationNode) {
|
|
19
|
+
return funcDec.name?.getText() || '';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async transform(methodDec: MethodDeclarationNode, context: SchemaExtractorContext): Promise<SchemaNode> {
|
|
23
|
+
const name = this.getName(methodDec);
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
25
|
+
const info = await context.getQuickInfo(methodDec.name!);
|
|
26
|
+
const displaySig = info?.body?.displayString;
|
|
27
|
+
const returnTypeStr = parseTypeFromQuickInfo(displaySig);
|
|
28
|
+
const args = await getParams(methodDec.parameters, context);
|
|
29
|
+
const returnType = await context.resolveType(methodDec, returnTypeStr);
|
|
30
|
+
return new FunctionSchema(name || '', args, returnType, displaySig || '');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { SchemaNode, FunctionSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
2
|
+
import ts, { Node, MethodSignature as MethodSignatureNode } from 'typescript';
|
|
3
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
4
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
5
|
+
import { parseTypeFromQuickInfo } from './utils/parse-type-from-quick-info';
|
|
6
|
+
import { getParams } from './utils/get-params';
|
|
7
|
+
|
|
8
|
+
export class MethodSignatureTransformer implements SchemaTransformer {
|
|
9
|
+
predicate(node: Node) {
|
|
10
|
+
return node.kind === ts.SyntaxKind.MethodSignature;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getName(node: MethodSignatureNode) {
|
|
14
|
+
return node.name.getText();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async getIdentifiers() {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async transform(methodSig: MethodSignatureNode, context: SchemaExtractorContext): Promise<SchemaNode> {
|
|
22
|
+
const name = this.getName(methodSig);
|
|
23
|
+
const info = await context.getQuickInfo(methodSig.name);
|
|
24
|
+
const displaySig = info?.body?.displayString;
|
|
25
|
+
const returnTypeStr = parseTypeFromQuickInfo(displaySig);
|
|
26
|
+
const args = await getParams(methodSig.parameters, context);
|
|
27
|
+
const returnType = await context.resolveType(methodSig || methodSig, returnTypeStr);
|
|
28
|
+
return new FunctionSchema(name, args, returnType, displaySig || '');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { SchemaNode, VariableSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
2
|
+
import ts, { Node, PropertyDeclaration as PropertyDeclarationNode } from 'typescript';
|
|
3
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
4
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
5
|
+
import { ExportIdentifier } from '../export-identifier';
|
|
6
|
+
import { parseTypeFromQuickInfo } from './utils/parse-type-from-quick-info';
|
|
7
|
+
|
|
8
|
+
export class PropertyDeclaration implements SchemaTransformer {
|
|
9
|
+
predicate(node: Node) {
|
|
10
|
+
return node.kind === ts.SyntaxKind.PropertyDeclaration;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getName(node: PropertyDeclarationNode) {
|
|
14
|
+
return node.name.getText();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async getIdentifiers(node: PropertyDeclarationNode) {
|
|
18
|
+
return [new ExportIdentifier(node.name.getText(), node.getSourceFile().fileName)];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async transform(propertyDec: PropertyDeclarationNode, context: SchemaExtractorContext): Promise<SchemaNode> {
|
|
22
|
+
const name = this.getName(propertyDec);
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
24
|
+
const info = await context.getQuickInfo(propertyDec.name!);
|
|
25
|
+
const displaySig = info?.body?.displayString;
|
|
26
|
+
const typeStr = parseTypeFromQuickInfo(displaySig);
|
|
27
|
+
const type = await context.resolveType(propertyDec, typeStr);
|
|
28
|
+
|
|
29
|
+
return new VariableSchema(name || '', displaySig || '', type);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { FunctionSchema, SchemaNode, VariableSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
2
|
+
import ts, { FunctionTypeNode, Node, PropertySignature as PropertySignatureNode } from 'typescript';
|
|
3
|
+
import { getParams } from './utils/get-params';
|
|
4
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
5
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
6
|
+
import { parseReturnTypeFromQuickInfo, parseTypeFromQuickInfo } from './utils/parse-type-from-quick-info';
|
|
7
|
+
|
|
8
|
+
export class PropertySignature implements SchemaTransformer {
|
|
9
|
+
predicate(node: Node) {
|
|
10
|
+
return node.kind === ts.SyntaxKind.PropertySignature;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getName(node: PropertySignatureNode) {
|
|
14
|
+
return node.name.getText();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async getIdentifiers() {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async transform(prop: PropertySignatureNode, context: SchemaExtractorContext): Promise<SchemaNode> {
|
|
22
|
+
const name = this.getName(prop);
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
24
|
+
const info = await context.getQuickInfo(prop.name!);
|
|
25
|
+
const displaySig = info?.body?.displayString || '';
|
|
26
|
+
if (prop.type?.kind === ts.SyntaxKind.FunctionType) {
|
|
27
|
+
// e.g. `propertySig: () => void;` inside interface
|
|
28
|
+
const propType = prop.type as FunctionTypeNode;
|
|
29
|
+
const args = await getParams(propType.parameters, context);
|
|
30
|
+
const typeStr = parseReturnTypeFromQuickInfo(displaySig);
|
|
31
|
+
const returnType = await context.resolveType(propType, typeStr);
|
|
32
|
+
return new FunctionSchema(name, args, returnType, displaySig);
|
|
33
|
+
}
|
|
34
|
+
const typeStr = parseTypeFromQuickInfo(displaySig);
|
|
35
|
+
const type = await context.resolveType(prop, typeStr);
|
|
36
|
+
return new VariableSchema(name, displaySig, type);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import ts, { Node, SourceFile } from 'typescript';
|
|
2
2
|
import { compact, flatten } from 'lodash';
|
|
3
|
+
import pMapSeries from 'p-map-series';
|
|
4
|
+
import { Module } from '@teambit/semantics.entities.semantic-schema';
|
|
3
5
|
import { SchemaTransformer } from '../schema-transformer';
|
|
4
6
|
import { ExportIdentifier } from '../export-identifier';
|
|
5
7
|
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
@@ -29,13 +31,11 @@ export class SourceFileTransformer implements SchemaTransformer {
|
|
|
29
31
|
|
|
30
32
|
async transform(node: SourceFile, context: SchemaExtractorContext) {
|
|
31
33
|
const exports = this.listExports(node);
|
|
32
|
-
const schemas = await
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
})
|
|
36
|
-
);
|
|
34
|
+
const schemas = await pMapSeries(exports, (exportNode) => {
|
|
35
|
+
return context.computeSchema(exportNode);
|
|
36
|
+
});
|
|
37
37
|
|
|
38
|
-
return schemas;
|
|
38
|
+
return new Module(schemas);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/**
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import ts, { Node, TypeAliasDeclaration } from 'typescript';
|
|
2
|
+
import { TypeSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
2
3
|
import { SchemaTransformer } from '../schema-transformer';
|
|
4
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
3
5
|
import { ExportIdentifier } from '../export-identifier';
|
|
4
6
|
|
|
5
7
|
export class TypeAliasTransformer implements SchemaTransformer {
|
|
@@ -11,7 +13,15 @@ export class TypeAliasTransformer implements SchemaTransformer {
|
|
|
11
13
|
return [new ExportIdentifier(node.name.getText(), node.getSourceFile().fileName)];
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
return
|
|
16
|
+
private getName(node: TypeAliasDeclaration): string {
|
|
17
|
+
return node.name.getText();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async transform(typeAlias: TypeAliasDeclaration, context: SchemaExtractorContext) {
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
22
|
+
const info = await context.getQuickInfo(typeAlias.name!);
|
|
23
|
+
const displaySig = info?.body?.displayString;
|
|
24
|
+
const type = await context.computeSchema(typeAlias.type);
|
|
25
|
+
return new TypeSchema(this.getName(typeAlias), type, displaySig as string);
|
|
16
26
|
}
|
|
17
27
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import ts, { Node, IntersectionTypeNode } from 'typescript';
|
|
2
|
+
import pMapSeries from 'p-map-series';
|
|
3
|
+
import { TypeIntersectionSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
4
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
5
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
6
|
+
|
|
7
|
+
export class TypeIntersectionTransformer implements SchemaTransformer {
|
|
8
|
+
predicate(node: Node) {
|
|
9
|
+
return node.kind === ts.SyntaxKind.IntersectionType;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async getIdentifiers() {
|
|
13
|
+
return [];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async transform(intersectionType: IntersectionTypeNode, context: SchemaExtractorContext) {
|
|
17
|
+
const types = await pMapSeries(intersectionType.types, async (type) => {
|
|
18
|
+
const typeSchema = await context.computeSchema(type);
|
|
19
|
+
return typeSchema;
|
|
20
|
+
});
|
|
21
|
+
return new TypeIntersectionSchema(types);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import ts, { Node, TypeLiteralNode } from 'typescript';
|
|
2
|
+
import pMapSeries from 'p-map-series';
|
|
3
|
+
import { TypeLiteralSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
4
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
5
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
6
|
+
import { ExportIdentifier } from '../export-identifier';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* not to be confused with "LiteralType", which is string/boolean/null.
|
|
10
|
+
* this "TypeLiteral" is an object with properties, such as: `{ a: string; b: number }`, similar to Interface.
|
|
11
|
+
*/
|
|
12
|
+
export class TypeLiteralTransformer implements SchemaTransformer {
|
|
13
|
+
predicate(node: Node) {
|
|
14
|
+
return node.kind === ts.SyntaxKind.TypeLiteral;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async getIdentifiers(): Promise<ExportIdentifier[]> {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async transform(typeLiteral: TypeLiteralNode, context: SchemaExtractorContext) {
|
|
22
|
+
const members = await pMapSeries(typeLiteral.members, async (member) => {
|
|
23
|
+
const typeSchema = await context.computeSchema(member);
|
|
24
|
+
return typeSchema;
|
|
25
|
+
});
|
|
26
|
+
return new TypeLiteralSchema(members);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import ts, { Node, TypeReferenceNode } from 'typescript';
|
|
2
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
3
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
4
|
+
|
|
5
|
+
export class TypeReferenceTransformer implements SchemaTransformer {
|
|
6
|
+
predicate(node: Node) {
|
|
7
|
+
return node.kind === ts.SyntaxKind.TypeReference;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async getIdentifiers() {
|
|
11
|
+
return [];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async transform(typeReference: TypeReferenceNode, context: SchemaExtractorContext) {
|
|
15
|
+
const name = typeReference.typeName.getText();
|
|
16
|
+
const type = await context.resolveType(typeReference, name);
|
|
17
|
+
return type;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import ts, { Node, UnionTypeNode } from 'typescript';
|
|
2
|
+
import pMapSeries from 'p-map-series';
|
|
3
|
+
import { TypeUnionSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
4
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
5
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
6
|
+
|
|
7
|
+
export class TypeUnionTransformer implements SchemaTransformer {
|
|
8
|
+
predicate(node: Node) {
|
|
9
|
+
return node.kind === ts.SyntaxKind.UnionType;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async getIdentifiers() {
|
|
13
|
+
return [];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async transform(unionType: UnionTypeNode, context: SchemaExtractorContext) {
|
|
17
|
+
const types = await pMapSeries(unionType.types, async (type) => {
|
|
18
|
+
const typeSchema = await context.computeSchema(type);
|
|
19
|
+
return typeSchema;
|
|
20
|
+
});
|
|
21
|
+
return new TypeUnionSchema(types);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Parameter, TypeRefSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
2
|
+
import pMapSeries from 'p-map-series';
|
|
3
|
+
import { ParameterDeclaration, NodeArray } from 'typescript';
|
|
4
|
+
import { SchemaExtractorContext } from '../../schema-extractor-context';
|
|
5
|
+
import { parseTypeFromQuickInfo } from './parse-type-from-quick-info';
|
|
6
|
+
|
|
7
|
+
export async function getParams(
|
|
8
|
+
parameterNodes: NodeArray<ParameterDeclaration>,
|
|
9
|
+
context: SchemaExtractorContext
|
|
10
|
+
): Promise<Parameter[]> {
|
|
11
|
+
return pMapSeries(parameterNodes, async (param) => {
|
|
12
|
+
return {
|
|
13
|
+
name: param.name.getText(),
|
|
14
|
+
type: await getParamType(param, context),
|
|
15
|
+
defaultValue: param.initializer ? param.initializer.getText() : undefined,
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function getParamType(param: ParameterDeclaration, context: SchemaExtractorContext): Promise<TypeRefSchema> {
|
|
21
|
+
if (param.type) {
|
|
22
|
+
const type = param.type;
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
24
|
+
return context.resolveType(type!, type?.getText() || 'any');
|
|
25
|
+
}
|
|
26
|
+
const info = await context.getQuickInfo(param.name);
|
|
27
|
+
const displaySig = info?.body?.displayString;
|
|
28
|
+
const parsed = parseTypeFromQuickInfo(displaySig);
|
|
29
|
+
return new TypeRefSchema(parsed || 'any');
|
|
30
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function parseTypeFromQuickInfo(displayString?: string) {
|
|
2
|
+
if (!displayString) return '';
|
|
3
|
+
const array = displayString.split(':');
|
|
4
|
+
return array[array.length - 1].trim();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function parseReturnTypeFromQuickInfo(displayString?: string) {
|
|
8
|
+
if (!displayString) return '';
|
|
9
|
+
const typeStr = parseTypeFromQuickInfo(displayString);
|
|
10
|
+
const array = typeStr.split('=>');
|
|
11
|
+
return array[array.length - 1].trim();
|
|
12
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { SchemaNode, VariableSchema, FunctionSchema } from '@teambit/semantics.entities.semantic-schema';
|
|
2
|
+
import ts, { Node, VariableDeclaration as VariableDeclarationNode, ArrowFunction } from 'typescript';
|
|
3
|
+
import { SchemaTransformer } from '../schema-transformer';
|
|
4
|
+
import { SchemaExtractorContext } from '../schema-extractor-context';
|
|
5
|
+
import { ExportIdentifier } from '../export-identifier';
|
|
6
|
+
import { getParams } from './utils/get-params';
|
|
7
|
+
import { parseReturnTypeFromQuickInfo, parseTypeFromQuickInfo } from './utils/parse-type-from-quick-info';
|
|
8
|
+
|
|
9
|
+
export class VariableDeclaration implements SchemaTransformer {
|
|
10
|
+
predicate(node: Node) {
|
|
11
|
+
return node.kind === ts.SyntaxKind.VariableDeclaration;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
getName(node: VariableDeclarationNode) {
|
|
15
|
+
return node.name.getText();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async getIdentifiers(node: VariableDeclarationNode) {
|
|
19
|
+
return [new ExportIdentifier(node.name.getText(), node.getSourceFile().fileName)];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async transform(varDec: VariableDeclarationNode, context: SchemaExtractorContext): Promise<SchemaNode> {
|
|
23
|
+
const name = this.getName(varDec);
|
|
24
|
+
const info = await context.getQuickInfo(varDec.name);
|
|
25
|
+
const displaySig = info?.body?.displayString || '';
|
|
26
|
+
if (varDec.initializer?.kind === ts.SyntaxKind.ArrowFunction) {
|
|
27
|
+
const args = await getParams((varDec.initializer as ArrowFunction).parameters, context);
|
|
28
|
+
const typeStr = parseReturnTypeFromQuickInfo(displaySig);
|
|
29
|
+
const returnType = await context.resolveType(varDec, typeStr);
|
|
30
|
+
return new FunctionSchema(name, args, returnType, displaySig);
|
|
31
|
+
}
|
|
32
|
+
const typeStr = parseTypeFromQuickInfo(displaySig);
|
|
33
|
+
const type = await context.resolveType(varDec, typeStr);
|
|
34
|
+
return new VariableSchema(name, displaySig, type);
|
|
35
|
+
}
|
|
36
|
+
}
|