]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/newline-per-chained-call.js
2 * @fileoverview Rule to ensure newline per method call when chaining calls
3 * @author Rajendra Patil
4 * @author Burak Yigit Kaya
9 const astUtils
= require("./utils/ast-utils");
11 //------------------------------------------------------------------------------
13 //------------------------------------------------------------------------------
20 description
: "require a newline after each call in a method chain",
22 url
: "https://eslint.org/docs/rules/newline-per-chained-call"
25 fixable
: "whitespace",
30 ignoreChainWithDepth
: {
37 additionalProperties
: false
40 expected
: "Expected line break before `{{callee}}`."
46 const options
= context
.options
[0] || {},
47 ignoreChainWithDepth
= options
.ignoreChainWithDepth
|| 2;
49 const sourceCode
= context
.getSourceCode();
52 * Get the prefix of a given MemberExpression node.
53 * If the MemberExpression node is a computed value it returns a
54 * left bracket. If not it returns a period.
55 * @param {ASTNode} node A MemberExpression node to get
56 * @returns {string} The prefix of the node.
58 function getPrefix(node
) {
72 * Gets the property text of a given MemberExpression node.
73 * If the text is multiline, this returns only the first line.
74 * @param {ASTNode} node A MemberExpression node to get.
75 * @returns {string} The property text of the node.
77 function getPropertyText(node
) {
78 const prefix
= getPrefix(node
);
79 const lines
= sourceCode
.getText(node
.property
).split(astUtils
.LINEBREAK_MATCHER
);
80 const suffix
= node
.computed
&& lines
.length
=== 1 ? "]" : "";
82 return prefix
+ lines
[0] + suffix
;
86 "CallExpression:exit"(node
) {
87 const callee
= astUtils
.skipChainExpression(node
.callee
);
89 if (callee
.type
!== "MemberExpression") {
93 let parent
= astUtils
.skipChainExpression(callee
.object
);
96 while (parent
&& parent
.callee
) {
98 parent
= astUtils
.skipChainExpression(astUtils
.skipChainExpression(parent
.callee
).object
);
101 if (depth
> ignoreChainWithDepth
&& astUtils
.isTokenOnSameLine(callee
.object
, callee
.property
)) {
102 const firstTokenAfterObject
= sourceCode
.getTokenAfter(callee
.object
, astUtils
.isNotClosingParenToken
);
105 node
: callee
.property
,
107 start
: firstTokenAfterObject
.loc
.start
,
110 messageId
: "expected",
112 callee
: getPropertyText(callee
)
115 return fixer
.insertTextBefore(firstTokenAfterObject
, "\n");