|
| 1 | +import {CallExpression} from '../../../../libraries/typescript/lib/typescript'; |
| 2 | +import {TypeChecker} from '../../entities/typescript'; |
| 3 | +import {Diagnostic} from '../../interfaces'; |
| 4 | +import {makeDiagnostic} from '../../utils'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Verifies that the argument of the assertion is identical to the generic type of the assertion. |
| 8 | + * |
| 9 | + * @param checker - The TypeScript type checker. |
| 10 | + * @param nodes - The `expectType` AST nodes. |
| 11 | + * @return List of custom diagnostics. |
| 12 | + */ |
| 13 | +export const isIdentical = (checker: TypeChecker, nodes: Set<CallExpression>): Diagnostic[] => { |
| 14 | + const diagnostics: Diagnostic[] = []; |
| 15 | + |
| 16 | + if (!nodes) { |
| 17 | + return diagnostics; |
| 18 | + } |
| 19 | + |
| 20 | + for (const node of nodes) { |
| 21 | + if (!node.typeArguments) { |
| 22 | + // Skip if the node does not have generics |
| 23 | + continue; |
| 24 | + } |
| 25 | + |
| 26 | + // Retrieve the type to be expected. This is the type inside the generic. |
| 27 | + const expectedType = checker.getTypeFromTypeNode(node.typeArguments[0]); |
| 28 | + |
| 29 | + // Retrieve the argument type. This is the type to be checked. |
| 30 | + const argumentType = checker.getTypeAtLocation(node.arguments[0]); |
| 31 | + |
| 32 | + if (!checker.isTypeAssignableTo(argumentType, expectedType)) { |
| 33 | + // The argument type is not assignable to the expected type. TypeScript will catch this for us. |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + if (!checker.isTypeAssignableTo(expectedType, argumentType)) { |
| 38 | + /** |
| 39 | + * The expected type is not assignable to the argument type, but the argument type is |
| 40 | + * assignable to the expected type. This means our type is too wide. |
| 41 | + */ |
| 42 | + diagnostics.push(makeDiagnostic(node, `Parameter type \`${checker.typeToString(expectedType)}\` is declared too wide for argument type \`${checker.typeToString(argumentType)}\`.`)); |
| 43 | + } else if (!checker.isTypeIdenticalTo(expectedType, argumentType)) { |
| 44 | + /** |
| 45 | + * The expected type and argument type are assignable in both directions. We still have to check |
| 46 | + * if the types are identical. See https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.11.2. |
| 47 | + */ |
| 48 | + diagnostics.push(makeDiagnostic(node, `Parameter type \`${checker.typeToString(expectedType)}\` is not identical to argument type \`${checker.typeToString(argumentType)}\`.`)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return diagnostics; |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * Verifies that the argument of the assertion is not identical to the generic type of the assertion. |
| 57 | + * |
| 58 | + * @param checker - The TypeScript type checker. |
| 59 | + * @param nodes - The `expectType` AST nodes. |
| 60 | + * @return List of custom diagnostics. |
| 61 | + */ |
| 62 | +export const isNotIdentical = (checker: TypeChecker, nodes: Set<CallExpression>): Diagnostic[] => { |
| 63 | + const diagnostics: Diagnostic[] = []; |
| 64 | + |
| 65 | + if (!nodes) { |
| 66 | + return diagnostics; |
| 67 | + } |
| 68 | + |
| 69 | + for (const node of nodes) { |
| 70 | + if (!node.typeArguments) { |
| 71 | + // Skip if the node does not have generics |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + // Retrieve the type to be expected. This is the type inside the generic. |
| 76 | + const expectedType = checker.getTypeFromTypeNode(node.typeArguments[0]); |
| 77 | + const argumentType = checker.getTypeAtLocation(node.arguments[0]); |
| 78 | + |
| 79 | + if (checker.isTypeIdenticalTo(expectedType, argumentType)) { |
| 80 | + diagnostics.push(makeDiagnostic(node, `Parameter type \`${checker.typeToString(expectedType)}\` is identical to argument type \`${checker.typeToString(argumentType)}\`.`)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return diagnostics; |
| 85 | +}; |
0 commit comments