@teambit/semantics.entities.semantic-schema 0.0.9 → 0.0.12

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 (138) hide show
  1. package/api-schema.ts +29 -25
  2. package/dist/api-schema.d.ts +6 -19
  3. package/dist/api-schema.js +23 -13
  4. package/dist/api-schema.js.map +1 -1
  5. package/dist/{schemas/export.d.ts → export.d.ts} +5 -10
  6. package/dist/{schemas/export.js → export.js} +4 -5
  7. package/dist/export.js.map +1 -0
  8. package/dist/index.d.ts +3 -2
  9. package/dist/index.js +8 -3
  10. package/dist/index.js.map +1 -1
  11. package/dist/schema-node.d.ts +14 -7
  12. package/dist/schema-node.js +16 -0
  13. package/dist/schema-node.js.map +1 -1
  14. package/dist/schema-obj-to-class.d.ts +10 -0
  15. package/dist/schema-obj-to-class.js +56 -0
  16. package/dist/schema-obj-to-class.js.map +1 -0
  17. package/dist/schemas/class.d.ts +4 -10
  18. package/dist/schemas/class.js +16 -9
  19. package/dist/schemas/class.js.map +1 -1
  20. package/dist/schemas/constructor.d.ts +4 -15
  21. package/dist/schemas/constructor.js +16 -8
  22. package/dist/schemas/constructor.js.map +1 -1
  23. package/dist/schemas/enum.d.ts +8 -0
  24. package/dist/schemas/enum.js +21 -0
  25. package/dist/schemas/enum.js.map +1 -1
  26. package/dist/schemas/function-like.d.ts +18 -0
  27. package/dist/schemas/function-like.js +51 -0
  28. package/dist/schemas/function-like.js.map +1 -0
  29. package/dist/schemas/get-accessor.d.ts +7 -14
  30. package/dist/schemas/get-accessor.js +17 -11
  31. package/dist/schemas/get-accessor.js.map +1 -1
  32. package/dist/schemas/index-signature.d.ts +6 -12
  33. package/dist/schemas/index-signature.js +19 -9
  34. package/dist/schemas/index-signature.js.map +1 -1
  35. package/dist/schemas/index.d.ts +5 -2
  36. package/dist/schemas/index.js +11 -5
  37. package/dist/schemas/index.js.map +1 -1
  38. package/dist/schemas/indexed-access-type.d.ts +8 -0
  39. package/dist/schemas/indexed-access-type.js +31 -0
  40. package/dist/schemas/indexed-access-type.js.map +1 -0
  41. package/dist/schemas/inference-type.d.ts +6 -8
  42. package/dist/schemas/inference-type.js +9 -8
  43. package/dist/schemas/inference-type.js.map +1 -1
  44. package/dist/schemas/interface.d.ts +6 -12
  45. package/dist/schemas/interface.js +16 -9
  46. package/dist/schemas/interface.js.map +1 -1
  47. package/dist/schemas/keyword-type.d.ts +4 -7
  48. package/dist/schemas/keyword-type.js +5 -8
  49. package/dist/schemas/keyword-type.js.map +1 -1
  50. package/dist/schemas/literal-type.d.ts +5 -4
  51. package/dist/schemas/literal-type.js +5 -2
  52. package/dist/schemas/literal-type.js.map +1 -1
  53. package/dist/schemas/module.d.ts +5 -8
  54. package/dist/schemas/module.js +16 -6
  55. package/dist/schemas/module.js.map +1 -1
  56. package/dist/schemas/parameter.d.ts +5 -13
  57. package/dist/schemas/parameter.js +17 -12
  58. package/dist/schemas/parameter.js.map +1 -1
  59. package/dist/schemas/parenthesized-type.d.ts +11 -0
  60. package/dist/schemas/parenthesized-type.js +31 -0
  61. package/dist/schemas/parenthesized-type.js.map +1 -0
  62. package/dist/schemas/set-accessor.d.ts +7 -12
  63. package/dist/schemas/set-accessor.js +17 -11
  64. package/dist/schemas/set-accessor.js.map +1 -1
  65. package/dist/schemas/tuple-type.d.ts +5 -10
  66. package/dist/schemas/tuple-type.js +16 -8
  67. package/dist/schemas/tuple-type.js.map +1 -1
  68. package/dist/schemas/type-array.d.ts +5 -10
  69. package/dist/schemas/type-array.js +16 -8
  70. package/dist/schemas/type-array.js.map +1 -1
  71. package/dist/schemas/type-intersection.d.ts +5 -10
  72. package/dist/schemas/type-intersection.js +16 -8
  73. package/dist/schemas/type-intersection.js.map +1 -1
  74. package/dist/schemas/type-literal.d.ts +5 -10
  75. package/dist/schemas/type-literal.js +16 -8
  76. package/dist/schemas/type-literal.js.map +1 -1
  77. package/dist/schemas/type-operator.d.ts +6 -12
  78. package/dist/schemas/type-operator.js +16 -9
  79. package/dist/schemas/type-operator.js.map +1 -1
  80. package/dist/schemas/type-predicate.d.ts +22 -0
  81. package/dist/schemas/type-predicate.js +44 -0
  82. package/dist/schemas/type-predicate.js.map +1 -0
  83. package/dist/schemas/type-query.d.ts +6 -10
  84. package/dist/schemas/type-query.js +17 -10
  85. package/dist/schemas/type-query.js.map +1 -1
  86. package/dist/schemas/type-ref.d.ts +15 -10
  87. package/dist/schemas/type-ref.js +34 -16
  88. package/dist/schemas/type-ref.js.map +1 -1
  89. package/dist/schemas/type-union.d.ts +5 -10
  90. package/dist/schemas/type-union.js +16 -8
  91. package/dist/schemas/type-union.js.map +1 -1
  92. package/dist/schemas/type.d.ts +7 -14
  93. package/dist/schemas/type.js +17 -11
  94. package/dist/schemas/type.js.map +1 -1
  95. package/dist/schemas/variable.d.ts +9 -14
  96. package/dist/schemas/variable.js +19 -13
  97. package/dist/schemas/variable.js.map +1 -1
  98. package/{schemas/export.ts → export.ts} +5 -8
  99. package/index.ts +7 -4
  100. package/package-tar/teambit-semantics.entities.semantic-schema-0.0.12.tgz +0 -0
  101. package/package.json +7 -4
  102. package/{preview-1652498684867.js → preview-1653362849981.js} +1 -1
  103. package/schema-node.ts +15 -5
  104. package/schema-obj-to-class.ts +36 -0
  105. package/schemas/class.ts +9 -10
  106. package/schemas/constructor.ts +10 -10
  107. package/schemas/enum.ts +13 -0
  108. package/schemas/function-like.ts +56 -0
  109. package/schemas/get-accessor.ts +10 -12
  110. package/schemas/index-signature.ts +13 -9
  111. package/schemas/index.ts +5 -2
  112. package/schemas/indexed-access-type.ts +18 -0
  113. package/schemas/inference-type.ts +7 -9
  114. package/schemas/interface.ts +10 -9
  115. package/schemas/keyword-type.ts +4 -9
  116. package/schemas/literal-type.ts +5 -3
  117. package/schemas/module.ts +9 -7
  118. package/schemas/parameter.ts +12 -13
  119. package/schemas/parenthesized-type.ts +20 -0
  120. package/schemas/set-accessor.ts +10 -12
  121. package/schemas/tuple-type.ts +10 -8
  122. package/schemas/type-array.ts +10 -8
  123. package/schemas/type-intersection.ts +10 -8
  124. package/schemas/type-literal.ts +10 -8
  125. package/schemas/type-operator.ts +10 -9
  126. package/schemas/type-predicate.ts +36 -0
  127. package/schemas/type-query.ts +10 -11
  128. package/schemas/type-ref.ts +39 -20
  129. package/schemas/type-union.ts +9 -8
  130. package/schemas/type.ts +10 -10
  131. package/schemas/variable.ts +12 -14
  132. package/tsconfig.json +1 -0
  133. package/dist/schemas/export.js.map +0 -1
  134. package/dist/schemas/function.d.ts +0 -25
  135. package/dist/schemas/function.js +0 -40
  136. package/dist/schemas/function.js.map +0 -1
  137. package/package-tar/teambit-semantics.entities.semantic-schema-0.0.9.tgz +0 -0
  138. package/schemas/function.ts +0 -42
