Skip to content

Commit c55a043

Browse files
committed
Address PR comments from Andy
I'll take a look at Wesley's next and see if those require any changes.
1 parent 5996139 commit c55a043

File tree

7 files changed

+64
-35
lines changed

7 files changed

+64
-35
lines changed

src/compiler/binder.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,10 +2170,11 @@ namespace ts {
21702170
}
21712171
// falls through
21722172
case SyntaxKind.JSDocPropertyTag:
2173-
return declareSymbolAndAddToSymbolTable(node as JSDocPropertyLikeTag,
2174-
(node as JSDocPropertyLikeTag).isBracketed || ((node as JSDocPropertyLikeTag).typeExpression && (node as JSDocPropertyLikeTag).typeExpression.type.kind === SyntaxKind.JSDocOptionalType) ?
2175-
SymbolFlags.Property | SymbolFlags.Optional : SymbolFlags.Property,
2176-
SymbolFlags.PropertyExcludes);
2173+
const propTag = node as JSDocPropertyLikeTag;
2174+
const flags = propTag.isBracketed || propTag.typeExpression.type.kind === SyntaxKind.JSDocOptionalType ?
2175+
SymbolFlags.Property | SymbolFlags.Optional :
2176+
SymbolFlags.Property;
2177+
return declareSymbolAndAddToSymbolTable(propTag, flags, SymbolFlags.PropertyExcludes);
21772178
case SyntaxKind.JSDocTypedefTag: {
21782179
const { fullName } = node as JSDocTypedefTag;
21792180
if (!fullName || fullName.kind === SyntaxKind.Identifier) {

src/compiler/checker.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5074,8 +5074,8 @@ namespace ts {
50745074
return unknownType;
50755075
}
50765076

5077-
const declaration = getDeclarationOfKind<JSDocTypedefTag>(symbol, SyntaxKind.JSDocTypedefTag) ||
5078-
getDeclarationOfKind<TypeAliasDeclaration>(symbol, SyntaxKind.TypeAliasDeclaration);
5077+
const declaration = <JSDocTypedefTag | TypeAliasDeclaration>findDeclaration(
5078+
symbol, d => d.kind === SyntaxKind.JSDocTypedefTag || d.kind === SyntaxKind.TypeAliasDeclaration);
50795079
let type = getTypeFromTypeNode(declaration.kind === SyntaxKind.JSDocTypedefTag ? declaration.typeExpression : declaration.type);
50805080

50815081
if (popTypeResolution()) {
@@ -22610,8 +22610,7 @@ namespace ts {
2261022610
}
2261122611

2261222612
if (entityName.parent!.kind === SyntaxKind.JSDocParameterTag) {
22613-
const parameter = getParameterFromJSDoc(entityName.parent as JSDocParameterTag);
22614-
return parameter && parameter.symbol;
22613+
return getParameterSymbolFromJSDoc(entityName.parent as JSDocParameterTag);
2261522614
}
2261622615

2261722616
if (entityName.parent.kind === SyntaxKind.TypeParameter && entityName.parent.parent.kind === SyntaxKind.JSDocTemplateTag) {

src/compiler/parser.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ namespace ts {
411411
return visitNodes(cbNode, cbNodes, (<JSDoc>node).tags);
412412
case SyntaxKind.JSDocParameterTag:
413413
case SyntaxKind.JSDocPropertyTag:
414-
if ((node as JSDocPropertyLikeTag).isParameterNameFirst) {
414+
if ((node as JSDocPropertyLikeTag).isNameFirst) {
415415
return visitNode(cbNode, (<JSDocPropertyLikeTag>node).fullName) ||
416416
visitNode(cbNode, (<JSDocPropertyLikeTag>node).typeExpression);
417417
}
@@ -431,12 +431,10 @@ namespace ts {
431431
if ((node as JSDocTypedefTag).typeExpression &&
432432
(node as JSDocTypedefTag).typeExpression.kind === SyntaxKind.JSDocTypeExpression) {
433433
return visitNode(cbNode, (<JSDocTypedefTag>node).typeExpression) ||
434-
visitNode(cbNode, (<JSDocTypedefTag>node).fullName) ||
435-
visitNode(cbNode, (<JSDocTypedefTag>node).name);
434+
visitNode(cbNode, (<JSDocTypedefTag>node).fullName);
436435
}
437436
else {
438437
return visitNode(cbNode, (<JSDocTypedefTag>node).fullName) ||
439-
visitNode(cbNode, (<JSDocTypedefTag>node).name) ||
440438
visitNode(cbNode, (<JSDocTypedefTag>node).typeExpression);
441439
}
442440
case SyntaxKind.JSDocTypeLiteral:
@@ -6520,7 +6518,27 @@ namespace ts {
65206518
const result: JSDocPropertyLikeTag = target ?
65216519
<JSDocParameterTag>createNode(SyntaxKind.JSDocParameterTag, atToken.pos) :
65226520
<JSDocPropertyTag>createNode(SyntaxKind.JSDocPropertyTag, atToken.pos);
6521+
const nestedTypeLiteral = parseNestedTypeLiteral(typeExpression, fullName);
6522+
if (nestedTypeLiteral) {
6523+
typeExpression = nestedTypeLiteral;
6524+
}
6525+
result.atToken = atToken;
6526+
result.tagName = tagName;
6527+
result.typeExpression = typeExpression;
6528+
if (typeExpression) {
6529+
result.type = typeExpression.type;
6530+
}
6531+
result.fullName = postName || preName;
6532+
result.name = ts.isIdentifier(result.fullName) ? result.fullName : result.fullName.right;
6533+
result.isNameFirst = !!nestedTypeLiteral || (postName ? false : !!preName);
6534+
result.isBracketed = isBracketed;
6535+
return finishNode(result);
6536+
6537+
}
6538+
6539+
function parseNestedTypeLiteral(typeExpression: JSDocTypeExpression, fullName: EntityName) {
65236540
if (typeExpression && isObjectOrObjectArrayTypeReference(typeExpression.type)) {
6541+
const typeLiteralExpression = <JSDocTypeExpression>createNode(SyntaxKind.JSDocTypeExpression, scanner.getTokenPos());
65246542
let child: JSDocPropertyLikeTag | false;
65256543
let jsdocTypeLiteral: JSDocTypeLiteral;
65266544
const start = scanner.getStartPos();
@@ -6535,21 +6553,10 @@ namespace ts {
65356553
if (typeExpression.type.kind === SyntaxKind.ArrayType) {
65366554
jsdocTypeLiteral.isArrayType = true;
65376555
}
6538-
typeExpression.type = finishNode(jsdocTypeLiteral);
6556+
typeLiteralExpression.type = finishNode(jsdocTypeLiteral);
6557+
return finishNode(typeLiteralExpression);
65396558
}
65406559
}
6541-
result.atToken = atToken;
6542-
result.tagName = tagName;
6543-
result.typeExpression = typeExpression;
6544-
if (typeExpression) {
6545-
result.type = typeExpression.type;
6546-
}
6547-
result.fullName = postName || preName;
6548-
result.name = ts.isIdentifier(result.fullName) ? result.fullName : result.fullName.right;
6549-
result.isParameterNameFirst = postName ? false : !!preName;
6550-
result.isBracketed = isBracketed;
6551-
return finishNode(result);
6552-
65536560
}
65546561

65556562
function parseReturnTag(atToken: AtToken, tagName: Identifier): JSDocReturnTag {

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ namespace ts {
21352135
name: Identifier;
21362136
typeExpression: JSDocTypeExpression;
21372137
/** Whether the property name came before the type -- non-standard for JSDoc, but Typescript-like */
2138-
isParameterNameFirst: boolean;
2138+
isNameFirst: boolean;
21392139
isBracketed: boolean;
21402140
}
21412141

src/compiler/utilities.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,19 +1552,19 @@ namespace ts {
15521552
}
15531553

15541554
/** Does the opposite of `getJSDocParameterTags`: given a JSDoc parameter, finds the parameter corresponding to it. */
1555-
export function getParameterFromJSDoc(node: JSDocParameterTag): ParameterDeclaration | undefined {
1556-
if (!isIdentifier(node.fullName)) {
1557-
// `@param {T} obj.prop` is not a top-level param, so it doesn't map to a top-level parameter
1558-
return undefined;
1555+
export function getParameterSymbolFromJSDoc(node: JSDocParameterTag): Symbol | undefined {
1556+
if (node.symbol) {
1557+
return node.symbol;
15591558
}
15601559
const name = node.name.text;
1561-
const grandParent = node.parent!.parent!;
15621560
Debug.assert(node.parent!.kind === SyntaxKind.JSDocComment);
1563-
if (!isFunctionLike(grandParent)) {
1561+
const func = node.parent!.parent!;
1562+
if (!isFunctionLike(func)) {
15641563
return undefined;
15651564
}
1566-
return find(grandParent.parameters, p =>
1565+
const parameter = find(func.parameters, p =>
15671566
p.name.kind === SyntaxKind.Identifier && p.name.text === name);
1567+
return parameter && parameter.symbol;
15681568
}
15691569

15701570
export function getTypeParameterFromJsDoc(node: TypeParameterDeclaration & { parent: JSDocTemplateTag }): TypeParameterDeclaration | undefined {

src/services/classifier.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ namespace ts {
755755
return;
756756

757757
function processJSDocParameterTag(tag: JSDocParameterTag) {
758-
if (tag.isParameterNameFirst) {
758+
if (tag.isNameFirst) {
759759
pushCommentRange(pos, tag.name.pos - pos);
760760
pushClassification(tag.name.pos, tag.name.end - tag.name.pos, ClassificationType.parameterName);
761761
pos = tag.name.end;
@@ -767,7 +767,7 @@ namespace ts {
767767
pos = tag.typeExpression.end;
768768
}
769769

770-
if (!tag.isParameterNameFirst) {
770+
if (!tag.isNameFirst) {
771771
pushCommentRange(pos, tag.name.pos - pos);
772772
pushClassification(tag.name.pos, tag.name.end - tag.name.pos, ClassificationType.parameterName);
773773
pos = tag.name.end;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
/// <reference path='fourslash.ts' />
4+
5+
// @Filename: foo.js
6+
/////**
7+
//// * @param {object} o - very important!
8+
//// * @param {string} o.x - a thing, its ok
9+
//// * @param {number} o.y - another thing
10+
//// * @param {Object} o.nested - very nested
11+
//// * @param {boolean} o.nested.[|great|] - much greatness
12+
//// * @param {number} o.nested.times - twice? probably!??
13+
//// */
14+
//// function f(o) { return o.nested.[|great|]; }
15+
16+
verify.rangesReferenceEachOther();
17+
18+
///**
19+
// * @param {object} [|o|] - very important!
20+
// * @param {string} o.x - a thing, its ok
21+
// */
22+
// function f([|o|]) { return [|o|].x; }

0 commit comments

Comments
 (0)