|
| 1 | +import { |
| 2 | + AST_NODE_TYPES, |
| 3 | + TSESTree, |
| 4 | +} from '@typescript-eslint/experimental-utils'; |
| 5 | +import * as ts from 'typescript'; |
| 6 | +import * as util from '../util'; |
| 7 | + |
| 8 | +type MessageIds = |
| 9 | + | 'unsafeArgument' |
| 10 | + | 'unsafeTupleSpread' |
| 11 | + | 'unsafeArraySpread' |
| 12 | + | 'unsafeSpread'; |
| 13 | + |
| 14 | +class FunctionSignature { |
| 15 | + public static create( |
| 16 | + checker: ts.TypeChecker, |
| 17 | + tsNode: ts.CallLikeExpression, |
| 18 | + ): FunctionSignature | null { |
| 19 | + const signature = checker.getResolvedSignature(tsNode); |
| 20 | + if (!signature) { |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + const paramTypes: ts.Type[] = []; |
| 25 | + let restType: ts.Type | null = null; |
| 26 | + |
| 27 | + for (const param of signature.getParameters()) { |
| 28 | + const type = checker.getTypeOfSymbolAtLocation(param, tsNode); |
| 29 | + |
| 30 | + const decl = param.getDeclarations()?.[0]; |
| 31 | + if (decl && ts.isParameter(decl) && decl.dotDotDotToken) { |
| 32 | + // is a rest param |
| 33 | + if (checker.isArrayType(type)) { |
| 34 | + restType = checker.getTypeArguments(type)[0]; |
| 35 | + } else { |
| 36 | + restType = type; |
| 37 | + } |
| 38 | + break; |
| 39 | + } |
| 40 | + |
| 41 | + paramTypes.push(type); |
| 42 | + } |
| 43 | + |
| 44 | + return new this(paramTypes, restType); |
| 45 | + } |
| 46 | + |
| 47 | + private hasConsumedArguments = false; |
| 48 | + |
| 49 | + private constructor( |
| 50 | + private paramTypes: ts.Type[], |
| 51 | + private restType: ts.Type | null, |
| 52 | + ) {} |
| 53 | + |
| 54 | + public getParameterType(index: number): ts.Type | null { |
| 55 | + if (index >= this.paramTypes.length || this.hasConsumedArguments) { |
| 56 | + return this.restType; |
| 57 | + } |
| 58 | + return this.paramTypes[index]; |
| 59 | + } |
| 60 | + |
| 61 | + public consumeRemainingArguments(): void { |
| 62 | + this.hasConsumedArguments = true; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +export default util.createRule<[], MessageIds>({ |
| 67 | + name: 'no-unsafe-argument', |
| 68 | + meta: { |
| 69 | + type: 'problem', |
| 70 | + docs: { |
| 71 | + description: 'Disallows calling an function with an any type value', |
| 72 | + category: 'Possible Errors', |
| 73 | + // TODO - enable this with next breaking |
| 74 | + recommended: false, |
| 75 | + requiresTypeChecking: true, |
| 76 | + }, |
| 77 | + messages: { |
| 78 | + unsafeArgument: |
| 79 | + 'Unsafe argument of type `{{sender}}` assigned to a parameter of type `{{receiver}}`.', |
| 80 | + unsafeTupleSpread: |
| 81 | + 'Unsafe spread of a tuple type. The {{index}} element is of type `{{sender}}` and is assigned to a parameter of type `{{reciever}}`.', |
| 82 | + unsafeArraySpread: 'Unsafe spread of an `any` array type.', |
| 83 | + unsafeSpread: 'Unsafe spread of an `any` type.', |
| 84 | + }, |
| 85 | + schema: [], |
| 86 | + }, |
| 87 | + defaultOptions: [], |
| 88 | + create(context) { |
| 89 | + const { program, esTreeNodeToTSNodeMap } = util.getParserServices(context); |
| 90 | + const checker = program.getTypeChecker(); |
| 91 | + |
| 92 | + return { |
| 93 | + 'CallExpression, NewExpression'( |
| 94 | + node: TSESTree.CallExpression | TSESTree.NewExpression, |
| 95 | + ): void { |
| 96 | + if (node.arguments.length === 0) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + // ignore any-typed calls as these are caught by no-unsafe-call |
| 101 | + if ( |
| 102 | + util.isTypeAnyType( |
| 103 | + checker.getTypeAtLocation(esTreeNodeToTSNodeMap.get(node.callee)), |
| 104 | + ) |
| 105 | + ) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + const tsNode = esTreeNodeToTSNodeMap.get(node); |
| 110 | + const signature = FunctionSignature.create(checker, tsNode); |
| 111 | + if (!signature) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + let parameterTypeIndex = 0; |
| 116 | + for ( |
| 117 | + let i = 0; |
| 118 | + i < node.arguments.length; |
| 119 | + i += 1, parameterTypeIndex += 1 |
| 120 | + ) { |
| 121 | + const argument = node.arguments[i]; |
| 122 | + |
| 123 | + switch (argument.type) { |
| 124 | + // spreads consume |
| 125 | + case AST_NODE_TYPES.SpreadElement: { |
| 126 | + const spreadArgType = checker.getTypeAtLocation( |
| 127 | + esTreeNodeToTSNodeMap.get(argument.argument), |
| 128 | + ); |
| 129 | + |
| 130 | + if (util.isTypeAnyType(spreadArgType)) { |
| 131 | + // foo(...any) |
| 132 | + context.report({ |
| 133 | + node: argument, |
| 134 | + messageId: 'unsafeSpread', |
| 135 | + }); |
| 136 | + } else if (util.isTypeAnyArrayType(spreadArgType, checker)) { |
| 137 | + // foo(...any[]) |
| 138 | + |
| 139 | + // TODO - we could break down the spread and compare the array type against each argument |
| 140 | + context.report({ |
| 141 | + node: argument, |
| 142 | + messageId: 'unsafeArraySpread', |
| 143 | + }); |
| 144 | + } else if (checker.isTupleType(spreadArgType)) { |
| 145 | + // foo(...[tuple1, tuple2]) |
| 146 | + const spreadTypeArguments = checker.getTypeArguments( |
| 147 | + spreadArgType, |
| 148 | + ); |
| 149 | + for ( |
| 150 | + let j = 0; |
| 151 | + j < spreadTypeArguments.length; |
| 152 | + j += 1, parameterTypeIndex += 1 |
| 153 | + ) { |
| 154 | + const tupleType = spreadTypeArguments[j]; |
| 155 | + const parameterType = signature.getParameterType( |
| 156 | + parameterTypeIndex, |
| 157 | + ); |
| 158 | + if (parameterType == null) { |
| 159 | + continue; |
| 160 | + } |
| 161 | + const result = util.isUnsafeAssignment( |
| 162 | + tupleType, |
| 163 | + parameterType, |
| 164 | + checker, |
| 165 | + ); |
| 166 | + if (result) { |
| 167 | + context.report({ |
| 168 | + node: argument, |
| 169 | + messageId: 'unsafeTupleSpread', |
| 170 | + data: { |
| 171 | + sender: checker.typeToString(tupleType), |
| 172 | + receiver: checker.typeToString(parameterType), |
| 173 | + }, |
| 174 | + }); |
| 175 | + } |
| 176 | + } |
| 177 | + if (spreadArgType.target.hasRestElement) { |
| 178 | + // the last element was a rest - so all remaining defined arguments can be considered "consumed" |
| 179 | + // all remaining arguments should be compared against the rest type (if one exists) |
| 180 | + signature.consumeRemainingArguments(); |
| 181 | + } |
| 182 | + } else { |
| 183 | + // something that's iterable |
| 184 | + // handling this will be pretty complex - so we ignore it for now |
| 185 | + // TODO - handle generic iterable case |
| 186 | + } |
| 187 | + break; |
| 188 | + } |
| 189 | + |
| 190 | + default: { |
| 191 | + const parameterType = signature.getParameterType(i); |
| 192 | + if (parameterType == null) { |
| 193 | + continue; |
| 194 | + } |
| 195 | + |
| 196 | + const argumentType = checker.getTypeAtLocation( |
| 197 | + esTreeNodeToTSNodeMap.get(argument), |
| 198 | + ); |
| 199 | + const result = util.isUnsafeAssignment( |
| 200 | + argumentType, |
| 201 | + parameterType, |
| 202 | + checker, |
| 203 | + ); |
| 204 | + if (result) { |
| 205 | + context.report({ |
| 206 | + node: argument, |
| 207 | + messageId: 'unsafeArgument', |
| 208 | + data: { |
| 209 | + sender: checker.typeToString(argumentType), |
| 210 | + receiver: checker.typeToString(parameterType), |
| 211 | + }, |
| 212 | + }); |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + }, |
| 218 | + }; |
| 219 | + }, |
| 220 | +}); |
0 commit comments