Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@typedef supports nested @property names just like @param does #22967

Merged
merged 1 commit into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6497,7 +6497,7 @@ namespace ts {
const result = target === PropertyLikeParse.Parameter ?
<JSDocParameterTag>createNode(SyntaxKind.JSDocParameterTag, atToken.pos) :
<JSDocPropertyTag>createNode(SyntaxKind.JSDocPropertyTag, atToken.pos);
const nestedTypeLiteral = parseNestedTypeLiteral(typeExpression, name);
const nestedTypeLiteral = parseNestedTypeLiteral(typeExpression, name, target);
if (nestedTypeLiteral) {
typeExpression = nestedTypeLiteral;
isNameFirst = true;
Expand All @@ -6511,15 +6511,17 @@ namespace ts {
return finishNode(result);
}

function parseNestedTypeLiteral(typeExpression: JSDocTypeExpression, name: EntityName) {
function parseNestedTypeLiteral(typeExpression: JSDocTypeExpression, name: EntityName, target: PropertyLikeParse) {
if (typeExpression && isObjectOrObjectArrayTypeReference(typeExpression.type)) {
const typeLiteralExpression = <JSDocTypeExpression>createNode(SyntaxKind.JSDocTypeExpression, scanner.getTokenPos());
let child: JSDocParameterTag | false;
let child: JSDocPropertyLikeTag | JSDocTypeTag | false;
let jsdocTypeLiteral: JSDocTypeLiteral;
const start = scanner.getStartPos();
let children: JSDocParameterTag[];
while (child = tryParse(() => parseChildParameterOrPropertyTag(PropertyLikeParse.Parameter, name))) {
children = append(children, child);
let children: JSDocPropertyLikeTag[];
while (child = tryParse(() => parseChildParameterOrPropertyTag(target, name))) {
if (child.kind === SyntaxKind.JSDocParameterTag || child.kind === SyntaxKind.JSDocPropertyTag) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we parsed an unexpected child we just drop it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alternative that I thought was to break here, but it seems worse to pop up a level and start ignoring the prefixes the way we did before this PR.
(You can see an example at the end of the test I added.)

This is consistent with our treatment of jsdoc elsewhere, although perhaps we should have an error like "@type in the middle of a nested @typedef property". I don't think this mistake will happen that often though.

children = append(children, child);
}
}
if (children) {
jsdocTypeLiteral = <JSDocTypeLiteral>createNode(SyntaxKind.JSDocTypeLiteral, start);
Expand Down Expand Up @@ -6623,7 +6625,7 @@ namespace ts {
let jsdocTypeLiteral: JSDocTypeLiteral;
let childTypeTag: JSDocTypeTag;
const start = scanner.getStartPos();
while (child = tryParse(() => parseChildParameterOrPropertyTag(PropertyLikeParse.Property))) {
while (child = tryParse(() => parseChildPropertyTag())) {
if (!jsdocTypeLiteral) {
jsdocTypeLiteral = <JSDocTypeLiteral>createNode(SyntaxKind.JSDocTypeLiteral, start);
}
Expand Down Expand Up @@ -6683,8 +6685,10 @@ namespace ts {
return a.escapedText === b.escapedText;
}

function parseChildParameterOrPropertyTag(target: PropertyLikeParse.Property): JSDocTypeTag | JSDocPropertyTag | false;
function parseChildParameterOrPropertyTag(target: PropertyLikeParse.Parameter, name: EntityName): JSDocParameterTag | false;
function parseChildPropertyTag() {
return parseChildParameterOrPropertyTag(PropertyLikeParse.Property) as JSDocTypeTag | JSDocPropertyTag | false;
}

function parseChildParameterOrPropertyTag(target: PropertyLikeParse, name?: EntityName): JSDocTypeTag | JSDocPropertyTag | JSDocParameterTag | false {
let canParseTag = true;
let seenAsterisk = false;
Expand Down
39 changes: 39 additions & 0 deletions tests/baselines/reference/typedefTagNested.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== tests/cases/conformance/jsdoc/a.js ===
/** @typedef {Object} App
* @property {string} name
* @property {Object} icons
* @property {string} icons.image32
* @property {string} icons.image64
*/
var ex;
>ex : Symbol(ex, Decl(a.js, 6, 3))

/** @type {App} */
const app = {
>app : Symbol(app, Decl(a.js, 9, 5))

name: 'name',
>name : Symbol(name, Decl(a.js, 9, 13))

icons: {
>icons : Symbol(icons, Decl(a.js, 10, 17))

image32: 'x.png',
>image32 : Symbol(image32, Decl(a.js, 11, 12))

image64: 'y.png',
>image64 : Symbol(image64, Decl(a.js, 12, 25))
}
}

/** @typedef {Object} Opp
* @property {string} name
* @property {Object} oops
* @property {string} horrible
* @type {string} idea
*/

/** @type {Opp} */
var mistake;
>mistake : Symbol(mistake, Decl(a.js, 25, 3))

44 changes: 44 additions & 0 deletions tests/baselines/reference/typedefTagNested.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
=== tests/cases/conformance/jsdoc/a.js ===
/** @typedef {Object} App
* @property {string} name
* @property {Object} icons
* @property {string} icons.image32
* @property {string} icons.image64
*/
var ex;
>ex : any

/** @type {App} */
const app = {
>app : { name: string; icons: { image32: string; image64: string; }; }
>{ name: 'name', icons: { image32: 'x.png', image64: 'y.png', }} : { name: string; icons: { image32: string; image64: string; }; }

name: 'name',
>name : string
>'name' : "name"

icons: {
>icons : { image32: string; image64: string; }
>{ image32: 'x.png', image64: 'y.png', } : { image32: string; image64: string; }

image32: 'x.png',
>image32 : string
>'x.png' : "x.png"

image64: 'y.png',
>image64 : string
>'y.png' : "y.png"
}
}

/** @typedef {Object} Opp
* @property {string} name
* @property {Object} oops
* @property {string} horrible
* @type {string} idea
*/

/** @type {Opp} */
var mistake;
>mistake : { name: string; oops: { horrible: string; }; }

32 changes: 32 additions & 0 deletions tests/cases/conformance/jsdoc/typedefTagNested.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @noEmit: true
// @allowJs: true
// @checkJs: true
// @strict: true
// @Filename: a.js

/** @typedef {Object} App
* @property {string} name
* @property {Object} icons
* @property {string} icons.image32
* @property {string} icons.image64
*/
var ex;

/** @type {App} */
const app = {
name: 'name',
icons: {
image32: 'x.png',
image64: 'y.png',
}
}

/** @typedef {Object} Opp
* @property {string} name
* @property {Object} oops
* @property {string} horrible
* @type {string} idea
*/

/** @type {Opp} */
var mistake;