@teambit/semantics.entities.semantic-schema 0.0.0-e4ef0e951da794f03096a5cc407420f4cde8b878

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 (233) hide show
  1. package/api-schema.ts +167 -0
  2. package/dist/api-schema.d.ts +36 -0
  3. package/dist/api-schema.js +171 -0
  4. package/dist/api-schema.js.map +1 -0
  5. package/dist/export.d.ts +38 -0
  6. package/dist/export.js +37 -0
  7. package/dist/export.js.map +1 -0
  8. package/dist/index.d.ts +11 -0
  9. package/dist/index.js +137 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/schema-node-constructor.d.ts +7 -0
  12. package/dist/schema-node-constructor.js +3 -0
  13. package/dist/schema-node-constructor.js.map +1 -0
  14. package/dist/schema-node.d.ts +51 -0
  15. package/dist/schema-node.js +67 -0
  16. package/dist/schema-node.js.map +1 -0
  17. package/dist/schema-registry.d.ts +17 -0
  18. package/dist/schema-registry.js +63 -0
  19. package/dist/schema-registry.js.map +1 -0
  20. package/dist/schemas/array-literal-expression.d.ts +15 -0
  21. package/dist/schemas/array-literal-expression.js +48 -0
  22. package/dist/schemas/array-literal-expression.js.map +1 -0
  23. package/dist/schemas/class.d.ts +59 -0
  24. package/dist/schemas/class.js +126 -0
  25. package/dist/schemas/class.js.map +1 -0
  26. package/dist/schemas/conditional-type.d.ts +25 -0
  27. package/dist/schemas/conditional-type.js +67 -0
  28. package/dist/schemas/conditional-type.js.map +1 -0
  29. package/dist/schemas/constructor.d.ts +9 -0
  30. package/dist/schemas/constructor.js +51 -0
  31. package/dist/schemas/constructor.js.map +1 -0
  32. package/dist/schemas/decorator.d.ts +35 -0
  33. package/dist/schemas/decorator.js +70 -0
  34. package/dist/schemas/decorator.js.map +1 -0
  35. package/dist/schemas/docs/doc.d.ts +30 -0
  36. package/dist/schemas/docs/doc.js +74 -0
  37. package/dist/schemas/docs/doc.js.map +1 -0
  38. package/dist/schemas/docs/index.d.ts +4 -0
  39. package/dist/schemas/docs/index.js +59 -0
  40. package/dist/schemas/docs/index.js.map +1 -0
  41. package/dist/schemas/docs/property-like-tag.d.ts +26 -0
  42. package/dist/schemas/docs/property-like-tag.js +59 -0
  43. package/dist/schemas/docs/property-like-tag.js.map +1 -0
  44. package/dist/schemas/docs/return-tag.d.ts +25 -0
  45. package/dist/schemas/docs/return-tag.js +55 -0
  46. package/dist/schemas/docs/return-tag.js.map +1 -0
  47. package/dist/schemas/docs/tag.d.ts +45 -0
  48. package/dist/schemas/docs/tag.js +75 -0
  49. package/dist/schemas/docs/tag.js.map +1 -0
  50. package/dist/schemas/enum-member.d.ts +36 -0
  51. package/dist/schemas/enum-member.js +66 -0
  52. package/dist/schemas/enum-member.js.map +1 -0
  53. package/dist/schemas/enum.d.ts +37 -0
  54. package/dist/schemas/enum.js +87 -0
  55. package/dist/schemas/enum.js.map +1 -0
  56. package/dist/schemas/export.d.ts +41 -0
  57. package/dist/schemas/export.js +92 -0
  58. package/dist/schemas/export.js.map +1 -0
  59. package/dist/schemas/expression-with-arguments.d.ts +19 -0
  60. package/dist/schemas/expression-with-arguments.js +59 -0
  61. package/dist/schemas/expression-with-arguments.js.map +1 -0
  62. package/dist/schemas/function-like.d.ts +66 -0
  63. package/dist/schemas/function-like.js +147 -0
  64. package/dist/schemas/function-like.js.map +1 -0
  65. package/dist/schemas/get-accessor.d.ts +22 -0
  66. package/dist/schemas/get-accessor.js +73 -0
  67. package/dist/schemas/get-accessor.js.map +1 -0
  68. package/dist/schemas/ignored.d.ts +17 -0
  69. package/dist/schemas/ignored.js +54 -0
  70. package/dist/schemas/ignored.js.map +1 -0
  71. package/dist/schemas/index-signature.d.ts +23 -0
  72. package/dist/schemas/index-signature.js +65 -0
  73. package/dist/schemas/index-signature.js.map +1 -0
  74. package/dist/schemas/index.d.ts +49 -0
  75. package/dist/schemas/index.js +623 -0
  76. package/dist/schemas/index.js.map +1 -0
  77. package/dist/schemas/indexed-access-type.d.ts +17 -0
  78. package/dist/schemas/indexed-access-type.js +55 -0
  79. package/dist/schemas/indexed-access-type.js.map +1 -0
  80. package/dist/schemas/inference-type.d.ts +22 -0
  81. package/dist/schemas/inference-type.js +65 -0
  82. package/dist/schemas/inference-type.js.map +1 -0
  83. package/dist/schemas/interface.d.ts +49 -0
  84. package/dist/schemas/interface.js +108 -0
  85. package/dist/schemas/interface.js.map +1 -0
  86. package/dist/schemas/keyword-type.d.ts +15 -0
  87. package/dist/schemas/keyword-type.js +43 -0
  88. package/dist/schemas/keyword-type.js.map +1 -0
  89. package/dist/schemas/literal-type.d.ts +15 -0
  90. package/dist/schemas/literal-type.js +43 -0
  91. package/dist/schemas/literal-type.js.map +1 -0
  92. package/dist/schemas/literal-value.d.ts +12 -0
  93. package/dist/schemas/literal-value.js +41 -0
  94. package/dist/schemas/literal-value.js.map +1 -0
  95. package/dist/schemas/module.d.ts +22 -0
  96. package/dist/schemas/module.js +87 -0
  97. package/dist/schemas/module.js.map +1 -0
  98. package/dist/schemas/named-tuple.d.ts +19 -0
  99. package/dist/schemas/named-tuple.js +53 -0
  100. package/dist/schemas/named-tuple.js.map +1 -0
  101. package/dist/schemas/object-literal-expression.d.ts +18 -0
  102. package/dist/schemas/object-literal-expression.js +52 -0
  103. package/dist/schemas/object-literal-expression.js.map +1 -0
  104. package/dist/schemas/parameter.d.ts +27 -0
  105. package/dist/schemas/parameter.js +95 -0
  106. package/dist/schemas/parameter.js.map +1 -0
  107. package/dist/schemas/parenthesized-type.d.ts +21 -0
  108. package/dist/schemas/parenthesized-type.js +54 -0
  109. package/dist/schemas/parenthesized-type.js.map +1 -0
  110. package/dist/schemas/property-assignment.d.ts +37 -0
  111. package/dist/schemas/property-assignment.js +67 -0
  112. package/dist/schemas/property-assignment.js.map +1 -0
  113. package/dist/schemas/set-accessor.d.ts +28 -0
  114. package/dist/schemas/set-accessor.js +65 -0
  115. package/dist/schemas/set-accessor.js.map +1 -0
  116. package/dist/schemas/template-literal-type-span.d.ts +19 -0
  117. package/dist/schemas/template-literal-type-span.js +53 -0
  118. package/dist/schemas/template-literal-type-span.js.map +1 -0
  119. package/dist/schemas/template-literal-type.d.ts +22 -0
  120. package/dist/schemas/template-literal-type.js +52 -0
  121. package/dist/schemas/template-literal-type.js.map +1 -0
  122. package/dist/schemas/this-type.d.ts +12 -0
  123. package/dist/schemas/this-type.js +37 -0
  124. package/dist/schemas/this-type.js.map +1 -0
  125. package/dist/schemas/tuple-type.d.ts +16 -0
  126. package/dist/schemas/tuple-type.js +47 -0
  127. package/dist/schemas/tuple-type.js.map +1 -0
  128. package/dist/schemas/type-array.d.ts +17 -0
  129. package/dist/schemas/type-array.js +50 -0
  130. package/dist/schemas/type-array.js.map +1 -0
  131. package/dist/schemas/type-intersection.d.ts +17 -0
  132. package/dist/schemas/type-intersection.js +50 -0
  133. package/dist/schemas/type-intersection.js.map +1 -0
  134. package/dist/schemas/type-literal.d.ts +18 -0
  135. package/dist/schemas/type-literal.js +64 -0
  136. package/dist/schemas/type-literal.js.map +1 -0
  137. package/dist/schemas/type-operator.d.ts +19 -0
  138. package/dist/schemas/type-operator.js +53 -0
  139. package/dist/schemas/type-operator.js.map +1 -0
  140. package/dist/schemas/type-predicate.d.ts +33 -0
  141. package/dist/schemas/type-predicate.js +75 -0
  142. package/dist/schemas/type-predicate.js.map +1 -0
  143. package/dist/schemas/type-query.d.ts +23 -0
  144. package/dist/schemas/type-query.js +59 -0
  145. package/dist/schemas/type-query.js.map +1 -0
  146. package/dist/schemas/type-ref.d.ts +80 -0
  147. package/dist/schemas/type-ref.js +159 -0
  148. package/dist/schemas/type-ref.js.map +1 -0
  149. package/dist/schemas/type-union.d.ts +17 -0
  150. package/dist/schemas/type-union.js +56 -0
  151. package/dist/schemas/type-union.js.map +1 -0
  152. package/dist/schemas/type.d.ts +23 -0
  153. package/dist/schemas/type.js +80 -0
  154. package/dist/schemas/type.js.map +1 -0
  155. package/dist/schemas/unimplemented-schema.d.ts +14 -0
  156. package/dist/schemas/unimplemented-schema.js +43 -0
  157. package/dist/schemas/unimplemented-schema.js.map +1 -0
  158. package/dist/schemas/unknown-schema.d.ts +18 -0
  159. package/dist/schemas/unknown-schema.js +47 -0
  160. package/dist/schemas/unknown-schema.js.map +1 -0
  161. package/dist/schemas/unresolved-schema.d.ts +14 -0
  162. package/dist/schemas/unresolved-schema.js +41 -0
  163. package/dist/schemas/unresolved-schema.js.map +1 -0
  164. package/dist/schemas/variable-like.d.ts +47 -0
  165. package/dist/schemas/variable-like.js +95 -0
  166. package/dist/schemas/variable-like.js.map +1 -0
  167. package/dist/semantic-schema.docs.md +6 -0
  168. package/dist/type.d.ts +1 -0
  169. package/dist/type.js +3 -0
  170. package/dist/type.js.map +1 -0
  171. package/dist/unknown-schema-factory.d.ts +4 -0
  172. package/dist/unknown-schema-factory.js +19 -0
  173. package/dist/unknown-schema-factory.js.map +1 -0
  174. package/export.ts +31 -0
  175. package/index.ts +13 -0
  176. package/package.json +46 -0
  177. package/schema-node-constructor.ts +8 -0
  178. package/schema-node.ts +96 -0
  179. package/schema-registry.ts +53 -0
  180. package/schemas/array-literal-expression.ts +33 -0
  181. package/schemas/class.ts +135 -0
  182. package/schemas/conditional-type.ts +63 -0
  183. package/schemas/constructor.ts +28 -0
  184. package/schemas/decorator.ts +54 -0
  185. package/schemas/docs/doc.ts +69 -0
  186. package/schemas/docs/index.ts +4 -0
  187. package/schemas/docs/property-like-tag.ts +47 -0
  188. package/schemas/docs/return-tag.ts +44 -0
  189. package/schemas/docs/tag.ts +66 -0
  190. package/schemas/enum-member.ts +57 -0
  191. package/schemas/enum.ts +73 -0
  192. package/schemas/export.ts +91 -0
  193. package/schemas/expression-with-arguments.ts +51 -0
  194. package/schemas/function-like.ts +154 -0
  195. package/schemas/get-accessor.ts +57 -0
  196. package/schemas/ignored.ts +43 -0
  197. package/schemas/index-signature.ts +49 -0
  198. package/schemas/index.ts +49 -0
  199. package/schemas/indexed-access-type.ts +47 -0
  200. package/schemas/inference-type.ts +60 -0
  201. package/schemas/interface.ts +94 -0
  202. package/schemas/keyword-type.ts +34 -0
  203. package/schemas/literal-type.ts +34 -0
  204. package/schemas/literal-value.ts +32 -0
  205. package/schemas/module.ts +87 -0
  206. package/schemas/named-tuple.ts +39 -0
  207. package/schemas/object-literal-expression.ts +42 -0
  208. package/schemas/parameter.ts +104 -0
  209. package/schemas/parenthesized-type.ts +42 -0
  210. package/schemas/property-assignment.ts +50 -0
  211. package/schemas/set-accessor.ts +47 -0
  212. package/schemas/template-literal-type-span.ts +42 -0
  213. package/schemas/template-literal-type.ts +40 -0
  214. package/schemas/this-type.ts +27 -0
  215. package/schemas/tuple-type.ts +35 -0
  216. package/schemas/type-array.ts +39 -0
  217. package/schemas/type-intersection.ts +39 -0
  218. package/schemas/type-literal.ts +60 -0
  219. package/schemas/type-operator.ts +41 -0
  220. package/schemas/type-predicate.ts +64 -0
  221. package/schemas/type-query.ts +49 -0
  222. package/schemas/type-ref.ts +143 -0
  223. package/schemas/type-union.ts +41 -0
  224. package/schemas/type.ts +61 -0
  225. package/schemas/unimplemented-schema.ts +35 -0
  226. package/schemas/unknown-schema.ts +38 -0
  227. package/schemas/unresolved-schema.ts +32 -0
  228. package/schemas/variable-like.ts +76 -0
  229. package/semantic-schema.docs.md +6 -0
  230. package/type.ts +1 -0
  231. package/types/asset.d.ts +41 -0
  232. package/types/style.d.ts +42 -0
  233. package/unknown-schema-factory.ts +10 -0
