2 * @fileoverview enforce the location of arrow function bodies
3 * @author Sharmila Jesupaul
7 const { isCommentToken, isNotOpeningParenToken } = require("./utils/ast-utils");
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
17 description: "enforce the location of arrow function bodies",
18 category: "Stylistic Issues",
20 url: "https://eslint.org/docs/rules/implicit-arrow-linebreak"
23 fixable: "whitespace",
27 enum: ["beside", "below"]
31 expected: "Expected a linebreak before this expression.",
32 unexpected: "Expected no linebreak before this expression."
37 const sourceCode = context.getSourceCode();
38 const option = context.options[0] || "beside";
41 * Validates the location of an arrow function body
42 * @param {ASTNode} node The arrow function body
45 function validateExpression(node) {
46 if (node.body.type === "BlockStatement") {
50 const arrowToken = sourceCode.getTokenBefore(node.body, isNotOpeningParenToken);
51 const firstTokenOfBody = sourceCode.getTokenAfter(arrowToken);
53 if (arrowToken.loc.end.line === firstTokenOfBody.loc.start.line && option === "below") {
55 node: firstTokenOfBody,
56 messageId: "expected",
57 fix: fixer => fixer.insertTextBefore(firstTokenOfBody, "\n")
59 } else if (arrowToken.loc.end.line !== firstTokenOfBody.loc.start.line && option === "beside") {
61 node: firstTokenOfBody,
62 messageId: "unexpected",
64 if (sourceCode.getFirstTokenBetween(arrowToken, firstTokenOfBody, { includeComments: true, filter: isCommentToken })) {
68 return fixer.replaceTextRange([arrowToken.range[1], firstTokenOfBody.range[0]], " ");
74 //----------------------------------------------------------------------
76 //----------------------------------------------------------------------
78 ArrowFunctionExpression: node => validateExpression(node)