Skip to content

Commit ad91cf3

Browse files
hayemaxijustinmk3
authored andcommitted
lint: disallow console.log and similar
1 parent f4487ce commit ad91cf3

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ module.exports = {
159159
'aws-toolkits/no-banned-usages': 'error',
160160
'aws-toolkits/no-incorrect-once-usage': 'error',
161161
'aws-toolkits/no-string-exec-for-child-process': 'error',
162+
'aws-toolkits/no-console-log': 'error',
162163

163164
'no-restricted-imports': [
164165
'error',

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ The package.json 'devDependencies' includes `eslint-plugin-aws-toolkits`. This i
477477
3. Register your rule in `plugins/eslint-plugin-aws-toolkits/index.ts`.
478478
4. Enable your rule in `.eslintrc`.
479479

480+
Writing lint rules can be tricky if you are unfamiliar with the process. Use an AST viewer such as https://astexplorer.net/
481+
480482
### AWS SDK generator
481483

482484
When the AWS SDK does not (yet) support a service but you have an API

plugins/eslint-plugin-aws-toolkits/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import NoBannedUsages from './lib/rules/no-banned-usages'
88
import NoIncorrectOnceUsage from './lib/rules/no-incorrect-once-usage'
99
import NoOnlyInTests from './lib/rules/no-only-in-tests'
1010
import NoStringExecForChildProcess from './lib/rules/no-string-exec-for-child-process'
11+
import NoConsoleLog from './lib/rules/no-console-log'
1112

1213
const rules = {
1314
'no-await-on-vscode-msg': NoAwaitOnVscodeMsg,
1415
'no-banned-usages': NoBannedUsages,
1516
'no-incorrect-once-usage': NoIncorrectOnceUsage,
1617
'no-only-in-tests': NoOnlyInTests,
1718
'no-string-exec-for-child-process': NoStringExecForChildProcess,
19+
'no-console-log': NoConsoleLog,
1820
}
1921

2022
export { rules }
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/**
7+
* Enforce usage of our getLogger() function instead of using node's built in console.log() and similar.
8+
* An eslint rule already exists for this (https://eslint.org/docs/latest/rules/no-console), but this
9+
* rule is trivial to implement and we can add our own error message, so here it is.
10+
*/
11+
12+
import { ESLintUtils, TSESTree } from '@typescript-eslint/utils'
13+
import { AST_NODE_TYPES } from '@typescript-eslint/types'
14+
import { Rule } from 'eslint'
15+
16+
export const errMsg =
17+
'do not use console.log, console.warn, or console.err, or similar; instead, use `getLogger().info`, `getLogger().warn`, etc; disable this rule for files that cannot import getLogger()'
18+
19+
const disallowFunctions = new Set(['info', 'debug', 'error', 'warn', 'log'])
20+
21+
export default ESLintUtils.RuleCreator.withoutDocs({
22+
meta: {
23+
docs: {
24+
description: 'disallow use of console.log() to encourage use of getLogger()',
25+
recommended: 'recommended',
26+
},
27+
messages: {
28+
errMsg,
29+
},
30+
type: 'problem',
31+
fixable: 'code',
32+
schema: [],
33+
},
34+
defaultOptions: [],
35+
create(context) {
36+
return {
37+
MemberExpression(node: TSESTree.MemberExpression) {
38+
// Exception for anything that isn't extension code (in a src/ folder) (e.g. build scripts, tests)
39+
if (context.physicalFilename.includes('test') || !context.physicalFilename.includes('src')) {
40+
return
41+
}
42+
43+
// Validate this node
44+
if (
45+
node.object.type !== AST_NODE_TYPES.Identifier ||
46+
node.object.name !== 'console' ||
47+
node.property.type !== AST_NODE_TYPES.Identifier
48+
) {
49+
return
50+
}
51+
52+
if (disallowFunctions.has(node.property.name)) {
53+
return context.report({
54+
node,
55+
messageId: 'errMsg',
56+
})
57+
}
58+
},
59+
}
60+
},
61+
}) as unknown as Rule.RuleModule
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { rules } from '../../index'
7+
import { errMsg } from '../../lib/rules/no-console-log'
8+
import { getRuleTester } from '../testUtil'
9+
10+
getRuleTester().run('no-console-log', rules['no-console-log'], {
11+
valid: [
12+
{
13+
code: "getLogger().warn('my message')",
14+
filename: '/src/client.ts',
15+
},
16+
{
17+
code: "console.log('hi')",
18+
filename: '/scripts/build.ts',
19+
},
20+
{
21+
code: "console.log('hi')",
22+
filename: '/src/test/client.test.ts',
23+
},
24+
],
25+
invalid: [
26+
{
27+
code: "console.log('test')",
28+
filename: '/src/client.ts',
29+
errors: [errMsg],
30+
},
31+
{
32+
code: "console.warn('test')",
33+
filename: '/src/client.ts',
34+
errors: [errMsg],
35+
},
36+
{
37+
code: "console.error('test')",
38+
filename: '/src/client.ts',
39+
errors: [errMsg],
40+
},
41+
{
42+
code: "console.debug('test')",
43+
filename: '/src/client.ts',
44+
errors: [errMsg],
45+
},
46+
{
47+
code: "console.info('test')",
48+
filename: '/src/client.ts',
49+
errors: [errMsg],
50+
},
51+
],
52+
})

0 commit comments

Comments
 (0)