-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: add no-unsafe-unary-minus
rule
#7390
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d114b9a
feat: add `no-unsafe-unary-minus` rule
samestep ff17b9e
Cover the early return case
samestep fae54f6
Merge branch 'main' into unary-minus
samestep 3ff8dae
Write more tests
samestep 1d431f8
Rewrite to use only public TypeScript API
samestep 9dc578e
Merge branch 'main' into unary-minus
samestep bc0430f
Handle `any`, `never`, and generics
samestep 86b634a
Replace functions with `declare` in docs
samestep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
packages/eslint-plugin/docs/rules/no-unsafe-unary-minus.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
description: 'Require unary negation to take a number.' | ||
--- | ||
|
||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/no-unsafe-unary-minus** for documentation. | ||
|
||
TypeScript does not prevent you from putting a minus sign before things other than numbers: | ||
|
||
```ts | ||
const s = 'hello'; | ||
const x = -s; // x is NaN | ||
``` | ||
|
||
This rule restricts the unary `-` operator to `number | bigint`. | ||
|
||
## Examples | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
declare const a: string; | ||
-a; | ||
|
||
declare const b: {}; | ||
-b; | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
```ts | ||
-42; | ||
-42n; | ||
|
||
declare const a: number; | ||
-a; | ||
|
||
declare const b: number; | ||
-b; | ||
|
||
declare const c: number | bigint; | ||
-c; | ||
|
||
declare const d: any; | ||
-d; | ||
|
||
declare const e: 1 | 2; | ||
-e; | ||
``` |
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
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
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as tsutils from 'ts-api-utils'; | ||
import * as ts from 'typescript'; | ||
|
||
import * as util from '../util'; | ||
|
||
type Options = []; | ||
type MessageIds = 'unaryMinus'; | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'no-unsafe-unary-minus', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Require unary negation to take a number', | ||
requiresTypeChecking: true, | ||
}, | ||
messages: { | ||
unaryMinus: 'Invalid type "{{type}}" of template literal expression.', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
UnaryExpression(node): void { | ||
if (node.operator !== '-') { | ||
return; | ||
} | ||
const services = util.getParserServices(context); | ||
const argType = util.getConstrainedTypeAtLocation( | ||
services, | ||
node.argument, | ||
); | ||
const checker = services.program.getTypeChecker(); | ||
if ( | ||
tsutils | ||
.unionTypeParts(argType) | ||
.some( | ||
type => | ||
!tsutils.isTypeFlagSet( | ||
type, | ||
ts.TypeFlags.Any | | ||
ts.TypeFlags.Never | | ||
ts.TypeFlags.BigIntLike | | ||
ts.TypeFlags.NumberLike, | ||
), | ||
) | ||
) { | ||
context.report({ | ||
messageId: 'unaryMinus', | ||
node, | ||
data: { type: checker.typeToString(argType) }, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
47 changes: 47 additions & 0 deletions
47
packages/eslint-plugin/tests/rules/no-unsafe-unary-minus.test.ts
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
|
||
import rule from '../../src/rules/no-unsafe-unary-minus'; | ||
import { getFixturesRootDir } from '../RuleTester'; | ||
|
||
const rootDir = getFixturesRootDir(); | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2015, | ||
tsconfigRootDir: rootDir, | ||
project: './tsconfig.json', | ||
}, | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no-unsafe-unary-minus', rule, { | ||
valid: [ | ||
'+42;', | ||
'-42;', | ||
'-42n;', | ||
'(a: number) => -a;', | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'(a: bigint) => -a;', | ||
'(a: number | bigint) => -a;', | ||
'(a: any) => -a;', | ||
'(a: 1 | 2) => -a;', | ||
'(a: string) => +a;', | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'(a: number[]) => -a[0];', | ||
'<T,>(t: T & number) => -t;', | ||
'(a: { x: number }) => -a.x;', | ||
'(a: never) => -a;', | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'<T extends number>(t: T) => -t;', | ||
], | ||
samestep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
invalid: [ | ||
{ code: '(a: string) => -a;', errors: [{ messageId: 'unaryMinus' }] }, | ||
{ code: '(a: {}) => -a;', errors: [{ messageId: 'unaryMinus' }] }, | ||
{ code: '(a: number[]) => -a;', errors: [{ messageId: 'unaryMinus' }] }, | ||
{ code: "-'hello';", errors: [{ messageId: 'unaryMinus' }] }, | ||
{ code: '-`hello`;', errors: [{ messageId: 'unaryMinus' }] }, | ||
{ | ||
code: '(a: { x: number }) => -a;', | ||
errors: [{ messageId: 'unaryMinus' }], | ||
}, | ||
{ code: '(a: unknown) => -a;', errors: [{ messageId: 'unaryMinus' }] }, | ||
{ code: '(a: void) => -a;', errors: [{ messageId: 'unaryMinus' }] }, | ||
{ code: '<T,>(t: T) => -t;', errors: [{ messageId: 'unaryMinus' }] }, | ||
], | ||
}); |
14 changes: 14 additions & 0 deletions
14
packages/eslint-plugin/tests/schema-snapshots/no-unsafe-unary-minus.shot
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.