]>
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 //------------------------------------------------------------------------------
15 /** @type {import('../shared/types').Rule} */
21 description
: "Require a newline after each call in a method chain",
23 url
: "https://eslint.org/docs/rules/newline-per-chained-call"
26 fixable
: "whitespace",
31 ignoreChainWithDepth
: {
38 additionalProperties
: false
41 expected
: "Expected line break before `{{callee}}`."
47 const options
= context
.options
[0] || {},
48 ignoreChainWithDepth
= options
.ignoreChainWithDepth
|| 2;
50 const sourceCode
= context
.getSourceCode();
53 * Get the prefix of a given MemberExpression node.
54 * If the MemberExpression node is a computed value it returns a
55 * left bracket. If not it returns a period.
56 * @param {ASTNode} node A MemberExpression node to get
57 * @returns {string} The prefix of the node.
59 function getPrefix(node
) {
73 * Gets the property text of a given MemberExpression node.
74 * If the text is multiline, this returns only the first line.
75 * @param {ASTNode} node A MemberExpression node to get.
76 * @returns {string} The property text of the node.
78 function getPropertyText(node
) {
79 const prefix
= getPrefix(node
);
80 const lines
= sourceCode
.getText(node
.property
).split(astUtils
.LINEBREAK_MATCHER
);
81 const suffix
= node
.computed
&& lines
.length
=== 1 ? "]" : "";
83 return prefix
+ lines
[0] + suffix
;
87 "CallExpression:exit"(node
) {
88 const callee
= astUtils
.skipChainExpression(node
.callee
);
90 if (callee
.type
!== "MemberExpression") {
94 let parent
= astUtils
.skipChainExpression(callee
.object
);
97 while (parent
&& parent
.callee
) {
99 parent
= astUtils
.skipChainExpression(astUtils
.skipChainExpression(parent
.callee
).object
);
102 if (depth
> ignoreChainWithDepth
&& astUtils
.isTokenOnSameLine(callee
.object
, callee
.property
)) {
103 const firstTokenAfterObject
= sourceCode
.getTokenAfter(callee
.object
, astUtils
.isNotClosingParenToken
);
106 node
: callee
.property
,
108 start
: firstTokenAfterObject
.loc
.start
,
111 messageId
: "expected",
113 callee
: getPropertyText(callee
)
116 return fixer
.insertTextBefore(firstTokenAfterObject
, "\n");