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.

implicit-arrow-linebreak.js 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @fileoverview enforce the location of arrow function bodies
  3. * @author Sharmila Jesupaul
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "layout",
  12. docs: {
  13. description: "enforce the location of arrow function bodies",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/implicit-arrow-linebreak"
  17. },
  18. fixable: "whitespace",
  19. schema: [
  20. {
  21. enum: ["beside", "below"]
  22. }
  23. ]
  24. },
  25. create(context) {
  26. const sourceCode = context.getSourceCode();
  27. //----------------------------------------------------------------------
  28. // Helpers
  29. //----------------------------------------------------------------------
  30. /**
  31. * Gets the applicable preference for a particular keyword
  32. * @returns {string} The applicable option for the keyword, e.g. 'beside'
  33. */
  34. function getOption() {
  35. return context.options[0] || "beside";
  36. }
  37. /**
  38. * Validates the location of an arrow function body
  39. * @param {ASTNode} node The arrow function body
  40. * @param {string} keywordName The applicable keyword name for the arrow function body
  41. * @returns {void}
  42. */
  43. function validateExpression(node) {
  44. const option = getOption();
  45. let tokenBefore = sourceCode.getTokenBefore(node.body);
  46. const hasParens = tokenBefore.value === "(";
  47. if (node.type === "BlockStatement") {
  48. return;
  49. }
  50. let fixerTarget = node.body;
  51. if (hasParens) {
  52. // Gets the first token before the function body that is not an open paren
  53. tokenBefore = sourceCode.getTokenBefore(node.body, token => token.value !== "(");
  54. fixerTarget = sourceCode.getTokenAfter(tokenBefore);
  55. }
  56. if (tokenBefore.loc.end.line === fixerTarget.loc.start.line && option === "below") {
  57. context.report({
  58. node: fixerTarget,
  59. message: "Expected a linebreak before this expression.",
  60. fix: fixer => fixer.insertTextBefore(fixerTarget, "\n")
  61. });
  62. } else if (tokenBefore.loc.end.line !== fixerTarget.loc.start.line && option === "beside") {
  63. context.report({
  64. node: fixerTarget,
  65. message: "Expected no linebreak before this expression.",
  66. fix: fixer => fixer.replaceTextRange([tokenBefore.range[1], fixerTarget.range[0]], " ")
  67. });
  68. }
  69. }
  70. //----------------------------------------------------------------------
  71. // Public
  72. //----------------------------------------------------------------------
  73. return {
  74. ArrowFunctionExpression: node => validateExpression(node)
  75. };
  76. }
  77. };