@teambit/typescript 0.0.894 → 0.0.896

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.
Files changed (71) hide show
  1. package/dist/schema-extractor-context.d.ts +6 -3
  2. package/dist/schema-extractor-context.js +41 -10
  3. package/dist/schema-extractor-context.js.map +1 -1
  4. package/dist/transformers/binding-element.js +1 -8
  5. package/dist/transformers/binding-element.js.map +1 -1
  6. package/dist/transformers/{class-deceleration.d.ts → class-declaration.d.ts} +2 -1
  7. package/dist/transformers/class-declaration.js +130 -0
  8. package/dist/transformers/class-declaration.js.map +1 -0
  9. package/dist/transformers/enum-declaration.js +20 -2
  10. package/dist/transformers/enum-declaration.js.map +1 -1
  11. package/dist/transformers/export-declaration.js +2 -2
  12. package/dist/transformers/export-declaration.js.map +1 -1
  13. package/dist/transformers/index.d.ts +1 -1
  14. package/dist/transformers/index.js +5 -5
  15. package/dist/transformers/index.js.map +1 -1
  16. package/dist/transformers/interface-declaration.d.ts +1 -0
  17. package/dist/transformers/interface-declaration.js +47 -5
  18. package/dist/transformers/interface-declaration.js.map +1 -1
  19. package/dist/transformers/source-file-transformer.d.ts +2 -2
  20. package/dist/transformers/source-file-transformer.js +1 -1
  21. package/dist/transformers/source-file-transformer.js.map +1 -1
  22. package/dist/transformers/type-alias.js +1 -8
  23. package/dist/transformers/type-alias.js.map +1 -1
  24. package/dist/transformers/utils/class-element-to-schema.js +7 -11
  25. package/dist/transformers/utils/class-element-to-schema.js.map +1 -1
  26. package/dist/transformers/utils/get-params.js +9 -1
  27. package/dist/transformers/utils/get-params.js.map +1 -1
  28. package/dist/transformers/utils/jsdoc-to-doc-schema.d.ts +4 -3
  29. package/dist/transformers/utils/jsdoc-to-doc-schema.js +11 -33
  30. package/dist/transformers/utils/jsdoc-to-doc-schema.js.map +1 -1
  31. package/dist/transformers/utils/parse-type-from-quick-info.js +9 -0
  32. package/dist/transformers/utils/parse-type-from-quick-info.js.map +1 -1
  33. package/dist/transformers/utils/to-function-like-schema.js +1 -8
  34. package/dist/transformers/utils/to-function-like-schema.js.map +1 -1
  35. package/dist/transformers/utils/type-element-to-schema.js +1 -8
  36. package/dist/transformers/utils/type-element-to-schema.js.map +1 -1
  37. package/dist/transformers/utils/type-node-to-schema.js +9 -9
  38. package/dist/transformers/utils/type-node-to-schema.js.map +1 -1
  39. package/dist/transformers/variable-declaration.js +13 -11
  40. package/dist/transformers/variable-declaration.js.map +1 -1
  41. package/dist/transformers/variable-statement.js +1 -1
  42. package/dist/transformers/variable-statement.js.map +1 -1
  43. package/dist/typescript.extractor.d.ts +2 -1
  44. package/dist/typescript.extractor.js +4 -4
  45. package/dist/typescript.extractor.js.map +1 -1
  46. package/dist/typescript.main.runtime.js +4 -4
  47. package/dist/typescript.main.runtime.js.map +1 -1
  48. package/package-tar/teambit-typescript-0.0.896.tgz +0 -0
  49. package/package.json +15 -14
  50. package/{preview-1667878820252.js → preview-1668051814135.js} +2 -2
  51. package/transformers/binding-element.ts +1 -2
  52. package/transformers/class-declaration.ts +97 -0
  53. package/transformers/enum-declaration.ts +14 -3
  54. package/transformers/export-declaration.ts +3 -3
  55. package/transformers/index.ts +1 -1
  56. package/transformers/interface-declaration.ts +49 -5
  57. package/transformers/source-file-transformer.ts +2 -2
  58. package/transformers/type-alias.ts +1 -2
  59. package/transformers/utils/class-element-to-schema.ts +4 -3
  60. package/transformers/utils/get-params.ts +15 -1
  61. package/transformers/utils/jsdoc-to-doc-schema.ts +15 -22
  62. package/transformers/utils/parse-type-from-quick-info.ts +7 -0
  63. package/transformers/utils/to-function-like-schema.ts +2 -2
  64. package/transformers/utils/type-element-to-schema.ts +1 -2
  65. package/transformers/utils/type-node-to-schema.ts +12 -3
  66. package/transformers/variable-declaration.ts +21 -3
  67. package/transformers/variable-statement.ts +2 -2
  68. package/dist/transformers/class-deceleration.js +0 -87
  69. package/dist/transformers/class-deceleration.js.map +0 -1
  70. package/package-tar/teambit-typescript-0.0.894.tgz +0 -0
  71. package/transformers/class-deceleration.ts +0 -37
