项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

84 lines
2.4 KiB

  1. /**
  2. * @fileoverview Rule to flag use of an empty block statement
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. description: "disallow empty block statements",
  17. category: "Possible Errors",
  18. recommended: true,
  19. url: "https://eslint.org/docs/rules/no-empty"
  20. },
  21. schema: [
  22. {
  23. type: "object",
  24. properties: {
  25. allowEmptyCatch: {
  26. type: "boolean"
  27. }
  28. },
  29. additionalProperties: false
  30. }
  31. ],
  32. messages: {
  33. unexpected: "Empty {{type}} statement."
  34. }
  35. },
  36. create(context) {
  37. const options = context.options[0] || {},
  38. allowEmptyCatch = options.allowEmptyCatch || false;
  39. const sourceCode = context.getSourceCode();
  40. return {
  41. BlockStatement(node) {
  42. // if the body is not empty, we can just return immediately
  43. if (node.body.length !== 0) {
  44. return;
  45. }
  46. // a function is generally allowed to be empty
  47. if (astUtils.isFunction(node.parent)) {
  48. return;
  49. }
  50. if (allowEmptyCatch && node.parent.type === "CatchClause") {
  51. return;
  52. }
  53. // any other block is only allowed to be empty, if it contains a comment
  54. if (sourceCode.getCommentsInside(node).length > 0) {
  55. return;
  56. }
  57. context.report({ node, messageId: "unexpected", data: { type: "block" } });
  58. },
  59. SwitchStatement(node) {
  60. if (typeof node.cases === "undefined" || node.cases.length === 0) {
  61. context.report({ node, messageId: "unexpected", data: { type: "switch" } });
  62. }
  63. }
  64. };
  65. }
  66. };