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.

no-return-assign.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @fileoverview Rule to flag when return statement contains assignment
  3. * @author Ilya Volodin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const SENTINEL_TYPE = /^(?:[a-zA-Z]+?Statement|ArrowFunctionExpression|FunctionExpression|ClassExpression)$/;
  14. //------------------------------------------------------------------------------
  15. // Rule Definition
  16. //------------------------------------------------------------------------------
  17. module.exports = {
  18. meta: {
  19. type: "suggestion",
  20. docs: {
  21. description: "disallow assignment operators in `return` statements",
  22. category: "Best Practices",
  23. recommended: false,
  24. url: "https://eslint.org/docs/rules/no-return-assign"
  25. },
  26. schema: [
  27. {
  28. enum: ["except-parens", "always"]
  29. }
  30. ]
  31. },
  32. create(context) {
  33. const always = (context.options[0] || "except-parens") !== "except-parens";
  34. const sourceCode = context.getSourceCode();
  35. return {
  36. AssignmentExpression(node) {
  37. if (!always && astUtils.isParenthesised(sourceCode, node)) {
  38. return;
  39. }
  40. let currentChild = node;
  41. let parent = currentChild.parent;
  42. // Find ReturnStatement or ArrowFunctionExpression in ancestors.
  43. while (parent && !SENTINEL_TYPE.test(parent.type)) {
  44. currentChild = parent;
  45. parent = parent.parent;
  46. }
  47. // Reports.
  48. if (parent && parent.type === "ReturnStatement") {
  49. context.report({
  50. node: parent,
  51. message: "Return statement should not contain assignment."
  52. });
  53. } else if (parent && parent.type === "ArrowFunctionExpression" && parent.body === currentChild) {
  54. context.report({
  55. node: parent,
  56. message: "Arrow function should not return assignment."
  57. });
  58. }
  59. }
  60. };
  61. }
  62. };