Skip to content

Commit 0447472

Browse files
authored
Add comment with warning when removing .promise() from untested cases (#397)
1 parent a024eeb commit 0447472

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

.changeset/mean-actors-give.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": minor
3+
---
4+
5+
Add comment with warning when removing .promise() from untested cases

src/transforms/v2-to-v3/apis/removePromiseCalls.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const removePromiseCalls = (
3636
},
3737
})
3838
.forEach((callExpression) => {
39-
removePromiseForCallExpression(callExpression);
39+
removePromiseForCallExpression(j, callExpression);
4040
});
4141

4242
// Remove .promise() from client API request stored in a variable.
@@ -64,7 +64,7 @@ export const removePromiseCalls = (
6464
},
6565
})
6666
.forEach((callExpression) => {
67-
removePromiseForCallExpression(callExpression);
67+
removePromiseForCallExpression(j, callExpression);
6868
});
6969
});
7070
}
Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1-
import { ASTPath, CallExpression, MemberExpression } from "jscodeshift";
2-
import { print } from "recast";
1+
import { ASTPath, CallExpression, JSCodeshift, MemberExpression } from "jscodeshift";
2+
import { emitWarning } from "process";
33

4-
export const removePromiseForCallExpression = (callExpression: ASTPath<CallExpression>) => {
4+
export const removePromiseForCallExpression = (
5+
j: JSCodeshift,
6+
callExpression: ASTPath<CallExpression>
7+
) => {
58
switch (callExpression.parentPath.value.type) {
69
case "MemberExpression": {
710
callExpression.parentPath.value.object = (
811
callExpression.value.callee as MemberExpression
912
).object;
1013
break;
1114
}
15+
default: {
16+
emitWarning(
17+
`Removal of .promise() not implemented for parentPath: ${callExpression.parentPath.value.type}\n` +
18+
`Code processed: ${j(callExpression.parentPath).toSource()}\n\n` +
19+
"Please report your use case on https://github.com/awslabs/aws-sdk-js-codemod\n"
20+
);
21+
const comments = callExpression.parentPath.node.comments || [];
22+
comments.push(
23+
j.commentLine(
24+
" The promise() call was removed by aws-sdk-js-codemod v2-to-v3 transform using best guess."
25+
)
26+
);
27+
comments.push(
28+
j.commentLine(
29+
" Please check that it is correct, and create an issue on GitHub to report this use case."
30+
)
31+
);
32+
callExpression.parentPath.node.comments = comments;
33+
}
34+
// eslint-disable-next-line no-fallthrough
1235
case "ArrowFunctionExpression":
1336
case "AwaitExpression":
1437
case "ExpressionStatement":
@@ -23,11 +46,5 @@ export const removePromiseForCallExpression = (callExpression: ASTPath<CallExpre
2346
callExpression.value.callee = currentCalleeObject.callee;
2447
break;
2548
}
26-
default:
27-
throw new Error(
28-
`Removal of .promise() not implemented for parentPath: ${callExpression.parentPath.value.type}\n` +
29-
`Code processed: ${print(callExpression.parentPath.node).code}\n\n` +
30-
"Please report your use case on https://github.com/awslabs/aws-sdk-js-codemod\n"
31-
);
3249
}
3350
};

0 commit comments

Comments
 (0)