Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save craigtaub/f4e41636360fce156812e84c41ecabdd to your computer and use it in GitHub Desktop.
Save craigtaub/f4e41636360fce156812e84c41ecabdd to your computer and use it in GitHub Desktop.
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