-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fixed types of parenthesized expressions at locations with @satisfies
and @type
mix
#61852
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
base: main
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -4588,13 +4588,13 @@ export function getJSDocCommentsAndTags(hostNode: Node, noCache?: boolean): read | |
let result: (JSDoc | JSDocTag)[] | undefined; | ||
// Pull parameter comments from declaring function as well | ||
if (isVariableLike(hostNode) && hasInitializer(hostNode) && hasJSDocNodes(hostNode.initializer!)) { | ||
result = addRange(result, filterOwnedJSDocTags(hostNode, hostNode.initializer.jsDoc!)); | ||
result = addRange(result, filterOwnedJSDocTags(hostNode, hostNode.initializer.jsDoc!, result)); | ||
} | ||
|
||
let node: Node | undefined = hostNode; | ||
while (node && node.parent) { | ||
if (hasJSDocNodes(node)) { | ||
result = addRange(result, filterOwnedJSDocTags(hostNode, node.jsDoc!)); | ||
result = addRange(result, filterOwnedJSDocTags(hostNode, node.jsDoc!, result)); | ||
} | ||
|
||
if (node.kind === SyntaxKind.Parameter) { | ||
|
@@ -4610,11 +4610,11 @@ export function getJSDocCommentsAndTags(hostNode: Node, noCache?: boolean): read | |
return result || emptyArray; | ||
} | ||
|
||
function filterOwnedJSDocTags(hostNode: Node, comments: JSDocArray) { | ||
function filterOwnedJSDocTags(hostNode: Node, comments: JSDocArray, result: (JSDoc | JSDocTag)[] | undefined) { | ||
const lastJsDoc = last(comments); | ||
return flatMap<JSDoc, JSDoc | JSDocTag>(comments, jsDoc => { | ||
if (jsDoc === lastJsDoc) { | ||
const ownedTags = filter(jsDoc.tags, tag => ownsJSDocTag(hostNode, tag)); | ||
const ownedTags = filter(jsDoc.tags, tag => ownsJSDocTag(hostNode, tag, result)); | ||
return jsDoc.tags === ownedTags ? [jsDoc] : ownedTags; | ||
} | ||
else { | ||
|
@@ -4627,14 +4627,24 @@ function filterOwnedJSDocTags(hostNode: Node, comments: JSDocArray) { | |
* Determines whether a host node owns a jsDoc tag. A `@type`/`@satisfies` tag attached to a | ||
* a ParenthesizedExpression belongs only to the ParenthesizedExpression. | ||
*/ | ||
function ownsJSDocTag(hostNode: Node, tag: JSDocTag) { | ||
return !(isJSDocTypeTag(tag) || isJSDocSatisfiesTag(tag)) | ||
|| !tag.parent | ||
function ownsJSDocTag(hostNode: Node, tag: JSDocTag, result: (JSDoc | JSDocTag)[] | undefined) { | ||
if ( | ||
(isJSDocTypeOrSatisfiesTag(tag)) && some(result, t => { | ||
return isJSDoc(t) ? some(t.tags, isJSDocTypeOrSatisfiesTag) : isJSDocTypeOrSatisfiesTag(t); | ||
}) | ||
) { | ||
return false; | ||
} | ||
Comment on lines
+4631
to
+4637
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. the rationale is that in TS files a single node can't own both at the same time |
||
return !tag.parent | ||
|| !isJSDoc(tag.parent) | ||
|| !isParenthesizedExpression(tag.parent.parent) | ||
|| tag.parent.parent === hostNode; | ||
} | ||
|
||
function isJSDocTypeOrSatisfiesTag(tag: JSDocTag) { | ||
return isJSDocTypeTag(tag) || isJSDocSatisfiesTag(tag); | ||
} | ||
|
||
/** @internal */ | ||
export function getNextJSDocCommentLocation(node: Node): Node | undefined { | ||
const parent = node.parent; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,8 +60,8 @@ const t3 = /** @satisfies {T1} */ ({}); | |
const t4 = /** @satisfies {T2} */ ({ a: "a" }); | ||
>t4 : T2 | ||
> : ^^ | ||
>({ a: "a" }) : T2 | ||
> : ^^ | ||
>({ a: "a" }) : { a: "a"; } | ||
> : ^^^^^^^^^^^ | ||
>{ a: "a" } : { a: "a"; } | ||
> : ^^^^^^^^^^^ | ||
>a : "a" | ||
|
@@ -74,7 +74,7 @@ const t5 = /** @satisfies {T3} */((m) => m.substring(0)); | |
>t5 : (m: string) => string | ||
> : ^ ^^ ^^^^^ | ||
>((m) => m.substring(0)) : (m: string) => string | ||
> : ^ ^^ ^^^^^ | ||
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. Sort of interesting, given the old one showed reuse here? 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. It makes some sense - this is now the function expression's type that is contextually-typed by the |
||
> : ^ ^^^^^^^^^^^^^^^^^^^ | ||
>(m) => m.substring(0) : (m: string) => string | ||
> : ^ ^^^^^^^^^^^^^^^^^^^ | ||
>m : string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
/a.js(13,5): error TS1223: 'satisfies' tag already specified. | ||
/a.js(18,5): error TS1223: 'satisfies' tag already specified. | ||
|
||
|
||
==== /a.js (2 errors) ==== | ||
==== /a.js (1 errors) ==== | ||
/** | ||
* @typedef {Object} T1 | ||
* @property {number} a | ||
|
@@ -23,8 +22,6 @@ | |
|
||
/** | ||
* @satisfies {number} | ||
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. as shown by the new 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. It's correct to drop the error, but the first tag shouldn't even be checked--the comment on ownsJSDocTag says that satisfies tags not on parentheses don't apply to anything. I think the real bug is that type tags shouldn't apply to the parenthesized expression in this case either, but fails because |
||
~~~~~~~~~ | ||
!!! error TS1223: 'satisfies' tag already specified. | ||
*/ | ||
const t2 = /** @satisfies {number} */ (1); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/a.js(9,17): error TS1360: Type 'number' does not satisfy the expected type 'string'. | ||
/a.js(12,5): error TS1360: Type 'number' does not satisfy the expected type 'string'. | ||
/a.js(17,5): error TS1360: Type 'number' does not satisfy the expected type 'string'. | ||
/a.js(19,17): error TS1360: Type 'number' does not satisfy the expected type 'string'. | ||
|
||
|
||
==== /a.js (4 errors) ==== | ||
/** | ||
* @satisfies {number} | ||
*/ | ||
const t1 = /** @satisfies {number} */ (1); | ||
|
||
/** | ||
* @satisfies {number} | ||
*/ | ||
const t2 = /** @satisfies {string} */ (1); | ||
~~~~~~~~~ | ||
!!! error TS1360: Type 'number' does not satisfy the expected type 'string'. | ||
|
||
/** | ||
* @satisfies {string} | ||
~~~~~~~~~ | ||
!!! error TS1360: Type 'number' does not satisfy the expected type 'string'. | ||
*/ | ||
const t3 = /** @satisfies {number} */ (1); | ||
|
||
/** | ||
* @satisfies {string} | ||
~~~~~~~~~ | ||
!!! error TS1360: Type 'number' does not satisfy the expected type 'string'. | ||
*/ | ||
const t4 = /** @satisfies {string} */ (1); | ||
~~~~~~~~~ | ||
!!! error TS1360: Type 'number' does not satisfy the expected type 'string'. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//// [tests/cases/conformance/jsdoc/checkJsdocSatisfiesTag16.ts] //// | ||
|
||
=== /a.js === | ||
/** | ||
* @satisfies {number} | ||
*/ | ||
const t1 = /** @satisfies {number} */ (1); | ||
>t1 : Symbol(t1, Decl(a.js, 3, 5)) | ||
|
||
/** | ||
* @satisfies {number} | ||
*/ | ||
const t2 = /** @satisfies {string} */ (1); | ||
>t2 : Symbol(t2, Decl(a.js, 8, 5)) | ||
|
||
/** | ||
* @satisfies {string} | ||
*/ | ||
const t3 = /** @satisfies {number} */ (1); | ||
>t3 : Symbol(t3, Decl(a.js, 13, 5)) | ||
|
||
/** | ||
* @satisfies {string} | ||
*/ | ||
const t4 = /** @satisfies {string} */ (1); | ||
>t4 : Symbol(t4, Decl(a.js, 18, 5)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//// [tests/cases/conformance/jsdoc/checkJsdocSatisfiesTag16.ts] //// | ||
|
||
=== /a.js === | ||
/** | ||
* @satisfies {number} | ||
*/ | ||
const t1 = /** @satisfies {number} */ (1); | ||
>t1 : 1 | ||
> : ^ | ||
>(1) : 1 | ||
> : ^ | ||
>1 : 1 | ||
> : ^ | ||
|
||
/** | ||
* @satisfies {number} | ||
*/ | ||
const t2 = /** @satisfies {string} */ (1); | ||
>t2 : 1 | ||
> : ^ | ||
>(1) : 1 | ||
> : ^ | ||
>1 : 1 | ||
> : ^ | ||
|
||
/** | ||
* @satisfies {string} | ||
*/ | ||
const t3 = /** @satisfies {number} */ (1); | ||
>t3 : 1 | ||
> : ^ | ||
>(1) : 1 | ||
> : ^ | ||
>1 : 1 | ||
> : ^ | ||
|
||
/** | ||
* @satisfies {string} | ||
*/ | ||
const t4 = /** @satisfies {string} */ (1); | ||
>t4 : 1 | ||
> : ^ | ||
>(1) : 1 | ||
> : ^ | ||
>1 : 1 | ||
> : ^ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,8 @@ | |
let obj = /** @satisfies {{ g(s: string): void } & Record<string, unknown>} */ ({ | ||
>obj : { f(s: string): void; } & Record<string, unknown> | ||
> : ^^^^ ^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>({ f(s) { }, // "incorrect" implicit any on 's' g(s) { }}) : { f(s: string): void; } & Record<string, unknown> | ||
> : ^^^^ ^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>({ f(s) { }, // "incorrect" implicit any on 's' g(s) { }}) : { f(s: any): void; g(s: string): void; } | ||
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. as we can see here - type of this parenthesized expression is now the same as the inner's expression, which I think is the actual desired outcome (and the actual result as implemented by microsoft/typescript-go#1157 ) |
||
> : ^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ | ||
>{ f(s) { }, // "incorrect" implicit any on 's' g(s) { }} : { f(s: any): void; g(s: string): void; } | ||
> : ^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// @strict: true | ||
// @allowJS: true | ||
// @checkJs: true | ||
// @noEmit: true | ||
|
||
// @filename: /a.js | ||
|
||
/** | ||
* @satisfies {number} | ||
*/ | ||
const t1 = /** @satisfies {number} */ (1); | ||
|
||
/** | ||
* @satisfies {number} | ||
*/ | ||
const t2 = /** @satisfies {string} */ (1); | ||
|
||
/** | ||
* @satisfies {string} | ||
*/ | ||
const t3 = /** @satisfies {number} */ (1); | ||
|
||
/** | ||
* @satisfies {string} | ||
*/ | ||
const t4 = /** @satisfies {string} */ (1); |
Uh oh!
There was an error while loading. Please reload this page.