@teambit/typescript 0.0.728 → 0.0.731
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/exceptions/transformer-not-found.js +14 -1
- package/dist/exceptions/transformer-not-found.js.map +1 -1
- package/dist/schema-extractor-context.d.ts +38 -7
- package/dist/schema-extractor-context.js +228 -31
- package/dist/schema-extractor-context.js.map +1 -1
- package/dist/transformers/class-deceleration.d.ts +11 -0
- package/dist/transformers/class-deceleration.js +141 -0
- package/dist/transformers/class-deceleration.js.map +1 -0
- package/dist/transformers/constructor.d.ts +10 -0
- package/dist/transformers/constructor.js +70 -0
- package/dist/transformers/constructor.js.map +1 -0
- package/dist/transformers/export-declaration.d.ts +2 -1
- package/dist/transformers/export-declaration.js +64 -13
- package/dist/transformers/export-declaration.js.map +1 -1
- package/dist/transformers/function-declaration.d.ts +1 -3
- package/dist/transformers/function-declaration.js +27 -32
- package/dist/transformers/function-declaration.js.map +1 -1
- package/dist/transformers/index-signature.d.ts +9 -0
- package/dist/transformers/index-signature.js +71 -0
- package/dist/transformers/index-signature.js.map +1 -0
- package/dist/transformers/index.d.ts +10 -0
- package/dist/transformers/index.js +160 -0
- package/dist/transformers/index.js.map +1 -1
- package/dist/transformers/interface-declaration.d.ts +10 -0
- package/dist/transformers/interface-declaration.js +73 -0
- package/dist/transformers/interface-declaration.js.map +1 -0
- package/dist/transformers/literal-type.d.ts +8 -0
- package/dist/transformers/literal-type.js +50 -0
- package/dist/transformers/literal-type.js.map +1 -0
- package/dist/transformers/method-declaration.d.ts +11 -0
- package/dist/transformers/method-declaration.js +94 -0
- package/dist/transformers/method-declaration.js.map +1 -0
- package/dist/transformers/method-signature.d.ts +10 -0
- package/dist/transformers/method-signature.js +81 -0
- package/dist/transformers/method-signature.js.map +1 -0
- package/dist/transformers/property-declaration.d.ts +11 -0
- package/dist/transformers/property-declaration.js +80 -0
- package/dist/transformers/property-declaration.js.map +1 -0
- package/dist/transformers/property-signature.d.ts +10 -0
- package/dist/transformers/property-signature.js +90 -0
- package/dist/transformers/property-signature.js.map +1 -0
- package/dist/transformers/source-file-transformer.d.ts +2 -1
- package/dist/transformers/source-file-transformer.js +23 -3
- package/dist/transformers/source-file-transformer.js.map +1 -1
- package/dist/transformers/type-alias.d.ts +4 -1
- package/dist/transformers/type-alias.js +31 -2
- package/dist/transformers/type-alias.js.map +1 -1
- package/dist/transformers/utils/get-params.d.ts +4 -0
- package/dist/transformers/utils/get-params.js +69 -0
- package/dist/transformers/utils/get-params.js.map +1 -0
- package/dist/transformers/utils/parse-type-from-quick-info.d.ts +35 -0
- package/dist/transformers/utils/parse-type-from-quick-info.js +112 -0
- package/dist/transformers/utils/parse-type-from-quick-info.js.map +1 -0
- package/dist/transformers/utils/type-node-to-schema.d.ts +4 -0
- package/dist/transformers/utils/type-node-to-schema.js +251 -0
- package/dist/transformers/utils/type-node-to-schema.js.map +1 -0
- package/dist/transformers/variable-declaration.d.ts +11 -0
- package/dist/transformers/variable-declaration.js +98 -0
- package/dist/transformers/variable-declaration.js.map +1 -0
- package/dist/transformers/variable-statement.d.ts +7 -1
- package/dist/transformers/variable-statement.js +42 -2
- package/dist/transformers/variable-statement.js.map +1 -1
- package/dist/typescript.extractor.d.ts +3 -3
- package/dist/typescript.extractor.js +7 -4
- package/dist/typescript.extractor.js.map +1 -1
- package/dist/typescript.main.runtime.js +1 -1
- package/dist/typescript.main.runtime.js.map +1 -1
- package/exceptions/transformer-not-found.ts +5 -4
- package/package-tar/teambit-typescript-0.0.731.tgz +0 -0
- package/package.json +15 -15
- package/{preview-1652153159767.js → preview-1652498684867.js} +2 -2
- package/transformers/class-deceleration.ts +53 -0
- package/transformers/constructor.ts +22 -0
- package/transformers/export-declaration.ts +42 -13
- package/transformers/function-declaration.ts +9 -28
- package/transformers/index-signature.ts +22 -0
- package/transformers/index.ts +10 -0
- package/transformers/interface-declaration.ts +24 -0
- package/transformers/literal-type.ts +18 -0
- package/transformers/method-declaration.ts +32 -0
- package/transformers/method-signature.ts +30 -0
- package/transformers/property-declaration.ts +30 -0
- package/transformers/property-signature.ts +37 -0
- package/transformers/source-file-transformer.ts +6 -6
- package/transformers/type-alias.ts +12 -2
- package/transformers/utils/get-params.ts +29 -0
- package/transformers/utils/parse-type-from-quick-info.ts +84 -0
- package/transformers/utils/type-node-to-schema.ts +205 -0
- package/transformers/variable-declaration.ts +36 -0
- package/transformers/variable-statement.ts +16 -2
- package/package-tar/teambit-typescript-0.0.728.tgz +0 -0
|
@@ -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":"
|
|
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"]}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { TsserverClient } from '@teambit/ts-server';
|
|
2
|
-
import ts, { ExportDeclaration, Node } from 'typescript';
|
|
2
|
+
import ts, { ExportDeclaration, Node, TypeNode } from 'typescript';
|
|
3
3
|
import type { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';
|
|
4
4
|
import { Component } from '@teambit/component';
|
|
5
|
-
import {
|
|
5
|
+
import { SchemaNode } from '@teambit/semantics.entities.semantic-schema';
|
|
6
6
|
import { TypeScriptExtractor } from './typescript.extractor';
|
|
7
7
|
import { ExportList } from './export-list';
|
|
8
8
|
export declare class SchemaExtractorContext {
|
|
@@ -10,27 +10,54 @@ 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
|
|
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
|
-
|
|
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
|
+
getQuickInfoDisplayString(node: Node): Promise<string>;
|
|
39
|
+
/**
|
|
40
|
+
* returns the type definition for a type.
|
|
41
|
+
*/
|
|
23
42
|
typeDefinition(node: Node): Promise<import("typescript/lib/protocol").TypeDefinitionResponse | undefined>;
|
|
24
43
|
visitTypeDefinition(): void;
|
|
25
44
|
private findFileInComponent;
|
|
45
|
+
private parsePackageNameFromPath;
|
|
26
46
|
/**
|
|
27
47
|
* return the file if part of the component.
|
|
28
48
|
* otherwise, a reference to the target package and the type name.
|
|
29
49
|
*/
|
|
30
50
|
private getSourceFile;
|
|
51
|
+
getSourceFileFromNode(node: Node): Promise<ts.SourceFile | undefined>;
|
|
52
|
+
/**
|
|
53
|
+
* get a definition for a given node.
|
|
54
|
+
*/
|
|
31
55
|
definition(node: Node): Promise<Node | undefined>;
|
|
56
|
+
/**
|
|
57
|
+
* visit a definition for node - e.g. return it's schema.
|
|
58
|
+
*/
|
|
32
59
|
visitDefinition(node: Node): Promise<SchemaNode | undefined>;
|
|
33
|
-
visit(node: Node): Promise<SchemaNode
|
|
60
|
+
visit(node: Node): Promise<SchemaNode>;
|
|
34
61
|
references(): void;
|
|
35
62
|
isExported(): void;
|
|
36
63
|
isFromComponent(): void;
|
|
@@ -38,7 +65,11 @@ export declare class SchemaExtractorContext {
|
|
|
38
65
|
_exports: ExportList | undefined;
|
|
39
66
|
setExports(exports: ExportList): this;
|
|
40
67
|
getExportedIdentifiers(node: Node): Promise<import("./export-identifier").ExportIdentifier[]>;
|
|
41
|
-
private isNative;
|
|
42
68
|
jump(file: AbstractVinyl, start: any): Promise<SchemaNode | undefined>;
|
|
43
|
-
|
|
69
|
+
/**
|
|
70
|
+
* resolve a type by a node and its identifier.
|
|
71
|
+
*/
|
|
72
|
+
resolveType(node: Node & {
|
|
73
|
+
type?: TypeNode;
|
|
74
|
+
}, typeStr: string, isTypeStrFromQuickInfo?: boolean): Promise<SchemaNode>;
|
|
44
75
|
}
|
|
@@ -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
|
|
|
@@ -61,6 +73,36 @@ function _semanticsEntities() {
|
|
|
61
73
|
return data;
|
|
62
74
|
}
|
|
63
75
|
|
|
76
|
+
function _typeNodeToSchema() {
|
|
77
|
+
const data = require("./transformers/utils/type-node-to-schema");
|
|
78
|
+
|
|
79
|
+
_typeNodeToSchema = function () {
|
|
80
|
+
return data;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return data;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function _exceptions() {
|
|
87
|
+
const data = require("./exceptions");
|
|
88
|
+
|
|
89
|
+
_exceptions = function () {
|
|
90
|
+
return data;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
return data;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function _parseTypeFromQuickInfo() {
|
|
97
|
+
const data = require("./transformers/utils/parse-type-from-quick-info");
|
|
98
|
+
|
|
99
|
+
_parseTypeFromQuickInfo = function () {
|
|
100
|
+
return data;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return data;
|
|
104
|
+
}
|
|
105
|
+
|
|
64
106
|
class SchemaExtractorContext {
|
|
65
107
|
constructor(tsserver, component, extractor) {
|
|
66
108
|
this.tsserver = tsserver;
|
|
@@ -72,40 +114,70 @@ class SchemaExtractorContext {
|
|
|
72
114
|
computeSchema(node) {
|
|
73
115
|
return this.extractor.computeSchema(node, this);
|
|
74
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* returns the location of a node in a source file.
|
|
119
|
+
*/
|
|
120
|
+
|
|
75
121
|
|
|
76
122
|
getLocation(node, targetSourceFile) {
|
|
77
123
|
const sourceFile = targetSourceFile || node.getSourceFile();
|
|
78
|
-
const position = sourceFile.getLineAndCharacterOfPosition(node.
|
|
124
|
+
const position = sourceFile.getLineAndCharacterOfPosition(node.getStart());
|
|
79
125
|
const line = position.line + 1;
|
|
80
|
-
const character = position.character +
|
|
81
|
-
|
|
126
|
+
const character = position.character + 1;
|
|
82
127
|
return {
|
|
83
128
|
line,
|
|
84
129
|
character
|
|
85
130
|
};
|
|
86
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* returns a signature for a node.
|
|
134
|
+
*/
|
|
135
|
+
|
|
87
136
|
|
|
88
137
|
async getSignature(node) {
|
|
89
138
|
return this.tsserver.getSignatureHelp(this.getPath(node), this.getLocation(node));
|
|
90
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* get the position for the tsserver.
|
|
142
|
+
*/
|
|
143
|
+
|
|
91
144
|
|
|
92
145
|
getPosition(sourceFile, line, offset) {
|
|
93
|
-
return sourceFile.getPositionOfLineAndCharacter(line - 1, offset -
|
|
146
|
+
return sourceFile.getPositionOfLineAndCharacter(line - 1, offset - 1);
|
|
94
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* get the path for a source file.
|
|
150
|
+
*/
|
|
151
|
+
|
|
95
152
|
|
|
96
153
|
getPath(node) {
|
|
97
154
|
const sourceFile = node.getSourceFile();
|
|
98
155
|
return sourceFile.fileName;
|
|
99
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* create a reference to a type from a component.
|
|
159
|
+
* think if we don't need this because of type ref
|
|
160
|
+
*/
|
|
161
|
+
// createRef() {
|
|
162
|
+
// return {};
|
|
163
|
+
// }
|
|
100
164
|
|
|
101
|
-
createRef() {
|
|
102
|
-
return {};
|
|
103
|
-
}
|
|
104
165
|
|
|
105
166
|
getQuickInfo(node) {
|
|
106
167
|
return this.tsserver.getQuickInfo(this.getPath(node), this.getLocation(node));
|
|
107
168
|
}
|
|
108
169
|
|
|
170
|
+
async getQuickInfoDisplayString(node) {
|
|
171
|
+
var _quickInfo$body;
|
|
172
|
+
|
|
173
|
+
const quickInfo = await this.tsserver.getQuickInfo(this.getPath(node), this.getLocation(node));
|
|
174
|
+
return (quickInfo === null || quickInfo === void 0 ? void 0 : (_quickInfo$body = quickInfo.body) === null || _quickInfo$body === void 0 ? void 0 : _quickInfo$body.displayString) || '';
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* returns the type definition for a type.
|
|
178
|
+
*/
|
|
179
|
+
|
|
180
|
+
|
|
109
181
|
typeDefinition(node) {
|
|
110
182
|
return this.tsserver.getTypeDefinition(this.getPath(node), this.getLocation(node));
|
|
111
183
|
}
|
|
@@ -126,6 +198,27 @@ class SchemaExtractorContext {
|
|
|
126
198
|
return false;
|
|
127
199
|
});
|
|
128
200
|
}
|
|
201
|
+
|
|
202
|
+
parsePackageNameFromPath(path) {
|
|
203
|
+
const parts = path.split('node_modules');
|
|
204
|
+
if (parts.length === 1) return '';
|
|
205
|
+
const lastPart = parts[parts.length - 1].replace(_path().sep, '');
|
|
206
|
+
const pkgParts = lastPart.split('/');
|
|
207
|
+
|
|
208
|
+
if (lastPart.startsWith('@')) {
|
|
209
|
+
// scoped package
|
|
210
|
+
return `${pkgParts[0]}/${pkgParts[1]}`;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const pkgName = pkgParts[0];
|
|
214
|
+
|
|
215
|
+
if (pkgName === 'typescript') {
|
|
216
|
+
// it's a built-in type, such as "string".
|
|
217
|
+
return '';
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return pkgName;
|
|
221
|
+
}
|
|
129
222
|
/**
|
|
130
223
|
* return the file if part of the component.
|
|
131
224
|
* otherwise, a reference to the target package and the type name.
|
|
@@ -138,22 +231,53 @@ class SchemaExtractorContext {
|
|
|
138
231
|
return this.extractor.parseSourceFile(file);
|
|
139
232
|
}
|
|
140
233
|
|
|
234
|
+
async getSourceFileFromNode(node) {
|
|
235
|
+
const def = await this.tsserver.getDefinition(this.getPath(node), this.getLocation(node));
|
|
236
|
+
const firstDef = (0, _lodash().head)(def.body);
|
|
237
|
+
|
|
238
|
+
if (!firstDef) {
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const sourceFile = this.getSourceFile(firstDef.file);
|
|
243
|
+
return sourceFile;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* get a definition for a given node.
|
|
247
|
+
*/
|
|
248
|
+
|
|
249
|
+
|
|
141
250
|
async definition(node) {
|
|
142
251
|
const def = await this.tsserver.getDefinition(this.getPath(node), this.getLocation(node));
|
|
143
252
|
const firstDef = (0, _lodash().head)(def.body);
|
|
144
|
-
|
|
253
|
+
|
|
254
|
+
if (!firstDef) {
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
|
|
145
258
|
const startPosition = firstDef.start;
|
|
146
259
|
const sourceFile = this.getSourceFile(firstDef.file);
|
|
147
|
-
|
|
260
|
+
|
|
261
|
+
if (!sourceFile) {
|
|
262
|
+
return undefined; // learn how to return a reference to a different component here.
|
|
263
|
+
}
|
|
148
264
|
|
|
149
265
|
const pos = this.getPosition(sourceFile, startPosition.line, startPosition.offset);
|
|
150
266
|
const nodeAtPos = (0, _tsutils().getTokenAtPosition)(sourceFile, pos);
|
|
151
267
|
return nodeAtPos;
|
|
152
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* visit a definition for node - e.g. return it's schema.
|
|
271
|
+
*/
|
|
272
|
+
|
|
153
273
|
|
|
154
274
|
async visitDefinition(node) {
|
|
155
275
|
const definition = await this.definition(node);
|
|
156
|
-
|
|
276
|
+
|
|
277
|
+
if (!definition) {
|
|
278
|
+
return undefined;
|
|
279
|
+
}
|
|
280
|
+
|
|
157
281
|
return this.visit(definition.parent);
|
|
158
282
|
}
|
|
159
283
|
|
|
@@ -188,34 +312,107 @@ class SchemaExtractorContext {
|
|
|
188
312
|
return this.extractor.computeExportedIdentifiers(node, this);
|
|
189
313
|
}
|
|
190
314
|
|
|
191
|
-
isNative(typeName) {
|
|
192
|
-
return ['string', 'number', 'bool', 'boolean', 'object', 'any', 'void'].includes(typeName);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
315
|
async jump(file, start) {
|
|
196
316
|
const sourceFile = this.extractor.parseSourceFile(file);
|
|
197
317
|
const pos = this.getPosition(sourceFile, start.line, start.offset);
|
|
198
318
|
const nodeAtPos = (0, _tsutils().getTokenAtPosition)(sourceFile, pos);
|
|
199
|
-
if (!nodeAtPos) return undefined;
|
|
200
|
-
|
|
319
|
+
if (!nodeAtPos) return undefined; // this causes some infinite loops. it's helpful for getting more data from types that are not exported.
|
|
320
|
+
// e.g.
|
|
321
|
+
// ```ts
|
|
322
|
+
// class Bar {}
|
|
323
|
+
// export const getBar = () => new Bar();
|
|
324
|
+
// ```
|
|
325
|
+
// if (nodeAtPos.kind === ts.SyntaxKind.Identifier) {
|
|
326
|
+
// // @todo: make sure with Ran that it's fine. Maybe it's better to do: `this.visit(nodeAtPos.parent);`
|
|
327
|
+
// return this.visitDefinition(nodeAtPos);
|
|
328
|
+
// }
|
|
329
|
+
|
|
330
|
+
try {
|
|
331
|
+
return await this.visit(nodeAtPos);
|
|
332
|
+
} catch (err) {
|
|
333
|
+
if (err instanceof _exceptions().TransformerNotFound) {
|
|
334
|
+
return undefined;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
throw err;
|
|
338
|
+
}
|
|
201
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* resolve a type by a node and its identifier.
|
|
342
|
+
*/
|
|
343
|
+
|
|
202
344
|
|
|
203
|
-
async resolveType(node, typeStr,
|
|
204
|
-
var _this$_exports
|
|
345
|
+
async resolveType(node, typeStr, isTypeStrFromQuickInfo = true) {
|
|
346
|
+
var _this$_exports;
|
|
205
347
|
|
|
206
|
-
if (this.isNative(typeStr)) return new (_semanticsEntities().TypeRefSchema)(typeStr);
|
|
207
348
|
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.
|
|
211
349
|
|
|
212
|
-
|
|
213
|
-
if
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
350
|
+
if (node.type && _typescript().default.isTypeNode(node.type)) {
|
|
351
|
+
// if a node has "type" prop, it has the type data of the node. this normally happens when the code has the type
|
|
352
|
+
// explicitly, e.g. `const str: string` vs implicitly `const str = 'some-string'`, which the node won't have "type"
|
|
353
|
+
return (0, _typeNodeToSchema().typeNodeToSchema)(node.type, this);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* tsserver has two different calls: "definition" and "typeDefinition".
|
|
357
|
+
* normally, we need the "typeDefinition" to get the type data of a node.
|
|
358
|
+
* sometimes, it has no data, for example when the node is of type TypeReference, and then using "definition" is
|
|
359
|
+
* helpful. (couldn't find a rule when to use each one. e.g. "VariableDeclaration" sometimes has data only in
|
|
360
|
+
* "definition" but it's not clear when/why).
|
|
361
|
+
*/
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
const getDef = async () => {
|
|
365
|
+
const typeDefinition = await this.typeDefinition(node);
|
|
366
|
+
const headTypeDefinition = (0, _lodash().head)(typeDefinition === null || typeDefinition === void 0 ? void 0 : typeDefinition.body);
|
|
367
|
+
|
|
368
|
+
if (headTypeDefinition) {
|
|
369
|
+
return headTypeDefinition;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const definition = await this.tsserver.getDefinition(node.getSourceFile().fileName, this.getLocation(node));
|
|
373
|
+
return (0, _lodash().head)(definition === null || definition === void 0 ? void 0 : definition.body);
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
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.
|
|
377
|
+
|
|
378
|
+
const unknownExactType = async () => {
|
|
379
|
+
if (isTypeStrFromQuickInfo) {
|
|
380
|
+
return new (_semanticsEntities().InferenceTypeSchema)(typeStr);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const info = await this.getQuickInfo(node);
|
|
384
|
+
const type = (0, _parseTypeFromQuickInfo().parseTypeFromQuickInfo)(info);
|
|
385
|
+
return new (_semanticsEntities().InferenceTypeSchema)(type);
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
if (!definition) {
|
|
389
|
+
return unknownExactType();
|
|
390
|
+
} // the reason for this check is to avoid infinite loop when calling `this.jump` with the same file+location
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
const isDefInSameLocation = () => {
|
|
394
|
+
if (definition.file !== node.getSourceFile().fileName) {
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const loc = this.getLocation(node);
|
|
399
|
+
return loc.line === definition.start.line && loc.character === definition.start.offset;
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const file = this.findFileInComponent(definition.file);
|
|
403
|
+
|
|
404
|
+
if (file) {
|
|
405
|
+
if (isDefInSameLocation()) {
|
|
406
|
+
return unknownExactType();
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const schemaNode = await this.jump(file, definition.start);
|
|
410
|
+
return schemaNode || unknownExactType();
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const pkgName = this.parsePackageNameFromPath(definition.file); // TODO: find component id is exists, otherwise add the package name.
|
|
414
|
+
|
|
415
|
+
return new (_semanticsEntities().TypeRefSchema)(typeStr, undefined, pkgName);
|
|
219
416
|
}
|
|
220
417
|
|
|
221
418
|
}
|
|
@@ -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,GAAG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,cAAL,CAAoBnB,IAApB,CAFV;AAIA,UAAMqC,GAAG,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","getQuickInfoDisplayString","quickInfo","body","displayString","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","pkgName","parseSourceFile","getSourceFileFromNode","def","getDefinition","firstDef","definition","startPosition","start","pos","nodeAtPos","visitDefinition","visit","parent","references","isExported","isFromComponent","getFileExports","exportDec","specifierPathStr","moduleSpecifier","getText","specifierPath","substring","absPath","computeExportedIdentifiers","setExports","exports","_exports","getExportedIdentifiers","jump","err","TransformerNotFound","resolveType","typeStr","isTypeStrFromQuickInfo","TypeRefSchema","type","ts","isTypeNode","getDef","headTypeDefinition","unknownExactType","InferenceTypeSchema","info","isDefInSameLocation","loc","schemaNode"],"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;;AAGA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEO,MAAMA,sBAAN,CAA6B;AAClCC,EAAAA,WAAW,CACAC,QADA,EAEAC,SAFA,EAGAC,SAHA,EAIT;AAAA,SAHSF,QAGT,GAH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,WAAO,KAAIqD,kCAAJ,EAAkBF,OAAlB,EAA2BnF,SAA3B,EAAsCiD,OAAtC,CAAP;AACD;;AA5RiC","sourcesContent":["import { TsserverClient } from '@teambit/ts-server';\nimport ts, { ExportDeclaration, Node, TypeNode } 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, InferenceTypeSchema } from '@teambit/semantics.entities.semantic-schema';\nimport { TypeScriptExtractor } from './typescript.extractor';\nimport { ExportList } from './export-list';\nimport { typeNodeToSchema } from './transformers/utils/type-node-to-schema';\nimport { TransformerNotFound } from './exceptions';\nimport { parseTypeFromQuickInfo } from './transformers/utils/parse-type-from-quick-info';\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 async getQuickInfoDisplayString(node: Node): Promise<string> {\n const quickInfo = await this.tsserver.getQuickInfo(this.getPath(node), this.getLocation(node));\n return quickInfo?.body?.displayString || '';\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 const pkgName = pkgParts[0];\n if (pkgName === 'typescript') {\n // it's a built-in type, such as \"string\".\n return '';\n }\n return pkgName;\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 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\n // this causes some infinite loops. it's helpful for getting more data from types that are not exported.\n // e.g.\n // ```ts\n // class Bar {}\n // export const getBar = () => new Bar();\n // ```\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 try {\n return await this.visit(nodeAtPos);\n } catch (err) {\n if (err instanceof TransformerNotFound) {\n return undefined;\n }\n throw err;\n }\n }\n\n /**\n * resolve a type by a node and its identifier.\n */\n async resolveType(\n node: Node & { type?: TypeNode },\n typeStr: string,\n isTypeStrFromQuickInfo = true\n ): Promise<SchemaNode> {\n if (this._exports?.includes(typeStr)) return new TypeRefSchema(typeStr);\n if (node.type && ts.isTypeNode(node.type)) {\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 return typeNodeToSchema(node.type, this);\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 = async () => {\n if (isTypeStrFromQuickInfo) {\n return new InferenceTypeSchema(typeStr);\n }\n const info = await this.getQuickInfo(node);\n const type = parseTypeFromQuickInfo(info);\n return new InferenceTypeSchema(type);\n };\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 const schemaNode = await this.jump(file, definition.start);\n return schemaNode || unknownExactType();\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
|
+
}
|