Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
no-unsafe-unary-minus
rule (#7390)
* feat: add `no-unsafe-unary-minus` rule * Cover the early return case * Write more tests * Rewrite to use only public TypeScript API * Handle `any`, `never`, and generics * Replace functions with `declare` in docs
- Loading branch information
Showing
6 changed files
with
172 additions
and
0 deletions.
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 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 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 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 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 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;', | ||
'(a: bigint) => -a;', | ||
'(a: number | bigint) => -a;', | ||
'(a: any) => -a;', | ||
'(a: 1 | 2) => -a;', | ||
'(a: string) => +a;', | ||
'(a: number[]) => -a[0];', | ||
'<T,>(t: T & number) => -t;', | ||
'(a: { x: number }) => -a.x;', | ||
'(a: never) => -a;', | ||
'<T extends number>(t: T) => -t;', | ||
], | ||
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.