Skip to content

Commit 107c556

Browse files
authored
Fix 'as const'-like behavior in JSDoc type cast (#45464)
1 parent 1dc1efb commit 107c556

File tree

11 files changed

+119
-23
lines changed

11 files changed

+119
-23
lines changed

src/compiler/checker.ts

+24-12
Original file line numberDiff line numberDiff line change
@@ -8402,12 +8402,12 @@ namespace ts {
84028402
}
84038403

84048404
function isNullOrUndefined(node: Expression) {
8405-
const expr = skipParentheses(node);
8405+
const expr = skipParentheses(node, /*excludeJSDocTypeAssertions*/ true);
84068406
return expr.kind === SyntaxKind.NullKeyword || expr.kind === SyntaxKind.Identifier && getResolvedSymbol(expr as Identifier) === undefinedSymbol;
84078407
}
84088408

84098409
function isEmptyArrayLiteral(node: Expression) {
8410-
const expr = skipParentheses(node);
8410+
const expr = skipParentheses(node, /*excludeJSDocTypeAssertions*/ true);
84118411
return expr.kind === SyntaxKind.ArrayLiteralExpression && (expr as ArrayLiteralExpression).elements.length === 0;
84128412
}
84138413

@@ -22968,7 +22968,7 @@ namespace ts {
2296822968
}
2296922969

2297022970
function isFalseExpression(expr: Expression): boolean {
22971-
const node = skipParentheses(expr);
22971+
const node = skipParentheses(expr, /*excludeJSDocTypeAssertions*/ true);
2297222972
return node.kind === SyntaxKind.FalseKeyword || node.kind === SyntaxKind.BinaryExpression && (
2297322973
(node as BinaryExpression).operatorToken.kind === SyntaxKind.AmpersandAmpersandToken && (isFalseExpression((node as BinaryExpression).left) || isFalseExpression((node as BinaryExpression).right)) ||
2297422974
(node as BinaryExpression).operatorToken.kind === SyntaxKind.BarBarToken && isFalseExpression((node as BinaryExpression).left) && isFalseExpression((node as BinaryExpression).right));
@@ -23290,7 +23290,7 @@ namespace ts {
2329023290
}
2329123291

2329223292
function narrowTypeByAssertion(type: Type, expr: Expression): Type {
23293-
const node = skipParentheses(expr);
23293+
const node = skipParentheses(expr, /*excludeJSDocTypeAssertions*/ true);
2329423294
if (node.kind === SyntaxKind.FalseKeyword) {
2329523295
return unreachableNeverType;
2329623296
}
@@ -25874,7 +25874,9 @@ namespace ts {
2587425874
case SyntaxKind.ParenthesizedExpression: {
2587525875
// Like in `checkParenthesizedExpression`, an `/** @type {xyz} */` comment before a parenthesized expression acts as a type cast.
2587625876
const tag = isInJSFile(parent) ? getJSDocTypeTag(parent) : undefined;
25877-
return tag ? getTypeFromTypeNode(tag.typeExpression.type) : getContextualType(parent as ParenthesizedExpression, contextFlags);
25877+
return !tag ? getContextualType(parent as ParenthesizedExpression, contextFlags) :
25878+
isJSDocTypeTag(tag) && isConstTypeReference(tag.typeExpression.type) ? tryFindWhenConstTypeReference(parent as ParenthesizedExpression) :
25879+
getTypeFromTypeNode(tag.typeExpression.type);
2587825880
}
2587925881
case SyntaxKind.NonNullExpression:
2588025882
return getContextualType(parent as NonNullExpression, contextFlags);
@@ -32857,8 +32859,10 @@ namespace ts {
3285732859
}
3285832860

3285932861
function isTypeAssertion(node: Expression) {
32860-
node = skipParentheses(node);
32861-
return node.kind === SyntaxKind.TypeAssertionExpression || node.kind === SyntaxKind.AsExpression;
32862+
node = skipParentheses(node, /*excludeJSDocTypeAssertions*/ true);
32863+
return node.kind === SyntaxKind.TypeAssertionExpression ||
32864+
node.kind === SyntaxKind.AsExpression ||
32865+
isJSDocTypeAssertion(node);
3286232866
}
3286332867

3286432868
function checkDeclarationInitializer(declaration: HasExpressionInitializer, contextualType?: Type | undefined) {
@@ -32933,6 +32937,7 @@ namespace ts {
3293332937
function isConstContext(node: Expression): boolean {
3293432938
const parent = node.parent;
3293532939
return isAssertionExpression(parent) && isConstTypeReference(parent.type) ||
32940+
isJSDocTypeAssertion(parent) && isConstTypeReference(getJSDocTypeAssertionType(parent)) ||
3293632941
(isParenthesizedExpression(parent) || isArrayLiteralExpression(parent) || isSpreadElement(parent)) && isConstContext(parent) ||
3293732942
(isPropertyAssignment(parent) || isShorthandPropertyAssignment(parent) || isTemplateSpan(parent)) && isConstContext(parent.parent);
3293832943
}
@@ -33145,7 +33150,14 @@ namespace ts {
3314533150
}
3314633151

3314733152
function getQuickTypeOfExpression(node: Expression) {
33148-
const expr = skipParentheses(node);
33153+
let expr = skipParentheses(node, /*excludeJSDocTypeAssertions*/ true);
33154+
if (isJSDocTypeAssertion(expr)) {
33155+
const type = getJSDocTypeAssertionType(expr);
33156+
if (!isConstTypeReference(type)) {
33157+
return getTypeFromTypeNode(type);
33158+
}
33159+
}
33160+
expr = skipParentheses(node);
3314933161
// Optimize for the common case of a call to a function with a single non-generic call
3315033162
// signature where we can just fetch the return type without checking the arguments.
3315133163
if (isCallExpression(expr) && expr.expression.kind !== SyntaxKind.SuperKeyword && !isRequireCall(expr, /*checkArgumentIsStringLiteralLike*/ true) && !isSymbolOrSymbolForCall(expr)) {
@@ -33232,9 +33244,9 @@ namespace ts {
3323233244
}
3323333245

3323433246
function checkParenthesizedExpression(node: ParenthesizedExpression, checkMode?: CheckMode): Type {
33235-
const tag = isInJSFile(node) ? getJSDocTypeTag(node) : undefined;
33236-
if (tag) {
33237-
return checkAssertionWorker(tag.typeExpression.type, tag.typeExpression.type, node.expression, checkMode);
33247+
if (isJSDocTypeAssertion(node)) {
33248+
const type = getJSDocTypeAssertionType(node);
33249+
return checkAssertionWorker(type, type, node.expression, checkMode);
3323833250
}
3323933251
return checkExpression(node.expression, checkMode);
3324033252
}
@@ -36184,7 +36196,7 @@ namespace ts {
3618436196
if (getFalsyFlags(type)) return;
3618536197

3618636198
const location = isBinaryExpression(condExpr) ? condExpr.right : condExpr;
36187-
if (isPropertyAccessExpression(location) && isAssertionExpression(skipParentheses(location.expression))) {
36199+
if (isPropertyAccessExpression(location) && isTypeAssertion(location.expression)) {
3618836200
return;
3618936201
}
3619036202

src/compiler/factory/utilities.ts

+15
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,24 @@ namespace ts {
416416
node.kind === SyntaxKind.CommaListExpression;
417417
}
418418

419+
export function isJSDocTypeAssertion(node: Node): node is JSDocTypeAssertion {
420+
return isParenthesizedExpression(node)
421+
&& isInJSFile(node)
422+
&& !!getJSDocTypeTag(node);
423+
}
424+
425+
export function getJSDocTypeAssertionType(node: JSDocTypeAssertion) {
426+
const type = getJSDocType(node);
427+
Debug.assertIsDefined(type);
428+
return type;
429+
}
430+
419431
export function isOuterExpression(node: Node, kinds = OuterExpressionKinds.All): node is OuterExpression {
420432
switch (node.kind) {
421433
case SyntaxKind.ParenthesizedExpression:
434+
if (kinds & OuterExpressionKinds.ExcludeJSDocTypeAssertion && isJSDocTypeAssertion(node)) {
435+
return false;
436+
}
422437
return (kinds & OuterExpressionKinds.Parentheses) !== 0;
423438
case SyntaxKind.TypeAssertionExpression:
424439
case SyntaxKind.AsExpression:

src/compiler/types.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,11 @@ namespace ts {
22532253
readonly expression: Expression;
22542254
}
22552255

2256+
/* @internal */
2257+
export interface JSDocTypeAssertion extends ParenthesizedExpression {
2258+
readonly _jsDocTypeAssertionBrand: never;
2259+
}
2260+
22562261
export interface ArrayLiteralExpression extends PrimaryExpression {
22572262
readonly kind: SyntaxKind.ArrayLiteralExpression;
22582263
readonly elements: NodeArray<Expression>;
@@ -6891,7 +6896,9 @@ namespace ts {
68916896
PartiallyEmittedExpressions = 1 << 3,
68926897

68936898
Assertions = TypeAssertions | NonNullAssertions,
6894-
All = Parentheses | Assertions | PartiallyEmittedExpressions
6899+
All = Parentheses | Assertions | PartiallyEmittedExpressions,
6900+
6901+
ExcludeJSDocTypeAssertion = 1 << 4,
68956902
}
68966903

68976904
/* @internal */

src/compiler/utilities.ts

+29-6
Original file line numberDiff line numberDiff line change
@@ -2634,13 +2634,13 @@ namespace ts {
26342634
let result: (JSDoc | JSDocTag)[] | undefined;
26352635
// Pull parameter comments from declaring function as well
26362636
if (isVariableLike(hostNode) && hasInitializer(hostNode) && hasJSDocNodes(hostNode.initializer!)) {
2637-
result = append(result, last((hostNode.initializer as HasJSDoc).jsDoc!));
2637+
result = addRange(result, filterOwnedJSDocTags(hostNode, last((hostNode.initializer as HasJSDoc).jsDoc!)));
26382638
}
26392639

26402640
let node: Node | undefined = hostNode;
26412641
while (node && node.parent) {
26422642
if (hasJSDocNodes(node)) {
2643-
result = append(result, last(node.jsDoc!));
2643+
result = addRange(result, filterOwnedJSDocTags(hostNode, last(node.jsDoc!)));
26442644
}
26452645

26462646
if (node.kind === SyntaxKind.Parameter) {
@@ -2656,6 +2656,26 @@ namespace ts {
26562656
return result || emptyArray;
26572657
}
26582658

2659+
function filterOwnedJSDocTags(hostNode: Node, jsDoc: JSDoc | JSDocTag) {
2660+
if (isJSDoc(jsDoc)) {
2661+
const ownedTags = filter(jsDoc.tags, tag => ownsJSDocTag(hostNode, tag));
2662+
return jsDoc.tags === ownedTags ? [jsDoc] : ownedTags;
2663+
}
2664+
return ownsJSDocTag(hostNode, jsDoc) ? [jsDoc] : undefined;
2665+
}
2666+
2667+
/**
2668+
* Determines whether a host node owns a jsDoc tag. A `@type` tag attached to a
2669+
* a ParenthesizedExpression belongs only to the ParenthesizedExpression.
2670+
*/
2671+
function ownsJSDocTag(hostNode: Node, tag: JSDocTag) {
2672+
return !isJSDocTypeTag(tag)
2673+
|| !tag.parent
2674+
|| !isJSDoc(tag.parent)
2675+
|| !isParenthesizedExpression(tag.parent.parent)
2676+
|| tag.parent.parent === hostNode;
2677+
}
2678+
26592679
export function getNextJSDocCommentLocation(node: Node) {
26602680
const parent = node.parent;
26612681
if (parent.kind === SyntaxKind.PropertyAssignment ||
@@ -2899,10 +2919,13 @@ namespace ts {
28992919
return [child, node];
29002920
}
29012921

2902-
export function skipParentheses(node: Expression): Expression;
2903-
export function skipParentheses(node: Node): Node;
2904-
export function skipParentheses(node: Node): Node {
2905-
return skipOuterExpressions(node, OuterExpressionKinds.Parentheses);
2922+
export function skipParentheses(node: Expression, excludeJSDocTypeAssertions?: boolean): Expression;
2923+
export function skipParentheses(node: Node, excludeJSDocTypeAssertions?: boolean): Node;
2924+
export function skipParentheses(node: Node, excludeJSDocTypeAssertions?: boolean): Node {
2925+
const flags = excludeJSDocTypeAssertions ?
2926+
OuterExpressionKinds.Parentheses | OuterExpressionKinds.ExcludeJSDocTypeAssertion :
2927+
OuterExpressionKinds.Parentheses;
2928+
return skipOuterExpressions(node, flags);
29062929
}
29072930

29082931
// a node is delete target iff. it is PropertyAccessExpression/ElementAccessExpression with parentheses skipped

tests/baselines/reference/api/tsserverlibrary.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3220,7 +3220,8 @@ declare namespace ts {
32203220
NonNullAssertions = 4,
32213221
PartiallyEmittedExpressions = 8,
32223222
Assertions = 6,
3223-
All = 15
3223+
All = 15,
3224+
ExcludeJSDocTypeAssertion = 16
32243225
}
32253226
export type TypeOfTag = "undefined" | "number" | "bigint" | "boolean" | "string" | "symbol" | "object" | "function";
32263227
export interface NodeFactory {

tests/baselines/reference/api/typescript.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3220,7 +3220,8 @@ declare namespace ts {
32203220
NonNullAssertions = 4,
32213221
PartiallyEmittedExpressions = 8,
32223222
Assertions = 6,
3223-
All = 15
3223+
All = 15,
3224+
ExcludeJSDocTypeAssertion = 16
32243225
}
32253226
export type TypeOfTag = "undefined" | "number" | "bigint" | "boolean" | "string" | "symbol" | "object" | "function";
32263227
export interface NodeFactory {

tests/baselines/reference/jsdocTypeTagCast.errors.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,7 @@ tests/cases/conformance/jsdoc/b.js(67,8): error TS2454: Variable 'numOrStr' is u
123123
}
124124

125125

126-
126+
var asConst1 = /** @type {const} */(1);
127+
var asConst2 = /** @type {const} */({
128+
x: 1
129+
});

tests/baselines/reference/jsdocTypeTagCast.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ if(/** @type {numOrStr is string} */(numOrStr === undefined)) { // Error
7474
}
7575

7676

77-
77+
var asConst1 = /** @type {const} */(1);
78+
var asConst2 = /** @type {const} */({
79+
x: 1
80+
});
7881

7982
//// [a.js]
8083
var W;
@@ -154,3 +157,7 @@ var str;
154157
if ( /** @type {numOrStr is string} */(numOrStr === undefined)) { // Error
155158
str = numOrStr; // Error, no narrowing occurred
156159
}
160+
var asConst1 = /** @type {const} */ (1);
161+
var asConst2 = /** @type {const} */ ({
162+
x: 1
163+
});

tests/baselines/reference/jsdocTypeTagCast.symbols

+9
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,13 @@ if(/** @type {numOrStr is string} */(numOrStr === undefined)) { // Error
157157
}
158158

159159

160+
var asConst1 = /** @type {const} */(1);
161+
>asConst1 : Symbol(asConst1, Decl(b.js, 70, 3))
160162

163+
var asConst2 = /** @type {const} */({
164+
>asConst2 : Symbol(asConst2, Decl(b.js, 71, 3))
165+
166+
x: 1
167+
>x : Symbol(x, Decl(b.js, 71, 37))
168+
169+
});

tests/baselines/reference/jsdocTypeTagCast.types

+14
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,18 @@ if(/** @type {numOrStr is string} */(numOrStr === undefined)) { // Error
209209
}
210210

211211

212+
var asConst1 = /** @type {const} */(1);
213+
>asConst1 : 1
214+
>(1) : 1
215+
>1 : 1
212216

217+
var asConst2 = /** @type {const} */({
218+
>asConst2 : { readonly x: 1; }
219+
>({ x: 1}) : { readonly x: 1; }
220+
>{ x: 1} : { readonly x: 1; }
221+
222+
x: 1
223+
>x : 1
224+
>1 : 1
225+
226+
});

tests/cases/conformance/jsdoc/jsdocTypeTagCast.ts

+4
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ if(/** @type {numOrStr is string} */(numOrStr === undefined)) { // Error
7676
}
7777

7878

79+
var asConst1 = /** @type {const} */(1);
80+
var asConst2 = /** @type {const} */({
81+
x: 1
82+
});

0 commit comments

Comments
 (0)