package/package.json CHANGED
@@ -1,40 +1,41 @@
1
1
  {
2
2
  "name": "@teambit/typescript",
3
- "version": "0.0.894",
3
+ "version": "0.0.896",
4
4
  "homepage": "https://bit.dev/teambit/typescript/typescript",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.typescript",
8
8
  "name": "typescript",
9
- "version": "0.0.894"
9
+ "version": "0.0.896"
10
10
  },
11
11
  "dependencies": {
12
12
  "lodash": "4.17.21",
13
+ "p-map-series": "2.1.0",
13
14
  "tsutils": "3.21.0",
14
15
  "typescript": "4.7.4",
15
16
  "chalk": "2.4.2",
16
17
  "fs-extra": "10.0.0",
17
18
  "yesno": "0.4.0",
18
- "p-map-series": "2.1.0",
19
19
  "@babel/runtime": "7.20.0",
20
20
  "core-js": "^3.0.0",
21
21
  "@teambit/harmony": "0.3.3",
22
- "@teambit/compiler": "0.0.894",
22
+ "@teambit/compiler": "0.0.896",
23
23
  "@teambit/typescript.modules.ts-config-mutator": "0.0.76",
24
- "@teambit/component": "0.0.894",
25
- "@teambit/dependency-resolver": "0.0.894",
26
- "@teambit/semantics.entities.semantic-schema": "0.0.46",
24
+ "@teambit/component": "0.0.896",
25
+ "@teambit/dependency-resolver": "0.0.896",
26
+ "@teambit/formatter": "0.0.447",
27
+ "@teambit/semantics.entities.semantic-schema": "0.0.48",
27
28
  "@teambit/ts-server": "0.0.39",
28
- "@teambit/aspect-loader": "0.0.894",
29
- "@teambit/envs": "0.0.894",
29
+ "@teambit/aspect-loader": "0.0.896",
30
+ "@teambit/envs": "0.0.896",
30
31
  "@teambit/logger": "0.0.692",
31
- "@teambit/workspace": "0.0.894",
32
+ "@teambit/workspace": "0.0.896",
32
33
  "@teambit/bit-error": "0.0.402",
33
- "@teambit/builder": "0.0.894",
34
- "@teambit/isolator": "0.0.894",
35
- "@teambit/schema": "0.0.894",
34
+ "@teambit/builder": "0.0.896",
35
+ "@teambit/isolator": "0.0.896",
36
+ "@teambit/schema": "0.0.896",
36
37
  "@teambit/cli": "0.0.599",
37
- "@teambit/pkg": "0.0.894"
38
+ "@teambit/pkg": "0.0.896"
38
39
  },
