Skip to content

add support for nonnull jsdoc #36219

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

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 9 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21712,7 +21712,8 @@ namespace ts {
case SyntaxKind.ParenthesizedExpression: {
// Like in `checkParenthesizedExpression`, an `/** @type {xyz} */` comment before a parenthesized expression acts as a type cast.
const tag = isInJSFile(parent) ? getJSDocTypeTag(parent) : undefined;
return tag ? getTypeFromTypeNode(tag.typeExpression.type) : getContextualType(<ParenthesizedExpression>parent, contextFlags);
const type = tag ? getTypeFromTypeNode(tag.typeExpression.type) : getContextualType(<ParenthesizedExpression>parent, contextFlags);
return type && getJSDocNonNullTag(node) ? getNonNullableType(type) : type;
Copy link
Contributor

Choose a reason for hiding this comment

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

getJSDocNonNullTag should be limited to JS files

}
case SyntaxKind.JsxExpression:
return getContextualTypeForJsxExpression(<JsxExpression>parent);
Expand Down Expand Up @@ -27982,10 +27983,8 @@ namespace ts {

function checkParenthesizedExpression(node: ParenthesizedExpression, checkMode?: CheckMode): Type {
const tag = isInJSFile(node) ? getJSDocTypeTag(node) : undefined;
if (tag) {
return checkAssertionWorker(tag, tag.typeExpression.type, node.expression, checkMode);
}
return checkExpression(node.expression, checkMode);
const type = tag ? checkAssertionWorker(tag, tag.typeExpression.type, node.expression, checkMode) : checkExpression(node.expression, checkMode);
return getJSDocNonNullTag(node) ? getNonNullableType(type) : type;
Copy link
Contributor

Choose a reason for hiding this comment

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

getJSDocNonNullTag should be limited to JS files

}

function checkExpressionWorker(node: Expression | QualifiedName, checkMode: CheckMode | undefined, forceTuple?: boolean): Type {
Expand Down Expand Up @@ -32376,7 +32375,7 @@ namespace ts {
if (getModifierFlags(member) & ModifierFlags.Ambient) {
continue;
}
if (isInstancePropertyWithoutInitializer(member)) {
if (isInstancePropertyWithoutInitializer(member) && !isNonNullPropertyWithJSDoc(member)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this the equivalent of a "definite assignment assertion" on properties?
If so, does this need to be expanded to variables as well?

const propName = (<PropertyDeclaration>member).name;
if (isIdentifier(propName) || isPrivateIdentifier(propName)) {
const type = getTypeOfSymbol(getSymbolOfNode(member));
Expand All @@ -32390,6 +32389,10 @@ namespace ts {
}
}

function isNonNullPropertyWithJSDoc (node: Node) {
return !!(isInJSFile(node) && getJSDocNonNullTag(node));
}

function isInstancePropertyWithoutInitializer(node: Node) {
return node.kind === SyntaxKind.PropertyDeclaration &&
!hasModifier(node, ModifierFlags.Static | ModifierFlags.Abstract) &&
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ namespace ts {
case SyntaxKind.JSDocPrivateTag:
case SyntaxKind.JSDocProtectedTag:
case SyntaxKind.JSDocReadonlyTag:
case SyntaxKind.JSDocNonNullTag:
return visitNode(cbNode, (node as JSDocTag).tagName);
case SyntaxKind.PartiallyEmittedExpression:
return visitNode(cbNode, (<PartiallyEmittedExpression>node).expression);
Expand Down Expand Up @@ -6942,6 +6943,9 @@ namespace ts {
case "readonly":
tag = parseSimpleTag(start, SyntaxKind.JSDocReadonlyTag, tagName);
break;
case "nonnull":
tag = parseSimpleTag(start, SyntaxKind.JSDocNonNullTag, tagName);
break;
case "this":
tag = parseThisTag(start, tagName);
break;
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ namespace ts {
JSDocTemplateTag,
JSDocTypedefTag,
JSDocPropertyTag,
JSDocNonNullTag,
Copy link
Contributor

Choose a reason for hiding this comment

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

LastJSDocNode and LastJSDocTagNode need to be adjusted


// Synthesized list
SyntaxList,
Expand Down Expand Up @@ -2637,6 +2638,10 @@ namespace ts {
kind: SyntaxKind.JSDocTag;
}

export interface JSDocNonNullTag extends JSDocTag {
kind: SyntaxKind.JSDocNonNullTag;
}

/**
* Note that `@extends` is a synonym of `@augments`.
* Both tags are represented by this interface.
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,10 @@ namespace ts {
return undefined;
}

export function getJSDocNonNullTag(node: Node): JSDocNonNullTag | undefined {
return getFirstJSDocTag(node, jsJSDocNonNullTag);
}

/**
* Gets the type node for the node if provided via JSDoc.
*
Expand Down Expand Up @@ -1622,6 +1626,10 @@ namespace ts {
return node.kind === SyntaxKind.JSDocTypeTag;
}

export function jsJSDocNonNullTag(node: Node): node is JSDocNonNullTag {
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: js -> is

return node.kind === SyntaxKind.JSDocNonNullTag;
}

export function isJSDocTemplateTag(node: Node): node is JSDocTemplateTag {
return node.kind === SyntaxKind.JSDocTemplateTag;
}
Expand Down
22 changes: 14 additions & 8 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,15 @@ declare namespace ts {
JSDocTemplateTag = 320,
JSDocTypedefTag = 321,
JSDocPropertyTag = 322,
SyntaxList = 323,
NotEmittedStatement = 324,
PartiallyEmittedExpression = 325,
CommaListExpression = 326,
MergeDeclarationMarker = 327,
EndOfDeclarationMarker = 328,
SyntheticReferenceExpression = 329,
Count = 330,
JSDocNonNullTag = 323,
SyntaxList = 324,
NotEmittedStatement = 325,
PartiallyEmittedExpression = 326,
CommaListExpression = 327,
MergeDeclarationMarker = 328,
EndOfDeclarationMarker = 329,
SyntheticReferenceExpression = 330,
Count = 331,
FirstAssignment = 62,
LastAssignment = 74,
FirstCompoundAssignment = 63,
Expand Down Expand Up @@ -1624,6 +1625,9 @@ declare namespace ts {
export interface JSDocUnknownTag extends JSDocTag {
kind: SyntaxKind.JSDocTag;
}
export interface JSDocNonNullTag extends JSDocTag {
kind: SyntaxKind.JSDocNonNullTag;
}
/**
* Note that `@extends` is a synonym of `@augments`.
* Both tags are represented by this interface.
Expand Down Expand Up @@ -3514,6 +3518,7 @@ declare namespace ts {
function getJSDocTemplateTag(node: Node): JSDocTemplateTag | undefined;
/** Gets the JSDoc type tag for the node if present and valid */
function getJSDocTypeTag(node: Node): JSDocTypeTag | undefined;
function getJSDocNonNullTag(node: Node): JSDocNonNullTag | undefined;
/**
* Gets the type node for the node if provided via JSDoc.
*
Expand Down Expand Up @@ -3721,6 +3726,7 @@ declare namespace ts {
function isJSDocParameterTag(node: Node): node is JSDocParameterTag;
function isJSDocReturnTag(node: Node): node is JSDocReturnTag;
function isJSDocTypeTag(node: Node): node is JSDocTypeTag;
function jsJSDocNonNullTag(node: Node): node is JSDocNonNullTag;
function isJSDocTemplateTag(node: Node): node is JSDocTemplateTag;
function isJSDocTypedefTag(node: Node): node is JSDocTypedefTag;
function isJSDocPropertyTag(node: Node): node is JSDocPropertyTag;
Expand Down
22 changes: 14 additions & 8 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,15 @@ declare namespace ts {
JSDocTemplateTag = 320,
JSDocTypedefTag = 321,
JSDocPropertyTag = 322,
SyntaxList = 323,
NotEmittedStatement = 324,
PartiallyEmittedExpression = 325,
CommaListExpression = 326,
MergeDeclarationMarker = 327,
EndOfDeclarationMarker = 328,
SyntheticReferenceExpression = 329,
Count = 330,
JSDocNonNullTag = 323,
SyntaxList = 324,
NotEmittedStatement = 325,
PartiallyEmittedExpression = 326,
CommaListExpression = 327,
MergeDeclarationMarker = 328,
EndOfDeclarationMarker = 329,
SyntheticReferenceExpression = 330,
Count = 331,
FirstAssignment = 62,
LastAssignment = 74,
FirstCompoundAssignment = 63,
Expand Down Expand Up @@ -1624,6 +1625,9 @@ declare namespace ts {
export interface JSDocUnknownTag extends JSDocTag {
kind: SyntaxKind.JSDocTag;
}
export interface JSDocNonNullTag extends JSDocTag {
kind: SyntaxKind.JSDocNonNullTag;
}
/**
* Note that `@extends` is a synonym of `@augments`.
* Both tags are represented by this interface.
Expand Down Expand Up @@ -3514,6 +3518,7 @@ declare namespace ts {
function getJSDocTemplateTag(node: Node): JSDocTemplateTag | undefined;
/** Gets the JSDoc type tag for the node if present and valid */
function getJSDocTypeTag(node: Node): JSDocTypeTag | undefined;
function getJSDocNonNullTag(node: Node): JSDocNonNullTag | undefined;
/**
* Gets the type node for the node if provided via JSDoc.
*
Expand Down Expand Up @@ -3721,6 +3726,7 @@ declare namespace ts {
function isJSDocParameterTag(node: Node): node is JSDocParameterTag;
function isJSDocReturnTag(node: Node): node is JSDocReturnTag;
function isJSDocTypeTag(node: Node): node is JSDocTypeTag;
function jsJSDocNonNullTag(node: Node): node is JSDocNonNullTag;
function isJSDocTemplateTag(node: Node): node is JSDocTemplateTag;
function isJSDocTypedefTag(node: Node): node is JSDocTypedefTag;
function isJSDocPropertyTag(node: Node): node is JSDocPropertyTag;
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/nonnulltag1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/conformance/jsdoc/a.js ===
/**
* @type {(()=>string)[]}
*/

let queue = [];
>queue : Symbol(queue, Decl(a.js, 4, 3))

while (queue.length) {
>queue.length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
>queue : Symbol(queue, Decl(a.js, 4, 3))
>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))

const top = /** @nonnull */(queue.pop());
>top : Symbol(top, Decl(a.js, 6, 9))
>queue.pop : Symbol(Array.pop, Decl(lib.es5.d.ts, --, --))
>queue : Symbol(queue, Decl(a.js, 4, 3))
>pop : Symbol(Array.pop, Decl(lib.es5.d.ts, --, --))

top();
>top : Symbol(top, Decl(a.js, 6, 9))
}

27 changes: 27 additions & 0 deletions tests/baselines/reference/nonnulltag1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/conformance/jsdoc/a.js ===
/**
* @type {(()=>string)[]}
*/

let queue = [];
>queue : (() => string)[]
>[] : never[]

while (queue.length) {
>queue.length : number
>queue : (() => string)[]
>length : number

const top = /** @nonnull */(queue.pop());
>top : (() => string) | undefined
>(queue.pop()) : (() => string) | undefined
>queue.pop() : (() => string) | undefined
>queue.pop : () => (() => string) | undefined
>queue : (() => string)[]
>pop : () => (() => string) | undefined

top();
>top() : string
>top : () => string
}

38 changes: 38 additions & 0 deletions tests/baselines/reference/nonnulltag2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
=== tests/cases/conformance/jsdoc/a.js ===
class ElementWrapper {
>ElementWrapper : Symbol(ElementWrapper, Decl(a.js, 0, 0))

/**
* @type {string}
* @nonnull
*/
element
>element : Symbol(ElementWrapper.element, Decl(a.js, 0, 22))

constructor() {
this.init();
>this.init : Symbol(ElementWrapper.init, Decl(a.js, 9, 5))
>this : Symbol(ElementWrapper, Decl(a.js, 0, 0))
>init : Symbol(ElementWrapper.init, Decl(a.js, 9, 5))
}

init() {
>init : Symbol(ElementWrapper.init, Decl(a.js, 9, 5))

this.element = "123"
>this.element : Symbol(ElementWrapper.element, Decl(a.js, 0, 22))
>this : Symbol(ElementWrapper, Decl(a.js, 0, 0))
>element : Symbol(ElementWrapper.element, Decl(a.js, 0, 22))
}

getElementStyle() {
>getElementStyle : Symbol(ElementWrapper.getElementStyle, Decl(a.js, 13, 5))

this.element.toLowerCase() // error: element is possibly undefined
>this.element.toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
>this.element : Symbol(ElementWrapper.element, Decl(a.js, 0, 22))
>this : Symbol(ElementWrapper, Decl(a.js, 0, 0))
>element : Symbol(ElementWrapper.element, Decl(a.js, 0, 22))
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.es5.d.ts, --, --))
}
}
42 changes: 42 additions & 0 deletions tests/baselines/reference/nonnulltag2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== tests/cases/conformance/jsdoc/a.js ===
class ElementWrapper {
>ElementWrapper : ElementWrapper

/**
* @type {string}
* @nonnull
*/
element
>element : string

constructor() {
this.init();
>this.init() : void
>this.init : () => void
>this : this
>init : () => void
}

init() {
>init : () => void

this.element = "123"
>this.element = "123" : "123"
>this.element : string
>this : this
>element : string
>"123" : "123"
}

getElementStyle() {
>getElementStyle : () => void

this.element.toLowerCase() // error: element is possibly undefined
>this.element.toLowerCase() : string
>this.element.toLowerCase : () => string
>this.element : string
>this : this
>element : string
>toLowerCase : () => string
}
}
15 changes: 15 additions & 0 deletions tests/cases/conformance/jsdoc/nonnulltag1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @noEmit: true
// @allowJs: true
// @checkJs: true
// @strict: true
// @Filename: a.js

/**
* @type {(()=>string)[]}
*/

let queue = [];
while (queue.length) {
const top = /** @nonnull */(queue.pop());
top();
}
25 changes: 25 additions & 0 deletions tests/cases/conformance/jsdoc/nonnulltag2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @noEmit: true
// @allowJs: true
// @checkJs: true
// @strict: true
// @Filename: a.js

class ElementWrapper {
/**
* @type {string}
* @nonnull
*/
element

constructor() {
this.init();
}

init() {
this.element = "123"
}

getElementStyle() {
this.element.toLowerCase() // error: element is possibly undefined
}
}