@@ -2555,6 +2555,22 @@ namespace ts {
2555
2555
return initializer || decl;
2556
2556
}
2557
2557
2558
+ /**
2559
+ * Get the real symbol of a declaration with an expando initializer.
2560
+ *
2561
+ * Normally, declarations have an associated symbol, but when a declaration has an expando
2562
+ * initializer, the expando's symbol is the one that has all the members merged into it.
2563
+ */
2564
+ function getExpandoSymbol(symbol: Symbol): Symbol | undefined {
2565
+ const decl = symbol.valueDeclaration;
2566
+ if (!decl || !isInJSFile(decl) || symbol.flags & SymbolFlags.TypeAlias) {
2567
+ return undefined;
2568
+ }
2569
+ const init = isVariableDeclaration(decl) ? getDeclaredExpandoInitializer(decl) : getAssignedExpandoInitializer(decl);
2570
+ return init && getSymbolOfNode(init) || undefined;
2571
+ }
2572
+
2573
+
2558
2574
function resolveExternalModuleName(location: Node, moduleReferenceExpression: Expression, ignoreErrors?: boolean): Symbol | undefined {
2559
2575
return resolveExternalModuleNameWorker(location, moduleReferenceExpression, ignoreErrors ? undefined : Diagnostics.Cannot_find_module_0);
2560
2576
}
@@ -9190,10 +9206,13 @@ namespace ts {
9190
9206
if (symbol === unknownSymbol) {
9191
9207
return errorType;
9192
9208
}
9209
+ symbol = getExpandoSymbol(symbol) || symbol;
9193
9210
9194
- const type = getTypeReferenceTypeWorker(node, symbol, typeArguments);
9195
- if (type) {
9196
- return type;
9211
+ if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface)) {
9212
+ return getTypeFromClassOrInterfaceReference(node, symbol, typeArguments);
9213
+ }
9214
+ if (symbol.flags & SymbolFlags.TypeAlias) {
9215
+ return getTypeFromTypeAliasReference(node, symbol, typeArguments);
9197
9216
}
9198
9217
9199
9218
// Get type from reference to named type that cannot be generic (enum or type parameter)
@@ -9204,62 +9223,34 @@ namespace ts {
9204
9223
errorType;
9205
9224
}
9206
9225
9207
- if (!(symbol.flags & SymbolFlags.Value && isJSDocTypeReference(node))) {
9208
- return errorType;
9209
- }
9210
-
9211
- const jsdocType = getJSDocTypeReference(node, symbol, typeArguments);
9212
- if (jsdocType) {
9213
- return jsdocType;
9226
+ if (symbol.flags & SymbolFlags.Value && isJSDocTypeReference(node)) {
9227
+ const jsdocType = getTypeFromJSAlias(node, symbol);
9228
+ if (jsdocType) {
9229
+ return jsdocType;
9230
+ }
9231
+ else {
9232
+ // Resolve the type reference as a Type for the purpose of reporting errors.
9233
+ resolveTypeReferenceName(getTypeReferenceName(node), SymbolFlags.Type);
9234
+ return getTypeOfSymbol(symbol);
9235
+ }
9214
9236
}
9215
9237
9216
- // Resolve the type reference as a Type for the purpose of reporting errors.
9217
- resolveTypeReferenceName(getTypeReferenceName(node), SymbolFlags.Type);
9218
- return getTypeOfSymbol(symbol);
9238
+ return errorType;
9219
9239
}
9220
9240
9221
9241
/**
9222
- * A jsdoc TypeReference may have resolved to a value (as opposed to a type). If
9223
- * the symbol is a constructor function, return the inferred class type; otherwise,
9224
- * the type of this reference is just the type of the value we resolved to .
9242
+ * A JSdoc TypeReference may be to a value imported from commonjs.
9243
+ * These should really be aliases, but this special-case code fakes alias resolution
9244
+ * by producing a type from a value.
9225
9245
*/
9226
- function getJSDocTypeReference(node: NodeWithTypeArguments, symbol: Symbol, typeArguments: Type[] | undefined): Type | undefined {
9227
- // In the case of an assignment of a function expression (binary expressions, variable declarations, etc.), we will get the
9228
- // correct instance type for the symbol on the LHS by finding the type for RHS. For example if we want to get the type of the symbol `foo`:
9229
- // var foo = function() {}
9230
- // We will find the static type of the assigned anonymous function.
9231
- const staticType = getTypeOfSymbol(symbol);
9232
- const instanceType =
9233
- staticType.symbol &&
9234
- staticType.symbol !== symbol && // Make sure this is an assignment like expression by checking that symbol -> type -> symbol doesn't roundtrips.
9235
- getTypeReferenceTypeWorker(node, staticType.symbol, typeArguments); // Get the instance type of the RHS symbol.
9236
- if (instanceType) {
9237
- return getSymbolLinks(symbol).resolvedJSDocType = instanceType;
9238
- }
9239
- }
9240
-
9241
- function getTypeReferenceTypeWorker(node: NodeWithTypeArguments, symbol: Symbol, typeArguments: Type[] | undefined): Type | undefined {
9242
- if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface)) {
9243
- if (symbol.valueDeclaration && symbol.valueDeclaration.parent && isBinaryExpression(symbol.valueDeclaration.parent)) {
9244
- const jsdocType = getJSDocTypeReference(node, symbol, typeArguments);
9245
- if (jsdocType) {
9246
- return jsdocType;
9247
- }
9248
- }
9249
- return getTypeFromClassOrInterfaceReference(node, symbol, typeArguments);
9250
- }
9251
-
9252
- if (symbol.flags & SymbolFlags.TypeAlias) {
9253
- return getTypeFromTypeAliasReference(node, symbol, typeArguments);
9254
- }
9255
-
9256
- if (symbol.flags & SymbolFlags.Function &&
9257
- isJSDocTypeReference(node) &&
9258
- isJSConstructor(symbol.valueDeclaration)) {
9259
- const resolved = resolveStructuredTypeMembers(<ObjectType>getTypeOfSymbol(symbol));
9260
- if (resolved.callSignatures.length === 1) {
9261
- return getReturnTypeOfSignature(resolved.callSignatures[0]);
9262
- }
9246
+ function getTypeFromJSAlias(node: NodeWithTypeArguments, symbol: Symbol): Type | undefined {
9247
+ const valueType = getTypeOfSymbol(symbol);
9248
+ const typeType =
9249
+ valueType.symbol &&
9250
+ valueType.symbol !== symbol && // Make sure this is a commonjs export by checking that symbol -> type -> symbol doesn't roundtrip.
9251
+ getTypeReferenceType(node, valueType.symbol);
9252
+ if (typeType) {
9253
+ return getSymbolLinks(symbol).resolvedJSDocType = typeType;
9263
9254
}
9264
9255
}
9265
9256
0 commit comments