Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [restrict-plus-operands] add allow* options#6161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
JoshuaKGoldberg merged 6 commits intotypescript-eslint:mainfromJoshuaKGoldberg:restrict-plus-operands-allow-optionsJun 15, 2023
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
548b7df feat(eslint-plugin): [restrict-plus-operands] add allow* options from…
JoshuaKGoldberg458ccbf Warn explicitly
JoshuaKGoldberge7eeb6c Merge branch 'main' into restrict-plus-operands-allow-options
JoshuaKGoldberg288af7c Update packages/eslint-plugin/docs/rules/restrict-plus-operands.md
JoshuaKGoldberg584205a Brad suggestion
JoshuaKGoldbergb78d9bb Fix lint-markdown
JoshuaKGoldbergFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
160 changes: 135 additions & 25 deletionspackages/eslint-plugin/docs/rules/restrict-plus-operands.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,70 +18,180 @@ This rule reports when a `+` operation combines two values of different types, o | ||
| ### ❌ Incorrect | ||
| ```ts | ||
| let foo = '5.5' + 5; | ||
| let foo = 1n + 1; | ||
| ``` | ||
| ### ✅ Correct | ||
| ```ts | ||
| let foo = parseInt('5.5', 10) + 10; | ||
| let foo = 1n + 1n; | ||
| ``` | ||
| ## Options | ||
| :::caution | ||
| We generally recommend against using these options, as they limit which varieties of incorrect `+` usage can be checked. | ||
| This in turn severely limits the validation that the rule can do to ensure that resulting strings and numbers are correct. | ||
| Safer alternatives to using the `allow*` options include: | ||
| - Using variadic forms of logging APIs to avoid needing to `+` values. | ||
| ```ts | ||
| // Remove this line | ||
| console.log('The result is ' + true); | ||
| // Add this line | ||
| console.log('The result is', true); | ||
| ``` | ||
| - Using `.toFixed()` to coerce numbers to well-formed string representations: | ||
| ```ts | ||
| const number = 1.123456789; | ||
| const result = 'The number is ' + number.toFixed(2); | ||
| // result === 'The number is 1.12' | ||
| ``` | ||
| - Calling `.toString()` on other types to mark explicit and intentional string coercion: | ||
| ```ts | ||
| const arg = '11'; | ||
| const regex = /[0-9]/; | ||
| const result = | ||
| 'The result of ' + | ||
| regex.toString() + | ||
| '.test("' + | ||
| arg + | ||
| '") is ' + | ||
| regex.test(arg).toString(); | ||
| // result === 'The result of /[0-9]/.test("11") is true' | ||
| ``` | ||
| ::: | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ### `allowAny` | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Examples of code for this rule with `{ allowAny: true }`: | ||
| <!--tabs--> | ||
| #### ❌ Incorrect | ||
| ```ts | ||
| let fn = (a: number, b: []) => a + b; | ||
| let fn = (a: string, b: []) => a + b; | ||
| ``` | ||
| #### ✅ Correct | ||
| ```ts | ||
| let fn = (a: number, b: any) => a + b; | ||
| let fn = (a: string, b: any) => a + b; | ||
| ``` | ||
| ### `allowBoolean` | ||
| Examples of code for this rule with `{ allowBoolean: true }`: | ||
| <!--tabs--> | ||
| #### ❌ Incorrect | ||
| ```ts | ||
| let fn = (a: number, b: unknown) => a + b; | ||
| let fn = (a: string, b: unknown) => a + b; | ||
| ``` | ||
| #### ✅ Correct | ||
| ```ts | ||
| let fn = (a: number, b: boolean) => a + b; | ||
| let fn = (a: string, b: boolean) => a + b; | ||
| ``` | ||
| ### `allowNullish` | ||
| Examples of code for this rule with `{ allowNullish: true }`: | ||
| <!--tabs--> | ||
| #### ❌ Incorrect | ||
| ```ts | ||
| let fn = (a: number, b: unknown) => a + b; | ||
| let fn = (a: number, b: never) => a + b; | ||
| let fn = (a: string, b: unknown) => a + b; | ||
| let fn = (a: string, b: never) => a + b; | ||
| ``` | ||
| #### ✅ Correct | ||
| ```ts | ||
| let fn = (a: number, b: undefined) => a + b; | ||
| let fn = (a: number, b: null) => a + b; | ||
| let fn = (a: string, b: undefined) => a + b; | ||
| let fn = (a: string, b: null) => a + b; | ||
| ``` | ||
| ### `allowNumberAndString` | ||
| Examples of code for this rule with `{ allowNumberAndString: true }`: | ||
| <!--tabs--> | ||
| #### ❌ Incorrect | ||
| ```ts | ||
| let fn = (a: number, b: unknown) => a + b; | ||
| let fn = (a: number, b: never) => a + b; | ||
| ``` | ||
| #### ✅ Correct | ||
| ```ts | ||
| let fn = (a: number, b: string) => a + b; | ||
| let fn = (a: number, b: number | string) => a + b; | ||
| ``` | ||
| ### `allowRegExp` | ||
| Examples of code for this rule with `{ allowRegExp: true }`: | ||
| <!--tabs--> | ||
| #### ❌ Incorrect | ||
| ```ts | ||
| let fn = (a: number, b: RegExp) => a + b; | ||
| ``` | ||
| #### ✅ Correct | ||
| ```ts | ||
| let fn = (a: string, b: RegExp) => a + b; | ||
| ``` | ||
| ### `checkCompoundAssignments` | ||
| Examples of code for this rule with `{ checkCompoundAssignments: true }`: | ||
| <!--tabs--> | ||
| #### ❌ Incorrect | ||
| ```ts | ||
| let foo: string | undefined; | ||
| foo += 'some data'; | ||
| let bar: string = ''; | ||
| bar += 0; | ||
| ``` | ||
| #### ✅ Correct | ||
| ```ts | ||
| let foo: number = 0; | ||
| foo += 1; | ||
| let bar = ''; | ||
| bar += 'test'; | ||
| ``` | ||
| ## When Not To Use It | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.