Last active
May 5, 2020 14:29
-
-
Save craigtaub/f4e41636360fce156812e84c41ecabdd to your computer and use it in GitHub Desktop.
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
const errors = []; | |
const ANNOTATED_TYPES = { | |
NumberTypeAnnotation: "number", | |
GenericTypeAnnotation: true | |
}; | |
// Logic for type checks | |
const typeChecks = { | |
expression: (declarationFullType, callerFullArg) => { | |
switch (declarationFullType.typeAnnotation.type) { | |
case "NumberTypeAnnotation": | |
return callerFullArg.type === "NumericLiteral"; | |
case "GenericTypeAnnotation": // non-native | |
// If called with Object, check properties | |
if (callerFullArg.type === "ObjectExpression") { | |
// Get Interface | |
const interfaceNode = ast.program.body.find( | |
node => node.type === "InterfaceDeclaration" | |
); | |
// Get properties | |
const properties = interfaceNode.body.properties; | |
// Check each property against caller | |
properties.map((prop, index) => { | |
const name = prop.key.name; | |
const associatedName = callerFullArg.properties[index].key.name; | |
if (name !== associatedName) { | |
errors.push( | |
`Property "${associatedName}" does not exist on interface "${interfaceNode.id.name}". Did you mean Property "${name}"?` | |
); | |
} | |
}); | |
} | |
return true; // as already logged | |
} | |
}, | |
annotationCheck: arg => { | |
return !!ANNOTATED_TYPES[arg]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment