|
2 | 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | | -import { ESLintUtils, TSESTree } from '@typescript-eslint/utils' |
| 5 | +import { AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils' |
| 6 | +import { isLoggerCall } from './no-json-stringify-in-log' |
| 7 | +import { Rule } from 'eslint' |
| 8 | + |
| 9 | +/** |
| 10 | + * reuse solution done in logger itself: https://github.com/winstonjs/winston/blob/195e55c7e7fc58914ae4967ea7b832c9e0ced930/lib/winston/logger.js#L27 |
| 11 | + */ |
| 12 | +function countSubTokens(literalNode: TSESTree.StringLiteral) { |
| 13 | + const formatRegExp = /%[scdjifoO%]/g |
| 14 | + return literalNode.value.match(formatRegExp)?.length || 0 |
| 15 | +} |
| 16 | +/** |
| 17 | + * Form the error message using templates or actual values. |
| 18 | + * Allows us to avoid copy pasting message into test file. |
| 19 | + */ |
| 20 | +export function formErrorMsg(substitutionTokens: string | number, numArgs: string | number): string { |
| 21 | + return `String substitution has ${substitutionTokens} substitution tokens, but ${numArgs} arguments.` |
| 22 | +} |
6 | 23 |
|
7 | 24 | export default ESLintUtils.RuleCreator.withoutDocs({ |
8 | 25 | meta: { |
9 | 26 | docs: { |
10 | 27 | description: 'ensure string substitution args and templates match', |
11 | 28 | recommended: 'recommended', |
12 | 29 | }, |
13 | | - messages: {}, |
| 30 | + messages: { |
| 31 | + errMsg: formErrorMsg('{{ substitutionTokens }}', '{{ args }}'), |
| 32 | + }, |
14 | 33 | type: 'problem', |
15 | 34 | fixable: 'code', |
16 | 35 | schema: [], |
17 | 36 | }, |
18 | 37 | defaultOptions: [], |
19 | 38 | create(context) { |
20 | | - return {} |
| 39 | + return { |
| 40 | + CallExpression(node: TSESTree.CallExpression) { |
| 41 | + if ( |
| 42 | + isLoggerCall(node) && |
| 43 | + node.arguments[0].type === AST_NODE_TYPES.Literal && |
| 44 | + typeof node.arguments[0].value === 'string' |
| 45 | + ) { |
| 46 | + const numSubTokens = countSubTokens(node.arguments[0]) |
| 47 | + const numExtraArgs = node.arguments.length - 1 |
| 48 | + if (numSubTokens !== numExtraArgs) { |
| 49 | + return context.report({ |
| 50 | + node: node, |
| 51 | + data: { |
| 52 | + substitutionTokens: numSubTokens, |
| 53 | + args: numExtraArgs, |
| 54 | + }, |
| 55 | + messageId: 'errMsg', |
| 56 | + }) |
| 57 | + } |
| 58 | + } |
| 59 | + }, |
| 60 | + } |
21 | 61 | }, |
22 | | -}) |
| 62 | +}) as unknown as Rule.RuleModule |
0 commit comments