@teambit/typescript 1.0.108 → 1.0.109
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/artifacts/__bit_junit.xml +49 -0
- package/artifacts/preview/teambit_typescript_typescript-preview.js +1 -0
- package/dist/{preview-1703647408454.js → preview-1703698405864.js} +2 -2
- package/package.json +18 -18
- package/compiler-options.ts +0 -27
- package/dedupe-path.spec.ts +0 -34
- package/export-identifier.ts +0 -14
- package/extractor-options.ts +0 -21
- package/identifier-list.ts +0 -14
- package/identifier.ts +0 -23
- package/index.ts +0 -14
- package/remove-types-task.ts +0 -33
- package/schema-extractor-context.ts +0 -625
- package/schema-transformer.plugin.ts +0 -14
- package/schema-transformer.ts +0 -23
- package/transform-source-file.spec.ts +0 -155
- package/tsconfig-writer.ts +0 -249
- package/typescript.aspect.ts +0 -5
- package/typescript.compiler.spec.ts +0 -63
- package/typescript.compiler.ts +0 -286
- package/typescript.extractor.ts +0 -247
- package/typescript.main.runtime.ts +0 -442
- package/typescript.parser.spec.ts +0 -221
- package/typescript.parser.ts +0 -123
|
@@ -1,625 +0,0 @@
|
|
|
1
|
-
import { TsserverClient } from '@teambit/ts-server';
|
|
2
|
-
import { getTokenAtPosition, canHaveJsDoc, getJsDoc } from 'tsutils';
|
|
3
|
-
import ts, { ExportAssignment, getTextOfJSDocComment, ExportDeclaration, Node, SyntaxKind, TypeNode } from 'typescript';
|
|
4
|
-
import { head, uniqBy } from 'lodash';
|
|
5
|
-
// @ts-ignore david we should figure fix this.
|
|
6
|
-
// eslint-disable-next-line import/no-unresolved
|
|
7
|
-
import protocol from 'typescript/lib/protocol';
|
|
8
|
-
import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
|
|
9
|
-
import { resolve, sep, relative, join, isAbsolute, extname } from 'path';
|
|
10
|
-
import { Component, ComponentID } from '@teambit/component';
|
|
11
|
-
import {
|
|
12
|
-
TypeRefSchema,
|
|
13
|
-
SchemaNode,
|
|
14
|
-
InferenceTypeSchema,
|
|
15
|
-
Location,
|
|
16
|
-
DocSchema,
|
|
17
|
-
} from '@teambit/semantics.entities.semantic-schema';
|
|
18
|
-
import isRelativeImport from '@teambit/legacy/dist/utils/is-relative-import';
|
|
19
|
-
import { ComponentDependency } from '@teambit/dependency-resolver';
|
|
20
|
-
import { Formatter } from '@teambit/formatter';
|
|
21
|
-
import pMapSeries from 'p-map-series';
|
|
22
|
-
import { TypeScriptExtractor } from './typescript.extractor';
|
|
23
|
-
import { IdentifierList } from './identifier-list';
|
|
24
|
-
import { parseTypeFromQuickInfo } from './transformers/utils/parse-type-from-quick-info';
|
|
25
|
-
import { tagParser } from './transformers/utils/jsdoc-to-doc-schema';
|
|
26
|
-
import { Identifier } from './identifier';
|
|
27
|
-
import { ExportIdentifier } from './export-identifier';
|
|
28
|
-
|
|
29
|
-
export class SchemaExtractorContext {
|
|
30
|
-
/**
|
|
31
|
-
* list of all declared identifiers (exported and internal) by filename
|
|
32
|
-
*/
|
|
33
|
-
private _identifiers = new Map<string, IdentifierList>();
|
|
34
|
-
private _internalIdentifiers = new Map<string, IdentifierList>();
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* computed nodes by filename and (position (line:character))
|
|
38
|
-
*/
|
|
39
|
-
private _computed = new Map<string, SchemaNode>();
|
|
40
|
-
|
|
41
|
-
get mainFile() {
|
|
42
|
-
return pathNormalizeToLinux(this.getPathRelativeToComponent(this.component.mainFile.path));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
get identifiers() {
|
|
46
|
-
return this._identifiers;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get internalIdentifiers() {
|
|
50
|
-
return this._internalIdentifiers;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
get computed() {
|
|
54
|
-
return this._computed;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
get mainFileIdentifierKey() {
|
|
58
|
-
const mainFile = this.component.mainFile;
|
|
59
|
-
return this.getIdentifierKey(mainFile.path);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
get mainModuleIdentifiers() {
|
|
63
|
-
return this.identifiers.get(this.mainFileIdentifierKey);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
constructor(
|
|
67
|
-
readonly tsserver: TsserverClient,
|
|
68
|
-
readonly component: Component,
|
|
69
|
-
readonly extractor: TypeScriptExtractor,
|
|
70
|
-
readonly componentDeps: ComponentDependency[],
|
|
71
|
-
readonly componentRootPath: string,
|
|
72
|
-
readonly hostRootPath: string,
|
|
73
|
-
readonly formatter?: Formatter
|
|
74
|
-
) {
|
|
75
|
-
this.componentRootPath = pathNormalizeToLinux(componentRootPath);
|
|
76
|
-
this.hostRootPath = pathNormalizeToLinux(hostRootPath);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
getComputedNodeKey({ filePath, line, character }: Location) {
|
|
80
|
-
return `${filePath}:${line}:${character}`;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
getIdentifierKeyForNode(node: Node) {
|
|
84
|
-
const filePath = node.getSourceFile().fileName;
|
|
85
|
-
return this.getIdentifierKey(filePath);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
getIdentifierKey(filePath: string) {
|
|
89
|
-
return pathNormalizeToLinux(filePath);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
setComputed(node: SchemaNode) {
|
|
93
|
-
const { location } = node;
|
|
94
|
-
const key = this.getComputedNodeKey(location);
|
|
95
|
-
this.computed.set(key, node);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
setIdentifiers(filePath: string, identifiers: IdentifierList) {
|
|
99
|
-
this._identifiers.set(this.getIdentifierKey(filePath), identifiers);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
setInternalIdentifiers(filePath: string, identifiers: IdentifierList) {
|
|
103
|
-
const existing = this._internalIdentifiers.get(filePath);
|
|
104
|
-
if (!existing) {
|
|
105
|
-
this._internalIdentifiers.set(filePath, identifiers);
|
|
106
|
-
} else {
|
|
107
|
-
const uniqueIdentifiers = uniqBy(existing.identifiers.concat(identifiers.identifiers), (k) => k.aliasId || k.id);
|
|
108
|
-
this._internalIdentifiers.set(filePath, new IdentifierList(uniqueIdentifiers));
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
findComputedSchemaByName(name: string) {
|
|
113
|
-
const computed = Array.from(this.computed.values());
|
|
114
|
-
return computed.filter((schema) => schema.name === name);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
async computeSchema(node: Node): Promise<SchemaNode> {
|
|
118
|
-
const location = this.getLocation(node);
|
|
119
|
-
const key = this.getComputedNodeKey(location);
|
|
120
|
-
const existingComputedSchema = this.computed.get(key);
|
|
121
|
-
if (existingComputedSchema) {
|
|
122
|
-
return existingComputedSchema;
|
|
123
|
-
}
|
|
124
|
-
const computedSchema = await this.extractor.computeSchema(node, this);
|
|
125
|
-
this.setComputed(computedSchema);
|
|
126
|
-
return computedSchema;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async transformSchemaNode(schema: SchemaNode) {
|
|
130
|
-
return this.extractor.transformAPI(schema, this);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* returns the location of a node in a source file.
|
|
135
|
-
*/
|
|
136
|
-
getLocation(node: Node, targetSourceFile?: ts.SourceFile, absolutePath = false): Location {
|
|
137
|
-
const sourceFile = targetSourceFile || node.getSourceFile();
|
|
138
|
-
const filePath = absolutePath ? sourceFile.fileName : this.getPathRelativeToComponent(sourceFile.fileName);
|
|
139
|
-
const position = sourceFile.getLineAndCharacterOfPosition(node.getStart());
|
|
140
|
-
const line = position.line + 1;
|
|
141
|
-
const character = position.character + 1;
|
|
142
|
-
|
|
143
|
-
return {
|
|
144
|
-
filePath: pathNormalizeToLinux(filePath),
|
|
145
|
-
line,
|
|
146
|
-
character,
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
getLocationAsString(node: Node): string {
|
|
151
|
-
const location = this.getLocation(node);
|
|
152
|
-
return `${node.getSourceFile().fileName}, line: ${location.line}, character: ${location.character}`;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
getPathRelativeToComponent(filePath: string): string {
|
|
156
|
-
const basePath = this.component.filesystem.files[0].base;
|
|
157
|
-
return relative(basePath, filePath);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* returns a signature for a node.
|
|
162
|
-
*/
|
|
163
|
-
async getSignature(node: Node) {
|
|
164
|
-
return this.tsserver.getSignatureHelp(this.getPath(node), this.getLocation(node));
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* get the position for the tsserver.
|
|
169
|
-
*/
|
|
170
|
-
getPosition(sourceFile: ts.SourceFile, line: number, offset: number): number {
|
|
171
|
-
return sourceFile.getPositionOfLineAndCharacter(line - 1, offset - 1);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* get the path for a source file.
|
|
176
|
-
*/
|
|
177
|
-
getPath(node: Node) {
|
|
178
|
-
const sourceFile = node.getSourceFile();
|
|
179
|
-
|
|
180
|
-
const fileName = sourceFile.fileName;
|
|
181
|
-
|
|
182
|
-
if (!fileName.startsWith(this.componentRootPath) && !fileName.startsWith(this.hostRootPath)) {
|
|
183
|
-
return join(this.componentRootPath, fileName);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return sourceFile.fileName;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
async getQuickInfo(node: Node) {
|
|
190
|
-
const location = this.getLocation(node);
|
|
191
|
-
try {
|
|
192
|
-
return await this.tsserver.getQuickInfo(this.getPath(node), location);
|
|
193
|
-
} catch (err: any) {
|
|
194
|
-
if (err.message === 'No content available.') {
|
|
195
|
-
throw new Error(
|
|
196
|
-
`unable to get quickinfo data from tsserver at ${this.getPath(node)}, Ln ${location.line}, Col ${
|
|
197
|
-
location.character
|
|
198
|
-
}`
|
|
199
|
-
);
|
|
200
|
-
}
|
|
201
|
-
throw err;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
async getQuickInfoDisplayString(node: Node): Promise<string> {
|
|
206
|
-
const quickInfo = await this.getQuickInfo(node);
|
|
207
|
-
return quickInfo?.body?.displayString || '';
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* returns the type definition for a type.
|
|
212
|
-
*/
|
|
213
|
-
typeDefinition(node: Node) {
|
|
214
|
-
return this.tsserver.getTypeDefinition(this.getPath(node), this.getLocation(node));
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
visitTypeDefinition() {}
|
|
218
|
-
|
|
219
|
-
private getPathWithoutExtension(filePath: string) {
|
|
220
|
-
const knownExtensions = ['ts', 'js', 'jsx', 'tsx'];
|
|
221
|
-
const fileExtension = extname(filePath).substring(1);
|
|
222
|
-
|
|
223
|
-
const filePathWithoutExtension = () => {
|
|
224
|
-
if (knownExtensions.includes(fileExtension)) {
|
|
225
|
-
return filePath.replace(new RegExp(`\\.${fileExtension}$`), '');
|
|
226
|
-
}
|
|
227
|
-
return filePath;
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
if (!isAbsolute(filePath)) {
|
|
231
|
-
return filePathWithoutExtension();
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
if (filePath.startsWith(this.componentRootPath)) {
|
|
235
|
-
return relative(this.componentRootPath, filePathWithoutExtension());
|
|
236
|
-
}
|
|
237
|
-
if (filePath.startsWith(this.hostRootPath)) {
|
|
238
|
-
return relative(this.hostRootPath, filePathWithoutExtension());
|
|
239
|
-
}
|
|
240
|
-
return filePathWithoutExtension();
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
private isIndexFile(filePath: string, currentFilePath: string) {
|
|
244
|
-
const indexFilePath = join(filePath, 'index');
|
|
245
|
-
return pathNormalizeToLinux(indexFilePath) === currentFilePath;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
findFileInComponent(filePath: string) {
|
|
249
|
-
const normalizedFilePath = pathNormalizeToLinux(filePath);
|
|
250
|
-
const pathToCompareWithoutExtension = this.getPathWithoutExtension(normalizedFilePath);
|
|
251
|
-
|
|
252
|
-
const matchingFile = this.component.filesystem.files.find((file) => {
|
|
253
|
-
const currentFilePath = pathNormalizeToLinux(file.path);
|
|
254
|
-
const currentFilePathWithoutExtension = this.getPathWithoutExtension(currentFilePath);
|
|
255
|
-
|
|
256
|
-
const isSameFilePath = pathToCompareWithoutExtension === currentFilePathWithoutExtension;
|
|
257
|
-
|
|
258
|
-
const matches =
|
|
259
|
-
isSameFilePath || this.isIndexFile(pathToCompareWithoutExtension, currentFilePathWithoutExtension);
|
|
260
|
-
|
|
261
|
-
return matches;
|
|
262
|
-
});
|
|
263
|
-
return matchingFile;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
private parsePackageNameFromPath(path: string) {
|
|
267
|
-
const parts = path.split('node_modules');
|
|
268
|
-
|
|
269
|
-
if (parts.length === 1) {
|
|
270
|
-
return path;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const lastPart = parts[parts.length - 1].replace(sep, '');
|
|
274
|
-
const pkgParts = lastPart.split('/');
|
|
275
|
-
if (lastPart.startsWith('@')) {
|
|
276
|
-
// scoped package
|
|
277
|
-
return `${pkgParts[0]}/${pkgParts[1]}`;
|
|
278
|
-
}
|
|
279
|
-
const pkgName = pkgParts[0];
|
|
280
|
-
if (pkgName === 'typescript') {
|
|
281
|
-
// it's a built-in type, such as "string".
|
|
282
|
-
return '';
|
|
283
|
-
}
|
|
284
|
-
return pkgName;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* return the file if part of the component.
|
|
289
|
-
* otherwise, a reference to the target package and the type name.
|
|
290
|
-
*/
|
|
291
|
-
getSourceFileInsideComponent(filePath: string) {
|
|
292
|
-
const file = this.findFileInComponent(filePath);
|
|
293
|
-
return file && this.extractor.parseSourceFile(file);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
async getSourceFileFromNode(node: Node) {
|
|
297
|
-
const filePath = await this.getFilePathByNode(node);
|
|
298
|
-
if (!filePath) {
|
|
299
|
-
return undefined;
|
|
300
|
-
}
|
|
301
|
-
return this.getSourceFileInsideComponent(filePath);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
async getFilePathByNode(node: Node) {
|
|
305
|
-
const def = await this.tsserver.getDefinition(this.getPath(node), this.getLocation(node));
|
|
306
|
-
|
|
307
|
-
const firstDef = head(def?.body);
|
|
308
|
-
return firstDef?.file;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
async definitionInfo(node: Node): Promise<protocol.DefinitionInfo | undefined> {
|
|
312
|
-
const location = this.getLocation(node);
|
|
313
|
-
const filePath = this.getPath(node);
|
|
314
|
-
|
|
315
|
-
const def = await this.tsserver.getDefinition(filePath, location);
|
|
316
|
-
|
|
317
|
-
const firstDef = head(def?.body);
|
|
318
|
-
|
|
319
|
-
return firstDef;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
* get a definition for a given node.
|
|
324
|
-
*/
|
|
325
|
-
async definition(definition: protocol.DefinitionInfo): Promise<Node | undefined> {
|
|
326
|
-
const startPosition = definition.start;
|
|
327
|
-
const sourceFile = this.getSourceFileInsideComponent(definition.file);
|
|
328
|
-
if (!sourceFile) {
|
|
329
|
-
// it might be an external reference, cant get the node
|
|
330
|
-
return undefined;
|
|
331
|
-
}
|
|
332
|
-
const pos = this.getPosition(sourceFile, startPosition.line, startPosition.offset);
|
|
333
|
-
const nodeAtPos = getTokenAtPosition(sourceFile, pos);
|
|
334
|
-
return nodeAtPos;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* visit a definition for node - e.g. return it's schema.
|
|
339
|
-
*/
|
|
340
|
-
async visitDefinition(node: Node): Promise<SchemaNode | undefined> {
|
|
341
|
-
const definitionInfo = await this.definitionInfo(node);
|
|
342
|
-
if (!definitionInfo) {
|
|
343
|
-
return undefined;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
const definition = await this.definition(definitionInfo);
|
|
347
|
-
if (!definition) {
|
|
348
|
-
return this.getTypeRefForExternalNode(node);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
return this.visit(definition.parent);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
async visit(node: Node): Promise<SchemaNode> {
|
|
355
|
-
if (node.kind === SyntaxKind.Identifier && node.parent.parent.kind !== SyntaxKind.SourceFile) {
|
|
356
|
-
return this.visit(node.parent);
|
|
357
|
-
}
|
|
358
|
-
return this.extractor.computeSchema(node, this);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
references() {}
|
|
362
|
-
|
|
363
|
-
isExported() {}
|
|
364
|
-
|
|
365
|
-
isFromComponent() {}
|
|
366
|
-
|
|
367
|
-
async getFileIdentifiers(exportDec: ExportDeclaration | ExportAssignment) {
|
|
368
|
-
const file = exportDec.getSourceFile().fileName;
|
|
369
|
-
const specifierPathStr =
|
|
370
|
-
(exportDec.kind === SyntaxKind.ExportDeclaration && exportDec.moduleSpecifier?.getText()) || '';
|
|
371
|
-
const specifierPath = specifierPathStr.substring(1, specifierPathStr.length - 1);
|
|
372
|
-
const absPath = resolve(file, '..', specifierPath);
|
|
373
|
-
const sourceFile = this.getSourceFileInsideComponent(absPath);
|
|
374
|
-
if (!sourceFile) return [];
|
|
375
|
-
return this.getIdentifiers(sourceFile);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
async getFileExports(exportDec: ExportDeclaration | ExportAssignment) {
|
|
379
|
-
const identifiers = await this.getFileIdentifiers(exportDec);
|
|
380
|
-
return identifiers.filter((identifier) => ExportIdentifier.isExportIdentifier(identifier));
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
async getFileInternals(exportDec: ExportDeclaration | ExportAssignment) {
|
|
384
|
-
const identifiers = await this.getFileIdentifiers(exportDec);
|
|
385
|
-
return identifiers.filter((identifier) => !ExportIdentifier.isExportIdentifier(identifier));
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
getIdentifiers(node: Node) {
|
|
389
|
-
return this.extractor.computeIdentifiers(node, this);
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* tsserver has two different calls: "definition" and "typeDefinition".
|
|
394
|
-
* normally, we need the "typeDefinition" to get the type data of a node.
|
|
395
|
-
* sometimes, it has no data, for example when the node is of type TypeReference, and then using "definition" is
|
|
396
|
-
* helpful. (couldn't find a rule when to use each one. e.g. "VariableDeclaration" sometimes has data only in
|
|
397
|
-
* "definition" but it's not clear when/why).
|
|
398
|
-
*/
|
|
399
|
-
async getDefinition(node: Node) {
|
|
400
|
-
const typeDefinition = await this.typeDefinition(node);
|
|
401
|
-
const headTypeDefinition = head(typeDefinition?.body);
|
|
402
|
-
if (headTypeDefinition) {
|
|
403
|
-
return headTypeDefinition;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
const definition = await this.tsserver.getDefinition(node.getSourceFile().fileName, this.getLocation(node));
|
|
407
|
-
const headDefinition = head(definition?.body);
|
|
408
|
-
|
|
409
|
-
return headDefinition;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
// when we can't figure out the component/package/type of this node, we'll use the typeStr as the type.
|
|
413
|
-
private async unknownExactType(node: Node, location: Location, typeStr = 'any', isTypeStrFromQuickInfo = true) {
|
|
414
|
-
if (isTypeStrFromQuickInfo) {
|
|
415
|
-
return new InferenceTypeSchema(location, typeStr || 'any');
|
|
416
|
-
}
|
|
417
|
-
const info = await this.getQuickInfo(node);
|
|
418
|
-
const type = parseTypeFromQuickInfo(info);
|
|
419
|
-
return new InferenceTypeSchema(location, type, typeStr);
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
// the reason for this check is to avoid infinite loop when calling `this.jump` with the same file+location
|
|
423
|
-
private isDefInSameLocation(node: Node, definition: protocol.FileSpanWithContext) {
|
|
424
|
-
if (definition.file !== node.getSourceFile().fileName) {
|
|
425
|
-
return false;
|
|
426
|
-
}
|
|
427
|
-
const loc = this.getLocation(node);
|
|
428
|
-
|
|
429
|
-
return loc.line === definition.start.line && loc.character === definition.start.offset;
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* resolve a type by a node and its identifier.
|
|
434
|
-
*/
|
|
435
|
-
async resolveType(
|
|
436
|
-
node: Node & { type?: TypeNode },
|
|
437
|
-
typeStr: string,
|
|
438
|
-
isTypeStrFromQuickInfo = true
|
|
439
|
-
): Promise<SchemaNode> {
|
|
440
|
-
const location = this.getLocation(node);
|
|
441
|
-
|
|
442
|
-
// check if internal ref with typeInfo
|
|
443
|
-
const internalRef = await this.getTypeRef(typeStr, this.getIdentifierKeyForNode(node), location);
|
|
444
|
-
|
|
445
|
-
if (internalRef) return internalRef;
|
|
446
|
-
|
|
447
|
-
// if a node has "type" prop, it has the type data of the node. this normally happens when the code has the type
|
|
448
|
-
// explicitly, e.g. `const str: string` vs implicitly `const str = 'some-string'`, which the node won't have "type"
|
|
449
|
-
if (node.type && ts.isTypeNode(node.type)) {
|
|
450
|
-
return this.computeSchema(node.type);
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
const definition = await this.getDefinition(node);
|
|
454
|
-
|
|
455
|
-
if (!definition) {
|
|
456
|
-
return this.unknownExactType(node, location, typeStr, isTypeStrFromQuickInfo);
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
if (this.isDefInSameLocation(node, definition)) {
|
|
460
|
-
return this.unknownExactType(node, location, typeStr, isTypeStrFromQuickInfo);
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
const definitionNode = await this.definition(definition);
|
|
464
|
-
|
|
465
|
-
if (!definitionNode) {
|
|
466
|
-
return this.unknownExactType(node, location, typeStr, isTypeStrFromQuickInfo);
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
const definitionNodeName = definitionNode?.getText();
|
|
470
|
-
|
|
471
|
-
// check if internal ref with definition info
|
|
472
|
-
const definitionInternalRef = await this.getTypeRef(
|
|
473
|
-
definitionNodeName,
|
|
474
|
-
this.getIdentifierKeyForNode(definitionNode),
|
|
475
|
-
location
|
|
476
|
-
);
|
|
477
|
-
|
|
478
|
-
if (definitionInternalRef) return definitionInternalRef;
|
|
479
|
-
|
|
480
|
-
const transformer = this.extractor.getTransformer(definitionNode, this);
|
|
481
|
-
|
|
482
|
-
if (transformer === undefined) {
|
|
483
|
-
const file = this.findFileInComponent(definition.file);
|
|
484
|
-
if (!file) return this.getTypeRefForExternalPath(typeStr, definition.file, location);
|
|
485
|
-
return this.unknownExactType(node, location, typeStr, isTypeStrFromQuickInfo);
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
const schemaNode = await this.visit(definitionNode);
|
|
489
|
-
|
|
490
|
-
if (!schemaNode) {
|
|
491
|
-
return this.unknownExactType(node, location, typeStr, isTypeStrFromQuickInfo);
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
const apiTransformer = this.extractor.getAPITransformer(schemaNode);
|
|
495
|
-
return apiTransformer ? apiTransformer.transform(schemaNode, this) : schemaNode;
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
private getCompIdByPkgName(pkgName: string): ComponentID | undefined {
|
|
499
|
-
return this.componentDeps.find((dep) => dep.packageName === pkgName)?.componentId;
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
async getTypeRef(typeStr: string, filePath: string, location: Location): Promise<TypeRefSchema | undefined> {
|
|
503
|
-
const nodeIdentifierKey = this.getIdentifierKey(filePath);
|
|
504
|
-
const mainFileIdentifierKey = this.mainFileIdentifierKey;
|
|
505
|
-
|
|
506
|
-
const nodeIdentifierList = this.identifiers.get(nodeIdentifierKey);
|
|
507
|
-
const mainIdentifierList = this.identifiers.get(mainFileIdentifierKey);
|
|
508
|
-
|
|
509
|
-
const nodeIdentifier = new Identifier(typeStr, nodeIdentifierKey);
|
|
510
|
-
const mainIdentifier = new Identifier(typeStr, mainFileIdentifierKey);
|
|
511
|
-
|
|
512
|
-
const parsedNodeIdentifier = nodeIdentifierList?.find(nodeIdentifier);
|
|
513
|
-
const parsedMainIdentifier = mainIdentifierList?.find(mainIdentifier);
|
|
514
|
-
const isExportedFromMain = parsedMainIdentifier && ExportIdentifier.isExportIdentifier(parsedMainIdentifier);
|
|
515
|
-
|
|
516
|
-
if (!parsedNodeIdentifier) return undefined;
|
|
517
|
-
|
|
518
|
-
const internalRef = !isExportedFromMain;
|
|
519
|
-
|
|
520
|
-
if (internalRef) {
|
|
521
|
-
this.setInternalIdentifiers(filePath, new IdentifierList([parsedNodeIdentifier]));
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
return this.resolveTypeRef(parsedNodeIdentifier, location, isExportedFromMain);
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
async resolveTypeRef(
|
|
528
|
-
identifier: Identifier,
|
|
529
|
-
location: Location,
|
|
530
|
-
isExportedFromMain?: boolean
|
|
531
|
-
): Promise<TypeRefSchema> {
|
|
532
|
-
const sourceFilePath = identifier.sourceFilePath;
|
|
533
|
-
|
|
534
|
-
if (!sourceFilePath || (isExportedFromMain && isRelativeImport(sourceFilePath))) {
|
|
535
|
-
return new TypeRefSchema(
|
|
536
|
-
location,
|
|
537
|
-
identifier.id,
|
|
538
|
-
undefined,
|
|
539
|
-
undefined,
|
|
540
|
-
!isExportedFromMain ? this.getPathRelativeToComponent(identifier.filePath) : undefined
|
|
541
|
-
);
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
if (!isRelativeImport(sourceFilePath)) {
|
|
545
|
-
const pkgName = this.parsePackageNameFromPath(sourceFilePath);
|
|
546
|
-
const compIdByPkg = this.getCompIdByPkgName(pkgName);
|
|
547
|
-
|
|
548
|
-
const compIdByPath = await this.extractor.getComponentIDByPath(sourceFilePath);
|
|
549
|
-
|
|
550
|
-
if (compIdByPath) {
|
|
551
|
-
return new TypeRefSchema(location, identifier.id, compIdByPath);
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
if (compIdByPkg) {
|
|
555
|
-
return new TypeRefSchema(location, identifier.id, compIdByPkg);
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
// package without comp id
|
|
559
|
-
return new TypeRefSchema(location, identifier.id, undefined, pkgName);
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
const relativeDir = identifier.filePath.substring(0, identifier.filePath.lastIndexOf('/'));
|
|
563
|
-
const absFilePath = resolve(this.componentRootPath, relativeDir, sourceFilePath);
|
|
564
|
-
|
|
565
|
-
const compFilePath = this.findFileInComponent(absFilePath);
|
|
566
|
-
if (!compFilePath) {
|
|
567
|
-
// @todo handle this better
|
|
568
|
-
throw new Error(
|
|
569
|
-
`cannot find file in component \n source file path ${sourceFilePath}\n
|
|
570
|
-
identifier file path ${identifier.filePath}\nrelative dir ${relativeDir}\n
|
|
571
|
-
absFilePath ${absFilePath}`
|
|
572
|
-
);
|
|
573
|
-
return new TypeRefSchema(location, identifier.id);
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
const idKey = this.getIdentifierKey(compFilePath?.path);
|
|
577
|
-
|
|
578
|
-
// if re exported from a file, recurse until definition
|
|
579
|
-
const exportedIdentifier = (this.identifiers.get(idKey)?.identifiers || []).find((i) => i.id === identifier.id);
|
|
580
|
-
|
|
581
|
-
if (exportedIdentifier) {
|
|
582
|
-
return this.resolveTypeRef(exportedIdentifier, location, isExportedFromMain);
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
return new TypeRefSchema(location, identifier.id);
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
async getTypeRefForExternalNode(node: Node): Promise<TypeRefSchema> {
|
|
589
|
-
const info = await this.getQuickInfo(node);
|
|
590
|
-
const typeStr = parseTypeFromQuickInfo(info);
|
|
591
|
-
const location = this.getLocation(node);
|
|
592
|
-
const filePath = this.getPath(node);
|
|
593
|
-
return this.getTypeRefForExternalPath(typeStr, filePath, location);
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
async getTypeRefForExternalPath(typeStr: string, filePath: string, location: Location): Promise<TypeRefSchema> {
|
|
597
|
-
const compIdByPath = await this.extractor.getComponentIDByPath(filePath);
|
|
598
|
-
if (compIdByPath) {
|
|
599
|
-
return new TypeRefSchema(location, typeStr, compIdByPath);
|
|
600
|
-
}
|
|
601
|
-
const pkgName = this.parsePackageNameFromPath(filePath);
|
|
602
|
-
const compIdByPkg = this.getCompIdByPkgName(pkgName);
|
|
603
|
-
if (compIdByPkg) {
|
|
604
|
-
return new TypeRefSchema(location, typeStr, compIdByPkg);
|
|
605
|
-
}
|
|
606
|
-
return new TypeRefSchema(location, typeStr, undefined, pkgName);
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
async jsDocToDocSchema(node: Node): Promise<DocSchema | undefined> {
|
|
610
|
-
if (!canHaveJsDoc(node)) {
|
|
611
|
-
return undefined;
|
|
612
|
-
}
|
|
613
|
-
const jsDocs = getJsDoc(node);
|
|
614
|
-
if (!jsDocs.length) {
|
|
615
|
-
return undefined;
|
|
616
|
-
}
|
|
617
|
-
// not sure how common it is to have multiple JSDocs. never seen it before.
|
|
618
|
-
// regardless, in typescript implementation of methods like `getJSDocDeprecatedTag()`, they use the first one. (`getFirstJSDocTag()`)
|
|
619
|
-
const jsDoc = jsDocs[0];
|
|
620
|
-
const location = this.getLocation(jsDoc);
|
|
621
|
-
const comment = getTextOfJSDocComment(jsDoc.comment);
|
|
622
|
-
const tags = jsDoc.tags ? await pMapSeries(jsDoc.tags, (tag) => tagParser(tag, this, this.formatter)) : undefined;
|
|
623
|
-
return new DocSchema(location, jsDoc.getText(), comment, tags);
|
|
624
|
-
}
|
|
625
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { PluginDefinition } from '@teambit/aspect-loader';
|
|
2
|
-
import { SchemaTransformerSlot } from './typescript.main.runtime';
|
|
3
|
-
|
|
4
|
-
export class SchemaTransformerPlugin implements PluginDefinition {
|
|
5
|
-
constructor(private schemaTransformerSlot: SchemaTransformerSlot) {}
|
|
6
|
-
|
|
7
|
-
pattern = '*.schema-extractor.*';
|
|
8
|
-
|
|
9
|
-
runtimes = ['main'];
|
|
10
|
-
|
|
11
|
-
register(object: any) {
|
|
12
|
-
return this.schemaTransformerSlot.register([object]);
|
|
13
|
-
}
|
|
14
|
-
}
|
package/schema-transformer.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Node } from 'typescript';
|
|
2
|
-
import { SchemaNode } from '@teambit/semantics.entities.semantic-schema';
|
|
3
|
-
import { SchemaExtractorContext } from './schema-extractor-context';
|
|
4
|
-
import { Identifier } from './identifier';
|
|
5
|
-
|
|
6
|
-
export type SchemaTransformer = {
|
|
7
|
-
/**
|
|
8
|
-
* determine whether to apply schema on given node.
|
|
9
|
-
*/
|
|
10
|
-
predicate(node: Node): boolean;
|
|
11
|
-
|
|
12
|
-
getIdentifiers(node: Node, context: SchemaExtractorContext): Promise<Identifier[]>;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* transform the node into JSONSchema.
|
|
16
|
-
*/
|
|
17
|
-
transform(node: Node, context: SchemaExtractorContext): Promise<SchemaNode>;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type SchemaNodeTransformer = {
|
|
21
|
-
predicate(node: SchemaNode): boolean;
|
|
22
|
-
transform(node: SchemaNode, context: SchemaExtractorContext): Promise<SchemaNode>;
|
|
23
|
-
};
|