guard-for-in.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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('../types').Rule.RuleModule} */
  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/latest/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 (
  41. body.type === "BlockStatement" &&
  42. body.body.length === 1 &&
  43. body.body[0].type === "IfStatement"
  44. ) {
  45. return;
  46. }
  47. // block that starts with if statement
  48. if (
  49. body.type === "BlockStatement" &&
  50. body.body.length >= 1 &&
  51. body.body[0].type === "IfStatement"
  52. ) {
  53. const i = body.body[0];
  54. // ... whose consequent is a continue
  55. if (i.consequent.type === "ContinueStatement") {
  56. return;
  57. }
  58. // ... whose consequent is a block that contains only a continue
  59. if (
  60. i.consequent.type === "BlockStatement" &&
  61. i.consequent.body.length === 1 &&
  62. i.consequent.body[0].type === "ContinueStatement"
  63. ) {
  64. return;
  65. }
  66. }
  67. context.report({ node, messageId: "wrap" });
  68. },
  69. };
  70. },
  71. };