Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add rule no-unsafe-assignment
- Loading branch information
1 parent
e5db36f
commit ef6a841
Showing
9 changed files
with
349 additions
and
6 deletions.
There are no files selected for viewing
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,59 @@ | ||
# Disallows returning any from a function (`no-unsafe-return`) | ||
|
||
Despite your best intentions, the `any` type can sometimes leak into your codebase. | ||
Assigning an `any` typed value to a variable can be hard to pick up on, particularly if it leaks in from an external library. Operations on the variable will not checked at all by TypeScript, so it creates a potential safety hole, and source of bugs in your codebase. | ||
|
||
## Rule Details | ||
|
||
This rule disallows the assigning `any` to a variable, and assigning `any[]` to an array destructuring. | ||
This rule also compares the assigned type to the variable's declared/inferred return type to ensure you don't return an unsafe `any` in a generic position to a receiver that's expecting a specific type. For example, it will error if you return `Set<any>` from a function declared as returning `Set<string>`. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
const x = 1 as any, | ||
y = 1 as any; | ||
const [x] = 1 as any; | ||
const [x] = [] as any[]; | ||
|
||
function foo(a = 1 as any) {} | ||
class Foo { | ||
constructor(private a = 1 as any) {} | ||
} | ||
class Foo { | ||
private a = 1 as any; | ||
} | ||
|
||
// generic position examples | ||
const x: Set<string> = new Set<any>(); | ||
const x: Map<string, string> = new Map<string, any>(); | ||
const x: Set<string[]> = new Set<any[]>(); | ||
const x: Set<Set<Set<string>>> = new Set<Set<Set<any>>>(); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
const x = 1, | ||
y = 1; | ||
const [x] = [1]; | ||
|
||
function foo(a = 1) {} | ||
class Foo { | ||
constructor(private a = 1) {} | ||
} | ||
class Foo { | ||
private a = 1; | ||
} | ||
|
||
// generic position examples | ||
const x: Set<string> = new Set<string>(); | ||
const x: Map<string, string> = new Map<string, string>(); | ||
const x: Set<string[]> = new Set<string[]>(); | ||
const x: Set<Set<Set<string>>> = new Set<Set<Set<string>>>(); | ||
``` | ||
|
||
## Related to | ||
|
||
- [`no-explicit-any`](./no-explicit-any.md) | ||
- TSLint: [`no-unsafe-any`](https://palantir.github.io/tslint/rules/no-unsafe-any/) |
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
133 changes: 133 additions & 0 deletions
133
packages/eslint-plugin/src/rules/no-unsafe-assignment.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,133 @@ | ||
import { | ||
TSESTree, | ||
AST_NODE_TYPES, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import * as util from '../util'; | ||
|
||
const enum ComparisonType { | ||
/** Do no assignment comparison */ | ||
None, | ||
/** Use the receiver's type for comparison */ | ||
Basic, | ||
/** Use the sender's contextual type for comparison */ | ||
Contextual, | ||
} | ||
|
||
export default util.createRule({ | ||
name: 'no-unsafe-assignment', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallows assigning any to variables and properties', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
requiresTypeChecking: true, | ||
}, | ||
messages: { | ||
anyAssignment: 'Unsafe assignment of an any value', | ||
unsafeArrayPattern: 'Unsafe array destructuring of an any array value', | ||
unsafeAssignment: | ||
'Unsafe asignment of type {{sender}} to a variable of type {{receiver}}', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const { program, esTreeNodeToTSNodeMap } = util.getParserServices(context); | ||
const checker = program.getTypeChecker(); | ||
|
||
function checkAssignment( | ||
receiverNode: TSESTree.Node, | ||
senderNode: TSESTree.Node, | ||
reportingNode: TSESTree.Node, | ||
comparisonType: ComparisonType, | ||
): void { | ||
const receiverType = checker.getTypeAtLocation( | ||
esTreeNodeToTSNodeMap.get(receiverNode), | ||
); | ||
const senderType = checker.getTypeAtLocation( | ||
esTreeNodeToTSNodeMap.get(senderNode), | ||
); | ||
|
||
if (util.isTypeAnyType(senderType)) { | ||
return context.report({ | ||
node: reportingNode, | ||
messageId: 'anyAssignment', | ||
}); | ||
} | ||
|
||
if ( | ||
receiverNode.type === AST_NODE_TYPES.ArrayPattern && | ||
util.isTypeAnyArrayType(senderType, checker) | ||
) { | ||
return context.report({ | ||
node: reportingNode, | ||
messageId: 'unsafeArrayPattern', | ||
}); | ||
} | ||
|
||
if (comparisonType === ComparisonType.None) { | ||
return; | ||
} | ||
|
||
const result = util.isUnsafeAssignment(senderType, receiverType, checker); | ||
if (!result) { | ||
return; | ||
} | ||
|
||
const { sender, receiver } = result; | ||
return context.report({ | ||
node: reportingNode, | ||
messageId: 'unsafeAssignment', | ||
data: { | ||
sender: checker.typeToString(sender), | ||
receiver: checker.typeToString(receiver), | ||
}, | ||
}); | ||
} | ||
|
||
function getComparisonType( | ||
typeAnnotation: TSESTree.TSTypeAnnotation | undefined, | ||
): ComparisonType { | ||
return typeAnnotation | ||
? // if there's a type annotation, we can do a comparison | ||
ComparisonType.Basic | ||
: // no type annotation means the variable's type will just be inferred, thus equal | ||
ComparisonType.None; | ||
} | ||
|
||
return { | ||
'VariableDeclarator[init != null]'( | ||
node: TSESTree.VariableDeclarator, | ||
): void { | ||
checkAssignment( | ||
node.id, | ||
node.init!, | ||
node, | ||
getComparisonType(node.id.typeAnnotation), | ||
); | ||
}, | ||
'ClassProperty[value != null]'(node: TSESTree.ClassProperty): void { | ||
checkAssignment( | ||
node.key, | ||
node.value!, | ||
node, | ||
getComparisonType(node.typeAnnotation), | ||
); | ||
}, | ||
'AssignmentExpression[operator = "="], AssignmentPattern'( | ||
node: TSESTree.AssignmentExpression | TSESTree.AssignmentPattern, | ||
): void { | ||
checkAssignment( | ||
node.left, | ||
node.right, | ||
node, | ||
// the variable already has some form of a type to compare against | ||
ComparisonType.Basic, | ||
); | ||
}, | ||
|
||
// TODO - { x: 1 } | ||
}; | ||
}, | ||
}); |
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
Oops, something went wrong.