Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const SENTINEL_TYPE = /^(?:[a-zA-Z]+?Statement|ArrowFunctionExpression|FunctionExpression|ClassExpression)$/u;
  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. messages: {
  32. returnAssignment: "Return statement should not contain assignment.",
  33. arrowAssignment: "Arrow function should not return assignment."
  34. }
  35. },
  36. create(context) {
  37. const always = (context.options[0] || "except-parens") !== "except-parens";
  38. const sourceCode = context.getSourceCode();
  39. return {
  40. AssignmentExpression(node) {
  41. if (!always && astUtils.isParenthesised(sourceCode, node)) {
  42. return;
  43. }
  44. let currentChild = node;
  45. let parent = currentChild.parent;
  46. // Find ReturnStatement or ArrowFunctionExpression in ancestors.
  47. while (parent && !SENTINEL_TYPE.test(parent.type)) {
  48. currentChild = parent;
  49. parent = parent.parent;
  50. }
  51. // Reports.
  52. if (parent && parent.type === "ReturnStatement") {
  53. context.report({
  54. node: parent,
  55. messageId: "returnAssignment"
  56. });
  57. } else if (parent && parent.type === "ArrowFunctionExpression" && parent.body === currentChild) {
  58. context.report({
  59. node: parent,
  60. messageId: "arrowAssignment"
  61. });
  62. }
  63. }
  64. };
  65. }
  66. };