Skip to content

Commit 07f3682

Browse files
committed
refactor and enforce one of the log functions
1 parent 0736cf4 commit 07f3682

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

plugins/eslint-plugin-aws-toolkits/lib/rules/no-json-stringify-in-log.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,18 @@ function isTemplateWithStringifyCall(node: TSESTree.CallExpressionArgument): boo
3636
*/
3737
function isLoggerCall(node: TSESTree.CallExpression): boolean {
3838
return (
39-
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
40-
node.callee.object.type === AST_NODE_TYPES.CallExpression &&
41-
node.callee.object.callee.type === AST_NODE_TYPES.Identifier &&
42-
node.callee.object.callee.name === 'getLogger') ||
43-
isDisguisedLoggerCall(node)
39+
node.callee.type === AST_NODE_TYPES.MemberExpression &&
40+
(isGetLoggerCall(node.callee.object) || isDisguisedGetLoggerCall(node.callee.object)) &&
41+
node.callee.property.type === AST_NODE_TYPES.Identifier &&
42+
['debug', 'verbose', 'info', 'warn', 'error'].includes(node.callee.property.name)
43+
)
44+
}
45+
46+
function isGetLoggerCall(node: TSESTree.Expression): boolean {
47+
return (
48+
node.type === AST_NODE_TYPES.CallExpression &&
49+
node.callee.type === AST_NODE_TYPES.Identifier &&
50+
node.callee.name === 'getLogger'
4451
)
4552
}
4653

@@ -53,15 +60,12 @@ function isLoggerCall(node: TSESTree.CallExpression): boolean {
5360
* 1) If the left side is an identifier including the word logger
5461
* 2) If the left side is a property of some object, including the word logger.
5562
*/
56-
function isDisguisedLoggerCall(node: TSESTree.CallExpression): boolean {
63+
function isDisguisedGetLoggerCall(node: TSESTree.Expression): boolean {
5764
return (
58-
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
59-
node.callee.object.type === AST_NODE_TYPES.Identifier &&
60-
node.callee.object.name.toLowerCase().includes('logger')) ||
61-
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
62-
node.callee.object.type === AST_NODE_TYPES.MemberExpression &&
63-
node.callee.object.property.type === AST_NODE_TYPES.Identifier &&
64-
node.callee.object.property.name.toLowerCase().includes('logger'))
65+
(node.type === AST_NODE_TYPES.Identifier && node.name.toLowerCase().includes('logger')) ||
66+
(node.type === AST_NODE_TYPES.MemberExpression &&
67+
node.property.type === AST_NODE_TYPES.Identifier &&
68+
node.property.name.toLowerCase().includes('logger'))
6569
)
6670
}
6771

plugins/eslint-plugin-aws-toolkits/test/rules/no-json-stringify-in-log.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ getRuleTester().run('no-json-stringify-in-log', rules['no-json-stringify-in-log'
1616
'getLogger().fakeFunction(`another example ${JSON.notString(something)}`)',
1717
'this.deps.devLogger?.debug("crashMonitoring: CLEAR_STATE: Succeeded")',
1818
'getLogger().debug(`called startBuilderIdSetup()`)',
19+
'this.logger.exit(`${JSON.stringify(obj)}`)',
1920
],
2021

2122
invalid: [

0 commit comments

Comments
 (0)