|
| 1 | +// Types assigned to variables and function arguments must match |
| 2 | + |
| 3 | +import * as ast from "../grammar/ast"; |
| 4 | +import { getRuleConfig } from "../config/util"; |
| 5 | +import { createDiagnosticsFactory } from "../diagnostic"; |
| 6 | +import { RuleFactory } from "../linting-rule"; |
| 7 | +import { InputSource } from "../semantics/program"; |
| 8 | +import { findFirstParent, isDollarsLiteral, isEvent, isFunctionDefinition } from "../util"; |
| 9 | +import { Type } from "../semantics"; |
| 10 | + |
| 11 | +export const typeMismatch = createDiagnosticsFactory(); |
| 12 | + |
| 13 | +export const type_check: RuleFactory = factoryContext => { |
| 14 | + const config = getRuleConfig("type_check", factoryContext.userConfig); |
| 15 | + |
| 16 | + if (config.isEnabled) { |
| 17 | + const createTypeMismatchDiagnostic = ( |
| 18 | + expectedType: Type, |
| 19 | + actualType: Type, |
| 20 | + source: InputSource, |
| 21 | + position: ast.SourceRange |
| 22 | + ) => { |
| 23 | + return typeMismatch( |
| 24 | + config.severity, |
| 25 | + `Type mismatch: Expected type '${expectedType.name}', but instead got something of type '${actualType.name}'`, |
| 26 | + source, |
| 27 | + position |
| 28 | + ); |
| 29 | + }; |
| 30 | + |
| 31 | + factoryContext.registerRule<ast.VariableDefinition>(ast.SyntaxKind.VariableDefinition, (node, context) => { |
| 32 | + // initializer type must match lhs type |
| 33 | + if (node.initializer && !isDollarsLiteral(node.initializer)) { |
| 34 | + const initializerType = context.typeChecker.typeOfNode(node.initializer); |
| 35 | + const variableType = context.typeChecker.typeOfNode(node.type); |
| 36 | + |
| 37 | + if (initializerType !== variableType) { |
| 38 | + return [ |
| 39 | + createTypeMismatchDiagnostic( |
| 40 | + variableType, |
| 41 | + initializerType, |
| 42 | + context.source, |
| 43 | + node.initializer.position |
| 44 | + ), |
| 45 | + ]; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return []; |
| 50 | + }); |
| 51 | + |
| 52 | + factoryContext.registerRule<ast.AssignmentStatement>(ast.SyntaxKind.AssignmentStatement, (node, context) => { |
| 53 | + // rhs type must match lhs type |
| 54 | + if (!isDollarsLiteral(node.right)) { |
| 55 | + const rhsType = context.typeChecker.typeOfNode(node.right); |
| 56 | + const lhsType = context.typeChecker.typeOfNode(node.left); |
| 57 | + |
| 58 | + if (rhsType !== lhsType) { |
| 59 | + return [createTypeMismatchDiagnostic(lhsType, rhsType, context.source, node.right.position)]; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return []; |
| 64 | + }); |
| 65 | + |
| 66 | + factoryContext.registerRule<ast.CallExpression>(ast.SyntaxKind.CallExpression, (node, context) => { |
| 67 | + const diagnostics = []; |
| 68 | + |
| 69 | + const functionSymbol = context.typeChecker.symbolOfNode(node.expression); |
| 70 | + let parameters: Array<ast.EventParameter | ast.FunctionParameter> = []; |
| 71 | + if ( |
| 72 | + functionSymbol?.declaration && |
| 73 | + (isEvent(functionSymbol.declaration) || isFunctionDefinition(functionSymbol.declaration)) |
| 74 | + ) { |
| 75 | + parameters = functionSymbol.declaration.parameters; |
| 76 | + } |
| 77 | + |
| 78 | + // rhs type must match lhs type |
| 79 | + let parameterIndex = 0; |
| 80 | + for (const argument of node.arguments.arguments) { |
| 81 | + if (!isDollarsLiteral(argument) && parameterIndex < parameters.length) { |
| 82 | + const argumentType = context.typeChecker.typeOfNode(argument); |
| 83 | + const parameterType = context.typeChecker.typeOfNode(parameters[parameterIndex]); |
| 84 | + |
| 85 | + if (argumentType !== parameterType) { |
| 86 | + diagnostics.push( |
| 87 | + createTypeMismatchDiagnostic(parameterType, argumentType, context.source, argument.position) |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | + parameterIndex++; |
| 92 | + } |
| 93 | + |
| 94 | + return diagnostics; |
| 95 | + }); |
| 96 | + |
| 97 | + factoryContext.registerRule<ast.ReturnStatement>(ast.SyntaxKind.ReturnStatement, (node, context) => { |
| 98 | + if (!node.returnValue || isDollarsLiteral(node.returnValue)) return []; |
| 99 | + |
| 100 | + const parentFunc = findFirstParent(node, isFunctionDefinition); |
| 101 | + |
| 102 | + if (!parentFunc) return []; // Would be weird to have a return not in a parent |
| 103 | + |
| 104 | + const returnType = context.typeChecker.typeOfNode(parentFunc.returnType); |
| 105 | + const returendType = context.typeChecker.typeOfNode(node.returnValue); |
| 106 | + |
| 107 | + if (returendType != returnType) { |
| 108 | + return [ |
| 109 | + createTypeMismatchDiagnostic(returnType, returendType, context.source, node.returnValue.position), |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
| 113 | + return []; |
| 114 | + }); |
| 115 | + } |
| 116 | +}; |
| 117 | + |
| 118 | +export default type_check; |
0 commit comments