Fixed emit of return statements with parenthesized assertions and comments (#56601)

This commit is contained in:
Mateusz Burzyński 2024-01-09 20:10:15 +01:00 committed by GitHub
parent f57e5104a3
commit 1e00399a38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 130 additions and 1 deletions

View File

@ -3607,7 +3607,14 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
function willEmitLeadingNewLine(node: Expression): boolean {
if (!currentSourceFile) return false;
if (some(getLeadingCommentRanges(currentSourceFile.text, node.pos), commentWillEmitNewLine)) return true;
const leadingCommentRanges = getLeadingCommentRanges(currentSourceFile.text, node.pos);
if (leadingCommentRanges) {
const parseNode = getParseTreeNode(node);
if (parseNode && isParenthesizedExpression(parseNode.parent)) {
return true;
}
}
if (some(leadingCommentRanges, commentWillEmitNewLine)) return true;
if (some(getSyntheticLeadingComments(node), commentWillEmitNewLine)) return true;
if (isPartiallyEmittedExpression(node)) {
if (node.pos !== node.expression.pos) {

View File

@ -0,0 +1,28 @@
//// [tests/cases/compiler/commentEmitOnParenthesizedAssertionInReturnStatement.ts] ////
//// [commentEmitOnParenthesizedAssertionInReturnStatement.ts]
export class Foo {
client = {
getThing: () => Promise.resolve('')
}
foo(): Promise<void> {
return (
/* TODO: Avoid using type assertions, please refactor. */ this.client
.getThing() as unknown as Promise<void>
);
}
}
//// [commentEmitOnParenthesizedAssertionInReturnStatement.js]
export class Foo {
client = {
getThing: () => Promise.resolve('')
};
foo() {
return (
/* TODO: Avoid using type assertions, please refactor. */ this.client
.getThing());
}
}

View File

@ -0,0 +1,35 @@
//// [tests/cases/compiler/commentEmitOnParenthesizedAssertionInReturnStatement.ts] ////
=== commentEmitOnParenthesizedAssertionInReturnStatement.ts ===
export class Foo {
>Foo : Symbol(Foo, Decl(commentEmitOnParenthesizedAssertionInReturnStatement.ts, 0, 0))
client = {
>client : Symbol(Foo.client, Decl(commentEmitOnParenthesizedAssertionInReturnStatement.ts, 0, 18))
getThing: () => Promise.resolve('')
>getThing : Symbol(getThing, Decl(commentEmitOnParenthesizedAssertionInReturnStatement.ts, 1, 12))
>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(): Promise<void> {
>foo : Symbol(Foo.foo, Decl(commentEmitOnParenthesizedAssertionInReturnStatement.ts, 3, 3))
>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, --, --))
return (
/* TODO: Avoid using type assertions, please refactor. */ this.client
>this.client .getThing : Symbol(getThing, Decl(commentEmitOnParenthesizedAssertionInReturnStatement.ts, 1, 12))
>this.client : Symbol(Foo.client, Decl(commentEmitOnParenthesizedAssertionInReturnStatement.ts, 0, 18))
>this : Symbol(Foo, Decl(commentEmitOnParenthesizedAssertionInReturnStatement.ts, 0, 0))
>client : Symbol(Foo.client, Decl(commentEmitOnParenthesizedAssertionInReturnStatement.ts, 0, 18))
.getThing() as unknown as Promise<void>
>getThing : Symbol(getThing, Decl(commentEmitOnParenthesizedAssertionInReturnStatement.ts, 1, 12))
>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, --, --))
);
}
}

View File

@ -0,0 +1,42 @@
//// [tests/cases/compiler/commentEmitOnParenthesizedAssertionInReturnStatement.ts] ////
=== commentEmitOnParenthesizedAssertionInReturnStatement.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(): Promise<void> {
>foo : () => Promise<void>
return (
>( /* TODO: Avoid using type assertions, please refactor. */ this.client .getThing() as unknown as Promise<void> ) : Promise<void>
/* TODO: Avoid using type assertions, please refactor. */ this.client
>this.client .getThing() as unknown as Promise<void> : Promise<void>
>this.client .getThing() as unknown : unknown
>this.client .getThing() : Promise<string>
>this.client .getThing : () => Promise<string>
>this.client : { getThing: () => Promise<string>; }
>this : this
>client : { getThing: () => Promise<string>; }
.getThing() as unknown as Promise<void>
>getThing : () => Promise<string>
);
}
}

View File

@ -0,0 +1,17 @@
// @strict: true
// @lib: esnext
// @target: esnext
// @removeComments: false
export class Foo {
client = {
getThing: () => Promise.resolve('')
}
foo(): Promise<void> {
return (
/* TODO: Avoid using type assertions, please refactor. */ this.client
.getThing() as unknown as Promise<void>
);
}
}