File tree 3 files changed +40
-6
lines changed
3 files changed +40
-6
lines changed Original file line number Diff line number Diff line change @@ -27,12 +27,34 @@ namespace ts.codefix {
27
27
}
28
28
29
29
const parenthesizedExpression = tryCast ( awaitExpression . parent , isParenthesizedExpression ) ;
30
- // (await 0).toFixed() should keep its parens (or add an extra dot for 0..toFixed())
31
- if ( parenthesizedExpression && isPropertyAccessExpression ( parenthesizedExpression . parent ) && isDecimalIntegerLiteral ( awaitExpression . expression ) ) {
30
+ const preserveParens = parenthesizedExpression && (
31
+ // (await 0).toFixed() should keep its parens (or add an extra dot for 0..toFixed())
32
+ isPropertyAccessExpression ( parenthesizedExpression . parent ) && isDecimalIntegerLiteral ( awaitExpression . expression , sourceFile ) ||
33
+ // new (await c).Class()
34
+ isPropertyAccessExpressionInNewExpression ( parenthesizedExpression . parent ) ||
35
+ // (await new C).foo
36
+ isNewExpressionWithoutParens ( awaitExpression . expression )
37
+ ) ;
38
+
39
+ if ( preserveParens ) {
32
40
changeTracker . replaceNode ( sourceFile , awaitExpression , awaitExpression . expression ) ;
33
41
}
34
42
else {
35
43
changeTracker . replaceNode ( sourceFile , parenthesizedExpression || awaitExpression , awaitExpression . expression ) ;
36
44
}
37
45
}
46
+
47
+ function isPropertyAccessExpressionInNewExpression ( expression : Node ) {
48
+ return isPropertyAccessExpression ( expression ) && ! ! findAncestor ( expression , ancestor => {
49
+ return isPropertyAccessExpression ( ancestor )
50
+ ? false
51
+ : isNewExpression ( ancestor )
52
+ ? true
53
+ : "quit" ;
54
+ } ) ;
55
+ }
56
+
57
+ function isNewExpressionWithoutParens ( expression : Node ) {
58
+ return isNewExpression ( expression ) && expression . getLastToken ( ) === expression . expression ;
59
+ }
38
60
}
Original file line number Diff line number Diff line change @@ -1992,8 +1992,8 @@ namespace ts {
1992
1992
return typeIsAccessible ? res : undefined ;
1993
1993
}
1994
1994
1995
- export function isDecimalIntegerLiteral ( node : Node ) : node is NumericLiteral {
1996
- return isNumericLiteral ( node ) && / ^ \d + $ / . test ( node . text ) ;
1995
+ export function isDecimalIntegerLiteral ( node : Node , sourceFile : SourceFile ) : node is NumericLiteral {
1996
+ return isNumericLiteral ( node ) && / ^ \d + $ / . test ( node . getText ( sourceFile ) ) ;
1997
1997
}
1998
1998
1999
1999
export function syntaxUsuallyHasTrailingSemicolon ( kind : SyntaxKind ) {
Original file line number Diff line number Diff line change 1
1
/// <reference path="fourslash.ts" />
2
+ ////declare class C { foo(): void }
3
+ ////declare function getC(): { foo: { Class: C } }
2
4
////async function f() {
3
5
//// await "";
4
6
//// await 0;
5
7
//// (await "").toLowerCase();
6
8
//// (await 0).toFixed();
7
9
//// (await 3.2).toFixed();
10
+ //// (await new C).foo();
11
+ //// new (await getC()).foo.Class();
8
12
//// }
9
13
10
14
verify . codeFix ( {
11
15
description : ts . Diagnostics . Remove_unnecessary_await . message ,
12
16
index : 0 ,
13
17
newFileContent :
14
- `async function f() {
18
+ `declare class C { foo(): void }
19
+ declare function getC(): { foo: { Class: C } }
20
+ async function f() {
15
21
"";
16
22
await 0;
17
23
(await "").toLowerCase();
18
24
(await 0).toFixed();
19
25
(await 3.2).toFixed();
26
+ (await new C).foo();
27
+ new (await getC()).foo.Class();
20
28
}`
21
29
} ) ;
22
30
23
31
verify . codeFixAll ( {
24
32
fixAllDescription : ts . Diagnostics . Remove_all_unnecessary_uses_of_await . message ,
25
33
fixId : "removeUnnecessaryAwait" ,
26
34
newFileContent :
27
- `async function f() {
35
+ `declare class C { foo(): void }
36
+ declare function getC(): { foo: { Class: C } }
37
+ async function f() {
28
38
"";
29
39
0;
30
40
"".toLowerCase();
31
41
(0).toFixed();
32
42
3.2.toFixed();
43
+ (new C).foo();
44
+ new (getC()).foo.Class();
33
45
}`
34
46
} ) ;
You can’t perform that action at this time.
0 commit comments