guard-for-in.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * @fileoverview Rule to flag for-in loops without if statements inside
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "Require `for-in` loops to include an `if` statement",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/guard-for-in"
  17. },
  18. schema: [],
  19. messages: {
  20. wrap: "The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype."
  21. }
  22. },
  23. create(context) {
  24. return {
  25. ForInStatement(node) {
  26. const body = node.body;
  27. // empty statement
  28. if (body.type === "EmptyStatement") {
  29. return;
  30. }
  31. // if statement
  32. if (body.type === "IfStatement") {
  33. return;
  34. }
  35. // empty block
  36. if (body.type === "BlockStatement" && body.body.length === 0) {
  37. return;
  38. }
  39. // block with just if statement
  40. if (body.type === "BlockStatement" && body.body.length === 1 && body.body[0].type === "IfStatement") {
  41. return;
  42. }
  43. // block that starts with if statement
  44. if (body.type === "BlockStatement" && body.body.length >= 1 && body.body[0].type === "IfStatement") {
  45. const i = body.body[0];
  46. // ... whose consequent is a continue
  47. if (i.consequent.type === "ContinueStatement") {
  48. return;
  49. }
  50. // ... whose consequent is a block that contains only a continue
  51. if (i.consequent.type === "BlockStatement" && i.consequent.body.length === 1 && i.consequent.body[0].type === "ContinueStatement") {
  52. return;
  53. }
  54. }
  55. context.report({ node, messageId: "wrap" });
  56. }
  57. };
  58. }
  59. };