]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/implicit-arrow-linebreak.js
2d09552440b2007bffc6de43d59caab4cc38d37a
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",
19 url
: "https://eslint.org/docs/rules/implicit-arrow-linebreak"
22 fixable
: "whitespace",
26 enum: ["beside", "below"]
30 expected
: "Expected a linebreak before this expression.",
31 unexpected
: "Expected no linebreak before this expression."
36 const sourceCode
= context
.getSourceCode();
37 const option
= context
.options
[0] || "beside";
40 * Validates the location of an arrow function body
41 * @param {ASTNode} node The arrow function body
44 function validateExpression(node
) {
45 if (node
.body
.type
=== "BlockStatement") {
49 const arrowToken
= sourceCode
.getTokenBefore(node
.body
, isNotOpeningParenToken
);
50 const firstTokenOfBody
= sourceCode
.getTokenAfter(arrowToken
);
52 if (arrowToken
.loc
.end
.line
=== firstTokenOfBody
.loc
.start
.line
&& option
=== "below") {
54 node
: firstTokenOfBody
,
55 messageId
: "expected",
56 fix
: fixer
=> fixer
.insertTextBefore(firstTokenOfBody
, "\n")
58 } else if (arrowToken
.loc
.end
.line
!== firstTokenOfBody
.loc
.start
.line
&& option
=== "beside") {
60 node
: firstTokenOfBody
,
61 messageId
: "unexpected",
63 if (sourceCode
.getFirstTokenBetween(arrowToken
, firstTokenOfBody
, { includeComments
: true, filter
: isCommentToken
})) {
67 return fixer
.replaceTextRange([arrowToken
.range
[1], firstTokenOfBody
.range
[0]], " ");
73 //----------------------------------------------------------------------
75 //----------------------------------------------------------------------
77 ArrowFunctionExpression
: node
=> validateExpression(node
)