@@ -0,0 +1,39 @@
1
+ import { SchemaLocation, SchemaNode } from '../schema-node';
2
+ import { SchemaRegistry } from '../schema-registry';
3
+
4
+ export class TypeArraySchema extends SchemaNode {
5
+ readonly type: SchemaNode;
6
+
7
+ constructor(
8
+ readonly location: SchemaLocation,
9
+ type: SchemaNode
10
+ ) {
11
+ super();
12
+ this.type = type;
13
+ }
14
+
15
+ getNodes() {
16
+ return [this.type];
17
+ }
18
+
19
+ toString(options?: { color?: boolean }) {
20
+ return `${this.type.toString(options)}[]`;
21
+ }
22
+
23
+ toFullSignature(options?: { showDocs?: boolean }): string {
24
+ return `${this.type.toFullSignature(options)}[]`;
25
+ }
26
+
27
+ toObject() {
28
+ return {
29
+ ...super.toObject(),
30
+ type: this.type.toObject(),
31
+ };
32
+ }
33
+
34
+ static fromObject(obj: Record<string, any>): TypeArraySchema {
35
+ const location = obj.location;
36
+ const type = SchemaRegistry.fromObject(obj.type);
37
+ return new TypeArraySchema(location, type);
38
+ }
39
+ }
@@ -0,0 +1,39 @@
1
+ import { SchemaLocation, SchemaNode } from '../schema-node';
2
+ import { SchemaRegistry } from '../schema-registry';
3
+
4
+ export class TypeIntersectionSchema extends SchemaNode {
5
+ readonly types: SchemaNode[];
6
+
7
+ constructor(
8
+ readonly location: SchemaLocation,
9
+ types: SchemaNode[]
10
+ ) {
11
+ super();
12
+ this.types = types;
13
+ }
14
+
15
+ getNodes() {
16
+ return this.types;
17
+ }
18
+
19
+ toString(options?: { color?: boolean }) {
20
+ return `${this.types.map((type) => type.toString(options)).join(' & ')}`;
21
+ }
22
+
23
+ toFullSignature(options?: { showDocs?: boolean }): string {
24
+ return this.types.map((type) => type.toFullSignature(options)).join(' & ');
25
+ }
26
+
27
+ toObject() {
28
+ return {
29
+ ...super.toObject(),
30
+ types: this.types.map((type) => type.toObject()),
31
+ };
32
+ }
33
+
34
+ static fromObject(obj: Record<string, any>): TypeIntersectionSchema {
35
+ const location = obj.location;
36
+ const types = obj.types.map((type: any) => SchemaRegistry.fromObject(type));
37
+ return new TypeIntersectionSchema(location, types);
38
+ }
39
+ }
@@ -0,0 +1,60 @@
1
+ import { SchemaLocation, SchemaNode } from '../schema-node';
2
+ import { SchemaRegistry } from '../schema-registry';
3
+
4
+ /**
5
+ * e.g. `{ a: string; b: number }`
6
+ */
7
+ export class TypeLiteralSchema extends SchemaNode {
8
+ readonly members: SchemaNode[];
9
+
10
+ constructor(
11
+ readonly location: SchemaLocation,
12
+ members: SchemaNode[]
13
+ ) {
14
+ super();
15
+ this.members = members;
16
+ }
17
+
18
+ getNodes() {
19
+ return this.members;
20
+ }
21
+
22
+ toString() {
23
+ return `{ ${this.members.map((type) => type.toString()).join('; ')} }`;
24
+ }
25
+
26
+ toFullSignature(options?: { showDocs?: boolean }): string {
27
+ const indent = (level: number) => ' '.repeat(level);
28
+ const level = 0;
29
+ let signature = '';
30
+
31
+ signature += '{\n';
32
+
33
+ this.members.forEach((member) => {
34
+ const memberSignature = member.toFullSignature(options);
35
+
36
+ const memberLines = memberSignature.split('\n');
37
+
38
+ const indentedLines = memberLines.map((line) => indent(level + 1) + line);
39
+
40
+ signature += indentedLines.join('\n') + '\n';
41
+ });
42
+
43
+ signature += indent(level) + '}';
44
+
45
+ return signature;
46
+ }
47
+
48
+ toObject() {
49
+ return {
50
+ ...super.toObject(),
51
+ members: this.members.map((type) => type.toObject()),
52
+ };
53
+ }
54
+
55
+ static fromObject(obj: Record<string, any>): TypeLiteralSchema {
56
+ const location = obj.location;
57
+ const members = obj.members.map((type: any) => SchemaRegistry.fromObject(type));
58
+ return new TypeLiteralSchema(location, members);
59
+ }
60
+ }
@@ -0,0 +1,41 @@
1
+ import { SchemaLocation, SchemaNode } from '../schema-node';
2
+ import { SchemaRegistry } from '../schema-registry';
3
+
4
+ export class TypeOperatorSchema extends SchemaNode {
5
+ type: SchemaNode;
6
+ constructor(
7
+ readonly location: SchemaLocation,
8
+ readonly name: string,
9
+ type: SchemaNode
10
+ ) {
11
+ super();
12
+ this.type = type;
13
+ }
14
+
15
+ getNodes() {
16
+ return [this.type];
17
+ }
18
+
19
+ toString(options?: { color?: boolean }) {
20
+ return `${this.name} ${this.type.toString(options)}`;
21
+ }
22
+
23
+ toFullSignature(options?: { showDocs?: boolean }): string {
24
+ return `${this.name} ${this.type.toFullSignature(options)}`;
25
+ }
26
+
27
+ toObject() {
28
+ return {
29
+ ...super.toObject(),
30
+ name: this.name,
31
+ type: this.type.toObject(),
32
+ };
33
+ }
34
+
35
+ static fromObject(obj: Record<string, any>): TypeOperatorSchema {
36
+ const location = obj.location;
37
+ const name = obj.name;
38
+ const type = SchemaRegistry.fromObject(obj.type);
39
+ return new TypeOperatorSchema(location, name, type);
40
+ }
41
+ }
@@ -0,0 +1,64 @@
1
+ import { SchemaLocation, SchemaNode } from '../schema-node';
2
+ import { SchemaRegistry } from '../schema-registry';
3
+
4
+ /**
5
+ * e.g. `function f(str: any): str is string {}`
6
+ *
7
+ * example when the type is empty:
8
+ * ```ts
9
+ * function assert(condition: any, msg?: string): asserts condition {
10
+ * if (!condition) {
11
+ * throw new AssertionError(msg);
12
+ * }
13
+ * }
14
+ * ```
15
+ *
16
+ */
17
+ export class TypePredicateSchema extends SchemaNode {
18
+ readonly type?: SchemaNode;
19
+ constructor(
20
+ readonly location: SchemaLocation,
21
+ readonly name: string,
22
+ type?: SchemaNode,
23
+ readonly hasAssertsModifier = false
24
+ ) {
25
+ super();
26
+ this.type = type;
27
+ }
28
+
29
+ toString(options?: { color?: boolean }) {
30
+ const assertsKeyword = this.hasAssertsModifier ? 'asserts ' : '';
31
+ const typeStr = this.type ? ` is ${this.type.toString(options)}` : '';
32
+ return assertsKeyword + this.name + typeStr;
33
+ }
34
+
35
+ toFullSignature(options?: { showDocs?: boolean }): string {
36
+ const assertsKeyword = this.hasAssertsModifier ? 'asserts ' : '';
37
+ const typeSignature = this.type ? ` is ${this.type.toFullSignature(options)}` : '';
38
+ let signature = `${assertsKeyword}${this.name}${typeSignature}`;
39
+
40
+ if (options?.showDocs && this.doc) {
41
+ const docString = this.doc.toFullSignature();
42
+ signature = `${docString}\n${signature}`;
43
+ }
44
+
45
+ return signature;
46
+ }
47
+
48
+ toObject() {
49
+ return {
50
+ ...super.toObject(),
51
+ name: this.name,
52
+ type: this.type?.toObject(),
53
+ hasAssertsModifier: this.hasAssertsModifier,
54
+ };
55
+ }
56
+
57
+ static fromObject(obj: Record<string, any>): TypePredicateSchema {
58
+ const location = obj.location;
59
+ const name = obj.name;
60
+ const type = obj.type ? SchemaRegistry.fromObject(obj.type) : undefined;
61
+ const hasAssertsModifier = obj.hasAssertsModifier;
62
+ return new TypePredicateSchema(location, name, type, hasAssertsModifier);
63
+ }
64
+ }
@@ -0,0 +1,49 @@
1
+ import { SchemaLocation, SchemaNode } from '../schema-node';
2
+ import { SchemaRegistry } from '../schema-registry';
3
+
4
+ /**
5
+ * e.g. `typeof Foo`
6
+ */
7
+ export class TypeQuerySchema extends SchemaNode {
8
+ readonly type: SchemaNode;
9
+
10
+ constructor(
11
+ readonly location: SchemaLocation,
12
+ type: SchemaNode,
13
+ readonly signature: string
14
+ ) {
15
+ super();
16
+ this.type = type;
17
+ }
18
+
19
+ getNodes() {
20
+ return [this.type];
21
+ }
22
+
23
+ getSignature() {
24
+ return this.signature;
25
+ }
26
+
27
+ toString(options?: { color?: boolean }) {
28
+ return `typeof ${this.type.toString(options)}`;
29
+ }
30
+
31
+ toFullSignature(options?: { showDocs?: boolean }): string {
32
+ return `typeof ${this.type.toFullSignature(options)}`;
33
+ }
34
+
35
+ toObject() {
36
+ return {
37
+ ...super.toObject(),
38
+ type: this.type.toObject(),
39
+ signature: this.signature,
40
+ };
41
+ }
42
+
43
+ static fromObject(obj: Record<string, any>): TypeQuerySchema {
44
+ const location = obj.location;
45
+ const type = SchemaRegistry.fromObject(obj.type);
46
+ const signature = obj.signature;
47
+ return new TypeQuerySchema(location, type, signature);
48
+ }
49
+ }
@@ -0,0 +1,143 @@
1
+ import { ComponentID } from '@teambit/component';
2
+ import chalk from 'chalk';
3
+ import { SchemaLocation, SchemaNode } from '../schema-node';
4
+ import { SchemaRegistry } from '../schema-registry';
5
+
6
+ export type PlainTypeRefSchema = {
7
+ name: string;
8
+ componentId?: string;
9
+ packageName?: string;
10
+ };
11
+
12
+ /**
13
+ * can be one of the following:
14
+ * 1. a reference to another "export" in the same component
15
+ * 2 a reference to another declaration in the same file (internal)
16
+ * 3. a reference to another component.
17
+ * 4. a reference to a package.
18
+ */
19
+ export class TypeRefSchema extends SchemaNode {
20
+ /**
21
+ * optional type arguments, e.g. type Foo = Bar<X, Y>. The X and Y are type arguments.
22
+ */
23
+ typeArgs?: SchemaNode[];
24
+
25
+ readonly componentId?: ComponentID;
26
+
27
+ constructor(
28
+ readonly location: SchemaLocation,
29
+ /**
30
+ * name of the reference to type.
31
+ */
32
+ readonly name: string,
33
+ /**
34
+ * target component id. existing if the type is defined in another component.
35
+ */
36
+ componentId?: ComponentID,
37
+ /**
38
+ * target package name. existing if type is defined in different package.
39
+ */
40
+ readonly packageName?: string,
41
+ /**
42
+ * if the reference is not exported from the component, it can be internal to the file.
43
+ */
44
+ readonly internalFilePath?: string
45
+ ) {
46
+ super();
47
+ this.componentId = componentId;
48
+ }
49
+
50
+ getNodes(): SchemaNode[] {
51
+ return this.typeArgs || [];
52
+ }
53
+
54
+ withTypeArgs(typeArgs: SchemaNode[]) {
55
+ this.typeArgs = typeArgs;
56
+ return this;
57
+ }
58
+
59
+ toString(options?: { color?: boolean }) {
60
+ const name = this.nameToString(options);
61
+ if (!this.typeArgs) {
62
+ return name;
63
+ }
64
+ const args = this.typeArgs.map((arg) => arg.toString(options)).join(', ');
65
+ return `${name}<${args}>`;
66
+ }
67
+
68
+ private nameToString(options?: { color?: boolean }) {
69
+ const dim = options?.color ? chalk.dim : (str: string) => str;
70
+ if (this.componentId) {
71
+ const compStr = dim(`(component: ${this.componentId.toStringWithoutVersion()})`);
72
+ return `${compStr} ${this.name}`;
73
+ }
74
+ if (this.packageName) {
75
+ const pkgStr = dim(`(package: ${this.packageName})`);
76
+ return `${pkgStr} ${this.name}`;
77
+ }
78
+ return this.name;
79
+ }
80
+
81
+ toFullSignature(options?: { showDocs?: boolean }): string {
82
+ let signature = this.name;
83
+
84
+ if (this.typeArgs && this.typeArgs.length > 0) {
85
+ const argsSignatures = this.typeArgs.map((arg) => arg.toFullSignature(options)).join(', ');
86
+ signature += `<${argsSignatures}>`;
87
+ }
88
+
89
+ if (options?.showDocs && this.doc) {
90
+ const docString = this.doc.toFullSignature();
91
+ signature = `${docString}\n${signature}`;
92
+ }
93
+
94
+ return signature;
95
+ }
96
+
97
+ /**
98
+ * whether this type was already exported in this component
99
+ */
100
+ isFromThisComponent() {
101
+ return !this.componentId && !this.packageName;
102
+ }
103
+
104
+ /**
105
+ * whether this type is internal to the file.
106
+ */
107
+ isInternalReference() {
108
+ return !!this.internalFilePath && this.isFromThisComponent();
109
+ }
110
+
111
+ /**
112
+ * whether this type is exported from this component.
113
+ */
114
+ isExported() {
115
+ return this.isFromThisComponent() && !this.isInternalReference();
116
+ }
117
+
118
+ toObject() {
119
+ return {
120
+ ...super.toObject(),
121
+ name: this.name,
122
+ componentId: this.componentId ? this.componentId.toObject() : undefined,
123
+ packageName: this.packageName,
124
+ internalFilePath: this.internalFilePath,
125
+ typeArgs: this.typeArgs?.map((type) => type.toObject()),
126
+ };
127
+ }
128
+
129
+ static fromObject(obj: Record<string, any>): TypeRefSchema {
130
+ const location = obj.location;
131
+ const name = obj.name;
132
+ let componentId;
133
+ try {
134
+ componentId = obj.componentId ? ComponentID.fromObject(obj.componentId) : undefined;
135
+ } catch {
136
+ componentId = undefined;
137
+ }
138
+ const packageName = obj.packageName;
139
+ const internalFilePath = obj.internalFilePath;
140
+ const typeArgs = obj.typeArgs?.map((type: any) => SchemaRegistry.fromObject(type));
141
+ return new TypeRefSchema(location, name, componentId, packageName, internalFilePath).withTypeArgs(typeArgs);
142
+ }
143
+ }
@@ -0,0 +1,41 @@
1
+ import { SchemaLocation, SchemaNode } from '../schema-node';
2
+ import { SchemaRegistry } from '../schema-registry';
3
+
4
+ export class TypeUnionSchema extends SchemaNode {
5
+ readonly types: SchemaNode[];
6
+ constructor(
7
+ readonly location: SchemaLocation,
8
+ types: SchemaNode[]
9
+ ) {
10
+ super();
11
+ this.types = types;
12
+ }
13
+ toString(options?: { color?: boolean }) {
14
+ return `${this.types.map((type) => type.toString(options)).join(' | ')}`;
15
+ }
16
+ toFullSignature(options?: { showDocs?: boolean }): string {
17
+ const typeSignatures = this.types.map((type) => type.toFullSignature(options));
18
+ let signature = typeSignatures.join(' | ');
19
+
20
+ if (options?.showDocs && this.doc) {
21
+ const docString = this.doc.toFullSignature();
22
+ signature = `${docString}\n${signature}`;
23
+ }
24
+
25
+ return signature;
26
+ }
27
+ getNodes() {
28
+ return this.types;
29
+ }
30
+ toObject() {
31
+ return {
32
+ ...super.toObject(),
33
+ types: this.types.map((type) => type.toObject()),
34
+ };
35
+ }
36
+ static fromObject(obj: Record<string, any>): TypeUnionSchema {
37
+ const location = obj.location;
38
+ const types = obj.types.map((type: any) => SchemaRegistry.fromObject(type));
39
+ return new TypeUnionSchema(location, types);
40
+ }
41
+ }
@@ -0,0 +1,61 @@
1
+ import chalk from 'chalk';
2
+ import { SchemaLocation, SchemaNode } from '../schema-node';
3
+ import { DocSchema } from './docs';
4
+ import { SchemaRegistry } from '../schema-registry';
5
+
6
+ export class TypeSchema extends SchemaNode {
7
+ readonly type: SchemaNode;
8
+ readonly doc?: DocSchema;
9
+
10
+ constructor(
11
+ readonly location: SchemaLocation,
12
+ readonly name: string,
13
+ type: SchemaNode,
14
+ readonly signature: string,
15
+ doc?: DocSchema
16
+ ) {
17
+ super();
18
+ this.type = type;
19
+ this.doc = doc;
20
+ }
21
+
22
+ toString(options?: { color?: boolean }) {
23
+ const bold = options?.color ? chalk.bold : (str: string) => str;
24
+ return `type ${bold(this.name)}: ${this.type.toString(options)}`;
25
+ }
26
+
27
+ toFullSignature(options?: { showDocs?: boolean }): string {
28
+ const typeSignature = this.type.toFullSignature(options);
29
+
30
+ let signature = `type ${this.name}: ${typeSignature}`;
31
+
32
+ if (options?.showDocs && this.doc) {
33
+ const docString = this.doc.toFullSignature();
34
+ signature = `${docString}\n${signature}`;
35
+ }
36
+
37
+ return signature;
38
+ }
39
+
40
+ getNodes() {
41
+ return [this.type];
42
+ }
43
+
44
+ toObject() {
45
+ return {
46
+ ...super.toObject(),
47
+ name: this.name,
48
+ type: this.type.toObject(),
49
+ signature: this.signature,
50
+ };
51
+ }
52
+
53
+ static fromObject(obj: Record<string, any>): TypeSchema {
54
+ const location = obj.location;
55
+ const name = obj.name;
56
+ const type = SchemaRegistry.fromObject(obj.type);
57
+ const signature = obj.signature;
58
+ const doc = obj.doc ? DocSchema.fromObject(obj.doc) : undefined;
59
+ return new TypeSchema(location, name, type, signature, doc);
60
+ }
61
+ }
@@ -0,0 +1,35 @@
1
+ import { SchemaLocation, SchemaNode } from '../schema-node';
2
+
3
+ export class UnImplementedSchema extends SchemaNode {
4
+ readonly displaySchemaName = 'Unimplemented Schemas';
5
+
6
+ constructor(
7
+ readonly location: SchemaLocation,
8
+ readonly name: string,
9
+ readonly type: string
10
+ ) {
11
+ super();
12
+ }
13
+
14
+ toString() {
15
+ return `<<unimplemented schema ${this.name} of type ${this.type}>>`;
16
+ }
17
+
18
+ toFullSignature(): string {
19
+ return `<<unimplemented schema ${this.name} of type ${this.type}>>`;
20
+ }
21
+
22
+ toObject() {
23
+ return {
24
+ ...super.toObject(),
25
+ type: this.type,
26
+ };
27
+ }
28
+
29
+ static fromObject(obj: Record<string, any>): UnImplementedSchema {
30
+ const location = obj.location;
31
+ const name = obj.name;
32
+ const type = obj.type;
33
+ return new UnImplementedSchema(location, name, type);
34
+ }
35
+ }
@@ -0,0 +1,38 @@
1
+ import { SchemaLocation, SchemaNode } from '../schema-node';
2
+
3
+ /**
4
+ * needed for better backward and forward compatibility.
5
+ * in case a previous version of the semantic-schema had a schema class that doesn't exist anymore, then it'll be
6
+ * wrapped in this class.
7
+ */
8
+ export class UnknownSchema extends SchemaNode {
9
+ constructor(
10
+ readonly location: SchemaLocation,
11
+ readonly name: string,
12
+ readonly schemaObj: Record<string, any>
13
+ ) {
14
+ super();
15
+ }
16
+
17
+ toString() {
18
+ return `<<unknown schema ${this.name}>>`;
19
+ }
20
+
21
+ toFullSignature(): string {
22
+ return this.toString();
23
+ }
24
+
25
+ toObject() {
26
+ return {
27
+ ...super.toObject(),
28
+ schemaObj: this.schemaObj,
29
+ };
30
+ }
31
+
32
+ static fromObject(obj: Record<string, any>): UnknownSchema {
33
+ const location = obj.location;
34
+ const name = obj.name;
35
+ const schemaObj = obj.schemaObj;
36
+ return new UnknownSchema(location, name, schemaObj);
37
+ }
38
+ }
@@ -0,0 +1,32 @@
1
+ import { SchemaLocation, SchemaNode } from '../schema-node';
2
+
3
+ /**
4
+ * needed for cases when the exported entity could not be resolved.
5
+ * e.g. exporting mdx variables in the main index.ts file.
6
+ */
7
+ export class UnresolvedSchema extends SchemaNode {
8
+ constructor(
9
+ readonly location: SchemaLocation,
10
+ readonly name: string
11
+ ) {
12
+ super();
13
+ }
14
+
15
+ toString() {
16
+ return this.name;
17
+ }
18
+
19
+ toFullSignature(): string {
20
+ return this.toString();
21
+ }
22
+
23
+ toObject() {
24
+ return super.toObject();
25
+ }
26
+
27
+ static fromObject(obj: Record<string, any>): UnresolvedSchema {
28
+ const location = obj.location;
29
+ const name = obj.name;
30
+ return new UnresolvedSchema(location, name);
31
+ }
32
+ }