@teambit/typescript 0.0.726 → 0.0.729

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/dist/exceptions/transformer-not-found.js +14 -1
  2. package/dist/exceptions/transformer-not-found.js.map +1 -1
  3. package/dist/schema-extractor-context.d.ts +33 -4
  4. package/dist/schema-extractor-context.js +163 -24
  5. package/dist/schema-extractor-context.js.map +1 -1
  6. package/dist/transformers/class-deceleration.d.ts +11 -0
  7. package/dist/transformers/class-deceleration.js +126 -0
  8. package/dist/transformers/class-deceleration.js.map +1 -0
  9. package/dist/transformers/constructor.d.ts +10 -0
  10. package/dist/transformers/constructor.js +70 -0
  11. package/dist/transformers/constructor.js.map +1 -0
  12. package/dist/transformers/export-declaration.d.ts +2 -1
  13. package/dist/transformers/export-declaration.js +64 -13
  14. package/dist/transformers/export-declaration.js.map +1 -1
  15. package/dist/transformers/function-declaration.d.ts +1 -3
  16. package/dist/transformers/function-declaration.js +25 -31
  17. package/dist/transformers/function-declaration.js.map +1 -1
  18. package/dist/transformers/index-signature.d.ts +9 -0
  19. package/dist/transformers/index-signature.js +61 -0
  20. package/dist/transformers/index-signature.js.map +1 -0
  21. package/dist/transformers/index.d.ts +14 -0
  22. package/dist/transformers/index.js +224 -0
  23. package/dist/transformers/index.js.map +1 -1
  24. package/dist/transformers/interface-declaration.d.ts +10 -0
  25. package/dist/transformers/interface-declaration.js +73 -0
  26. package/dist/transformers/interface-declaration.js.map +1 -0
  27. package/dist/transformers/literal-type.d.ts +8 -0
  28. package/dist/transformers/literal-type.js +50 -0
  29. package/dist/transformers/literal-type.js.map +1 -0
  30. package/dist/transformers/method-declaration.d.ts +11 -0
  31. package/dist/transformers/method-declaration.js +94 -0
  32. package/dist/transformers/method-declaration.js.map +1 -0
  33. package/dist/transformers/method-signature.d.ts +10 -0
  34. package/dist/transformers/method-signature.js +81 -0
  35. package/dist/transformers/method-signature.js.map +1 -0
  36. package/dist/transformers/property-declaration.d.ts +11 -0
  37. package/dist/transformers/property-declaration.js +81 -0
  38. package/dist/transformers/property-declaration.js.map +1 -0
  39. package/dist/transformers/property-signature.d.ts +10 -0
  40. package/dist/transformers/property-signature.js +91 -0
  41. package/dist/transformers/property-signature.js.map +1 -0
  42. package/dist/transformers/source-file-transformer.d.ts +2 -1
  43. package/dist/transformers/source-file-transformer.js +23 -3
  44. package/dist/transformers/source-file-transformer.js.map +1 -1
  45. package/dist/transformers/type-alias.d.ts +4 -1
  46. package/dist/transformers/type-alias.js +22 -2
  47. package/dist/transformers/type-alias.js.map +1 -1
  48. package/dist/transformers/type-intersection.d.ts +9 -0
  49. package/dist/transformers/type-intersection.js +63 -0
  50. package/dist/transformers/type-intersection.js.map +1 -0
  51. package/dist/transformers/type-literal.d.ts +14 -0
  52. package/dist/transformers/type-literal.js +67 -0
  53. package/dist/transformers/type-literal.js.map +1 -0
  54. package/dist/transformers/type-reference.d.ts +8 -0
  55. package/dist/transformers/type-reference.js +41 -0
  56. package/dist/transformers/type-reference.js.map +1 -0
  57. package/dist/transformers/type-union.d.ts +9 -0
  58. package/dist/transformers/type-union.js +63 -0
  59. package/dist/transformers/type-union.js.map +1 -0
  60. package/dist/transformers/utils/get-params.d.ts +4 -0
  61. package/dist/transformers/utils/get-params.js +67 -0
  62. package/dist/transformers/utils/get-params.js.map +1 -0
  63. package/dist/transformers/utils/parse-type-from-quick-info.d.ts +2 -0
  64. package/dist/transformers/utils/parse-type-from-quick-info.js +26 -0
  65. package/dist/transformers/utils/parse-type-from-quick-info.js.map +1 -0
  66. package/dist/transformers/variable-declaration.d.ts +11 -0
  67. package/dist/transformers/variable-declaration.js +98 -0
  68. package/dist/transformers/variable-declaration.js.map +1 -0
  69. package/dist/transformers/variable-statement.d.ts +7 -1
  70. package/dist/transformers/variable-statement.js +42 -2
  71. package/dist/transformers/variable-statement.js.map +1 -1
  72. package/dist/typescript.extractor.d.ts +3 -3
  73. package/dist/typescript.extractor.js +7 -4
  74. package/dist/typescript.extractor.js.map +1 -1
  75. package/dist/typescript.main.runtime.js +1 -1
  76. package/dist/typescript.main.runtime.js.map +1 -1
  77. package/exceptions/transformer-not-found.ts +5 -4
  78. package/package-tar/teambit-typescript-0.0.729.tgz +0 -0
  79. package/package.json +15 -15
  80. package/{preview-1651721125153.js → preview-1652239597845.js} +2 -2
  81. package/transformers/class-deceleration.ts +49 -0
  82. package/transformers/constructor.ts +22 -0
  83. package/transformers/export-declaration.ts +42 -13
  84. package/transformers/function-declaration.ts +7 -27
  85. package/transformers/index-signature.ts +21 -0
  86. package/transformers/index.ts +14 -0
  87. package/transformers/interface-declaration.ts +24 -0
  88. package/transformers/literal-type.ts +18 -0
  89. package/transformers/method-declaration.ts +32 -0
  90. package/transformers/method-signature.ts +30 -0
  91. package/transformers/property-declaration.ts +31 -0
  92. package/transformers/property-signature.ts +38 -0
  93. package/transformers/source-file-transformer.ts +6 -6
  94. package/transformers/type-alias.ts +12 -2
  95. package/transformers/type-intersection.ts +23 -0
  96. package/transformers/type-literal.ts +28 -0
  97. package/transformers/type-reference.ts +19 -0
  98. package/transformers/type-union.ts +23 -0
  99. package/transformers/utils/get-params.ts +30 -0
  100. package/transformers/utils/parse-type-from-quick-info.ts +12 -0
  101. package/transformers/variable-declaration.ts +36 -0
  102. package/transformers/variable-statement.ts +16 -2
  103. package/package-tar/teambit-typescript-0.0.726.tgz +0 -0
@@ -1,13 +1,26 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
6
8
  exports.TransformerNotFound = void 0;
7
9
 
