项目原始demo,不改动
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.
 
 
 
 

91 строка
2.7 KiB

  1. /**
  2. * @fileoverview Rule to enforce a particular function style
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "enforce the consistent use of either `function` declarations or expressions",
  13. category: "Stylistic Issues",
  14. recommended: false,
  15. url: "https://eslint.org/docs/rules/func-style"
  16. },
  17. schema: [
  18. {
  19. enum: ["declaration", "expression"]
  20. },
  21. {
  22. type: "object",
  23. properties: {
  24. allowArrowFunctions: {
  25. type: "boolean"
  26. }
  27. },
  28. additionalProperties: false
  29. }
  30. ]
  31. },
  32. create(context) {
  33. const style = context.options[0],
  34. allowArrowFunctions = context.options[1] && context.options[1].allowArrowFunctions === true,
  35. enforceDeclarations = (style === "declaration"),
  36. stack = [];
  37. const nodesToCheck = {
  38. FunctionDeclaration(node) {
  39. stack.push(false);
  40. if (!enforceDeclarations && node.parent.type !== "ExportDefaultDeclaration") {
  41. context.report({ node, message: "Expected a function expression." });
  42. }
  43. },
  44. "FunctionDeclaration:exit"() {
  45. stack.pop();
  46. },
  47. FunctionExpression(node) {
  48. stack.push(false);
  49. if (enforceDeclarations && node.parent.type === "VariableDeclarator") {
  50. context.report({ node: node.parent, message: "Expected a function declaration." });
  51. }
  52. },
  53. "FunctionExpression:exit"() {
  54. stack.pop();
  55. },
  56. ThisExpression() {
  57. if (stack.length > 0) {
  58. stack[stack.length - 1] = true;
  59. }
  60. }
  61. };
  62. if (!allowArrowFunctions) {
  63. nodesToCheck.ArrowFunctionExpression = function() {
  64. stack.push(false);
  65. };
  66. nodesToCheck["ArrowFunctionExpression:exit"] = function(node) {
  67. const hasThisExpr = stack.pop();
  68. if (enforceDeclarations && !hasThisExpr && node.parent.type === "VariableDeclarator") {
  69. context.report({ node: node.parent, message: "Expected a function declaration." });
  70. }
  71. };
  72. }
  73. return nodesToCheck;
  74. }
  75. };