check-for-error-and-loading
Ast Rule: assignment
check-for-error-and-loading
function visit(node, filename, code) {
/**
* If the query argument uses a callback with onError or onCompleted, then, skip everything altogether.
*/
const shouldSkip = (node) => {
if (node && node.arguments && node.arguments.values && node.arguments.values.length === 2) {
const secondArgument = node.arguments.values[1].value;
if (secondArgument.astType === "object") {
return secondArgument.elements.filter(e => e.name && (e.name.value === "onError" || e.name.value === "onCompleted")).length > 0;
}
}
return false;
};
if (!node.right.astType === "functioncall" || !node.right.functionName || !node.right.functionName.value) {
return;
}
if ((node.right.functionName.value !== "useQuery") && (node.right.functionName.value !== "useMutation")) {
return;
}
if (!node.left.astType === "object" || !node.left.elements) {
return;
}
if (shouldSkip(node.right)) {
return;
}
const els = node.left.elements;
WORDS_TO_CHECK = ["error", "loading"];
WORDS_TO_CHECK.forEach((w) => {
const hasElement = els.filter(e => (e.value && e.value.astType === "string" && e.value.value && e.value.value === w) || (e.name && e.name.astType === "string" && e.name.value === w)).length > 0;
if (!hasElement) {
const error = buildError(node.left.start.line, node.left.start.col, node.left.end.line, node.left.end.col,
`Must catch potential the content of ${w} when using GraphQL Apollo`, "WARNING", "BEST_PRACTICE");
const edit = buildEditAdd(node.left.end.line, node.left.end.col - 1, `, ${w}`);
const fix = buildFix(`add ${w} variable`, [edit]);
addError(error.addFix(fix));
}
});
}
skip.js
Expected test result: no error
with-rename-and-no-error.js
Expected test result: has error
with-rename-and-no-loading.js
Expected test result: has error
with-rename-and-error.js
Expected test result: no error
with-error.js
Expected test result: no error
no-error.js
Expected test result: has error