mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 12:32:08 -06:00
Fixed emit of return statements with parenthesized assertions and comments (#56601)
This commit is contained in:
parent
f57e5104a3
commit
1e00399a38
@ -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) {
|
||||
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
@ -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, --, --))
|
||||
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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>
|
||||
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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>
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user