项目原始demo,不改动
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.

no-case-declarations.js 1.8 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @fileoverview Rule to flag use of an lexical declarations inside a case clause
  3. * @author Erik Arvidsson
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "disallow lexical declarations in case clauses",
  13. category: "Best Practices",
  14. recommended: true,
  15. url: "https://eslint.org/docs/rules/no-case-declarations"
  16. },
  17. schema: [],
  18. messages: {
  19. unexpected: "Unexpected lexical declaration in case block."
  20. }
  21. },
  22. create(context) {
  23. /**
  24. * Checks whether or not a node is a lexical declaration.
  25. * @param {ASTNode} node A direct child statement of a switch case.
  26. * @returns {boolean} Whether or not the node is a lexical declaration.
  27. */
  28. function isLexicalDeclaration(node) {
  29. switch (node.type) {
  30. case "FunctionDeclaration":
  31. case "ClassDeclaration":
  32. return true;
  33. case "VariableDeclaration":
  34. return node.kind !== "var";
  35. default:
  36. return false;
  37. }
  38. }
  39. return {
  40. SwitchCase(node) {
  41. for (let i = 0; i < node.consequent.length; i++) {
  42. const statement = node.consequent[i];
  43. if (isLexicalDeclaration(statement)) {
  44. context.report({
  45. node,
  46. messageId: "unexpected"
  47. });
  48. }
  49. }
  50. }
  51. };
  52. }
  53. };