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-confusing-arrow.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @fileoverview A rule to warn against using arrow functions when they could be
  3. * confused with comparisions
  4. * @author Jxck <https://github.com/Jxck>
  5. */
  6. "use strict";
  7. const astUtils = require("../util/ast-utils.js");
  8. //------------------------------------------------------------------------------
  9. // Helpers
  10. //------------------------------------------------------------------------------
  11. /**
  12. * Checks whether or not a node is a conditional expression.
  13. * @param {ASTNode} node - node to test
  14. * @returns {boolean} `true` if the node is a conditional expression.
  15. */
  16. function isConditional(node) {
  17. return node && node.type === "ConditionalExpression";
  18. }
  19. //------------------------------------------------------------------------------
  20. // Rule Definition
  21. //------------------------------------------------------------------------------
  22. module.exports = {
  23. meta: {
  24. type: "suggestion",
  25. docs: {
  26. description: "disallow arrow functions where they could be confused with comparisons",
  27. category: "ECMAScript 6",
  28. recommended: false,
  29. url: "https://eslint.org/docs/rules/no-confusing-arrow"
  30. },
  31. fixable: "code",
  32. schema: [{
  33. type: "object",
  34. properties: {
  35. allowParens: { type: "boolean" }
  36. },
  37. additionalProperties: false
  38. }],
  39. messages: {
  40. confusing: "Arrow function used ambiguously with a conditional expression."
  41. }
  42. },
  43. create(context) {
  44. const config = context.options[0] || {};
  45. const sourceCode = context.getSourceCode();
  46. /**
  47. * Reports if an arrow function contains an ambiguous conditional.
  48. * @param {ASTNode} node - A node to check and report.
  49. * @returns {void}
  50. */
  51. function checkArrowFunc(node) {
  52. const body = node.body;
  53. if (isConditional(body) && !(config.allowParens && astUtils.isParenthesised(sourceCode, body))) {
  54. context.report({
  55. node,
  56. messageId: "confusing",
  57. fix(fixer) {
  58. // if `allowParens` is not set to true dont bother wrapping in parens
  59. return config.allowParens && fixer.replaceText(node.body, `(${sourceCode.getText(node.body)})`);
  60. }
  61. });
  62. }
  63. }
  64. return {
  65. ArrowFunctionExpression: checkArrowFunc
  66. };
  67. }
  68. };