Skip to content

Commit 3186fc4

Browse files
authored
Merge pull request microsoft#16094 from Microsoft/master-@propWithBracket
[Master] @prop with bracket for optional property
2 parents 3cd9f3d + 8ae2fba commit 3186fc4

File tree

7 files changed

+29
-11
lines changed

7 files changed

+29
-11
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,7 @@ namespace ts {
21792179
return bindPropertyWorker(node as JSDocRecordMember);
21802180
case SyntaxKind.JSDocPropertyTag:
21812181
return declareSymbolAndAddToSymbolTable(node as JSDocPropertyTag,
2182-
(node as JSDocPropertyTag).typeExpression && (node as JSDocPropertyTag).typeExpression.type.kind === SyntaxKind.JSDocOptionalType ?
2182+
(node as JSDocPropertyTag).isBracketed || ((node as JSDocPropertyTag).typeExpression && (node as JSDocPropertyTag).typeExpression.type.kind === SyntaxKind.JSDocOptionalType) ?
21832183
SymbolFlags.Property | SymbolFlags.Optional : SymbolFlags.Property,
21842184
SymbolFlags.PropertyExcludes);
21852185
case SyntaxKind.JSDocFunctionType:

src/compiler/parser.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6631,10 +6631,7 @@ namespace ts {
66316631
});
66326632
}
66336633

6634-
function parseParamTag(atToken: AtToken, tagName: Identifier) {
6635-
let typeExpression = tryParseTypeExpression();
6636-
skipWhitespace();
6637-
6634+
function parseBracketNameInPropertyAndParamTag() {
66386635
let name: Identifier;
66396636
let isBracketed: boolean;
66406637
// Looking for something like '[foo]' or 'foo'
@@ -6653,6 +6650,14 @@ namespace ts {
66536650
else if (tokenIsIdentifierOrKeyword(token())) {
66546651
name = parseJSDocIdentifierName();
66556652
}
6653+
return { name, isBracketed };
6654+
}
6655+
6656+
function parseParamTag(atToken: AtToken, tagName: Identifier) {
6657+
let typeExpression = tryParseTypeExpression();
6658+
skipWhitespace();
6659+
6660+
const { name, isBracketed } = parseBracketNameInPropertyAndParamTag();
66566661

66576662
if (!name) {
66586663
parseErrorAtPosition(scanner.getStartPos(), 0, Diagnostics.Identifier_expected);
@@ -6709,8 +6714,9 @@ namespace ts {
67096714
function parsePropertyTag(atToken: AtToken, tagName: Identifier): JSDocPropertyTag {
67106715
const typeExpression = tryParseTypeExpression();
67116716
skipWhitespace();
6712-
const name = parseJSDocIdentifierName();
6717+
const { name, isBracketed } = parseBracketNameInPropertyAndParamTag();
67136718
skipWhitespace();
6719+
67146720
if (!name) {
67156721
parseErrorAtPosition(scanner.getStartPos(), /*length*/ 0, Diagnostics.Identifier_expected);
67166722
return undefined;
@@ -6721,6 +6727,7 @@ namespace ts {
67216727
result.tagName = tagName;
67226728
result.name = name;
67236729
result.typeExpression = typeExpression;
6730+
result.isBracketed = isBracketed;
67246731
return finishNode(result);
67256732
}
67266733

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,7 @@ namespace ts {
21462146
kind: SyntaxKind.JSDocPropertyTag;
21472147
name: Identifier;
21482148
typeExpression: JSDocTypeExpression;
2149+
isBracketed: boolean;
21492150
}
21502151

21512152
export interface JSDocTypeLiteral extends JSDocType {

tests/baselines/reference/checkJsdocTypedefInParamTag1.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @typedef {Object} Opts
55
* @property {string} x
66
* @property {string=} y
7+
* @property {string} [z]
8+
* @property {string} [w="hi"]
79
*
810
* @param {Opts} opts
911
*/
@@ -17,6 +19,8 @@ foo({x: 'abc'});
1719
* @typedef {Object} Opts
1820
* @property {string} x
1921
* @property {string=} y
22+
* @property {string} [z]
23+
* @property {string} [w="hi"]
2024
*
2125
* @param {Opts} opts
2226
*/

tests/baselines/reference/checkJsdocTypedefInParamTag1.symbols

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
* @typedef {Object} Opts
55
* @property {string} x
66
* @property {string=} y
7+
* @property {string} [z]
8+
* @property {string} [w="hi"]
79
*
810
* @param {Opts} opts
911
*/
1012
function foo(opts) {}
1113
>foo : Symbol(foo, Decl(0.js, 0, 0))
12-
>opts : Symbol(opts, Decl(0.js, 8, 13))
14+
>opts : Symbol(opts, Decl(0.js, 10, 13))
1315

1416
foo({x: 'abc'});
1517
>foo : Symbol(foo, Decl(0.js, 0, 0))
16-
>x : Symbol(x, Decl(0.js, 10, 5))
18+
>x : Symbol(x, Decl(0.js, 12, 5))
1719

tests/baselines/reference/checkJsdocTypedefInParamTag1.types

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
* @typedef {Object} Opts
55
* @property {string} x
66
* @property {string=} y
7+
* @property {string} [z]
8+
* @property {string} [w="hi"]
79
*
810
* @param {Opts} opts
911
*/
1012
function foo(opts) {}
11-
>foo : (opts: { x: string; y?: string; }) => void
12-
>opts : { x: string; y?: string; }
13+
>foo : (opts: { x: string; y?: string; z?: string; w?: string; }) => void
14+
>opts : { x: string; y?: string; z?: string; w?: string; }
1315

1416
foo({x: 'abc'});
1517
>foo({x: 'abc'}) : void
16-
>foo : (opts: { x: string; y?: string; }) => void
18+
>foo : (opts: { x: string; y?: string; z?: string; w?: string; }) => void
1719
>{x: 'abc'} : { x: string; }
1820
>x : string
1921
>'abc' : "abc"

tests/cases/conformance/jsdoc/checkJsdocTypedefInParamTag1.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @typedef {Object} Opts
88
* @property {string} x
99
* @property {string=} y
10+
* @property {string} [z]
11+
* @property {string} [w="hi"]
1012
*
1113
* @param {Opts} opts
1214
*/

0 commit comments

Comments
 (0)