@@ -0,0 +1,56 @@
1
+ import { Transform } from 'class-transformer';
2
+ import chalk from 'chalk';
3
+ import { Location, SchemaNode } from '../schema-node';
4
+ import { schemaObjArrayToInstances, schemaObjToInstance } from '../schema-obj-to-class';
5
+ import { ParameterSchema } from './parameter';
6
+
7
+ export type Modifier =
8
+ | 'static'
9
+ | 'public'
10
+ | 'private'
11
+ | 'protected'
12
+ | 'readonly'
13
+ | 'abstract'
14
+ | 'async'
15
+ | 'override'
16
+ | 'export';
17
+
18
+ /**
19
+ * function-like can be a function, method, arrow-function, variable-function, etc.
20
+ */
21
+ export class FunctionLikeSchema extends SchemaNode {
22
+ @Transform(schemaObjToInstance)
23
+ readonly returnType: SchemaNode;
24
+
25
+ @Transform(schemaObjArrayToInstances)
26
+ readonly params: ParameterSchema[];
27
+
28
+ constructor(
29
+ readonly location: Location,
30
+ readonly name: string,
31
+ // readonly doc: any,
32
+ params: ParameterSchema[],
33
+
34
+ returnType: SchemaNode,
35
+ readonly signature: string,
36
+ readonly modifiers: Modifier[] = []
37
+ ) {
38
+ super();
39
+ this.params = params;
40
+ this.returnType = returnType;
41
+ }
42
+
43
+ getSignature() {
44
+ return this.signature;
45
+ }
46
+
47
+ toString() {
48
+ const paramsStr = this.params.map((param) => param.toString()).join(', ');
49
+ return `${this.modifiersToString()}${chalk.bold(this.name)}(${paramsStr}): ${this.returnType.toString()}`;
50
+ }
51
+
52
+ private modifiersToString() {
53
+ const modifiersToPrint = this.modifiers.filter((modifier) => modifier !== 'export');
54
+ return modifiersToPrint.length ? `${modifiersToPrint.join(' ')} ` : '';
55
+ }
56
+ }
@@ -1,21 +1,19 @@
1
+ import { Transform } from 'class-transformer';
1
2
  import chalk from 'chalk';
