]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/function-call-argument-newline.js
ed4e296fd6e90f03fdca8968fe0cc02b511a09d4
2 * @fileoverview Rule to enforce line breaks between arguments of a function call
3 * @author Alexey Gonchar <https://github.com/finico>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description
: "enforce line breaks between arguments of a function call",
19 url
: "https://eslint.org/docs/rules/function-call-argument-newline"
22 fixable
: "whitespace",
26 enum: ["always", "never", "consistent"]
31 unexpectedLineBreak
: "There should be no line break here.",
32 missingLineBreak
: "There should be a line break after this argument."
37 const sourceCode
= context
.getSourceCode();
41 messageId
: "unexpectedLineBreak",
42 check
: (prevToken
, currentToken
) => prevToken
.loc
.end
.line
!== currentToken
.loc
.start
.line
,
43 createFix
: (token
, tokenBefore
) => fixer
=>
44 fixer
.replaceTextRange([tokenBefore
.range
[1], token
.range
[0]], " ")
47 messageId
: "missingLineBreak",
48 check
: (prevToken
, currentToken
) => prevToken
.loc
.end
.line
=== currentToken
.loc
.start
.line
,
49 createFix
: (token
, tokenBefore
) => fixer
=>
50 fixer
.replaceTextRange([tokenBefore
.range
[1], token
.range
[0]], "\n")
55 * Check all arguments for line breaks in the CallExpression
56 * @param {CallExpression} node node to evaluate
57 * @param {{ messageId: string, check: Function }} checker selected checker
61 function checkArguments(node
, checker
) {
62 for (let i
= 1; i
< node
.arguments
.length
; i
++) {
63 const prevArgToken
= sourceCode
.getLastToken(node
.arguments
[i
- 1]);
64 const currentArgToken
= sourceCode
.getFirstToken(node
.arguments
[i
]);
66 if (checker
.check(prevArgToken
, currentArgToken
)) {
67 const tokenBefore
= sourceCode
.getTokenBefore(
69 { includeComments
: true }
72 const hasLineCommentBefore
= tokenBefore
.type
=== "Line";
77 start
: tokenBefore
.loc
.end
,
78 end
: currentArgToken
.loc
.start
80 messageId
: checker
.messageId
,
81 fix
: hasLineCommentBefore
? null : checker
.createFix(currentArgToken
, tokenBefore
)
88 * Check if open space is present in a function name
89 * @param {CallExpression} node node to evaluate
93 function check(node
) {
94 if (node
.arguments
.length
< 2) {
98 const option
= context
.options
[0] || "always";
100 if (option
=== "never") {
101 checkArguments(node
, checkers
.unexpected
);
102 } else if (option
=== "always") {
103 checkArguments(node
, checkers
.missing
);
104 } else if (option
=== "consistent") {
105 const firstArgToken
= sourceCode
.getLastToken(node
.arguments
[0]);
106 const secondArgToken
= sourceCode
.getFirstToken(node
.arguments
[1]);
108 if (firstArgToken
.loc
.end
.line
=== secondArgToken
.loc
.start
.line
) {
109 checkArguments(node
, checkers
.unexpected
);
111 checkArguments(node
, checkers
.missing
);
117 CallExpression
: check
,