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",
18 category: "Stylistic Issues",
20 url: "https://eslint.org/docs/rules/function-call-argument-newline"
23 fixable: "whitespace",
27 enum: ["always", "never", "consistent"]
32 unexpectedLineBreak: "There should be no line break here.",
33 missingLineBreak: "There should be a line break after this argument."
38 const sourceCode = context.getSourceCode();
42 messageId: "unexpectedLineBreak",
43 check: (prevToken, currentToken) => prevToken.loc.end.line !== currentToken.loc.start.line,
44 createFix: (token, tokenBefore) => fixer =>
45 fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], " ")
48 messageId: "missingLineBreak",
49 check: (prevToken, currentToken) => prevToken.loc.end.line === currentToken.loc.start.line,
50 createFix: (token, tokenBefore) => fixer =>
51 fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], "\n")
56 * Check all arguments for line breaks in the CallExpression
57 * @param {CallExpression} node node to evaluate
58 * @param {{ messageId: string, check: Function }} checker selected checker
62 function checkArguments(node, checker) {
63 for (let i = 1; i < node.arguments.length; i++) {
64 const prevArgToken = sourceCode.getLastToken(node.arguments[i - 1]);
65 const currentArgToken = sourceCode.getFirstToken(node.arguments[i]);
67 if (checker.check(prevArgToken, currentArgToken)) {
68 const tokenBefore = sourceCode.getTokenBefore(
70 { includeComments: true }
73 const hasLineCommentBefore = tokenBefore.type === "Line";
78 start: tokenBefore.loc.end,
79 end: currentArgToken.loc.start
81 messageId: checker.messageId,
82 fix: hasLineCommentBefore ? null : checker.createFix(currentArgToken, tokenBefore)
89 * Check if open space is present in a function name
90 * @param {CallExpression} node node to evaluate
94 function check(node) {
95 if (node.arguments.length < 2) {
99 const option = context.options[0] || "always";
101 if (option === "never") {
102 checkArguments(node, checkers.unexpected);
103 } else if (option === "always") {
104 checkArguments(node, checkers.missing);
105 } else if (option === "consistent") {
106 const firstArgToken = sourceCode.getLastToken(node.arguments[0]);
107 const secondArgToken = sourceCode.getFirstToken(node.arguments[1]);
109 if (firstArgToken.loc.end.line === secondArgToken.loc.start.line) {
110 checkArguments(node, checkers.unexpected);
112 checkArguments(node, checkers.missing);
118 CallExpression: check,