Support the case with variable name as @typedef type name

This commit is contained in:
Zhengbo Li
2016-04-06 20:22:23 -07:00
parent 23bca4eda6
commit 9a8f6395e4
2 changed files with 22 additions and 13 deletions
+5 -8
View File
@@ -3403,7 +3403,7 @@ namespace ts {
return <InterfaceType>links.declaredType;
}
function getDeclaredTypeOfTypeAlias(symbol: Symbol, node?: Node): Type {
function getDeclaredTypeOfTypeAlias(symbol: Symbol): Type {
const links = getSymbolLinks(symbol);
if (!links.declaredType) {
// Note that we use the links object as the target here because the symbol object is used as the unique
@@ -3412,10 +3412,7 @@ namespace ts {
return unknownType;
}
let declaration: JSDocTypedefTag | TypeAliasDeclaration;
if (node && (node.flags & NodeFlags.JavaScriptFile)) {
declaration = <JSDocTypedefTag>getDeclarationOfKind(symbol, SyntaxKind.JSDocTypedefTag);
}
let declaration: JSDocTypedefTag | TypeAliasDeclaration = <JSDocTypedefTag>getDeclarationOfKind(symbol, SyntaxKind.JSDocTypedefTag);
if (!declaration) {
declaration = <TypeAliasDeclaration>getDeclarationOfKind(symbol, SyntaxKind.TypeAliasDeclaration);
}
@@ -3469,13 +3466,13 @@ namespace ts {
return links.declaredType;
}
function getDeclaredTypeOfSymbol(symbol: Symbol, node?: Node): Type {
function getDeclaredTypeOfSymbol(symbol: Symbol): Type {
Debug.assert((symbol.flags & SymbolFlags.Instantiated) === 0);
if (symbol.flags & (SymbolFlags.Class | SymbolFlags.Interface)) {
return getDeclaredTypeOfClassOrInterface(symbol);
}
if (symbol.flags & SymbolFlags.TypeAlias) {
return getDeclaredTypeOfTypeAlias(symbol, node);
return getDeclaredTypeOfTypeAlias(symbol);
}
if (symbol.flags & SymbolFlags.Enum) {
return getDeclaredTypeOfEnum(symbol);
@@ -4571,7 +4568,7 @@ namespace ts {
// references to the type parameters of the alias. We replace those with the actual type arguments by instantiating the
// declared type. Instantiations are cached using the type identities of the type arguments as the key.
function getTypeFromTypeAliasReference(node: TypeReferenceNode | ExpressionWithTypeArguments | JSDocTypeReference, symbol: Symbol): Type {
const type = getDeclaredTypeOfSymbol(symbol, node);
const type = getDeclaredTypeOfSymbol(symbol);
const links = getSymbolLinks(symbol);
const typeParameters = links.typeParameters;
if (typeParameters) {
+17 -5
View File
@@ -5977,7 +5977,7 @@ namespace ts {
const saveParseDiagnosticsLength = parseDiagnostics.length;
const saveParseErrorBeforeNextFinishedNode = parseErrorBeforeNextFinishedNode;
const comment = parseJSDocCommentWorker(start, length);
const comment = parseJSDocCommentWorker(start, length, parent);
if (comment) {
comment.parent = parent;
}
@@ -5989,7 +5989,7 @@ namespace ts {
return comment;
}
export function parseJSDocCommentWorker(start: number, length: number): JSDocComment {
export function parseJSDocCommentWorker(start: number, length: number, parentNode?: Node): JSDocComment {
const content = sourceText;
start = start || 0;
const end = length === undefined ? content.length : start + length;
@@ -6334,10 +6334,22 @@ namespace ts {
function handleTypedefTag(atToken: Node, tagName: Identifier): JSDocTypedefTag {
const typeExpression = tryParseTypeExpression();
skipWhitespace();
const name = parseJSDocIdentifier();
let name = parseJSDocIdentifier();
if (!name) {
parseErrorAtPosition(scanner.getStartPos(), 0, Diagnostics.Identifier_expected);
return undefined;
let foundNameFromParentNode = false;
if (parentNode && parentNode.kind === SyntaxKind.VariableStatement) {
if ((<VariableStatement>parentNode).declarationList.declarations.length > 0) {
const nameFromParentNode = (<VariableStatement>parentNode).declarationList.declarations[0].name;
if (nameFromParentNode.kind === SyntaxKind.Identifier) {
foundNameFromParentNode = true;
name = <Identifier>nameFromParentNode;
}
}
}
if (!foundNameFromParentNode) {
parseErrorAtPosition(scanner.getStartPos(), 0, Diagnostics.Identifier_expected);
return undefined;
}
}
const result = <JSDocTypedefTag>createNode(SyntaxKind.JSDocTypedefTag, atToken.pos);