-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
case SyntaxKind.JsxExpression: | ||
return getContextualTypeForJsxExpression(<JsxExpression>parent); | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
function checkExpressionWorker(node: Expression | QualifiedName, checkMode: CheckMode | undefined, forceTuple?: boolean): Type { | ||
|
@@ -32376,7 +32375,7 @@ namespace ts { | |
if (getModifierFlags(member) & ModifierFlags.Ambient) { | ||
continue; | ||
} | ||
if (isInstancePropertyWithoutInitializer(member)) { | ||
if (isInstancePropertyWithoutInitializer(member) && !isNonNullPropertyWithJSDoc(member)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this the equivalent of a "definite assignment assertion" on properties? |
||
const propName = (<PropertyDeclaration>member).name; | ||
if (isIdentifier(propName) || isPrivateIdentifier(propName)) { | ||
const type = getTypeOfSymbol(getSymbolOfNode(member)); | ||
|
@@ -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) && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -483,6 +483,7 @@ namespace ts { | |
JSDocTemplateTag, | ||
JSDocTypedefTag, | ||
JSDocPropertyTag, | ||
JSDocNonNullTag, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// Synthesized list | ||
SyntaxList, | ||
|
@@ -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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -1622,6 +1626,10 @@ namespace ts { | |
return node.kind === SyntaxKind.JSDocTypeTag; | ||
} | ||
|
||
export function jsJSDocNonNullTag(node: Node): node is JSDocNonNullTag { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: |
||
return node.kind === SyntaxKind.JSDocNonNullTag; | ||
} | ||
|
||
export function isJSDocTemplateTag(node: Node): node is JSDocTemplateTag { | ||
return node.kind === SyntaxKind.JSDocTemplateTag; | ||
} | ||
|
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)) | ||
} | ||
|
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 | ||
} | ||
|
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, --, --)) | ||
} | ||
} |
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 | ||
} | ||
} |
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(); | ||
} |
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 | ||
} | ||
} |
There was a problem hiding this comment.
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