forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeclarationReferenceGenerator.ts
More file actions
279 lines (255 loc) · 10.4 KB
/
DeclarationReferenceGenerator.ts
File metadata and controls
279 lines (255 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
// tslint:disable:no-bitwise
import * as ts from 'typescript';
import {
DeclarationReference,
ModuleSource,
GlobalSource,
Navigation,
Meaning
} from '@microsoft/tsdoc/lib/beta/DeclarationReference';
import { PackageJsonLookup, INodePackageJson, InternalError } from '@microsoft/node-core-library';
import { TypeScriptHelpers } from '../analyzer/TypeScriptHelpers';
import { TypeScriptInternals } from '../analyzer/TypeScriptInternals';
export class DeclarationReferenceGenerator {
public static readonly unknownReference: string = '?';
private _packageJsonLookup: PackageJsonLookup;
private _workingPackageName: string;
private _program: ts.Program;
private _typeChecker: ts.TypeChecker;
constructor(packageJsonLookup: PackageJsonLookup, workingPackageName: string, program: ts.Program,
typeChecker: ts.TypeChecker) {
this._packageJsonLookup = packageJsonLookup;
this._workingPackageName = workingPackageName;
this._program = program;
this._typeChecker = typeChecker;
}
/**
* Gets the UID for a TypeScript Identifier that references a type.
*/
public getDeclarationReferenceForIdentifier(node: ts.Identifier): DeclarationReference | undefined {
const symbol: ts.Symbol | undefined = this._typeChecker.getSymbolAtLocation(node);
if (symbol !== undefined) {
const isExpression: boolean = isInExpressionContext(node);
return this.getDeclarationReferenceForSymbol(symbol, isExpression ? ts.SymbolFlags.Value : ts.SymbolFlags.Type)
|| this.getDeclarationReferenceForSymbol(symbol, isExpression ? ts.SymbolFlags.Type : ts.SymbolFlags.Value)
|| this.getDeclarationReferenceForSymbol(symbol, ts.SymbolFlags.Namespace);
}
}
/**
* Gets the DeclarationReference for a TypeScript Symbol for a given meaning.
*/
public getDeclarationReferenceForSymbol(symbol: ts.Symbol, meaning: ts.SymbolFlags
): DeclarationReference | undefined {
return this._symbolToDeclarationReference(symbol, meaning, /*includeModuleSymbols*/ false);
}
private _symbolToDeclarationReference(symbol: ts.Symbol, meaning: ts.SymbolFlags, includeModuleSymbols: boolean
): DeclarationReference | undefined {
if (symbol.flags & ts.SymbolFlags.ExportValue) {
symbol = this._typeChecker.getExportSymbolOfSymbol(symbol);
}
if (symbol.flags & ts.SymbolFlags.Alias) {
symbol = this._typeChecker.getAliasedSymbol(symbol);
}
if (isExternalModuleSymbol(symbol)) {
if (!includeModuleSymbols) {
return undefined;
}
const sourceFile: ts.SourceFile | undefined =
symbol.declarations
&& symbol.declarations[0]
&& symbol.declarations[0].getSourceFile();
return new DeclarationReference(this._sourceFileToModuleSource(sourceFile));
}
// Do not generate a declaration reference for a type parameter.
if (symbol.flags & ts.SymbolFlags.TypeParameter) {
return undefined;
}
const parent: ts.Symbol | undefined = TypeScriptInternals.getSymbolParent(symbol);
let parentRef: DeclarationReference | undefined;
if (parent) {
parentRef = this._symbolToDeclarationReference(parent, ts.SymbolFlags.Namespace, /*includeModuleSymbols*/ true);
} else {
// this may be a local symbol in a module...
const sourceFile: ts.SourceFile | undefined =
symbol.declarations
&& symbol.declarations[0]
&& symbol.declarations[0].getSourceFile();
if (ts.isExternalModule(sourceFile)) {
parentRef = new DeclarationReference(this._sourceFileToModuleSource(sourceFile));
} else {
parentRef = new DeclarationReference(GlobalSource.instance);
}
}
if (parentRef === undefined) {
return undefined;
}
let localName: string = symbol.name;
if (symbol.escapedName === ts.InternalSymbolName.Constructor) {
localName = 'constructor';
} else {
const wellKnownName: string | undefined = TypeScriptHelpers.tryDecodeWellKnownSymbolName(symbol.escapedName);
if (wellKnownName) {
// TypeScript binds well-known ECMAScript symbols like 'Symbol.iterator' as '__@iterator'.
// This converts a string like '__@iterator' into the property name '[Symbol.iterator]'.
localName = wellKnownName;
} else if (TypeScriptHelpers.isUniqueSymbolName(symbol.escapedName)) {
for (const decl of symbol.declarations || []) {
const declName: ts.DeclarationName | undefined = ts.getNameOfDeclaration(decl);
if (declName && ts.isComputedPropertyName(declName)) {
const lateName: string | undefined = TypeScriptHelpers.tryGetLateBoundName(declName);
if (lateName !== undefined) {
localName = lateName;
break;
}
}
}
}
}
let navigation: Navigation | 'global' = getNavigationToSymbol(symbol);
if (navigation === 'global') {
if (parentRef.source !== GlobalSource.instance) {
parentRef = new DeclarationReference(GlobalSource.instance);
}
navigation = Navigation.Exports;
}
return parentRef
.addNavigationStep(navigation, localName)
.withMeaning(getMeaningOfSymbol(symbol, meaning));
}
private _getPackageName(sourceFile: ts.SourceFile): string {
if (this._program.isSourceFileFromExternalLibrary(sourceFile)) {
const packageJson: INodePackageJson | undefined = this._packageJsonLookup
.tryLoadNodePackageJsonFor(sourceFile.fileName);
if (packageJson && packageJson.name) {
return packageJson.name;
}
return DeclarationReferenceGenerator.unknownReference;
}
return this._workingPackageName;
}
private _sourceFileToModuleSource(sourceFile: ts.SourceFile | undefined): GlobalSource | ModuleSource {
if (sourceFile && ts.isExternalModule(sourceFile)) {
return new ModuleSource(this._getPackageName(sourceFile));
}
return GlobalSource.instance;
}
}
function isExternalModuleSymbol(symbol: ts.Symbol): boolean {
return !!(symbol.flags & ts.SymbolFlags.ValueModule)
&& symbol.valueDeclaration !== undefined
&& ts.isSourceFile(symbol.valueDeclaration);
}
function isSameSymbol(left: ts.Symbol | undefined, right: ts.Symbol): boolean {
return left === right
|| !!(left && left.valueDeclaration && right.valueDeclaration && left.valueDeclaration === right.valueDeclaration);
}
function getNavigationToSymbol(symbol: ts.Symbol): Navigation | 'global' {
const parent: ts.Symbol | undefined = TypeScriptInternals.getSymbolParent(symbol);
// First, try to determine navigation to symbol via its parent.
if (parent) {
if (parent.exports && isSameSymbol(parent.exports.get(symbol.escapedName), symbol)) {
return Navigation.Exports;
}
if (parent.members && isSameSymbol(parent.members.get(symbol.escapedName), symbol)) {
return Navigation.Members;
}
if (parent.globalExports && isSameSymbol(parent.globalExports.get(symbol.escapedName), symbol)) {
return 'global';
}
}
// Next, try determining navigation to symbol by its node
if (symbol.valueDeclaration) {
const declaration: ts.Declaration = ts.isBindingElement(symbol.valueDeclaration)
? ts.walkUpBindingElementsAndPatterns(symbol.valueDeclaration)
: symbol.valueDeclaration;
if (ts.isClassElement(declaration) && ts.isClassLike(declaration.parent)) {
// class members are an "export" if they have the static modifier.
return ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Static
? Navigation.Exports
: Navigation.Members;
}
if (ts.isTypeElement(declaration) || ts.isObjectLiteralElement(declaration)) {
// type and object literal element members are just members
return Navigation.Members;
}
if (ts.isEnumMember(declaration)) {
// enum members are exports
return Navigation.Exports;
}
if (ts.isExportSpecifier(declaration)
|| ts.isExportAssignment(declaration)
|| ts.isExportSpecifier(declaration)
|| ts.isExportDeclaration(declaration)
|| ts.isNamedExports(declaration)
) {
return Navigation.Exports;
}
// declarations are exports if they have an `export` modifier.
if (ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export) {
return Navigation.Exports;
}
if (ts.isSourceFile(declaration.parent) && !ts.isExternalModule(declaration.parent)) {
// declarations in a source file are global if the source file is not a module.
return 'global';
}
}
// all other declarations are locals
return Navigation.Locals;
}
function getMeaningOfSymbol(symbol: ts.Symbol, meaning: ts.SymbolFlags): Meaning | undefined {
if (symbol.flags & meaning & ts.SymbolFlags.Class) {
return Meaning.Class;
}
if (symbol.flags & meaning & ts.SymbolFlags.Enum) {
return Meaning.Enum;
}
if (symbol.flags & meaning & ts.SymbolFlags.Interface) {
return Meaning.Interface;
}
if (symbol.flags & meaning & ts.SymbolFlags.TypeAlias) {
return Meaning.TypeAlias;
}
if (symbol.flags & meaning & ts.SymbolFlags.Function) {
return Meaning.Function;
}
if (symbol.flags & meaning & ts.SymbolFlags.Variable) {
return Meaning.Variable;
}
if (symbol.flags & meaning & ts.SymbolFlags.Module) {
return Meaning.Namespace;
}
if (symbol.flags & meaning & ts.SymbolFlags.ClassMember) {
return Meaning.Member;
}
if (symbol.flags & meaning & ts.SymbolFlags.Constructor) {
return Meaning.Constructor;
}
if (symbol.flags & meaning & ts.SymbolFlags.EnumMember) {
return Meaning.Member;
}
if (symbol.flags & meaning & ts.SymbolFlags.Signature) {
if (symbol.escapedName === ts.InternalSymbolName.Call) {
return Meaning.CallSignature;
}
if (symbol.escapedName === ts.InternalSymbolName.New) {
return Meaning.ConstructSignature;
}
if (symbol.escapedName === ts.InternalSymbolName.Index) {
return Meaning.IndexSignature;
}
}
if (symbol.flags & meaning & ts.SymbolFlags.TypeParameter) {
// This should have already been handled in `getDeclarationReferenceOfSymbol`.
throw new InternalError('Not supported.');
}
return undefined;
}
function isInExpressionContext(node: ts.Node): boolean {
switch (node.parent.kind) {
case ts.SyntaxKind.TypeQuery: return true;
case ts.SyntaxKind.QualifiedName: return isInExpressionContext(node.parent);
default: return false;
}
}