check-fetch-policy
Ast Rule: function call
check-fetch-policy
function visit(node, filename, code) {
const checkFetchPolicyValue = (element) => {
const VALID_CACHE_POLICY = ['cache-first', 'cache-only', 'cache-and-network', 'network-only', 'no-cache', 'standby'];
if (element.astType !== "string") {
return;
}
if (!element.value || element.value.length === 0) {
return;
}
const v = element.value.substring(1, element.value.length - 1);
if (!VALID_CACHE_POLICY.includes(v)) {
const error = buildError(element.start.line, element.start.col, element.end.line, element.end.col,
"Invalid fetch policy", "WARNING", "BEST_PRACTICE");
const edit = buildEditUpdate(element.start.line, element.start.col, element.end.line, element.end.col, '"cache-first"');
const fix = buildFix('Use cache-first policy', [edit]);
addError(error.addFix(fix));
}
}
if (!node || !node.functionName || !node.functionName.value || node.functionName.value !== "useQuery" || !node.arguments) {
return;
}
const arguments = node.arguments.values;
if (arguments.length === 2) {
const secondArgument = arguments[1].value;
if (secondArgument.astType !== "object") {
return;
}
ITEMS_TO_CHECK = ["fetchPolicy", "nextFetchPolicy"];
ITEMS_TO_CHECK.forEach((fp) => {
const fetchPolicy = secondArgument.elements.filter(e => e.name && e.name.value && e.name.value === fp);
if (fetchPolicy.length > 0) {
checkFetchPolicyValue(fetchPolicy[0].value);
}
});
}
}
no-policy.js
Expected test result: no error
correct.js
Expected test result: no error
error.js
Expected test result: has error