Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

guard-for-in.js 2.3KB

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. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "require `for-in` loops to include an `if` statement",
  14. category: "Best Practices",
  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. };