-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
isUnsafeAssignment.ts
85 lines (72 loc) · 2.65 KB
/
isUnsafeAssignment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tools from 'ts-api-utils';
import type * as ts from 'typescript';
import { isTypeAnyType, isTypeUnknownType } from './predicates';
/**
* Does a simple check to see if there is an any being assigned to a non-any type.
*
* This also checks generic positions to ensure there's no unsafe sub-assignments.
* Note: in the case of generic positions, it makes the assumption that the two types are the same.
*
* @example See tests for examples
*
* @returns false if it's safe, or an object with the two types if it's unsafe
*/
export function isUnsafeAssignment(
type: ts.Type,
receiver: ts.Type,
checker: ts.TypeChecker,
senderNode: TSESTree.Node | null,
): false | { sender: ts.Type; receiver: ts.Type } {
if (isTypeAnyType(type)) {
// Allow assignment of any ==> unknown.
if (isTypeUnknownType(receiver)) {
return false;
}
if (!isTypeAnyType(receiver)) {
return { sender: type, receiver };
}
}
if (tools.isTypeReference(type) && tools.isTypeReference(receiver)) {
// TODO - figure out how to handle cases like this,
// where the types are assignable, but not the same type
/*
function foo(): ReadonlySet<number> { return new Set<any>(); }
// and
type Test<T> = { prop: T }
type Test2 = { prop: string }
declare const a: Test<any>;
const b: Test2 = a;
*/
if (type.target !== receiver.target) {
// if the type references are different, assume safe, as we won't know how to compare the two types
// the generic positions might not be equivalent for both types
return false;
}
if (
senderNode?.type === AST_NODE_TYPES.NewExpression &&
senderNode.callee.type === AST_NODE_TYPES.Identifier &&
senderNode.callee.name === 'Map' &&
senderNode.arguments.length === 0 &&
senderNode.typeParameters == null
) {
// special case to handle `new Map()`
// unfortunately Map's default empty constructor is typed to return `Map<any, any>` :(
// https://github.com/typescript-eslint/typescript-eslint/issues/2109#issuecomment-634144396
return false;
}
const typeArguments = type.typeArguments ?? [];
const receiverTypeArguments = receiver.typeArguments ?? [];
for (let i = 0; i < typeArguments.length; i += 1) {
const arg = typeArguments[i];
const receiverArg = receiverTypeArguments[i];
const unsafe = isUnsafeAssignment(arg, receiverArg, checker, senderNode);
if (unsafe) {
return { sender: type, receiver };
}
}
return false;
}
return false;
}