2
- import { SchemaNode } from '../schema-node';
3
+ import { Location, SchemaNode } from '../schema-node';
4
+ import { schemaObjToInstance } from '../schema-obj-to-class';
3
5
 
4
- export class GetAccessorSchema implements SchemaNode {
5
- constructor(private name: string, private type: SchemaNode, private signature: string) {}
6
+ export class GetAccessorSchema extends SchemaNode {
7
+ @Transform(schemaObjToInstance)
8
+ readonly type: SchemaNode;
9
+ constructor(readonly location: Location, readonly name: string, type: SchemaNode, readonly signature: string) {
10
+ super();
11
+ this.type = type;
12
+ }
6
13
  getSignature() {
7
14
  return this.signature;
8
15
  }
9
16
 
10
- toObject() {
11
- return {
12
- constructorName: this.constructor.name,
13
- name: this.name,
14
- type: this.type.toObject(),
15
- signature: this.signature,
16
- };
17
- }
18
-
19
17
  toString() {
20
18
  return `get ${chalk.bold(this.name)}(): ${this.type.toString()}`;
21
19
  }
@@ -1,19 +1,23 @@
1
- import { SchemaNode } from '../schema-node';
1
+ import { Transform } from 'class-transformer';
2
+ import { Location, SchemaNode } from '../schema-node';
2
3
  import { ParameterSchema } from './parameter';
4
+ import { schemaObjArrayToInstances, schemaObjToInstance } from '../schema-obj-to-class';
3
5
 
4
6
  /**
5
7
  * e.g. `{ [key: string]: boolean };`
6
8
  * the "[key: string]" is the "parameter", and the "boolean" is the "type".
7
9
  */
8
- export class IndexSignatureSchema implements SchemaNode {
9
- constructor(private params: ParameterSchema[], private type: SchemaNode) {}
10
- toObject() {
11
- return {
12
- constructorName: this.constructor.name,
13
- parameters: this.params,
14
- type: this.type.toObject(),
15
- };
10
+ export class IndexSignatureSchema extends SchemaNode {
11
+ @Transform(schemaObjArrayToInstances)
12
+ readonly params: ParameterSchema[];
13
+ @Transform(schemaObjToInstance)
14
+ readonly type: SchemaNode;
15
+ constructor(readonly location: Location, params: ParameterSchema[], type: SchemaNode) {
16
+ super();
17
+ this.params = params;
18
+ this.type = type;
16
19
  }
20
+
17
21
  toString() {
18
22
  const paramsStr = this.params.map((param) => param.toString()).join(', ');
19
23
  return `[${paramsStr}]: ${this.type.toString()}`;
package/schemas/index.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  export { Module } from './module';
2
- export { Export, StaticProperties } from './export';
3
- export { FunctionSchema, Modifier } from './function';
2
+ export { FunctionLikeSchema, Modifier } from './function-like';
4
3
  export { TypeRefSchema } from './type-ref';
5
4
  export { VariableSchema } from './variable';
6
5
  export { ClassSchema } from './class';
@@ -21,3 +20,7 @@ export { TypeArraySchema } from './type-array';
21
20
  export { TypeOperatorSchema } from './type-operator';
22
21
  export { TupleTypeSchema } from './tuple-type';
23
22
  export { ParameterSchema } from './parameter';
23
+ export { EnumSchema } from './enum';
24
+ export { ParenthesizedTypeSchema } from './parenthesized-type';
25
+ export { TypePredicateSchema } from './type-predicate';
26
+ export { IndexedAccessSchema } from './indexed-access-type';
@@ -0,0 +1,18 @@
1
+ import { Transform } from 'class-transformer';
2
+ import { Location, SchemaNode } from '../schema-node';
3
+ import { schemaObjToInstance } from '../schema-obj-to-class';
4
+
5
+ export class IndexedAccessSchema extends SchemaNode {
6
+ @Transform(schemaObjToInstance)
7
+ readonly objectType: SchemaNode;
8
+ @Transform(schemaObjToInstance)
9
+ readonly indexType: SchemaNode;
10
+ constructor(readonly location: Location, objectType: SchemaNode, indexType: SchemaNode) {
11
+ super();
12
+ this.objectType = objectType;
13
+ this.indexType = indexType;
14
+ }
15
+ toString() {
16
+ return `${this.objectType.toString()}[${this.indexType.toString()}]`;
17
+ }
18
+ }
@@ -1,19 +1,17 @@
1
- import { SchemaNode } from '../schema-node';
1
+ import { Location, SchemaNode } from '../schema-node';
2
2
 
3
3
  /**
4
4
  * where there is no explicit type, the type is taken from the "quickinfo" of tsserver
5
5
  */
6
- export class InferenceTypeSchema implements SchemaNode {
7
- constructor(private type: string) {}
8
-
9
- toObject() {
10
- return {
11
- constructorName: this.constructor.name,
12
- type: this.type,
13
- };
6
+ export class InferenceTypeSchema extends SchemaNode {
7
+ constructor(readonly location: Location, readonly type: string, readonly name?: string) {
8
+ super();
14
9
  }
15
10
 
16
11
  toString() {
12
+ if (this.name) {
13
+ return `${this.name}: ${this.type}`;
14
+ }
17
15
  return this.type;
18
16
  }
19
17
  }
@@ -1,15 +1,16 @@
1
+ import { Transform } from 'class-transformer';
1
2
  import chalk from 'chalk';
2
- import { SchemaNode } from '../schema-node';
3
+ import { Location, SchemaNode } from '../schema-node';
4
+ import { schemaObjArrayToInstances } from '../schema-obj-to-class';
3
5
 
4
- export class InterfaceSchema implements SchemaNode {
5
- constructor(private name: string, private members: SchemaNode[]) {}
6
- toObject() {
7
- return {
8
- constructorName: this.constructor.name,
9
- name: this.name,
10
- types: this.members.map((type) => type.toObject()),
11
- };
6
+ export class InterfaceSchema extends SchemaNode {
7
+ @Transform(schemaObjArrayToInstances)
8
+ readonly members: SchemaNode[];
9
+ constructor(readonly location: Location, readonly name: string, members: SchemaNode[]) {
10
+ super();
11
+ this.members = members;
12
12
  }
13
+
13
14
  toString() {
14
15
  const membersStr = this.members.map((m) => `* ${m.toString()}`).join('\n');
15
16
  return `${chalk.bold.underline(this.name)}\n${membersStr}`;
@@ -1,16 +1,11 @@
1
- import { SchemaNode } from '../schema-node';
1
+ import { Location, SchemaNode } from '../schema-node';
2
2
 
3
3
  /**
4
4
  * e.g. 'string', 'boolean', etc.
5
5
  */
6
- export class KeywordTypeSchema implements SchemaNode {
7
- constructor(private name: string) {}
8
-
9
- toObject() {
10
- return {
11
- constructorName: this.constructor.name,
12
- name: this.name,
13
- };
6
+ export class KeywordTypeSchema extends SchemaNode {
7
+ constructor(readonly location: Location, private name: string) {
8
+ super();
14
9
  }
15
10
 
16
11
  toString() {
@@ -1,10 +1,12 @@
1
- import { SchemaNode } from '../schema-node';
1
+ import { Location, SchemaNode } from '../schema-node';
2
2
 
3
3
  /**
4
4
  * e.g. const a: 'a';
5
5
  */
6
- export class LiteralTypeSchema implements SchemaNode {
7
- constructor(private name: string) {}
6
+ export class LiteralTypeSchema extends SchemaNode {
7
+ constructor(readonly location: Location, readonly name: string) {
8
+ super();
9
+ }
8
10
 
9
11
  toObject() {
10
12
  return {
package/schemas/module.ts CHANGED
@@ -1,13 +1,15 @@
1
1
  import chalk from 'chalk';
2
- import { SchemaNode } from '../schema-node';
3
- import { Export } from '../schemas';
2
+ import { Transform } from 'class-transformer';
3
+ import { Location, SchemaNode } from '../schema-node';
4
+ import { schemaObjArrayToInstances } from '../schema-obj-to-class';
4
5
 
5
- export class Module implements SchemaNode {
6
+ export class Module extends SchemaNode {
7
+ @Transform(schemaObjArrayToInstances)
8
+ exports: SchemaNode[];
6
9
  namespace?: string;
7
- constructor(public exports: SchemaNode[]) {}
8
-
9
- getExportSchemas(): Export[] {
10
- return this.exports.filter((e) => e instanceof Export) as Export[];
10
+ constructor(readonly location: Location, exports: SchemaNode[]) {
11
+ super();
12
+ this.exports = exports;
11
13
  }
12
14
 
13
15
  toObject() {
@@ -1,22 +1,21 @@
1
- import { SchemaNode } from '../schema-node';
1
+ import { Transform } from 'class-transformer';
2
+ import { Location, SchemaNode } from '../schema-node';
3
+ import { schemaObjToInstance } from '../schema-obj-to-class';
2
4
 
3
- export class ParameterSchema implements SchemaNode {
5
+ export class ParameterSchema extends SchemaNode {
6
+ @Transform(schemaObjToInstance)
7
+ readonly type: SchemaNode;
4
8
  constructor(
9
+ readonly location: Location,
5
10
  readonly name: string,
6
- readonly type: SchemaNode,
11
+ type: SchemaNode,
7
12
  readonly defaultValue?: any,
8
13
  readonly description?: string
9
- ) {}
10
-
11
- toObject() {
12
- return {
13
- constructorName: this.constructor.name,
14
- name: this.name,
15
- type: this.type.toObject(),
16
- defaultValue: this.defaultValue,
17
- description: this.description,
18
- };
14
+ ) {
15
+ super();
16
+ this.type = type;
19
17
  }
18
+
20
19
  toString() {
21
20
  return `${this.name}: ${this.type.toString()}`;
22
21
  }
@@ -0,0 +1,20 @@
1
+ import { Transform } from 'class-transformer';
2
+ import { Location, SchemaNode } from '../schema-node';
3
+ import { schemaObjToInstance } from '../schema-obj-to-class';
4
+
5
+ /**
6
+ * type inside parentheses
7
+ * e.g. (T1 | T2)
8
+ */
9
+ export class ParenthesizedTypeSchema extends SchemaNode {
10
+ @Transform(schemaObjToInstance)
11
+ readonly type: SchemaNode;
12
+ constructor(readonly location: Location, type: SchemaNode) {
13
+ super();
14
+ this.type = type;
15
+ }
16
+
17
+ toString() {
18
+ return `(${this.type.toString()})`;
19
+ }
20
+ }
@@ -1,22 +1,20 @@
1
+ import { Transform } from 'class-transformer';
1
2
  import chalk from 'chalk';
2
- import { SchemaNode } from '../schema-node';
3
+ import { Location, SchemaNode } from '../schema-node';
3
4
  import { ParameterSchema } from './parameter';
5
+ import { schemaObjToInstance } from '../schema-obj-to-class';
4
6
 
5
- export class SetAccessorSchema implements SchemaNode {
6
- constructor(private name: string, private param: ParameterSchema, private signature: string) {}
7
+ export class SetAccessorSchema extends SchemaNode {
8
+ @Transform(schemaObjToInstance)
9
+ readonly param: ParameterSchema;
10
+ constructor(readonly location: Location, readonly name: string, param: ParameterSchema, readonly signature: string) {
11
+ super();
12
+ this.param = param;
13
+ }
7
14
  getSignature() {
8
15
  return this.signature;
9
16
  }
10
17
 
11
- toObject() {
12
- return {
13
- constructorName: this.constructor.name,
14
- name: this.name,
15
- param: this.param,
16
- signature: this.signature,
17
- };
18
- }
19
-
20
18
  toString() {
21
19
  return `set ${chalk.bold(this.name)}(${this.param.toString()})`;
22
20
  }
@@ -1,13 +1,15 @@
1
- import { SchemaNode } from '../schema-node';
1
+ import { Transform } from 'class-transformer';
2
+ import { Location, SchemaNode } from '../schema-node';
3
+ import { schemaObjArrayToInstances } from '../schema-obj-to-class';
2
4
 
3
- export class TupleTypeSchema implements SchemaNode {
4
- constructor(private elements: SchemaNode[]) {}
5
- toObject() {
6
- return {
7
- constructorName: this.constructor.name,
8
- elements: this.elements.map((elem) => elem.toObject()),
9
- };
5
+ export class TupleTypeSchema extends SchemaNode {
6
+ @Transform(schemaObjArrayToInstances)
7
+ readonly elements: SchemaNode[];
8
+ constructor(readonly location: Location, elements: SchemaNode[]) {
9
+ super();
10
+ this.elements = elements;
10
11
  }
12
+
11
13
  toString() {
12
14
  return `[${this.elements.map((elem) => elem.toString()).join(', ')}]`;
13
15
  }
@@ -1,13 +1,15 @@
1
- import { SchemaNode } from '../schema-node';
1
+ import { Transform } from 'class-transformer';
2
+ import { Location, SchemaNode } from '../schema-node';
3
+ import { schemaObjToInstance } from '../schema-obj-to-class';
2
4
 
3
- export class TypeArraySchema implements SchemaNode {
4
- constructor(private type: SchemaNode) {}
5
- toObject() {
6
- return {
7
- constructorName: this.constructor.name,
8
- type: this.type.toObject(),
9
- };
5
+ export class TypeArraySchema extends SchemaNode {
6
+ @Transform(schemaObjToInstance)
7
+ readonly type: SchemaNode;
8
+ constructor(readonly location: Location, type: SchemaNode) {
9
+ super();
10
+ this.type = type;
10
11
  }
12
+
11
13
  toString() {
12
14
  return `${this.type.toString()}[]`;
13
15
  }
@@ -1,13 +1,15 @@
1
- import { SchemaNode } from '../schema-node';
1
+ import { Transform } from 'class-transformer';
2
+ import { Location, SchemaNode } from '../schema-node';
3
+ import { schemaObjArrayToInstances } from '../schema-obj-to-class';
2
4
 
3
- export class TypeIntersectionSchema implements SchemaNode {
4
- constructor(private types: SchemaNode[]) {}
5
- toObject() {
6
- return {
7
- constructorName: this.constructor.name,
8
- types: this.types.map((type) => type.toObject()),
9
- };
5
+ export class TypeIntersectionSchema extends SchemaNode {
6
+ @Transform(schemaObjArrayToInstances)
7
+ readonly types: SchemaNode[];
8
+ constructor(readonly location: Location, types: SchemaNode[]) {
9
+ super();
10
+ this.types = types;
10
11
  }
12
+
11
13
  toString() {
12
14
  return `${this.types.map((type) => type.toString()).join(' & ')}`;
13
15
  }
@@ -1,16 +1,18 @@
1
- import { SchemaNode } from '../schema-node';
1
+ import { Transform } from 'class-transformer';
2
+ import { Location, SchemaNode } from '../schema-node';
3
+ import { schemaObjArrayToInstances } from '../schema-obj-to-class';
2
4
 
3
5
  /**
4
6
  * e.g. `{ a: string; b: number }`
5
7
  */
6
- export class TypeLiteralSchema implements SchemaNode {
7
- constructor(private members: SchemaNode[]) {}
8
- toObject() {
9
- return {
10
- constructorName: this.constructor.name,
11
- types: this.members.map((type) => type.toObject()),
12
- };
8
+ export class TypeLiteralSchema extends SchemaNode {
9
+ @Transform(schemaObjArrayToInstances)
10
+ readonly members: SchemaNode[];
11
+ constructor(readonly location: Location, members: SchemaNode[]) {
12
+ super();
13
+ this.members = members;
13
14
  }
15
+
14
16
  toString() {
15
17
  return `{ ${this.members.map((type) => type.toString()).join('; ')} }`;
16
18
  }
@@ -1,14 +1,15 @@
1
- import { SchemaNode } from '../schema-node';
1
+ import { Transform } from 'class-transformer';
2
+ import { Location, SchemaNode } from '../schema-node';
3
+ import { schemaObjToInstance } from '../schema-obj-to-class';
2
4
 
3
- export class TypeOperatorSchema implements SchemaNode {
4
- constructor(private operatorName: string, private type: SchemaNode) {}
5
- toObject() {
6
- return {
7
- constructorName: this.constructor.name,
8
- operatorName: this.operatorName,
9
- type: this.type.toObject(),
10
- };
5
+ export class TypeOperatorSchema extends SchemaNode {
6
+ @Transform(schemaObjToInstance)
7
+ type: SchemaNode;
8
+ constructor(readonly location: Location, readonly operatorName: string, type: SchemaNode) {
9
+ super();
10
+ this.type = type;
11
11
  }
12
+
12
13
  toString() {
13
14
  return `${this.operatorName} ${this.type.toString()}`;
14
15
  }
@@ -0,0 +1,36 @@
1
+ import { Transform } from 'class-transformer';
2
+ import { Location, SchemaNode } from '../schema-node';
3
+ import { schemaObjToInstance } from '../schema-obj-to-class';
4
+
5
+ /**
6
+ * e.g. `function f(str: any): str is string {}`
7
+ *
8
+ * example when the type is empty:
9
+ * ```ts
10
+ * function assert(condition: any, msg?: string): asserts condition {
11
+ * if (!condition) {
12
+ * throw new AssertionError(msg);
13
+ * }
14
+ * }
15
+ * ```
16
+ *
17
+ */
18
+ export class TypePredicateSchema extends SchemaNode {
19
+ @Transform(schemaObjToInstance)
20
+ readonly type?: SchemaNode;
21
+ constructor(
22
+ readonly location: Location,
23
+ readonly parameterName: string,
24
+ type?: SchemaNode,
25
+ readonly hasAssertsModifier = false
26
+ ) {
27
+ super();
28
+ this.type = type;
29
+ }
30
+
31
+ toString() {
32
+ const assertsKeyword = this.hasAssertsModifier ? 'asserts ' : '';
33
+ const typeStr = this.type ? ` is ${this.type.toString()}` : '';
34
+ return assertsKeyword + this.parameterName + typeStr;
35
+ }
36
+ }
@@ -1,22 +1,21 @@
1
- import { SchemaNode } from '../schema-node';
1
+ import { Transform } from 'class-transformer';
2
+ import { Location, SchemaNode } from '../schema-node';
3
+ import { schemaObjToInstance } from '../schema-obj-to-class';
2
4
 
3
5
  /**
4
6
  * e.g. `typeof Foo`
5
7
  */
6
- export class TypeQuerySchema implements SchemaNode {
7
- constructor(private type: SchemaNode, private signature: string) {}
8
+ export class TypeQuerySchema extends SchemaNode {
9
+ @Transform(schemaObjToInstance)
10
+ readonly type: SchemaNode;
11
+ constructor(readonly location: Location, type: SchemaNode, readonly signature: string) {
12
+ super();
13
+ this.type = type;
14
+ }
8
15
  getSignature() {
9
16
  return this.signature;
10
17
  }
11
18
 
12
- toObject() {
13
- return {
14
- constructorName: this.constructor.name,
15
- type: this.type,
16
- signature: this.signature,
17
- };
18
- }
19
-
20
19
  toString() {
21
20
  return `typeof ${this.type.toString()}`;
22
21
  }
@@ -1,6 +1,8 @@
1
+ import { Transform } from 'class-transformer';
1
2
  import { ComponentID } from '@teambit/component';
2
3
  import chalk from 'chalk';
3
- import { SchemaNode } from '../schema-node';
4
+ import { Location, SchemaNode } from '../schema-node';
5
+ import { schemaObjArrayToInstances } from '../schema-obj-to-class';
4
6
 
5
7
  export type PlainTypeRefSchema = {
6
8
  name: string;
@@ -8,8 +10,21 @@ export type PlainTypeRefSchema = {
8
10
  packageName?: string;
9
11
  };
10
12
 
11
- export class TypeRefSchema implements SchemaNode {
13
+ /**
14
+ * can be one of the following:
15
+ * 1. a reference to another "export" in the same component
16
+ * 2. a reference to another component.
17
+ * 3. a reference to a package.
18
+ */
19
+ export class TypeRefSchema extends SchemaNode {
20
+ @Transform(schemaObjArrayToInstances)
21
+ /**
22
+ * optional type arguments, e.g. type Foo = Bar<X, Y>. The X and Y are type arguments.
23
+ */
24
+ typeArgs?: SchemaNode[];
25
+
12
26
  constructor(
27
+ readonly location: Location,
13
28
  /**
14
29
  * name of the reference to type.
15
30
  */
@@ -24,23 +39,27 @@ export class TypeRefSchema implements SchemaNode {
24
39
  * target package name. existing if type is defined in different package.
25
40
  */
26
41
  readonly packageName?: string
27
- ) {}
28
-
29
- toObject() {
30
- return {
31
- constructorName: this.constructor.name,
32
- name: this.name,
33
- componentId: this.componentId,
34
- packageName: this.packageName,
35
- };
42
+ ) {
43
+ super();
36
44
  }
37
45
 
38
46
  toString() {
47
+ const name = this.nameToString();
48
+ if (!this.typeArgs) {
49
+ return name;
50
+ }
51
+ const args = this.typeArgs.map((arg) => arg.toString()).join(', ');
52
+ return `${name}<${args}>`;
53
+ }
54
+
55
+ private nameToString() {
39
56
  if (this.componentId) {
40
- return `${this.componentId}/${this.name}`;
57
+ const compStr = chalk.dim(`(component: ${this.componentId.toStringWithoutVersion()})`);
58
+ return `${compStr} ${this.name}`;
41
59
  }
42
60
  if (this.packageName) {
43
- return `${chalk.dim(this.packageName)}/${this.name}`;
61
+ const pkgStr = chalk.dim(`(package: ${this.packageName})`);
62
+ return `${pkgStr} ${this.name}`;
44
63
  }
45
64
  return this.name;
46
65
  }
@@ -52,11 +71,11 @@ export class TypeRefSchema implements SchemaNode {
52
71
  return !this.componentId && !this.packageName;
53
72
  }
54
73
 
55
- static from(plainSchema: PlainTypeRefSchema) {
56
- return new TypeRefSchema(
57
- plainSchema.name,
58
- plainSchema.componentId ? ComponentID.fromString(plainSchema.componentId) : undefined,
59
- plainSchema.packageName
60
- );
61
- }
74
+ // static from(plainSchema: PlainTypeRefSchema) {
75
+ // return new TypeRefSchema(
76
+ // plainSchema.name,
77
+ // plainSchema.componentId ? ComponentID.fromString(plainSchema.componentId) : undefined,
78
+ // plainSchema.packageName
79
+ // );
80
+ // }
62
81
  }