From ceb01e092d75aa98f4c4ea576ae92ffa10c50f88 Mon Sep 17 00:00:00 2001 From: lonyele Date: Fri, 23 Aug 2019 15:50:35 +0900 Subject: [PATCH 1/2] feat: add allowAny options to restric-plus-operands rule --- .../src/rules/restrict-plus-operands.ts | 34 +++++++++++--- .../rules/restrict-plus-operands.test.ts | 47 +++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts index 1f715f1f9022..8ebb2e366911 100644 --- a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts +++ b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts @@ -2,7 +2,15 @@ import { TSESTree } from '@typescript-eslint/experimental-utils'; import ts from 'typescript'; import * as util from '../util'; -export default util.createRule({ +type Options = [ + { + allowAny?: boolean; + }, +]; + +type MessageId = 'notStrings' | 'notBigInts' | 'notNumbers'; + +export default util.createRule({ name: 'restrict-plus-operands', meta: { type: 'problem', @@ -20,15 +28,24 @@ export default util.createRule({ "Operands of '+' operation must either be both strings or both numbers. Consider using a template literal.", notBigInts: "Operands of '+' operation must be both bigints.", }, - schema: [], + schema: [ + { + type: 'object', + properties: { + allowAny: { + type: 'boolean', + }, + }, + }, + ], }, - defaultOptions: [], - create(context) { + defaultOptions: [{ allowAny: false }], + create(context, [option]) { const service = util.getParserServices(context); const typeChecker = service.program.getTypeChecker(); - type BaseLiteral = 'string' | 'number' | 'bigint' | 'invalid'; + type BaseLiteral = 'string' | 'number' | 'bigint' | 'invalid' | 'any'; /** * Helper function to get base type of node @@ -65,7 +82,8 @@ export default util.createRule({ if ( stringType === 'number' || stringType === 'string' || - stringType === 'bigint' + stringType === 'bigint' || + stringType === 'any' ) { return stringType; } @@ -88,6 +106,10 @@ export default util.createRule({ const leftType = getNodeType(node.left); const rightType = getNodeType(node.right); + if (option.allowAny && (leftType === 'any' || rightType === 'any')) { + return; + } + if ( leftType === 'invalid' || rightType === 'invalid' || diff --git a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts index 44583a202a1d..c124d2aa9405 100644 --- a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts @@ -81,6 +81,36 @@ function foo(a: T) { return a + 1; } `, + { + code: ` +export const f = (a: any, b: any) => a + b; + `, + options: [ + { + allowAny: true, + }, + ], + }, + { + code: ` +export const f = (a: string, b: any) => a + b; + `, + options: [ + { + allowAny: true, + }, + ], + }, + { + code: ` +export const f = (a: boolean, b: any) => a + b; + `, + options: [ + { + allowAny: true, + }, + ], + }, ], invalid: [ { @@ -383,5 +413,22 @@ function foo(a: T) { }, ], }, + { + code: ` +export const f = (a: string, b: number) => a + b; + `, + errors: [ + { + messageId: 'notStrings', + line: 2, + column: 44, + }, + ], + options: [ + { + allowAny: true, + }, + ], + }, ], }); From 40e64194d3ffe8123883920181ae6c54d1afb803 Mon Sep 17 00:00:00 2001 From: lonyele Date: Sat, 24 Aug 2019 09:21:55 +0900 Subject: [PATCH 2/2] docs: add about allowAny option --- .../docs/rules/restrict-plus-operands.md | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/restrict-plus-operands.md b/packages/eslint-plugin/docs/rules/restrict-plus-operands.md index 1efffa83e69e..77353b74eced 100644 --- a/packages/eslint-plugin/docs/rules/restrict-plus-operands.md +++ b/packages/eslint-plugin/docs/rules/restrict-plus-operands.md @@ -16,12 +16,34 @@ var foo = 1n + 1; ## Options +Options may be provided as an object with: + +- `allowAny` to ignore this rule when either left or + right of plus operand is a type `any` + ```json { - "@typescript-eslint/restrict-plus-operands": "error" + "@typescript-eslint/estrict-plus-operands": [ + "error", + { + "allowAny": true + } + ] } ``` +### allowAny + +If the rule is too strict then making this option `true` +can be a help. Though It is not recommended since lint errors are potentially a real runtime errors in many cases. + +Examples of **correct** code for this rule with `{ allowAny: true`: + +```ts +const x = (a: any, b: string) => a + b; +const y = (a: boolean, b: any) => a + b; +``` + ## Compatibility - TSLint: [restrict-plus-operands](https://palantir.github.io/tslint/rules/restrict-plus-operands/)