Skip to content

Commit eb08f70

Browse files
committed
Merge branch 'lint/noJsonStringify' into lint/noStringSubDrop
2 parents a391f69 + 07f3682 commit eb08f70

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils'
77
import { Rule } from 'eslint'
88

9-
export const errMsg = 'Avoid using JSON.stringify within logging and error messages, prefer %O.'
9+
export const errMsg =
10+
'Avoid using JSON.stringify within logging and error messages, prefer %O. Note: %O has a depth limit of 2'
1011

1112
/**
1213
* Check if a given expression is a JSON.stringify call.
@@ -35,11 +36,18 @@ function isTemplateWithStringifyCall(node: TSESTree.CallExpressionArgument): boo
3536
*/
3637
export function isLoggerCall(node: TSESTree.CallExpression): boolean {
3738
return (
38-
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
39-
node.callee.object.type === AST_NODE_TYPES.CallExpression &&
40-
node.callee.object.callee.type === AST_NODE_TYPES.Identifier &&
41-
node.callee.object.callee.name === 'getLogger') ||
42-
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'
4351
)
4452
}
4553

@@ -52,15 +60,12 @@ export function isLoggerCall(node: TSESTree.CallExpression): boolean {
5260
* 1) If the left side is an identifier including the word logger
5361
* 2) If the left side is a property of some object, including the word logger.
5462
*/
55-
function isDisguisedLoggerCall(node: TSESTree.CallExpression): boolean {
63+
function isDisguisedGetLoggerCall(node: TSESTree.Expression): boolean {
5664
return (
57-
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
58-
node.callee.object.type === AST_NODE_TYPES.Identifier &&
59-
node.callee.object.name.toLowerCase().includes('logger')) ||
60-
(node.callee.type === AST_NODE_TYPES.MemberExpression &&
61-
node.callee.object.type === AST_NODE_TYPES.MemberExpression &&
62-
node.callee.object.property.type === AST_NODE_TYPES.Identifier &&
63-
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'))
6469
)
6570
}
6671

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)