Ohm-Management - Projektarbeit B-ME
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.

func-style.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. type: "suggestion",
  12. docs: {
  13. description: "enforce the consistent use of either `function` declarations or expressions",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/func-style"
  17. },
  18. schema: [
  19. {
  20. enum: ["declaration", "expression"]
  21. },
  22. {
  23. type: "object",
  24. properties: {
  25. allowArrowFunctions: {
  26. type: "boolean"
  27. }
  28. },
  29. additionalProperties: false
  30. }
  31. ],
  32. messages: {
  33. expression: "Expected a function expression.",
  34. declaration: "Expected a function declaration."
  35. }
  36. },
  37. create(context) {
  38. const style = context.options[0],
  39. allowArrowFunctions = context.options[1] && context.options[1].allowArrowFunctions === true,
  40. enforceDeclarations = (style === "declaration"),
  41. stack = [];
  42. const nodesToCheck = {
  43. FunctionDeclaration(node) {
  44. stack.push(false);
  45. if (!enforceDeclarations && node.parent.type !== "ExportDefaultDeclaration") {
  46. context.report({ node, messageId: "expression" });
  47. }
  48. },
  49. "FunctionDeclaration:exit"() {
  50. stack.pop();
  51. },
  52. FunctionExpression(node) {
  53. stack.push(false);
  54. if (enforceDeclarations && node.parent.type === "VariableDeclarator") {
  55. context.report({ node: node.parent, messageId: "declaration" });
  56. }
  57. },
  58. "FunctionExpression:exit"() {
  59. stack.pop();
  60. },
  61. ThisExpression() {
  62. if (stack.length > 0) {
  63. stack[stack.length - 1] = true;
  64. }
  65. }
  66. };
  67. if (!allowArrowFunctions) {
  68. nodesToCheck.ArrowFunctionExpression = function() {
  69. stack.push(false);
  70. };
  71. nodesToCheck["ArrowFunctionExpression:exit"] = function(node) {
  72. const hasThisExpr = stack.pop();
  73. if (enforceDeclarations && !hasThisExpr && node.parent.type === "VariableDeclarator") {
  74. context.report({ node: node.parent, messageId: "declaration" });
  75. }
  76. };
  77. }
  78. return nodesToCheck;
  79. }
  80. };