39
40
  "devDependencies": {
40
41
  "@types/lodash": "4.14.165",
@@ -1,5 +1,5 @@
1
- import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.typescript_typescript@0.0.894/dist/typescript.composition.js';
2
- import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.typescript_typescript@0.0.894/dist/typescript.docs.mdx';
1
+ import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.typescript_typescript@0.0.896/dist/typescript.composition.js';
2
+ import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.typescript_typescript@0.0.896/dist/typescript.docs.mdx';
3
3
 
4
4
  export const compositions = [compositions_0];
5
5
  export const overview = [overview_0];
@@ -4,7 +4,6 @@ import { SchemaTransformer } from '../schema-transformer';
4
4
  import { SchemaExtractorContext } from '../schema-extractor-context';
5
5
  import { ExportIdentifier } from '../export-identifier';
6
6
  import { parseTypeFromQuickInfo } from './utils/parse-type-from-quick-info';
7
- import { jsDocToDocSchema } from './utils/jsdoc-to-doc-schema';
8
7
 
9
8
  /**
10
9
  * for example:
@@ -28,7 +27,7 @@ export class BindingElementTransformer implements SchemaTransformer {
28
27
  const displaySig = info?.body?.displayString || '';
29
28
  const typeStr = parseTypeFromQuickInfo(info);
30
29
  const type = await context.resolveType(node, typeStr);
31
- const doc = await jsDocToDocSchema(node, context);
30
+ const doc = await context.jsDocToDocSchema(node);
32
31
  return new VariableLikeSchema(context.getLocation(node), name, displaySig, type, false, doc);
33
32
  }
34
33
  }
@@ -0,0 +1,97 @@
1
+ import pMapSeries from 'p-map-series';
2
+ import { compact } from 'lodash';
3
+ import {
4
+ ClassSchema,
5
+ UnresolvedSchema,
6
+ ExpressionWithTypeArgumentsSchema,
7
+ } from '@teambit/semantics.entities.semantic-schema';
8
+ import ts, { Node, ClassDeclaration } from 'typescript';
9
+ import { SchemaTransformer } from '../schema-transformer';
10
+ import { SchemaExtractorContext } from '../schema-extractor-context';
11
+ import { ExportIdentifier } from '../export-identifier';
12
+ import { classElementToSchema } from './utils/class-element-to-schema';
13
+ import { typeNodeToSchema } from './utils/type-node-to-schema';
14
+
15
+ export class ClassDeclarationTransformer implements SchemaTransformer {
16
+ predicate(node: Node) {
17
+ return node.kind === ts.SyntaxKind.ClassDeclaration;
18
+ }
19
+
20
+ // @todo: in case of `export default class` the class has no name.
21
+ private getName(node: ClassDeclaration) {
22
+ return node.name?.getText() || 'default';
23
+ }
24
+
25
+ async getIdentifiers(node: ClassDeclaration) {
26
+ return [new ExportIdentifier(this.getName(node), node.getSourceFile().fileName)];
27
+ }
28
+
29
+ private async getExpressionWithTypeArgs(
30
+ node: ClassDeclaration,
31
+ context: SchemaExtractorContext,
32
+ token: ts.SyntaxKind.ExtendsKeyword | ts.SyntaxKind.ImplementsKeyword
33
+ ) {
34
+ if (!node.heritageClauses) return [];
35
+
36
+ return pMapSeries(
37
+ node.heritageClauses
38
+ .filter((heritageClause: ts.HeritageClause) => heritageClause.token === token)
39
+ .flatMap((h: ts.HeritageClause) => {
40
+ const { types } = h;
41
+ const name = h.getText();
42
+ return types.map((type) => ({ ...type, name }));
43
+ }),
44
+ async (expressionWithTypeArgs: ts.ExpressionWithTypeArguments & { name: string }) => {
45
+ const { typeArguments, expression, name } = expressionWithTypeArgs;
46
+ const typeArgsNodes = typeArguments ? await pMapSeries(typeArguments, (t) => typeNodeToSchema(t, context)) : [];
47
+ const location = context.getLocation(expression);
48
+ const expressionNode =
49
+ (await context.visitDefinition(expression)) || new UnresolvedSchema(location, expression.getText());
50
+ return new ExpressionWithTypeArgumentsSchema(typeArgsNodes, expressionNode, name, location);
51
+ }
52
+ );
53
+ }
54
+
55
+ async transform(node: ClassDeclaration, context: SchemaExtractorContext) {
56
+ const className = this.getName(node);
57
+ const extendsExpressionsWithTypeArgs = await this.getExpressionWithTypeArgs(
58
+ node,
59
+ context,
60
+ ts.SyntaxKind.ExtendsKeyword
61
+ );
62
+
63
+ const implementsExpressionsWithTypeArgs = await this.getExpressionWithTypeArgs(
64
+ node,
65
+ context,
66
+ ts.SyntaxKind.ImplementsKeyword
67
+ );
68
+
69
+ const typeParameters = node.typeParameters?.map((typeParam) => {
70
+ return typeParam.name.getText();
71
+ });
72
+ const signature = node.name ? await context.getQuickInfoDisplayString(node.name) : undefined;
73
+ const members = await pMapSeries(node.members, async (member) => {
74
+ const isPrivate = member.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.PrivateKeyword);
75
+ if (isPrivate) {
76
+ return null;
77
+ }
78
+ return classElementToSchema(member, context);
79
+ });
80
+ const doc = await context.jsDocToDocSchema(node);
81
+
82
+ if (!signature) {
83
+ throw Error(`Missing signature for class ${className} declaration`);
84
+ }
85
+
86
+ return new ClassSchema(
87
+ className,
88
+ compact(members),
89
+ context.getLocation(node),
90
+ signature,
91
+ doc,
92
+ typeParameters,
93
+ extendsExpressionsWithTypeArgs,
94
+ implementsExpressionsWithTypeArgs
95
+ );
96
+ }
97
+ }
@@ -1,5 +1,6 @@
1
1
  import ts, { Node, EnumDeclaration } from 'typescript';
2
- import { EnumSchema } from '@teambit/semantics.entities.semantic-schema';
2
+ import { EnumMemberSchema, EnumSchema } from '@teambit/semantics.entities.semantic-schema';
3
+ import pMapSeries from 'p-map-series';
3
4
  import { SchemaTransformer } from '../schema-transformer';
4
5
  import { SchemaExtractorContext } from '../schema-extractor-context';
5
6
  import { ExportIdentifier } from '../export-identifier';
@@ -14,7 +15,17 @@ export class EnumDeclarationTransformer implements SchemaTransformer {
14
15
  }
15
16
 
16
17
  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);
18
+ const name = enumDec.name.getText();
19
+ const members = await pMapSeries(enumDec.members, async (member) => {
20
+ const memberName = member.name.getText();
21
+ const memberSignature = await context.getQuickInfoDisplayString(member);
22
+ const memberDoc = await context.jsDocToDocSchema(member);
23
+ const memberLocation = await context.getLocation(member);
24
+ const memberValue = member.initializer?.getText();
25
+ return new EnumMemberSchema(memberLocation, memberName, memberSignature, memberValue, memberDoc);
26
+ });
27
+ const signature = await context.getQuickInfoDisplayString(enumDec.name);
28
+ const doc = await context.jsDocToDocSchema(enumDec);
29
+ return new EnumSchema(context.getLocation(enumDec), name, members, signature, doc);
19
30
  }
20
31
  }
@@ -1,4 +1,4 @@
1
- import { SchemaNode, Module, UnresolvedSchema } from '@teambit/semantics.entities.semantic-schema';
1
+ import { SchemaNode, ModuleSchema, UnresolvedSchema } from '@teambit/semantics.entities.semantic-schema';
2
2
  import ts, {
3
3
  Node,
4
4
  SyntaxKind,
@@ -54,7 +54,7 @@ export class ExportDeclaration implements SchemaTransformer {
54
54
  // e.g. `export { button1, button2 } as Composition from './button';
55
55
  if (exportClause.kind === SyntaxKind.NamedExports) {
56
56
  const schemas = await namedExport(exportClause, context);
57
- return new Module(context.getLocation(exportDec), schemas);
57
+ return new ModuleSchema(context.getLocation(exportDec), schemas);
58
58
  }
59
59
  // e.g. `export * as Composition from './button';
60
60
  if (exportClause.kind === SyntaxKind.NamespaceExport) {
@@ -128,7 +128,7 @@ async function namespaceExport(
128
128
  return context.getTypeRefForExternalPath(namespace, filePath, context.getLocation(exportDec));
129
129
  }
130
130
  const result = await context.computeSchema(sourceFile);
131
- if (!(result instanceof Module)) {
131
+ if (!(result instanceof ModuleSchema)) {
132
132
  throw new Error(`expect result to be instance of Module`);
133
133
  }
134
134
  result.namespace = namespace;
@@ -4,7 +4,7 @@ export { VariableStatementTransformer } from './variable-statement';
4
4
  export { VariableDeclaration } from './variable-declaration';
5
5
  export { SourceFileTransformer } from './source-file-transformer';
6
6
  export { TypeAliasTransformer } from './type-alias';
7
- export { ClassDecelerationTransformer } from './class-deceleration';
7
+ export { ClassDeclarationTransformer } from './class-declaration';
8
8
  export { InterfaceDeclarationTransformer } from './interface-declaration';
9
9
  export { EnumDeclarationTransformer } from './enum-declaration';
10
10
  export { BindingElementTransformer } from './binding-element';
@@ -1,11 +1,15 @@
1
- import { Node, InterfaceDeclaration, SyntaxKind } from 'typescript';
1
+ import ts, { Node, InterfaceDeclaration, SyntaxKind } from 'typescript';
2
2
  import pMapSeries from 'p-map-series';
3
- import { InterfaceSchema } from '@teambit/semantics.entities.semantic-schema';
3
+ import {
4
+ ExpressionWithTypeArgumentsSchema,
5
+ InterfaceSchema,
6
+ UnresolvedSchema,
7
+ } from '@teambit/semantics.entities.semantic-schema';
4
8
  import { SchemaTransformer } from '../schema-transformer';
5
9
  import { SchemaExtractorContext } from '../schema-extractor-context';
6
10
  import { ExportIdentifier } from '../export-identifier';
7
11
  import { typeElementToSchema } from './utils/type-element-to-schema';
8
- import { jsDocToDocSchema } from './utils/jsdoc-to-doc-schema';
12
+ import { typeNodeToSchema } from './utils/type-node-to-schema';
9
13
 
10
14
  export class InterfaceDeclarationTransformer implements SchemaTransformer {
11
15
  predicate(node: Node) {
@@ -16,9 +20,49 @@ export class InterfaceDeclarationTransformer implements SchemaTransformer {
16
20
  return [new ExportIdentifier(node.name.getText(), node.getSourceFile().fileName)];
17
21
  }
18
22
 
23
+ private async getExpressionWithTypeArgs(node: InterfaceDeclaration, context: SchemaExtractorContext) {
24
+ if (!node.heritageClauses) return [];
25
+
26
+ return pMapSeries(
27
+ node.heritageClauses
28
+ .filter((heritageClause: ts.HeritageClause) => heritageClause.token === SyntaxKind.ExtendsKeyword)
29
+ .flatMap((h: ts.HeritageClause) => {
30
+ const { types } = h;
31
+ const name = h.getText();
32
+ return types.map((type) => ({ ...type, name }));
33
+ }),
34
+ async (expressionWithTypeArgs: ts.ExpressionWithTypeArguments & { name: string }) => {
35
+ const { typeArguments, expression, name } = expressionWithTypeArgs;
36
+ const typeArgsNodes = typeArguments ? await pMapSeries(typeArguments, (t) => typeNodeToSchema(t, context)) : [];
37
+ const location = context.getLocation(expression);
38
+ const expressionNode =
39
+ (await context.visitDefinition(expression)) || new UnresolvedSchema(location, expression.getText());
40
+ return new ExpressionWithTypeArgumentsSchema(typeArgsNodes, expressionNode, name, location);
41
+ }
42
+ );
43
+ }
44
+
19
45
  async transform(interfaceDec: InterfaceDeclaration, context: SchemaExtractorContext) {
20
46
  const members = await pMapSeries(interfaceDec.members, (member) => typeElementToSchema(member, context));
21
- const doc = await jsDocToDocSchema(interfaceDec, context);
22
- return new InterfaceSchema(context.getLocation(interfaceDec), interfaceDec.name.getText(), members, doc);
47
+ const doc = await context.jsDocToDocSchema(interfaceDec);
48
+ const signature = interfaceDec.name ? await context.getQuickInfoDisplayString(interfaceDec.name) : undefined;
49
+ const extendsNodes = await this.getExpressionWithTypeArgs(interfaceDec, context);
50
+ const typeParameters = interfaceDec.typeParameters?.map((typeParam) => {
51
+ return typeParam.name.getText();
52
+ });
53
+
54
+ if (!signature) {
55
+ throw Error(`Missing signature for interface ${interfaceDec.name.getText()} declaration`);
56
+ }
57
+
58
+ return new InterfaceSchema(
59
+ context.getLocation(interfaceDec),
60
+ interfaceDec.name.getText(),
61
+ signature,
62
+ extendsNodes,
63
+ members,
64
+ doc,
65
+ typeParameters
66
+ );
23
67
  }
24
68
  }
@@ -1,7 +1,7 @@
1
1
  import ts, { Node, SourceFile } from 'typescript';
2
2
  import { compact, flatten } from 'lodash';
3
3
  import pMapSeries from 'p-map-series';
4
- import { Module } from '@teambit/semantics.entities.semantic-schema';
4
+ import { ModuleSchema } from '@teambit/semantics.entities.semantic-schema';
5
5
  import { SchemaTransformer } from '../schema-transformer';
6
6
  import { ExportIdentifier } from '../export-identifier';
7
7
  import { SchemaExtractorContext } from '../schema-extractor-context';
@@ -35,7 +35,7 @@ export class SourceFileTransformer implements SchemaTransformer {
35
35
  return context.computeSchema(exportNode);
36
36
  });
37
37
 
38
- return new Module(context.getLocation(node), schemas);
38
+ return new ModuleSchema(context.getLocation(node), schemas);
39
39
  }
40
40
 
41
41
  /**
@@ -4,7 +4,6 @@ import { SchemaTransformer } from '../schema-transformer';
4
4
  import { SchemaExtractorContext } from '../schema-extractor-context';
5
5
  import { ExportIdentifier } from '../export-identifier';
6
6
  import { typeNodeToSchema } from './utils/type-node-to-schema';
7
- import { jsDocToDocSchema } from './utils/jsdoc-to-doc-schema';
8
7
 
9
8
  export class TypeAliasTransformer implements SchemaTransformer {
10
9
  predicate(node: Node) {
@@ -22,7 +21,7 @@ export class TypeAliasTransformer implements SchemaTransformer {
22
21
  async transform(typeAlias: TypeAliasDeclaration, context: SchemaExtractorContext) {
23
22
  const type = await typeNodeToSchema(typeAlias.type, context);
24
23
  const displaySig = await context.getQuickInfoDisplayString(typeAlias.name);
25
- const doc = await jsDocToDocSchema(typeAlias, context);
24
+ const doc = await context.jsDocToDocSchema(typeAlias);
26
25
  return new TypeSchema(context.getLocation(typeAlias), this.getName(typeAlias), type, displaySig, doc);
27
26
  }
28
27
  }
@@ -14,7 +14,6 @@ import { getParams } from './get-params';
14
14
  import { getAccessor, indexSignature, setAccessor } from './type-element-to-schema';
15
15
  import { parseTypeFromQuickInfo } from './parse-type-from-quick-info';
16
16
  import { toFunctionLikeSchema } from './to-function-like-schema';
17
- import { jsDocToDocSchema } from './jsdoc-to-doc-schema';
18
17
 
19
18
  export async function classElementToSchema(
20
19
  node: ClassElement,
@@ -44,7 +43,9 @@ export async function classElementToSchema(
44
43
 
45
44
  async function constructor(node: ConstructorDeclaration, context: SchemaExtractorContext) {
46
45
  const args = await getParams(node.parameters, context);
47
- return new ConstructorSchema(context.getLocation(node), args);
46
+ const info = await context.getQuickInfo(node);
47
+ const displaySig = info?.body?.displayString;
48
+ return new ConstructorSchema(context.getLocation(node), args, displaySig);
48
49
  }
49
50
 
50
51
  async function propertyDeclaration(node: PropertyDeclaration, context: SchemaExtractorContext) {
@@ -54,7 +55,7 @@ async function propertyDeclaration(node: PropertyDeclaration, context: SchemaExt
54
55
  const typeStr = parseTypeFromQuickInfo(info);
55
56
  const type = await context.resolveType(node, typeStr);
56
57
  const isOptional = Boolean(node.questionToken);
57
- const doc = await jsDocToDocSchema(node, context);
58
+ const doc = await context.jsDocToDocSchema(node);
58
59
  return new VariableLikeSchema(context.getLocation(node), name, displaySig || '', type, isOptional, doc);
59
60
  }
60
61
 
@@ -28,7 +28,9 @@ export async function getParams(
28
28
  getParamName(param),
29
29
  await getParamType(param, context),
30
30
  Boolean(param.questionToken),
31
- param.initializer ? param.initializer.getText() : undefined
31
+ param.initializer ? param.initializer.getText() : undefined,
32
+ undefined,
33
+ await getParamObjectBindingNodes(param, context)
32
34
  );
33
35
  });
34
36
  }
@@ -47,6 +49,18 @@ function getParamName(param: ParameterDeclaration): string {
47
49
  return `{ ${elementsStr} }`;
48
50
  }
49
51
 
52
+ async function getParamObjectBindingNodes(
53
+ param: ParameterDeclaration,
54
+ context: SchemaExtractorContext
55
+ ): Promise<SchemaNode[] | undefined> {
56
+ if (param.name.kind !== SyntaxKind.ObjectBindingPattern) return undefined;
57
+ return pMapSeries(param.name.elements, async (elem: BindingElement) => {
58
+ const info = await context.getQuickInfo(elem.name);
59
+ const parsed = parseTypeFromQuickInfo(info);
60
+ return new InferenceTypeSchema(context.getLocation(param), parsed, elem.name.getText());
61
+ });
62
+ }
63
+
50
64
  async function getParamType(param: ParameterDeclaration, context: SchemaExtractorContext): Promise<SchemaNode> {
51
65
  if (param.type) {
52
66
  const type = param.type;
@@ -6,39 +6,23 @@ import {
6
6
  JSDocPropertyTag,
7
7
  JSDocReturnTag,
8
8
  JSDocTag,
9
- Node,
10
9
  SyntaxKind,
11
10
  } from 'typescript';
12
- import { getJsDoc, canHaveJsDoc } from 'tsutils';
13
- import pMapSeries from 'p-map-series';
14
11
  import {
15
- DocSchema,
16
12
  PropertyLikeTagSchema,
17
13
  ReturnTagSchema,
18
14
  TagName,
19
15
  TagSchema,
20
16
  } from '@teambit/semantics.entities.semantic-schema';
17
+ import { Formatter } from '@teambit/formatter';
21
18
  import { SchemaExtractorContext } from '../../schema-extractor-context';
22
19
  import { typeNodeToSchema } from './type-node-to-schema';
23
20
 
24
- export async function jsDocToDocSchema(node: Node, context: SchemaExtractorContext): Promise<DocSchema | undefined> {
25
- if (!canHaveJsDoc(node)) {
26
- return undefined;
27
- }
28
- const jsDocs = getJsDoc(node);
29
- if (!jsDocs.length) {
30
- return undefined;
31
- }
32
- // not sure how common it is to have multiple JSDocs. never seen it before.
33
- // regardless, in typescript implementation of methods like `getJSDocDeprecatedTag()`, they use the first one. (`getFirstJSDocTag()`)
34
- const jsDoc = jsDocs[0];
35
- const location = context.getLocation(jsDoc);
36
- const comment = getTextOfJSDocComment(jsDoc.comment);
37
- const tags = jsDoc.tags ? await pMapSeries(jsDoc.tags, (tag) => tagParser(tag, context)) : undefined;
38
- return new DocSchema(location, jsDoc.getText(), comment, tags);
39
- }
40
-
41
- async function tagParser(tag: JSDocTag, context: SchemaExtractorContext): Promise<TagSchema> {
21
+ export async function tagParser(
22
+ tag: JSDocTag,
23
+ context: SchemaExtractorContext,
24
+ formatter: Formatter
25
+ ): Promise<TagSchema> {
42
26
  // for some reason, in some cases, if `tag.getSourceFile()` is not provided to the `getText()`, it throws "Cannot read property 'text' of undefined"
43
27
 
44
28
  switch (tag.kind) {
@@ -84,6 +68,15 @@ async function tagParser(tag: JSDocTag, context: SchemaExtractorContext): Promis
84
68
  return simpleTag(tag, TagName.implements, context);
85
69
  default: {
86
70
  const tagName: TagName | string = tag.tagName.getText(tag.getSourceFile());
71
+ if (tagName === 'example') {
72
+ const comment = getTextOfJSDocComment(tag.comment);
73
+ try {
74
+ const formattedComment = comment && (await formatter.formatSnippet(comment));
75
+ return new TagSchema(context.getLocation(tag), tagName, formattedComment);
76
+ } catch (e) {
77
+ return simpleTag(tag, tagName, context);
78
+ }
79
+ }
87
80
  return simpleTag(tag, tagName, context);
88
81
  }
89
82
  }
@@ -40,6 +40,9 @@ export function parseTypeFromQuickInfo(quickInfo: protocol.QuickInfoResponse | u
40
40
  const displayString = quickInfo.body.displayString;
41
41
  const splitByColon = displayString.split(':');
42
42
  switch (quickInfo.body.kind) {
43
+ case 'type parameter':
44
+ // (type parameter) T in concat<T, K, V>(array1: T[], array2: T[]): T[]
45
+ return displayString.replace(`(${quickInfo.body.kind}) `, '').split(' ')[0];
43
46
  case 'const':
44
47
  case 'property':
45
48
  case 'let':
@@ -79,6 +82,10 @@ export function parseTypeFromQuickInfo(quickInfo: protocol.QuickInfoResponse | u
79
82
  const [, ...tail] = splitByEqual;
80
83
  return tail.join('=').trim();
81
84
  }
85
+ case 'parameter': {
86
+ const typeColonIndex = displayString.indexOf(':');
87
+ return displayString.slice(typeColonIndex + 1).trim();
88
+ }
82
89
  default:
83
90
  return splitByColon[splitByColon.length - 1].trim();
84
91
  }
@@ -3,7 +3,6 @@ import { FunctionLikeSchema, Modifier } from '@teambit/semantics.entities.semant
3
3
  import { SchemaExtractorContext } from '../../schema-extractor-context';
4
4
  import { getParams } from './get-params';
5
5
  import { parseTypeFromQuickInfo } from './parse-type-from-quick-info';
6
- import { jsDocToDocSchema } from './jsdoc-to-doc-schema';
7
6
 
8
7
  export async function toFunctionLikeSchema(
9
8
  node: SignatureDeclaration,
@@ -21,11 +20,12 @@ export async function toFunctionLikeSchema(
21
20
  const returnTypeStr = info ? parseTypeFromQuickInfo(info) : 'any';
22
21
  const displaySig = info?.body?.displayString || '';
23
22
  const args = await getParams(node.parameters, context);
23
+
24
24
  const returnType = await context.resolveType(node, returnTypeStr, Boolean(info));
25
25
  const modifiers = node.modifiers?.map((modifier) => modifier.getText()) || [];
26
26
  const typeParameters = node.typeParameters?.map((typeParam) => typeParam.name.getText());
27
27
  const location = context.getLocation(node);
28
- const doc = await jsDocToDocSchema(node, context);
28
+ const doc = await context.jsDocToDocSchema(node);
29
29
  return new FunctionLikeSchema(
30
30
  location,
31
31
  name,
@@ -21,7 +21,6 @@ import { SchemaExtractorContext } from '../../schema-extractor-context';
21
21
  import { parseTypeFromQuickInfo } from './parse-type-from-quick-info';
22
22
  import { typeNodeToSchema } from './type-node-to-schema';
23
23
  import { getParams } from './get-params';
24
- import { jsDocToDocSchema } from './jsdoc-to-doc-schema';
25
24
 
26
25
  export async function typeElementToSchema(node: TypeElement, context: SchemaExtractorContext): Promise<SchemaNode> {
27
26
  switch (node.kind) {
@@ -51,7 +50,7 @@ async function propertySignature(node: ts.PropertySignature, context: SchemaExtr
51
50
  const typeStr = parseTypeFromQuickInfo(info);
52
51
  const type = await context.resolveType(node, typeStr);
53
52
  const isOptional = Boolean(node.questionToken);
54
- const doc = await jsDocToDocSchema(node, context);
53
+ const doc = await context.jsDocToDocSchema(node);
55
54
  return new VariableLikeSchema(context.getLocation(node), name, displaySig, type, isOptional, doc);
56
55
  }
57
56
 
@@ -19,6 +19,7 @@ import {
19
19
  TemplateLiteralTypeSpan,
20
20
  ThisTypeNode,
21
21
  ConditionalTypeNode,
22
+ NamedTupleMember,
22
23
  } from 'typescript';
23
24
  import {
24
25
  SchemaNode,
@@ -41,13 +42,13 @@ import {
41
42
  ThisTypeSchema,
42
43
  Modifier,
43
44
  ConditionalTypeSchema,
45
+ NamedTupleSchema,
44
46
  UnImplementedSchema,
45
47
  } from '@teambit/semantics.entities.semantic-schema';
46
48
  import pMapSeries from 'p-map-series';
47
49
  import { SchemaExtractorContext } from '../../schema-extractor-context';
48
50
  import { getParams } from './get-params';
49
51
  import { typeElementToSchema } from './type-element-to-schema';
50
- import { jsDocToDocSchema } from './jsdoc-to-doc-schema';
51
52
 
52
53
  // eslint-disable-next-line complexity
53
54
  export async function typeNodeToSchema(node: TypeNode, context: SchemaExtractorContext): Promise<SchemaNode> {
@@ -90,8 +91,9 @@ export async function typeNodeToSchema(node: TypeNode, context: SchemaExtractorC
90
91
  return thisType(node as ThisTypeNode, context);
91
92
  case SyntaxKind.ConditionalType:
92
93
  return conditionalType(node as ConditionalTypeNode, context);
93
- case SyntaxKind.ConstructorType:
94
94
  case SyntaxKind.NamedTupleMember:
95
+ return namedTupleType(node as NamedTupleMember, context);
96
+ case SyntaxKind.ConstructorType:
95
97
  case SyntaxKind.OptionalType:
96
98
  case SyntaxKind.RestType:
97
99
  case SyntaxKind.InferType:
@@ -192,7 +194,7 @@ async function functionType(node: FunctionTypeNode, context: SchemaExtractorCont
192
194
  const returnType = await typeNodeToSchema(node.type, context);
193
195
  const location = context.getLocation(node);
194
196
  const modifiers = node.modifiers?.map((modifier) => modifier.getText()) || [];
195
- const doc = await jsDocToDocSchema(node, context);
197
+ const doc = await context.jsDocToDocSchema(node);
196
198
  return new FunctionLikeSchema(location, name, params, returnType, '', modifiers as Modifier[], doc);
197
199
  }
198
200
 
@@ -283,3 +285,10 @@ async function conditionalType(node: ConditionalTypeNode, context: SchemaExtract
283
285
  const falseType = await typeNodeToSchema(node.falseType, context);
284
286
  return new ConditionalTypeSchema(context.getLocation(node), checkType, extendsType, trueType, falseType);
285
287
  }
288
+
289
+ async function namedTupleType(node: NamedTupleMember, context: SchemaExtractorContext) {
290
+ const name = node.name.getText();
291
+ const location = context.getLocation(node);
292
+ const type = await typeNodeToSchema(node.type, context);
293
+ return new NamedTupleSchema(location, type, name);
294
+ }
@@ -10,7 +10,6 @@ import { SchemaExtractorContext } from '../schema-extractor-context';
10
10
  import { ExportIdentifier } from '../export-identifier';
11
11
  import { getParams } from './utils/get-params';
12
12
  import { parseReturnTypeFromQuickInfo, parseTypeFromQuickInfo } from './utils/parse-type-from-quick-info';
13
- import { jsDocToDocSchema } from './utils/jsdoc-to-doc-schema';
14
13
 
15
14
  export class VariableDeclaration implements SchemaTransformer {
16
15
  predicate(node: Node) {
@@ -30,12 +29,27 @@ export class VariableDeclaration implements SchemaTransformer {
30
29
  const info = await context.getQuickInfo(varDec.name);
31
30
  const displaySig = info?.body?.displayString || '';
32
31
  const location = context.getLocation(varDec);
33
- const doc = await jsDocToDocSchema(varDec, context);
32
+ const doc = await context.jsDocToDocSchema(varDec);
33
+ const modifiers = varDec.modifiers?.map((modifier) => modifier.getText()) || [];
34
34
  if (varDec.initializer?.kind === ts.SyntaxKind.ArrowFunction) {
35
35
  const args = await getParams((varDec.initializer as ArrowFunction).parameters, context);
36
+ // example => export const useLanesContext: () => LanesContextModel | undefined = () => {
37
+ if (varDec.type) {
38
+ const funcType = await context.resolveType(varDec, '');
39
+ if (isFunctionLike(funcType)) {
40
+ return new FunctionLikeSchema(
41
+ location,
42
+ name,
43
+ funcType.params,
44
+ funcType.returnType,
45
+ displaySig,
46
+ modifiers as Modifier[],
47
+ doc
48
+ );
49
+ }
50
+ }
36
51
  const typeStr = parseReturnTypeFromQuickInfo(info);
37
52
  const returnType = await context.resolveType(varDec, typeStr);
38
- const modifiers = varDec.modifiers?.map((modifier) => modifier.getText()) || [];
39
53
  return new FunctionLikeSchema(location, name, args, returnType, displaySig, modifiers as Modifier[], doc);
40
54
  }
41
55
  const typeStr = parseTypeFromQuickInfo(info);
@@ -43,3 +57,7 @@ export class VariableDeclaration implements SchemaTransformer {
43
57
  return new VariableLikeSchema(location, name, displaySig, type, false, doc);
44
58
  }
45
59
  }
60
+
61
+ function isFunctionLike(node: SchemaNode): node is FunctionLikeSchema {
62
+ return node instanceof FunctionLikeSchema;
63
+ }
@@ -1,4 +1,4 @@
1
- import { SchemaNode, Module } from '@teambit/semantics.entities.semantic-schema';
1
+ import { SchemaNode, ModuleSchema } from '@teambit/semantics.entities.semantic-schema';
2
2
  import { compact } from 'lodash';
3
3
  import pMapSeries from 'p-map-series';
4
4
  import ts, { Node, VariableStatement } from 'typescript';
@@ -27,6 +27,6 @@ export class VariableStatementTransformer implements SchemaTransformer {
27
27
  const schema = await context.visitDefinition(dec.name);
28
28
  return schema;
29
29
  });
30
- return new Module(context.getLocation(node), compact(schemas));
30
+ return new ModuleSchema(context.getLocation(node), compact(schemas));
31
31
  }
32
32
  }