10
+ function _typescript() {
11
+ const data = _interopRequireDefault(require("typescript"));
12
+
13
+ _typescript = function () {
14
+ return data;
15
+ };
16
+
17
+ return data;
18
+ }
19
+
8
20
  class TransformerNotFound extends Error {
9
21
  constructor(node, component) {
10
- super(`typescript: could not find schema transformer for node of kind ${node.kind} in component ${component.id.toString()}`);
22
+ super(`typescript: could not find schema transformer for node of kind ${node.kind} (${_typescript().default.SyntaxKind[node.kind]}) in component ${component.id.toString()}.
23
+ node text: ${node.getText()}`);
11
24
  this.node = node;
12
25
  this.component = component;
13
26
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["transformer-not-found.ts"],"names":["TransformerNotFound","Error","constructor","node","component","kind","id","toString"],"mappings":";;;;;;;AAGO,MAAMA,mBAAN,SAAkCC,KAAlC,CAAwC;AAC7CC,EAAAA,WAAW,CAAUC,IAAV,EAA+BC,SAA/B,EAAqD;AAC9D,UACG,kEACCD,IAAI,CAACE,IACN,iBAAgBD,SAAS,CAACE,EAAV,CAAaC,QAAb,EAAwB,EAH3C;AAD8D,SAA3CJ,IAA2C,GAA3CA,IAA2C;AAAA,SAAtBC,SAAsB,GAAtBA,SAAsB;AAM/D;;AAP4C","sourcesContent":["import { Node } from 'typescript';\nimport { Component } from '@teambit/component';\n\nexport class TransformerNotFound extends Error {\n constructor(readonly node: Node, readonly component: Component) {\n super(\n `typescript: could not find schema transformer for node of kind ${\n node.kind\n } in component ${component.id.toString()}`\n );\n }\n}\n"]}
1
+ {"version":3,"sources":["transformer-not-found.ts"],"names":["TransformerNotFound","Error","constructor","node","component","kind","ts","SyntaxKind","id","toString","getText"],"mappings":";;;;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAGO,MAAMA,mBAAN,SAAkCC,KAAlC,CAAwC;AAC7CC,EAAAA,WAAW,CAAUC,IAAV,EAA+BC,SAA/B,EAAqD;AAC9D,UACG,kEAAiED,IAAI,CAACE,IAAK,KAC1EC,sBAAGC,UAAH,CAAcJ,IAAI,CAACE,IAAnB,CACD,kBAAiBD,SAAS,CAACI,EAAV,CAAaC,QAAb,EAAwB;AAChD,aAAaN,IAAI,CAACO,OAAL,EAAe,EAJxB;AAD8D,SAA3CP,IAA2C,GAA3CA,IAA2C;AAAA,SAAtBC,SAAsB,GAAtBA,SAAsB;AAO/D;;AAR4C","sourcesContent":["import ts, { Node } from 'typescript';\nimport { Component } from '@teambit/component';\n\nexport class TransformerNotFound extends Error {\n constructor(readonly node: Node, readonly component: Component) {\n super(\n `typescript: could not find schema transformer for node of kind ${node.kind} (${\n ts.SyntaxKind[node.kind]\n }) in component ${component.id.toString()}.\nnode text: ${node.getText()}`\n );\n }\n}\n"]}
@@ -10,27 +10,53 @@ export declare class SchemaExtractorContext {
10
10
  readonly component: Component;
11
11
  readonly extractor: TypeScriptExtractor;
12
12
  constructor(tsserver: TsserverClient, component: Component, extractor: TypeScriptExtractor);
13
- computeSchema(node: Node): Promise<SchemaNode | undefined>;
13
+ computeSchema(node: Node): Promise<SchemaNode>;
14
+ /**
15
+ * returns the location of a node in a source file.
16
+ */
14
17
  getLocation(node: Node, targetSourceFile?: ts.SourceFile): {
15
18
  line: number;
16
19
  character: number;
17
20
  };
21
+ /**
22
+ * returns a signature for a node.
23
+ */
18
24
  getSignature(node: Node): Promise<import("typescript/lib/protocol").SignatureHelpResponse | undefined>;
25
+ /**
26
+ * get the position for the tsserver.
27
+ */
19
28
  getPosition(sourceFile: ts.SourceFile, line: number, offset: number): number;
29
+ /**
30
+ * get the path for a source file.
31
+ */
20
32
  getPath(node: Node): string;
21
- createRef(): {};
33
+ /**
34
+ * create a reference to a type from a component.
35
+ * think if we don't need this because of type ref
36
+ */
22
37
  getQuickInfo(node: Node): Promise<import("typescript/lib/protocol").QuickInfoResponse | undefined>;
38
+ /**
39
+ * returns the type definition for a type.
40
+ */
23
41
  typeDefinition(node: Node): Promise<import("typescript/lib/protocol").TypeDefinitionResponse | undefined>;
24
42
  visitTypeDefinition(): void;
25
43
  private findFileInComponent;
44
+ private parsePackageNameFromPath;
26
45
  /**
27
46
  * return the file if part of the component.
28
47
  * otherwise, a reference to the target package and the type name.
29
48
  */
30
49
  private getSourceFile;
50
+ getSourceFileFromNode(node: Node): Promise<ts.SourceFile | undefined>;
51
+ /**
52
+ * get a definition for a given node.
53
+ */
31
54
  definition(node: Node): Promise<Node | undefined>;
55
+ /**
56
+ * visit a definition for node - e.g. return it's schema.
57
+ */
32
58
  visitDefinition(node: Node): Promise<SchemaNode | undefined>;
33
- visit(node: Node): Promise<SchemaNode | undefined>;
59
+ visit(node: Node): Promise<SchemaNode>;
34
60
  references(): void;
35
61
  isExported(): void;
36
62
  isFromComponent(): void;
@@ -40,5 +66,8 @@ export declare class SchemaExtractorContext {
40
66
  getExportedIdentifiers(node: Node): Promise<import("./export-identifier").ExportIdentifier[]>;
41
67
  private isNative;
42
68
  jump(file: AbstractVinyl, start: any): Promise<SchemaNode | undefined>;
43
- resolveType(node: Node, typeStr: string, type?: boolean): Promise<TypeRefSchema>;
69
+ /**
70
+ * resolve a type by a node and its identifier.
71
+ */
72
+ resolveType(node: Node, typeStr: string): Promise<TypeRefSchema>;
44
73
  }
@@ -2,10 +2,12 @@
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
4
 
5
- require("core-js/modules/es.array.iterator.js");
6
-
7
5
  require("core-js/modules/es.promise.js");
8
6
 
7
+ require("core-js/modules/es.regexp.exec.js");
8
+
9
+ require("core-js/modules/es.string.replace.js");
10
+
9
11
  Object.defineProperty(exports, "__esModule", {
10
12
  value: true
11
13
  });
@@ -21,6 +23,16 @@ function _defineProperty2() {
21
23
  return data;
22
24
  }
23
25
 
26
+ function _typescript() {
27
+ const data = _interopRequireDefault(require("typescript"));
28
+
29
+ _typescript = function () {
30
+ return data;
31
+ };
32
+
33
+ return data;
34
+ }
35
+
24
36
  function _tsutils() {
25
37
  const data = require("tsutils");
26
38
 
@@ -72,39 +84,62 @@ class SchemaExtractorContext {
72
84
  computeSchema(node) {
73
85
  return this.extractor.computeSchema(node, this);
74
86
  }
87
+ /**
88
+ * returns the location of a node in a source file.
89
+ */
90
+
75
91
 
76
92
  getLocation(node, targetSourceFile) {
77
93
  const sourceFile = targetSourceFile || node.getSourceFile();
78
- const position = sourceFile.getLineAndCharacterOfPosition(node.pos);
94
+ const position = sourceFile.getLineAndCharacterOfPosition(node.getStart());
79
95
  const line = position.line + 1;
80
- const character = position.character + 2; // need to verify why a 2 char difference here.
81
-
96
+ const character = position.character + 1;
82
97
  return {
83
98
  line,
84
99
  character
85
100
  };
86
101
  }
102
+ /**
103
+ * returns a signature for a node.
104
+ */
105
+
87
106
 
88
107
  async getSignature(node) {
89
108
  return this.tsserver.getSignatureHelp(this.getPath(node), this.getLocation(node));
90
109
  }
110
+ /**
111
+ * get the position for the tsserver.
112
+ */
113
+
91
114
 
92
115
  getPosition(sourceFile, line, offset) {
93
- return sourceFile.getPositionOfLineAndCharacter(line - 1, offset - 2);
116
+ return sourceFile.getPositionOfLineAndCharacter(line - 1, offset - 1);
94
117
  }
118
+ /**
119
+ * get the path for a source file.
120
+ */
121
+
95
122
 
96
123
  getPath(node) {
97
124
  const sourceFile = node.getSourceFile();
98
125
  return sourceFile.fileName;
99
126
  }
127
+ /**
128
+ * create a reference to a type from a component.
129
+ * think if we don't need this because of type ref
130
+ */
131
+ // createRef() {
132
+ // return {};
133
+ // }
100
134
 
101
- createRef() {
102
- return {};
103
- }
104
135
 
105
136
  getQuickInfo(node) {
106
137
  return this.tsserver.getQuickInfo(this.getPath(node), this.getLocation(node));
107
138
  }
139
+ /**
140
+ * returns the type definition for a type.
141
+ */
142
+
108
143
 
109
144
  typeDefinition(node) {
110
145
  return this.tsserver.getTypeDefinition(this.getPath(node), this.getLocation(node));
@@ -126,6 +161,20 @@ class SchemaExtractorContext {
126
161
  return false;
127
162
  });
128
163
  }
164
+
165
+ parsePackageNameFromPath(path) {
166
+ const parts = path.split('node_modules');
167
+ if (parts.length === 1) return '';
168
+ const lastPart = parts[parts.length - 1].replace(_path().sep, '');
169
+ const pkgParts = lastPart.split('/');
170
+
171
+ if (lastPart.startsWith('@')) {
172
+ // scoped package
173
+ return `${pkgParts[0]}/${pkgParts[1]}`;
174
+ }
175
+
176
+ return pkgParts[0];
177
+ }
129
178
  /**
130
179
  * return the file if part of the component.
131
180
  * otherwise, a reference to the target package and the type name.
@@ -138,22 +187,53 @@ class SchemaExtractorContext {
138
187
  return this.extractor.parseSourceFile(file);
139
188
  }
140
189
 
190
+ async getSourceFileFromNode(node) {
191
+ const def = await this.tsserver.getDefinition(this.getPath(node), this.getLocation(node));
192
+ const firstDef = (0, _lodash().head)(def.body);
193
+
194
+ if (!firstDef) {
195
+ return undefined;
196
+ }
197
+
198
+ const sourceFile = this.getSourceFile(firstDef.file);
199
+ return sourceFile;
200
+ }
201
+ /**
202
+ * get a definition for a given node.
203
+ */
204
+
205
+
141
206
  async definition(node) {
142
207
  const def = await this.tsserver.getDefinition(this.getPath(node), this.getLocation(node));
143
208
  const firstDef = (0, _lodash().head)(def.body);
144
- if (!firstDef) return undefined;
209
+
210
+ if (!firstDef) {
211
+ return undefined;
212
+ }
213
+
145
214
  const startPosition = firstDef.start;
146
215
  const sourceFile = this.getSourceFile(firstDef.file);
147
- if (!sourceFile) return undefined; // learn how to return a reference to a different component here.
216
+
217
+ if (!sourceFile) {
218
+ return undefined; // learn how to return a reference to a different component here.
219
+ }
148
220
 
149
221
  const pos = this.getPosition(sourceFile, startPosition.line, startPosition.offset);
150
222
  const nodeAtPos = (0, _tsutils().getTokenAtPosition)(sourceFile, pos);
151
223
  return nodeAtPos;
152
224
  }
225
+ /**
226
+ * visit a definition for node - e.g. return it's schema.
227
+ */
228
+
153
229
 
154
230
  async visitDefinition(node) {
155
231
  const definition = await this.definition(node);
156
- if (!definition) return undefined;
232
+
233
+ if (!definition) {
234
+ return undefined;
235
+ }
236
+
157
237
  return this.visit(definition.parent);
158
238
  }
159
239
 
@@ -197,25 +277,84 @@ class SchemaExtractorContext {
197
277
  const pos = this.getPosition(sourceFile, start.line, start.offset);
198
278
  const nodeAtPos = (0, _tsutils().getTokenAtPosition)(sourceFile, pos);
199
279
  if (!nodeAtPos) return undefined;
280
+
281
+ if (nodeAtPos.kind === _typescript().default.SyntaxKind.Identifier) {
282
+ // @todo: make sure with Ran that it's fine. Maybe it's better to do: `this.visit(nodeAtPos.parent);`
283
+ return this.visitDefinition(nodeAtPos);
284
+ }
285
+
200
286
  return this.visit(nodeAtPos);
201
287
  }
288
+ /**
289
+ * resolve a type by a node and its identifier.
290
+ */
291
+
202
292
 
203
- async resolveType(node, typeStr, type = true) {
204
- var _this$_exports, _typeDef$body;
293
+ async resolveType(node, typeStr) {
294
+ var _this$_exports;
205
295
 
296
+ // if a node has "type" prop, it has the type data of the node. this normally happens when the code has the type
297
+ // explicitly, e.g. `const str: string` vs implicitly `const str = 'some-string'`, which the node won't have "type"
298
+ // @ts-ignore
299
+ node = node.type || node;
206
300
  if (this.isNative(typeStr)) return new (_semanticsEntities().TypeRefSchema)(typeStr);
301
+
302
+ if (node.kind === _typescript().default.SyntaxKind.LiteralType) {
303
+ return new (_semanticsEntities().TypeRefSchema)(node.literal.getText());
304
+ }
305
+
207
306
  if ((_this$_exports = this._exports) !== null && _this$_exports !== void 0 && _this$_exports.includes(typeStr)) return new (_semanticsEntities().TypeRefSchema)(typeStr);
208
- const typeDef = type ? await this.tsserver.getDefinition(node.getSourceFile().fileName, this.getLocation(node)) : await this.typeDefinition(node);
209
- const def = await Promise.all((typeDef === null || typeDef === void 0 ? void 0 : (_typeDef$body = typeDef.body) === null || _typeDef$body === void 0 ? void 0 : _typeDef$body.map(async definition => {
210
- const file = this.findFileInComponent(definition.file); // TODO: find component id is exists, otherwise add the package name.
307
+ /**
308
+ * tsserver has two different calls: "definition" and "typeDefinition".
309
+ * normally, we need the "typeDefinition" to get the type data of a node.
310
+ * sometimes, it has no data, for example when the node is of type TypeReference, and then using "definition" is
311
+ * helpful. (couldn't find a rule when to use each one. e.g. "VariableDeclaration" sometimes has data only in
312
+ * "definition" but it's not clear when/why).
313
+ */
314
+
315
+ const getDef = async () => {
316
+ const typeDefinition = await this.typeDefinition(node);
317
+ const headTypeDefinition = (0, _lodash().head)(typeDefinition === null || typeDefinition === void 0 ? void 0 : typeDefinition.body);
318
+
319
+ if (headTypeDefinition) {
320
+ return headTypeDefinition;
321
+ }
211
322
 
212
- if (!file) return new (_semanticsEntities().TypeRefSchema)(typeStr, undefined, '');
213
- if (file) return new (_semanticsEntities().TypeRefSchema)(typeStr, undefined, undefined, this.jump(file, definition.start));
214
- return undefined;
215
- })) || []);
216
- const headDef = (0, _lodash().head)(def);
217
- if (headDef) return headDef;
218
- return new (_semanticsEntities().TypeRefSchema)('any');
323
+ const definition = await this.tsserver.getDefinition(node.getSourceFile().fileName, this.getLocation(node));
324
+ return (0, _lodash().head)(definition === null || definition === void 0 ? void 0 : definition.body);
325
+ };
326
+
327
+ const definition = await getDef(); // when we can't figure out the component/package/type of this node, we'll use the typeStr as the type.
328
+
329
+ const unknownExactType = new (_semanticsEntities().TypeRefSchema)(typeStr);
330
+
331
+ if (!definition) {
332
+ return unknownExactType;
333
+ } // the reason for this check is to avoid infinite loop when calling `this.jump` with the same file+location
334
+
335
+
336
+ const isDefInSameLocation = () => {
337
+ if (definition.file !== node.getSourceFile().fileName) {
338
+ return false;
339
+ }
340
+
341
+ const loc = this.getLocation(node);
342
+ return loc.line === definition.start.line && loc.character === definition.start.offset;
343
+ };
344
+
345
+ const file = this.findFileInComponent(definition.file);
346
+
347
+ if (file) {
348
+ if (isDefInSameLocation()) {
349
+ return unknownExactType;
350
+ }
351
+
352
+ return new (_semanticsEntities().TypeRefSchema)(typeStr, undefined, undefined, await this.jump(file, definition.start));
353
+ }
354
+
355
+ const pkgName = this.parsePackageNameFromPath(definition.file); // TODO: find component id is exists, otherwise add the package name.
356
+
357
+ return new (_semanticsEntities().TypeRefSchema)(typeStr, undefined, pkgName);
219
358
  }
220
359
 
221
360
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["schema-extractor-context.ts"],"names":["SchemaExtractorContext","constructor","tsserver","component","extractor","undefined","computeSchema","node","getLocation","targetSourceFile","sourceFile","getSourceFile","position","getLineAndCharacterOfPosition","pos","line","character","getSignature","getSignatureHelp","getPath","getPosition","offset","getPositionOfLineAndCharacter","fileName","createRef","getQuickInfo","typeDefinition","getTypeDefinition","visitTypeDefinition","findFileInComponent","filePath","filesystem","files","find","file","path","includes","strings","map","format","endsWith","string","parseSourceFile","definition","def","getDefinition","firstDef","body","startPosition","start","nodeAtPos","visitDefinition","visit","parent","references","isExported","isFromComponent","getFileExports","exportDec","specifierPathStr","moduleSpecifier","getText","specifierPath","substring","length","absPath","computeExportedIdentifiers","setExports","exports","_exports","getExportedIdentifiers","isNative","typeName","jump","resolveType","typeStr","type","TypeRefSchema","typeDef","Promise","all","headDef"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAGA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAIO,MAAMA,sBAAN,CAA6B;AAClCC,EAAAA,WAAW,CACAC,QADA,EAEAC,SAFA,EAGAC,SAHA,EAIT;AAAA,SAHSF,QAGT,GAHSA,QAGT;AAAA,SAFSC,SAET,GAFSA,SAET;AAAA,SADSC,SACT,GADSA,SACT;AAAA,sDA+GiCC,SA/GjC;AAAE;;AAEJC,EAAAA,aAAa,CAACC,IAAD,EAAa;AACxB,WAAO,KAAKH,SAAL,CAAeE,aAAf,CAA6BC,IAA7B,EAAmC,IAAnC,CAAP;AACD;;AAEDC,EAAAA,WAAW,CAACD,IAAD,EAAaE,gBAAb,EAA+C;AACxD,UAAMC,UAAU,GAAGD,gBAAgB,IAAIF,IAAI,CAACI,aAAL,EAAvC;AACA,UAAMC,QAAQ,GAAGF,UAAU,CAACG,6BAAX,CAAyCN,IAAI,CAACO,GAA9C,CAAjB;AACA,UAAMC,IAAI,GAAGH,QAAQ,CAACG,IAAT,GAAgB,CAA7B;AACA,UAAMC,SAAS,GAAGJ,QAAQ,CAACI,SAAT,GAAqB,CAAvC,CAJwD,C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AG,GAAG,MAAMmC,OAAO,CAACC,GAAR,CAChB,CAAAF,OAAO,SAAP,IAAAA,OAAO,WAAP,6BAAAA,OAAO,CAAE/B,IAAT,gEAAeT,GAAf,CAAmB,MAAOK,UAAP,IAAsB;AACvC,YAAMT,IAAI,GAAG,KAAKL,mBAAL,CAAyBc,UAAU,CAACT,IAApC,CAAb,CADuC,CAEvC;;AACA,UAAI,CAACA,IAAL,EAAW,OAAO,KAAI2C,kCAAJ,EAAkBF,OAAlB,EAA2BtE,SAA3B,EAAsC,EAAtC,CAAP;AACX,UAAI6B,IAAJ,EAAU,OAAO,KAAI2C,kCAAJ,EAAkBF,OAAlB,EAA2BtE,SAA3B,EAAsCA,SAAtC,EAAiD,KAAKoE,IAAL,CAAUvC,IAAV,EAAgBS,UAAU,CAACM,KAA3B,CAAjD,CAAP;AACV,aAAO5C,SAAP;AACD,KAND,MAMM,EAPU,CAAlB;AAUA,UAAM4E,OAAO,GAAG,oBAAKrC,GAAL,CAAhB;AACA,QAAIqC,OAAJ,EAAa,OAAOA,OAAP;AACb,WAAO,KAAIJ,kCAAJ,EAAkB,KAAlB,CAAP;AACD;;AAhKiC","sourcesContent":["import { TsserverClient } from '@teambit/ts-server';\nimport ts, { ExportDeclaration, Node } from 'typescript';\nimport { getTokenAtPosition } from 'tsutils';\nimport { head } from 'lodash';\n// @ts-ignore david we should figure fix this.\nimport type { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';\nimport { resolve } from 'path';\nimport { Component } from '@teambit/component';\nimport { TypeRefSchema, SchemaNode } from '@teambit/semantics.entities.semantic-schema';\nimport { TypeScriptExtractor } from './typescript.extractor';\nimport { ExportList } from './export-list';\n\nexport class SchemaExtractorContext {\n constructor(\n readonly tsserver: TsserverClient,\n readonly component: Component,\n readonly extractor: TypeScriptExtractor\n ) {}\n\n computeSchema(node: Node) {\n return this.extractor.computeSchema(node, this);\n }\n\n getLocation(node: Node, targetSourceFile?: ts.SourceFile) {\n const sourceFile = targetSourceFile || node.getSourceFile();\n const position = sourceFile.getLineAndCharacterOfPosition(node.pos);\n const line = position.line + 1;\n const character = position.character + 2; // need to verify why a 2 char difference here.\n\n return {\n line,\n character,\n };\n }\n\n async getSignature(node: Node) {\n return this.tsserver.getSignatureHelp(this.getPath(node), this.getLocation(node));\n }\n\n getPosition(sourceFile: ts.SourceFile, line: number, offset: number): number {\n return sourceFile.getPositionOfLineAndCharacter(line - 1, offset - 2);\n }\n\n getPath(node: Node) {\n const sourceFile = node.getSourceFile();\n return sourceFile.fileName;\n }\n\n createRef() {\n return {};\n }\n\n getQuickInfo(node: Node) {\n return this.tsserver.getQuickInfo(this.getPath(node), this.getLocation(node));\n }\n\n typeDefinition(node: Node) {\n return this.tsserver.getTypeDefinition(this.getPath(node), this.getLocation(node));\n }\n\n visitTypeDefinition() {}\n\n private findFileInComponent(filePath: string) {\n return this.component.filesystem.files.find((file) => {\n // TODO: fix this line to support further extensions.\n if (file.path.includes(filePath)) {\n const strings = ['ts', 'tsx', 'js', 'jsx'].map((format) => {\n if (filePath.endsWith(format)) return filePath;\n return `${filePath}.${format}`;\n });\n\n return strings.find((string) => string === file.path);\n }\n\n return false;\n });\n }\n\n /**\n * return the file if part of the component.\n * otherwise, a reference to the target package and the type name.\n */\n private getSourceFile(filePath: string) {\n const file = this.findFileInComponent(filePath);\n if (!file) return undefined;\n return this.extractor.parseSourceFile(file);\n }\n\n async definition(node: Node): Promise<Node | undefined> {\n const def = await this.tsserver.getDefinition(this.getPath(node), this.getLocation(node));\n\n const firstDef = head(def.body);\n if (!firstDef) return undefined;\n\n const startPosition = firstDef.start;\n const sourceFile = this.getSourceFile(firstDef.file);\n if (!sourceFile) return undefined; // learn how to return a reference to a different component here.\n const pos = this.getPosition(sourceFile, startPosition.line, startPosition.offset);\n const nodeAtPos = getTokenAtPosition(sourceFile, pos);\n return nodeAtPos;\n }\n\n async visitDefinition(node: Node): Promise<SchemaNode | undefined> {\n const definition = await this.definition(node);\n if (!definition) return undefined;\n return this.visit(definition.parent);\n }\n\n async visit(node: Node): Promise<SchemaNode | undefined> {\n return this.extractor.computeSchema(node, this);\n }\n\n references() {}\n\n isExported() {}\n\n isFromComponent() {}\n\n async getFileExports(exportDec: ExportDeclaration) {\n const file = exportDec.getSourceFile().fileName;\n const specifierPathStr = exportDec.moduleSpecifier?.getText() || '';\n const specifierPath = specifierPathStr.substring(1, specifierPathStr.length - 1);\n const absPath = resolve(file, '..', specifierPath);\n const sourceFile = this.getSourceFile(absPath);\n if (!sourceFile) return [];\n return this.extractor.computeExportedIdentifiers(sourceFile, this);\n }\n\n _exports: ExportList | undefined = undefined;\n\n setExports(exports: ExportList) {\n this._exports = exports;\n return this;\n }\n\n getExportedIdentifiers(node: Node) {\n return this.extractor.computeExportedIdentifiers(node, this);\n }\n\n private isNative(typeName: string) {\n return ['string', 'number', 'bool', 'boolean', 'object', 'any', 'void'].includes(typeName);\n }\n\n async jump(file: AbstractVinyl, start: any) {\n const sourceFile = this.extractor.parseSourceFile(file);\n const pos = this.getPosition(sourceFile, start.line, start.offset);\n const nodeAtPos = getTokenAtPosition(sourceFile, pos);\n if (!nodeAtPos) return undefined;\n return this.visit(nodeAtPos);\n }\n\n async resolveType(node: Node, typeStr: string, type = true): Promise<TypeRefSchema> {\n if (this.isNative(typeStr)) return new TypeRefSchema(typeStr);\n if (this._exports?.includes(typeStr)) return new TypeRefSchema(typeStr);\n\n const typeDef = type\n ? await this.tsserver.getDefinition(node.getSourceFile().fileName, this.getLocation(node))\n : await this.typeDefinition(node);\n\n const def = await Promise.all(\n typeDef?.body?.map(async (definition) => {\n const file = this.findFileInComponent(definition.file);\n // TODO: find component id is exists, otherwise add the package name.\n if (!file) return new TypeRefSchema(typeStr, undefined, '');\n if (file) return new TypeRefSchema(typeStr, undefined, undefined, this.jump(file, definition.start));\n return undefined;\n }) || []\n );\n\n const headDef = head(def);\n if (headDef) return headDef;\n return new TypeRefSchema('any');\n }\n}\n"]}
1
+ {"version":3,"sources":["schema-extractor-context.ts"],"names":["SchemaExtractorContext","constructor","tsserver","component","extractor","undefined","computeSchema","node","getLocation","targetSourceFile","sourceFile","getSourceFile","position","getLineAndCharacterOfPosition","getStart","line","character","getSignature","getSignatureHelp","getPath","getPosition","offset","getPositionOfLineAndCharacter","fileName","getQuickInfo","typeDefinition","getTypeDefinition","visitTypeDefinition","findFileInComponent","filePath","filesystem","files","find","file","path","includes","strings","map","format","endsWith","string","parsePackageNameFromPath","parts","split","length","lastPart","replace","sep","pkgParts","startsWith","parseSourceFile","getSourceFileFromNode","def","getDefinition","firstDef","body","definition","startPosition","start","pos","nodeAtPos","visitDefinition","visit","parent","references","isExported","isFromComponent","getFileExports","exportDec","specifierPathStr","moduleSpecifier","getText","specifierPath","substring","absPath","computeExportedIdentifiers","setExports","exports","_exports","getExportedIdentifiers","isNative","typeName","jump","kind","ts","SyntaxKind","Identifier","resolveType","typeStr","type","TypeRefSchema","LiteralType","literal","getDef","headTypeDefinition","unknownExactType","isDefInSameLocation","loc","pkgName"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAGA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAIO,MAAMA,sBAAN,CAA6B;AAClCC,EAAAA,WAAW,CACAC,QADA,EAEAC,SAFA,EAGAC,SAHA,EAIT;AAAA,SAHSF,QAGT,GAHSA,QAGT;AAAA,SAFSC,SAET,GAFSA,SAET;AAAA,SADSC,SACT,GADSA,SACT;AAAA,sDAuKiCC,SAvKjC;AAAE;;AAEJC,EAAAA,aAAa,CAACC,IAAD,EAAa;AACxB,WAAO,KAAKH,SAAL,CAAeE,aAAf,CAA6BC,IAA7B,EAAmC,IAAnC,CAAP;AACD;AAED;AACF;AACA;;;AACE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D,IAAjB,CAAZ;AACA,aAAO0F,GAAG,CAAClF,IAAJ,KAAayC,UAAU,CAACE,KAAX,CAAiB3C,IAA9B,IAAsCkF,GAAG,CAACjF,SAAJ,KAAkBwC,UAAU,CAACE,KAAX,CAAiBrC,MAAhF;AACD,KAND;;AAQA,UAAMY,IAAI,GAAG,KAAKL,mBAAL,CAAyB4B,UAAU,CAACvB,IAApC,CAAb;;AACA,QAAIA,IAAJ,EAAU;AACR,UAAI+D,mBAAmB,EAAvB,EAA2B;AACzB,eAAOD,gBAAP;AACD;;AACD,aAAO,KAAIL,kCAAJ,EAAkBF,OAAlB,EAA2BnF,SAA3B,EAAsCA,SAAtC,EAAiD,MAAM,KAAK6E,IAAL,CAAUjD,IAAV,EAAgBuB,UAAU,CAACE,KAA3B,CAAvD,CAAP;AACD;;AACD,UAAMwC,OAAO,GAAG,KAAKzD,wBAAL,CAA8Be,UAAU,CAACvB,IAAzC,CAAhB,CAnDqE,CAoDrE;;AACA,WAAO,KAAIyD,kCAAJ,EAAkBF,OAAlB,EAA2BnF,SAA3B,EAAsC6F,OAAtC,CAAP;AACD;;AAhQiC","sourcesContent":["import { TsserverClient } from '@teambit/ts-server';\nimport ts, { ExportDeclaration, Node, LiteralTypeNode } from 'typescript';\nimport { getTokenAtPosition } from 'tsutils';\nimport { head } from 'lodash';\n// @ts-ignore david we should figure fix this.\nimport type { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';\nimport { resolve, sep } from 'path';\nimport { Component } from '@teambit/component';\nimport { TypeRefSchema, SchemaNode } from '@teambit/semantics.entities.semantic-schema';\nimport { TypeScriptExtractor } from './typescript.extractor';\nimport { ExportList } from './export-list';\n\nexport class SchemaExtractorContext {\n constructor(\n readonly tsserver: TsserverClient,\n readonly component: Component,\n readonly extractor: TypeScriptExtractor\n ) {}\n\n computeSchema(node: Node) {\n return this.extractor.computeSchema(node, this);\n }\n\n /**\n * returns the location of a node in a source file.\n */\n getLocation(node: Node, targetSourceFile?: ts.SourceFile) {\n const sourceFile = targetSourceFile || node.getSourceFile();\n const position = sourceFile.getLineAndCharacterOfPosition(node.getStart());\n const line = position.line + 1;\n const character = position.character + 1;\n\n return {\n line,\n character,\n };\n }\n\n /**\n * returns a signature for a node.\n */\n async getSignature(node: Node) {\n return this.tsserver.getSignatureHelp(this.getPath(node), this.getLocation(node));\n }\n\n /**\n * get the position for the tsserver.\n */\n getPosition(sourceFile: ts.SourceFile, line: number, offset: number): number {\n return sourceFile.getPositionOfLineAndCharacter(line - 1, offset - 1);\n }\n\n /**\n * get the path for a source file.\n */\n getPath(node: Node) {\n const sourceFile = node.getSourceFile();\n return sourceFile.fileName;\n }\n\n /**\n * create a reference to a type from a component.\n * think if we don't need this because of type ref\n */\n // createRef() {\n // return {};\n // }\n\n getQuickInfo(node: Node) {\n return this.tsserver.getQuickInfo(this.getPath(node), this.getLocation(node));\n }\n\n /**\n * returns the type definition for a type.\n */\n typeDefinition(node: Node) {\n return this.tsserver.getTypeDefinition(this.getPath(node), this.getLocation(node));\n }\n\n visitTypeDefinition() {}\n\n private findFileInComponent(filePath: string) {\n return this.component.filesystem.files.find((file) => {\n // TODO: fix this line to support further extensions.\n if (file.path.includes(filePath)) {\n const strings = ['ts', 'tsx', 'js', 'jsx'].map((format) => {\n if (filePath.endsWith(format)) return filePath;\n return `${filePath}.${format}`;\n });\n\n return strings.find((string) => string === file.path);\n }\n\n return false;\n });\n }\n\n private parsePackageNameFromPath(path: string) {\n const parts = path.split('node_modules');\n if (parts.length === 1) return '';\n const lastPart = parts[parts.length - 1].replace(sep, '');\n const pkgParts = lastPart.split('/');\n if (lastPart.startsWith('@')) {\n // scoped package\n return `${pkgParts[0]}/${pkgParts[1]}`;\n }\n return pkgParts[0];\n }\n\n /**\n * return the file if part of the component.\n * otherwise, a reference to the target package and the type name.\n */\n private getSourceFile(filePath: string) {\n const file = this.findFileInComponent(filePath);\n if (!file) return undefined;\n return this.extractor.parseSourceFile(file);\n }\n\n async getSourceFileFromNode(node: Node) {\n const def = await this.tsserver.getDefinition(this.getPath(node), this.getLocation(node));\n\n const firstDef = head(def.body);\n if (!firstDef) {\n return undefined;\n }\n\n const sourceFile = this.getSourceFile(firstDef.file);\n\n return sourceFile;\n }\n\n /**\n * get a definition for a given node.\n */\n async definition(node: Node): Promise<Node | undefined> {\n const def = await this.tsserver.getDefinition(this.getPath(node), this.getLocation(node));\n\n const firstDef = head(def.body);\n if (!firstDef) {\n return undefined;\n }\n\n const startPosition = firstDef.start;\n const sourceFile = this.getSourceFile(firstDef.file);\n if (!sourceFile) {\n return undefined; // learn how to return a reference to a different component here.\n }\n const pos = this.getPosition(sourceFile, startPosition.line, startPosition.offset);\n const nodeAtPos = getTokenAtPosition(sourceFile, pos);\n return nodeAtPos;\n }\n\n /**\n * visit a definition for node - e.g. return it's schema.\n */\n async visitDefinition(node: Node): Promise<SchemaNode | undefined> {\n const definition = await this.definition(node);\n if (!definition) {\n return undefined;\n }\n return this.visit(definition.parent);\n }\n\n async visit(node: Node): Promise<SchemaNode> {\n return this.extractor.computeSchema(node, this);\n }\n\n references() {}\n\n isExported() {}\n\n isFromComponent() {}\n\n async getFileExports(exportDec: ExportDeclaration) {\n const file = exportDec.getSourceFile().fileName;\n const specifierPathStr = exportDec.moduleSpecifier?.getText() || '';\n const specifierPath = specifierPathStr.substring(1, specifierPathStr.length - 1);\n const absPath = resolve(file, '..', specifierPath);\n const sourceFile = this.getSourceFile(absPath);\n if (!sourceFile) return [];\n return this.extractor.computeExportedIdentifiers(sourceFile, this);\n }\n\n _exports: ExportList | undefined = undefined;\n\n setExports(exports: ExportList) {\n this._exports = exports;\n return this;\n }\n\n getExportedIdentifiers(node: Node) {\n return this.extractor.computeExportedIdentifiers(node, this);\n }\n\n private isNative(typeName: string) {\n return ['string', 'number', 'bool', 'boolean', 'object', 'any', 'void'].includes(typeName);\n }\n\n async jump(file: AbstractVinyl, start: any): Promise<SchemaNode | undefined> {\n const sourceFile = this.extractor.parseSourceFile(file);\n const pos = this.getPosition(sourceFile, start.line, start.offset);\n const nodeAtPos = getTokenAtPosition(sourceFile, pos);\n if (!nodeAtPos) return undefined;\n if (nodeAtPos.kind === ts.SyntaxKind.Identifier) {\n // @todo: make sure with Ran that it's fine. Maybe it's better to do: `this.visit(nodeAtPos.parent);`\n return this.visitDefinition(nodeAtPos);\n }\n return this.visit(nodeAtPos);\n }\n\n /**\n * resolve a type by a node and its identifier.\n */\n async resolveType(node: Node, typeStr: string): Promise<TypeRefSchema> {\n // if a node has \"type\" prop, it has the type data of the node. this normally happens when the code has the type\n // explicitly, e.g. `const str: string` vs implicitly `const str = 'some-string'`, which the node won't have \"type\"\n // @ts-ignore\n node = node.type || node;\n if (this.isNative(typeStr)) return new TypeRefSchema(typeStr);\n if (node.kind === ts.SyntaxKind.LiteralType) {\n return new TypeRefSchema((node as LiteralTypeNode).literal.getText());\n }\n if (this._exports?.includes(typeStr)) return new TypeRefSchema(typeStr);\n\n /**\n * tsserver has two different calls: \"definition\" and \"typeDefinition\".\n * normally, we need the \"typeDefinition\" to get the type data of a node.\n * sometimes, it has no data, for example when the node is of type TypeReference, and then using \"definition\" is\n * helpful. (couldn't find a rule when to use each one. e.g. \"VariableDeclaration\" sometimes has data only in\n * \"definition\" but it's not clear when/why).\n */\n const getDef = async () => {\n const typeDefinition = await this.typeDefinition(node);\n const headTypeDefinition = head(typeDefinition?.body);\n if (headTypeDefinition) {\n return headTypeDefinition;\n }\n const definition = await this.tsserver.getDefinition(node.getSourceFile().fileName, this.getLocation(node));\n return head(definition?.body);\n };\n const definition = await getDef();\n\n // when we can't figure out the component/package/type of this node, we'll use the typeStr as the type.\n const unknownExactType = new TypeRefSchema(typeStr);\n if (!definition) {\n return unknownExactType;\n }\n\n // the reason for this check is to avoid infinite loop when calling `this.jump` with the same file+location\n const isDefInSameLocation = () => {\n if (definition.file !== node.getSourceFile().fileName) {\n return false;\n }\n const loc = this.getLocation(node);\n return loc.line === definition.start.line && loc.character === definition.start.offset;\n };\n\n const file = this.findFileInComponent(definition.file);\n if (file) {\n if (isDefInSameLocation()) {\n return unknownExactType;\n }\n return new TypeRefSchema(typeStr, undefined, undefined, await this.jump(file, definition.start));\n }\n const pkgName = this.parsePackageNameFromPath(definition.file);\n // TODO: find component id is exists, otherwise add the package name.\n return new TypeRefSchema(typeStr, undefined, pkgName);\n }\n}\n"]}
@@ -0,0 +1,11 @@
1
+ import { ClassSchema } from '@teambit/semantics.entities.semantic-schema';
2
+ import { Node, ClassDeclaration } from 'typescript';
3
+ import { SchemaTransformer } from '../schema-transformer';
4
+ import { SchemaExtractorContext } from '../schema-extractor-context';
5
+ import { ExportIdentifier } from '../export-identifier';
6
+ export declare class ClassDecelerationTransformer implements SchemaTransformer {
7
+ predicate(node: Node): boolean;
8
+ private getName;
9
+ getIdentifiers(node: ClassDeclaration): Promise<ExportIdentifier[]>;
10
+ transform(node: ClassDeclaration, context: SchemaExtractorContext): Promise<ClassSchema>;
11
+ }
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ require("core-js/modules/es.promise.js");
6
+
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ exports.ClassDecelerationTransformer = void 0;
11
+
12
+ function _pMapSeries() {
13
+ const data = _interopRequireDefault(require("p-map-series"));
14
+
15
+ _pMapSeries = function () {
16
+ return data;
17
+ };
18
+
19
+ return data;
20
+ }
21
+
22
+ function _semanticsEntities() {
23
+ const data = require("@teambit/semantics.entities.semantic-schema");
24
+
25
+ _semanticsEntities = function () {
26
+ return data;
27
+ };
28
+
29
+ return data;
30
+ }
31
+
32
+ function _typescript() {
33
+ const data = _interopRequireDefault(require("typescript"));
34
+
35
+ _typescript = function () {
36
+ return data;
37
+ };
38
+
39
+ return data;
40
+ }
41
+
42
+ function _exportIdentifier() {
43
+ const data = require("../export-identifier");
44
+
45
+ _exportIdentifier = function () {
46
+ return data;
47
+ };
48
+
49
+ return data;
50
+ }
51
+
52
+ function _parseTypeFromQuickInfo() {
53
+ const data = require("./utils/parse-type-from-quick-info");
54
+
55
+ _parseTypeFromQuickInfo = function () {
56
+ return data;
57
+ };
58
+
59
+ return data;
60
+ }
61
+
62
+ function _getParams() {
63
+ const data = require("./utils/get-params");
64
+
65
+ _getParams = function () {
66
+ return data;
67
+ };
68
+
69
+ return data;
70
+ }
71
+
72
+ class ClassDecelerationTransformer {
73
+ predicate(node) {
74
+ return node.kind === _typescript().default.SyntaxKind.ClassDeclaration;
75
+ } // @todo: in case of `export default class` the class has no name.
76
+
77
+
78
+ getName(node) {
79
+ var _node$name;
80
+
81
+ return ((_node$name = node.name) === null || _node$name === void 0 ? void 0 : _node$name.getText()) || 'default';
82
+ }
83
+
84
+ async getIdentifiers(node) {
85
+ return [new (_exportIdentifier().ExportIdentifier)(this.getName(node), node.getSourceFile().fileName)];
86
+ }
87
+
88
+ async transform(node, context) {
89
+ const className = this.getName(node);
90
+ const members = await (0, _pMapSeries().default)(node.members, async member => {
91
+ switch (member.kind) {
92
+ case _typescript().default.SyntaxKind.GetAccessor:
93
+ {
94
+ var _info$body;
95
+
96
+ const getter = member;
97
+ const info = await context.getQuickInfo(getter.name);
98
+ const displaySig = (info === null || info === void 0 ? void 0 : (_info$body = info.body) === null || _info$body === void 0 ? void 0 : _info$body.displayString) || '';
99
+ const typeStr = (0, _parseTypeFromQuickInfo().parseTypeFromQuickInfo)(displaySig);
100
+ const type = await context.resolveType(getter, typeStr);
101
+ return new (_semanticsEntities().GetAccessorSchema)(getter.name.getText(), type, displaySig);
102
+ }
103
+
104
+ case _typescript().default.SyntaxKind.SetAccessor:
105
+ {
106
+ var _info$body2;
107
+
108
+ const setter = member;
109
+ const params = await (0, _getParams().getParams)(setter.parameters, context);
110
+ const info = await context.getQuickInfo(setter.name);
111
+ const displaySig = (info === null || info === void 0 ? void 0 : (_info$body2 = info.body) === null || _info$body2 === void 0 ? void 0 : _info$body2.displayString) || '';
112
+ return new (_semanticsEntities().SetAccessorSchema)(setter.name.getText(), params[0], displaySig);
113
+ }
114
+
115
+ default:
116
+ return context.computeSchema(member);
117
+ }
118
+ });
119
+ return new (_semanticsEntities().ClassSchema)(className, members);
120
+ }
121
+
122
+ }
123
+
124
+ exports.ClassDecelerationTransformer = ClassDecelerationTransformer;
125
+
126
+ //# sourceMappingURL=class-deceleration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["class-deceleration.ts"],"names":["ClassDecelerationTransformer","predicate","node","kind","ts","SyntaxKind","ClassDeclaration","getName","name","getText","getIdentifiers","ExportIdentifier","getSourceFile","fileName","transform","context","className","members","member","GetAccessor","getter","info","getQuickInfo","displaySig","body","displayString","typeStr","type","resolveType","GetAccessorSchema","SetAccessor","setter","params","parameters","SetAccessorSchema","computeSchema","ClassSchema"],"mappings":";;;;;;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAGA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEO,MAAMA,4BAAN,CAAgE;AACrEC,EAAAA,SAAS,CAACC,IAAD,EAAa;AACpB,WAAOA,IAAI,CAACC,IAAL,KAAcC,sBAAGC,UAAH,CAAcC,gBAAnC;AACD,GAHoE,CAKrE;;;AACQC,EAAAA,OAAO,CAACL,IAAD,EAAyB;AAAA;;AACtC,WAAO,eAAAA,IAAI,CAACM,IAAL,0DAAWC,OAAX,OAAwB,SAA/B;AACD;;AAEmB,QAAdC,cAAc,CAACR,IAAD,EAAyB;AAC3C,WAAO,CAAC,KAAIS,oCAAJ,EAAqB,KAAKJ,OAAL,CAAaL,IAAb,CAArB,EAAyCA,IAAI,CAACU,aAAL,GAAqBC,QAA9D,CAAD,CAAP;AACD;;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,CAAYC,OAAZ,EAAtB,EAA6CuB,MAAM,CAAC,CAAD,CAAnD,EAAwDT,UAAxD,CAAP;AACD;;AACD;AACE,iBAAOR,OAAO,CAACoB,aAAR,CAAsBjB,MAAtB,CAAP;AAjBJ;AAmBD,KApBqB,CAAtB;AAqBA,WAAO,KAAIkB,gCAAJ,EAAgBpB,SAAhB,EAA2BC,OAA3B,CAAP;AACD;;AAtCoE","sourcesContent":["import pMapSeries from 'p-map-series';\nimport { ClassSchema, GetAccessorSchema, SetAccessorSchema } from '@teambit/semantics.entities.semantic-schema';\nimport ts, { Node, ClassDeclaration } from 'typescript';\nimport { SchemaTransformer } from '../schema-transformer';\nimport { SchemaExtractorContext } from '../schema-extractor-context';\nimport { ExportIdentifier } from '../export-identifier';\nimport { parseTypeFromQuickInfo } from './utils/parse-type-from-quick-info';\nimport { getParams } from './utils/get-params';\n\nexport class ClassDecelerationTransformer implements SchemaTransformer {\n predicate(node: Node) {\n return node.kind === ts.SyntaxKind.ClassDeclaration;\n }\n\n // @todo: in case of `export default class` the class has no name.\n private getName(node: ClassDeclaration) {\n return node.name?.getText() || 'default';\n }\n\n async getIdentifiers(node: ClassDeclaration) {\n return [new ExportIdentifier(this.getName(node), node.getSourceFile().fileName)];\n }\n\n async transform(node: ClassDeclaration, context: SchemaExtractorContext) {\n const className = this.getName(node);\n const members = await pMapSeries(node.members, async (member) => {\n switch (member.kind) {\n case ts.SyntaxKind.GetAccessor: {\n const getter = member as ts.GetAccessorDeclaration;\n const info = await context.getQuickInfo(getter.name);\n const displaySig = info?.body?.displayString || '';\n const typeStr = parseTypeFromQuickInfo(displaySig);\n const type = await context.resolveType(getter, typeStr);\n return new GetAccessorSchema(getter.name.getText(), type, displaySig);\n }\n case ts.SyntaxKind.SetAccessor: {\n const setter = member as ts.SetAccessorDeclaration;\n const params = await getParams(setter.parameters, context);\n const info = await context.getQuickInfo(setter.name);\n const displaySig = info?.body?.displayString || '';\n return new SetAccessorSchema(setter.name.getText(), params[0], displaySig);\n }\n default:\n return context.computeSchema(member);\n }\n });\n return new ClassSchema(className, members);\n }\n}\n"]}
@@ -0,0 +1,10 @@
1
+ import { SchemaNode } from '@teambit/semantics.entities.semantic-schema';
2
+ import { Node, ConstructorDeclaration } from 'typescript';
3
+ import { SchemaExtractorContext } from '../schema-extractor-context';
4
+ import { SchemaTransformer } from '../schema-transformer';
5
+ import { ExportIdentifier } from '../export-identifier';
6
+ export declare class Constructor implements SchemaTransformer {
7
+ predicate(node: Node): boolean;
8
+ getIdentifiers(node: ConstructorDeclaration): Promise<ExportIdentifier[]>;
9
+ transform(constructorDec: ConstructorDeclaration, context: SchemaExtractorContext): Promise<SchemaNode>;
10
+ }