check-for-error-and-loading

Try in Playground
apollo-graphql-client-javascriptBest PracticeWarning

0

No tags

No CWE or CVE

Ast Rule: assignment


check-for-error-and-loading

How to write a rule
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

  const {
    data: cookbookData,
    loading: cookbookLoading
  } = useQuery(GET_PUBLIC_COOKBOOK_BY_ID, {
		onError: null,
    skip: tmpCookbookId == undefined || mode === RecipesMode.PRIVATE_MODE,
    variables,
  });

with-rename-and-no-error.js

Expected test result: has error

  const {
    data: cookbookData,
    loading: cookbookLoading
  } = useQuery(GET_PUBLIC_COOKBOOK_BY_ID, {
    skip: tmpCookbookId == undefined || mode === RecipesMode.PRIVATE_MODE,
    variables,
  });

with-rename-and-no-loading.js

Expected test result: has error

  const {
    data: cookbookData,
    error: cookbookError
  } = useQuery(GET_PUBLIC_COOKBOOK_BY_ID, {
    skip: tmpCookbookId == undefined || mode === RecipesMode.PRIVATE_MODE,
    variables,
  });

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

Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.