File tree 2 files changed +38
-2
lines changed
2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -153,8 +153,15 @@ export default util.createRule({
153
153
function insertAwait (
154
154
fixer : TSESLint . RuleFixer ,
155
155
node : TSESTree . Expression ,
156
- ) : TSESLint . RuleFix | null {
157
- return fixer . insertTextBefore ( node , 'await ' ) ;
156
+ ) : TSESLint . RuleFix | TSESLint . RuleFix [ ] {
157
+ if ( node . type !== AST_NODE_TYPES . TSAsExpression ) {
158
+ return fixer . insertTextBefore ( node , 'await ' ) ;
159
+ }
160
+
161
+ return [
162
+ fixer . insertTextBefore ( node , 'await (' ) ,
163
+ fixer . insertTextAfter ( node , ')' ) ,
164
+ ] ;
158
165
}
159
166
160
167
function test ( node : TSESTree . Expression , expression : ts . Node ) : void {
Original file line number Diff line number Diff line change @@ -842,6 +842,35 @@ const buzz = async () => ((await foo()) ? 1 : await bar());
842
842
} ,
843
843
] ,
844
844
} ,
845
+ {
846
+ // https://github.com/typescript-eslint/typescript-eslint/issues/2109
847
+ code : `
848
+ async function test<T>(): Promise<T> {
849
+ const res = await fetch('...');
850
+ try {
851
+ return res.json() as Promise<T>;
852
+ } catch (err) {
853
+ throw Error('Request Failed.');
854
+ }
855
+ }
856
+ ` ,
857
+ output : `
858
+ async function test<T>(): Promise<T> {
859
+ const res = await fetch('...');
860
+ try {
861
+ return await (res.json() as Promise<T>);
862
+ } catch (err) {
863
+ throw Error('Request Failed.');
864
+ }
865
+ }
866
+ ` ,
867
+ errors : [
868
+ {
869
+ line : 5 ,
870
+ messageId : 'requiredPromiseAwait' ,
871
+ } ,
872
+ ] ,
873
+ } ,
845
874
{
846
875
code : `
847
876
async function test() {
You can’t perform that action at this time.
0 commit comments