no-fetch-credentials.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @fileoverview Prevent fetch() calls with default arguments
  3. * @author Andrei Oprea
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "Prevent fetch() calls with default arguments",
  13. category: "Fill me in",
  14. recommended: false
  15. },
  16. fixable: null,
  17. schema: [/*TODO*/]
  18. },
  19. create: function(context) {
  20. const isHTTPCall = (node) => {
  21. switch (node.type) {
  22. case "TemplateLiteral":
  23. return node.quasis[0].value.raw.startsWith("http");
  24. case "Literal":
  25. return node.value.startsWith("http");
  26. case "Identifier":
  27. return true;
  28. default:
  29. return false;
  30. }
  31. }
  32. return {
  33. CallExpression(node) {
  34. if (node.callee.name === "fetch") {
  35. if (node.arguments.length === 1 && isHTTPCall(node.arguments[0])) {
  36. context.report({
  37. node,
  38. message: "Please provide options for fetch() call",
  39. });
  40. } else if (node.arguments.length > 1 &&
  41. !node.arguments[1].properties.find(p => p.key && p.key.name === "credentials")) {
  42. context.report({
  43. node,
  44. message: "Fetch call does not omit credentials",
  45. });
  46. }
  47. }
  48. }
  49. };
  50. }
  51. };