Skip to content
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

Fix bug#56997 - Parenthesized SatisfiesExpressions with comments are not unwrapped consistently in emitted JS #57281

Merged
merged 13 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 1 addition & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37684,7 +37684,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function checkReferenceExpression(expr: Expression, invalidReferenceMessage: DiagnosticMessage, invalidOptionalChainMessage: DiagnosticMessage): boolean {
// References are combinations of identifiers, parentheses, and property accesses.
const node = skipOuterExpressions(expr, OuterExpressionKinds.Assertions | OuterExpressionKinds.Parentheses);
const node = skipOuterExpressions(expr, OuterExpressionKinds.Assertions | OuterExpressionKinds.SatisfiesExpressions | OuterExpressionKinds.Parentheses);
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
if (node.kind !== SyntaxKind.Identifier && !isAccessExpression(node)) {
error(expr, invalidReferenceMessage);
return false;
Expand Down Expand Up @@ -38814,7 +38814,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// requires VarExpr to be classified as a reference
// A compound assignment furthermore requires VarExpr to be classified as a reference (section 4.1)
// and the type of the non-compound operation to be assignable to the type of VarExpr.

if (checkReferenceExpression(left, Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access, Diagnostics.The_left_hand_side_of_an_assignment_expression_may_not_be_an_optional_property_access)) {
let headMessage: DiagnosticMessage | undefined;
if (exactOptionalPropertyTypes && isPropertyAccessExpression(left) && maybeTypeOfKind(valueType, TypeFlags.Undefined)) {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/factory/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,10 +646,11 @@ export function isOuterExpression(node: Node, kinds = OuterExpressionKinds.All):
case SyntaxKind.TypeAssertionExpression:
case SyntaxKind.AsExpression:
case SyntaxKind.ExpressionWithTypeArguments:
case SyntaxKind.SatisfiesExpression:
jean-michelet marked this conversation as resolved.
Show resolved Hide resolved
return (kinds & OuterExpressionKinds.TypeAssertions) !== 0;
case SyntaxKind.NonNullExpression:
return (kinds & OuterExpressionKinds.NonNullAssertions) !== 0;
case SyntaxKind.SatisfiesExpression:
return (kinds & OuterExpressionKinds.SatisfiesExpressions) !== 0;
case SyntaxKind.PartiallyEmittedExpression:
return (kinds & OuterExpressionKinds.PartiallyEmittedExpressions) !== 0;
}
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ import {
isPrivateIdentifier,
isPropertyAccessExpression,
isPropertyName,
isSatisfiesExpression,
isShorthandPropertyAssignment,
isSimpleInlineableExpression,
isSourceFile,
Expand Down Expand Up @@ -1696,8 +1697,9 @@ export function transformTypeScript(context: TransformationContext) {
}

function visitParenthesizedExpression(node: ParenthesizedExpression): Expression {
const innerExpression = skipOuterExpressions(node.expression, ~OuterExpressionKinds.Assertions);
if (isAssertionExpression(innerExpression)) {
const kinds = OuterExpressionKinds.Assertions | OuterExpressionKinds.SatisfiesExpressions;
const innerExpression = skipOuterExpressions(node.expression, ~kinds);
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
if (isAssertionExpression(innerExpression) || isSatisfiesExpression(innerExpression)) {
// Make sure we consider all nested cast expressions, e.g.:
// (<any><number><any>-A).x;
const expression = visitNode(node.expression, visitor, isExpression);
Expand Down
7 changes: 4 additions & 3 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8235,12 +8235,13 @@ export const enum OuterExpressionKinds {
Parentheses = 1 << 0,
TypeAssertions = 1 << 1,
NonNullAssertions = 1 << 2,
PartiallyEmittedExpressions = 1 << 3,
SatisfiesExpressions = 1 << 3,
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
PartiallyEmittedExpressions = 1 << 4,

Assertions = TypeAssertions | NonNullAssertions,
All = Parentheses | Assertions | PartiallyEmittedExpressions,
All = Parentheses | Assertions | SatisfiesExpressions | PartiallyEmittedExpressions,

ExcludeJSDocTypeAssertion = 1 << 4,
ExcludeJSDocTypeAssertion = 1 << 5,
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
}

/** @internal */
Expand Down
7 changes: 4 additions & 3 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7940,10 +7940,11 @@ declare namespace ts {
Parentheses = 1,
TypeAssertions = 2,
NonNullAssertions = 4,
PartiallyEmittedExpressions = 8,
SatisfiesExpressions = 8,
PartiallyEmittedExpressions = 16,
Assertions = 6,
All = 15,
ExcludeJSDocTypeAssertion = 16,
All = 31,
ExcludeJSDocTypeAssertion = 32,
}
type ImmediatelyInvokedFunctionExpression = CallExpression & {
readonly expression: FunctionExpression;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [tests/cases/compiler/commentEmitOnParenthesizedAssertionInReturnStatement2.ts] ////

//// [commentEmitOnParenthesizedAssertionInReturnStatement2.ts]
export class Foo {
client = {
getThing: () => Promise.resolve('')
}

foo() {
return (
/* TODO: please refactor */ this.client
.getThing() satisfies Promise<string>);
}
}

//// [commentEmitOnParenthesizedAssertionInReturnStatement2.js]
export class Foo {
client = {
getThing: () => Promise.resolve('')
};
foo() {
return (
/* TODO: please refactor */ this.client
.getThing());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/commentEmitOnParenthesizedAssertionInReturnStatement2.ts] ////

=== commentEmitOnParenthesizedAssertionInReturnStatement2.ts ===
export class Foo {
>Foo : Symbol(Foo, Decl(commentEmitOnParenthesizedAssertionInReturnStatement2.ts, 0, 0))

client = {
>client : Symbol(Foo.client, Decl(commentEmitOnParenthesizedAssertionInReturnStatement2.ts, 0, 18))

getThing: () => Promise.resolve('')
>getThing : Symbol(getThing, Decl(commentEmitOnParenthesizedAssertionInReturnStatement2.ts, 1, 14))
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
}

foo() {
>foo : Symbol(Foo.foo, Decl(commentEmitOnParenthesizedAssertionInReturnStatement2.ts, 3, 5))

return (
/* TODO: please refactor */ this.client
>this.client .getThing : Symbol(getThing, Decl(commentEmitOnParenthesizedAssertionInReturnStatement2.ts, 1, 14))
>this.client : Symbol(Foo.client, Decl(commentEmitOnParenthesizedAssertionInReturnStatement2.ts, 0, 18))
>this : Symbol(Foo, Decl(commentEmitOnParenthesizedAssertionInReturnStatement2.ts, 0, 0))
>client : Symbol(Foo.client, Decl(commentEmitOnParenthesizedAssertionInReturnStatement2.ts, 0, 18))

.getThing() satisfies Promise<string>);
>getThing : Symbol(getThing, Decl(commentEmitOnParenthesizedAssertionInReturnStatement2.ts, 1, 14))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [tests/cases/compiler/commentEmitOnParenthesizedAssertionInReturnStatement2.ts] ////

=== commentEmitOnParenthesizedAssertionInReturnStatement2.ts ===
export class Foo {
>Foo : Foo

client = {
>client : { getThing: () => Promise<string>; }
>{ getThing: () => Promise.resolve('') } : { getThing: () => Promise<string>; }

getThing: () => Promise.resolve('')
>getThing : () => Promise<string>
>() => Promise.resolve('') : () => Promise<string>
>Promise.resolve('') : Promise<string>
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T_1>(value: T_1 | PromiseLike<T_1>): Promise<Awaited<T_1>>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T_1>(value: T_1 | PromiseLike<T_1>): Promise<Awaited<T_1>>; }
>'' : ""
}

foo() {
>foo : () => Promise<string>

return (
>( /* TODO: please refactor */ this.client .getThing() satisfies Promise<string>) : Promise<string>

/* TODO: please refactor */ this.client
>this.client .getThing() satisfies Promise<string> : Promise<string>
>this.client .getThing() : Promise<string>
>this.client .getThing : () => Promise<string>
>this.client : { getThing: () => Promise<string>; }
>this : this
>client : { getThing: () => Promise<string>; }

.getThing() satisfies Promise<string>);
>getThing : () => Promise<string>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [tests/cases/compiler/parenthesizedSatisfiesExpressionWithComments.ts] ////

//// [parenthesizedSatisfiesExpressionWithComments.ts]
"use strict";
const a = (/*comm*/ 10 satisfies number);
const b = ((/*comm*/ 10 satisfies number));

//// [parenthesizedSatisfiesExpressionWithComments.js]
"use strict";
var a = /*comm*/ 10;
var b = /*comm*/ 10;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [tests/cases/compiler/parenthesizedSatisfiesExpressionWithComments.ts] ////

=== parenthesizedSatisfiesExpressionWithComments.ts ===
"use strict";
const a = (/*comm*/ 10 satisfies number);
>a : Symbol(a, Decl(parenthesizedSatisfiesExpressionWithComments.ts, 1, 5))

const b = ((/*comm*/ 10 satisfies number));
>b : Symbol(b, Decl(parenthesizedSatisfiesExpressionWithComments.ts, 2, 5))

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [tests/cases/compiler/parenthesizedSatisfiesExpressionWithComments.ts] ////

=== parenthesizedSatisfiesExpressionWithComments.ts ===
"use strict";
>"use strict" : "use strict"

const a = (/*comm*/ 10 satisfies number);
>a : 10
>(/*comm*/ 10 satisfies number) : 10
>10 satisfies number : 10
>10 : 10

const b = ((/*comm*/ 10 satisfies number));
>b : 10
>((/*comm*/ 10 satisfies number)) : 10
>(/*comm*/ 10 satisfies number) : 10
>10 satisfies number : 10
>10 : 10

64 changes: 64 additions & 0 deletions tests/baselines/reference/referenceSatisfiesExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//// [tests/cases/compiler/referenceSatisfiesExpression.ts] ////

//// [referenceSatisfiesExpression.ts]
let a = 10;

// called in checkPrefixUnaryExpression
--(a satisfies number);
++(a satisfies number);

// called in checkPostfixUnaryExpression
(a satisfies number)++;
(a satisfies number)--;

// called in checkAssignmentOperatorWorker
let b: number;
(b satisfies number) = 10;

// called in checkReferenceAssignment called in checkDestructuringAssignment
let c: number;
[(c satisfies number)] = [10];

let d: number, e = 1;
({ d: (e satisfies number) } = { d: 10 });

// called in checkForInStatement
let x: string = "hello"
for ((x satisfies string) in { a: 10 }) {
console.log(x)
}

// called in checkForOfStatement
let g = 1
for ((g satisfies number) of [10]) {
console.log(g)
}


//// [referenceSatisfiesExpression.js]
var a = 10;
// called in checkPrefixUnaryExpression
--a;
++a;
// called in checkPostfixUnaryExpression
a++;
a--;
// called in checkAssignmentOperatorWorker
var b;
b = 10;
// called in checkReferenceAssignment called in checkDestructuringAssignment
var c;
c = [10][0];
var d, e = 1;
(e = { d: 10 }.d);
// called in checkForInStatement
var x = "hello";
for (x in { a: 10 }) {
console.log(x);
}
// called in checkForOfStatement
var g = 1;
for (var _i = 0, _a = [10]; _i < _a.length; _i++) {
g = _a[_i];
console.log(g);
}
72 changes: 72 additions & 0 deletions tests/baselines/reference/referenceSatisfiesExpression.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//// [tests/cases/compiler/referenceSatisfiesExpression.ts] ////

=== referenceSatisfiesExpression.ts ===
let a = 10;
>a : Symbol(a, Decl(referenceSatisfiesExpression.ts, 0, 3))

// called in checkPrefixUnaryExpression
--(a satisfies number);
>a : Symbol(a, Decl(referenceSatisfiesExpression.ts, 0, 3))

++(a satisfies number);
>a : Symbol(a, Decl(referenceSatisfiesExpression.ts, 0, 3))

// called in checkPostfixUnaryExpression
(a satisfies number)++;
>a : Symbol(a, Decl(referenceSatisfiesExpression.ts, 0, 3))

(a satisfies number)--;
>a : Symbol(a, Decl(referenceSatisfiesExpression.ts, 0, 3))

// called in checkAssignmentOperatorWorker
let b: number;
>b : Symbol(b, Decl(referenceSatisfiesExpression.ts, 11, 3))

(b satisfies number) = 10;
>b : Symbol(b, Decl(referenceSatisfiesExpression.ts, 11, 3))

// called in checkReferenceAssignment called in checkDestructuringAssignment
let c: number;
>c : Symbol(c, Decl(referenceSatisfiesExpression.ts, 15, 3))

[(c satisfies number)] = [10];
>c : Symbol(c, Decl(referenceSatisfiesExpression.ts, 15, 3))

let d: number, e = 1;
>d : Symbol(d, Decl(referenceSatisfiesExpression.ts, 18, 3))
>e : Symbol(e, Decl(referenceSatisfiesExpression.ts, 18, 14))

({ d: (e satisfies number) } = { d: 10 });
>d : Symbol(d, Decl(referenceSatisfiesExpression.ts, 19, 2))
>e : Symbol(e, Decl(referenceSatisfiesExpression.ts, 18, 14))
>d : Symbol(d, Decl(referenceSatisfiesExpression.ts, 19, 32))

// called in checkForInStatement
let x: string = "hello"
>x : Symbol(x, Decl(referenceSatisfiesExpression.ts, 22, 3))

for ((x satisfies string) in { a: 10 }) {
>x : Symbol(x, Decl(referenceSatisfiesExpression.ts, 22, 3))
>a : Symbol(a, Decl(referenceSatisfiesExpression.ts, 23, 30))

console.log(x)
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>x : Symbol(x, Decl(referenceSatisfiesExpression.ts, 22, 3))
}

// called in checkForOfStatement
let g = 1
>g : Symbol(g, Decl(referenceSatisfiesExpression.ts, 28, 3))

for ((g satisfies number) of [10]) {
>g : Symbol(g, Decl(referenceSatisfiesExpression.ts, 28, 3))

console.log(g)
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>g : Symbol(g, Decl(referenceSatisfiesExpression.ts, 28, 3))
}

Loading
Loading