[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to enforce line breaks between arguments of a function call
|
---|
| 3 | * @author Alexey Gonchar <https://github.com/finico>
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | "use strict";
|
---|
| 8 |
|
---|
| 9 | //------------------------------------------------------------------------------
|
---|
| 10 | // Rule Definition
|
---|
| 11 | //------------------------------------------------------------------------------
|
---|
| 12 |
|
---|
| 13 | /** @type {import('../shared/types').Rule} */
|
---|
| 14 | module.exports = {
|
---|
| 15 | meta: {
|
---|
| 16 | deprecated: true,
|
---|
| 17 | replacedBy: [],
|
---|
| 18 | type: "layout",
|
---|
| 19 |
|
---|
| 20 | docs: {
|
---|
| 21 | description: "Enforce line breaks between arguments of a function call",
|
---|
| 22 | recommended: false,
|
---|
| 23 | url: "https://eslint.org/docs/latest/rules/function-call-argument-newline"
|
---|
| 24 | },
|
---|
| 25 |
|
---|
| 26 | fixable: "whitespace",
|
---|
| 27 |
|
---|
| 28 | schema: [
|
---|
| 29 | {
|
---|
| 30 | enum: ["always", "never", "consistent"]
|
---|
| 31 | }
|
---|
| 32 | ],
|
---|
| 33 |
|
---|
| 34 | messages: {
|
---|
| 35 | unexpectedLineBreak: "There should be no line break here.",
|
---|
| 36 | missingLineBreak: "There should be a line break after this argument."
|
---|
| 37 | }
|
---|
| 38 | },
|
---|
| 39 |
|
---|
| 40 | create(context) {
|
---|
| 41 | const sourceCode = context.sourceCode;
|
---|
| 42 |
|
---|
| 43 | const checkers = {
|
---|
| 44 | unexpected: {
|
---|
| 45 | messageId: "unexpectedLineBreak",
|
---|
| 46 | check: (prevToken, currentToken) => prevToken.loc.end.line !== currentToken.loc.start.line,
|
---|
| 47 | createFix: (token, tokenBefore) => fixer =>
|
---|
| 48 | fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], " ")
|
---|
| 49 | },
|
---|
| 50 | missing: {
|
---|
| 51 | messageId: "missingLineBreak",
|
---|
| 52 | check: (prevToken, currentToken) => prevToken.loc.end.line === currentToken.loc.start.line,
|
---|
| 53 | createFix: (token, tokenBefore) => fixer =>
|
---|
| 54 | fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], "\n")
|
---|
| 55 | }
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * Check all arguments for line breaks in the CallExpression
|
---|
| 60 | * @param {CallExpression} node node to evaluate
|
---|
| 61 | * @param {{ messageId: string, check: Function }} checker selected checker
|
---|
| 62 | * @returns {void}
|
---|
| 63 | * @private
|
---|
| 64 | */
|
---|
| 65 | function checkArguments(node, checker) {
|
---|
| 66 | for (let i = 1; i < node.arguments.length; i++) {
|
---|
| 67 | const prevArgToken = sourceCode.getLastToken(node.arguments[i - 1]);
|
---|
| 68 | const currentArgToken = sourceCode.getFirstToken(node.arguments[i]);
|
---|
| 69 |
|
---|
| 70 | if (checker.check(prevArgToken, currentArgToken)) {
|
---|
| 71 | const tokenBefore = sourceCode.getTokenBefore(
|
---|
| 72 | currentArgToken,
|
---|
| 73 | { includeComments: true }
|
---|
| 74 | );
|
---|
| 75 |
|
---|
| 76 | const hasLineCommentBefore = tokenBefore.type === "Line";
|
---|
| 77 |
|
---|
| 78 | context.report({
|
---|
| 79 | node,
|
---|
| 80 | loc: {
|
---|
| 81 | start: tokenBefore.loc.end,
|
---|
| 82 | end: currentArgToken.loc.start
|
---|
| 83 | },
|
---|
| 84 | messageId: checker.messageId,
|
---|
| 85 | fix: hasLineCommentBefore ? null : checker.createFix(currentArgToken, tokenBefore)
|
---|
| 86 | });
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * Check if open space is present in a function name
|
---|
| 93 | * @param {CallExpression} node node to evaluate
|
---|
| 94 | * @returns {void}
|
---|
| 95 | * @private
|
---|
| 96 | */
|
---|
| 97 | function check(node) {
|
---|
| 98 | if (node.arguments.length < 2) {
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | const option = context.options[0] || "always";
|
---|
| 103 |
|
---|
| 104 | if (option === "never") {
|
---|
| 105 | checkArguments(node, checkers.unexpected);
|
---|
| 106 | } else if (option === "always") {
|
---|
| 107 | checkArguments(node, checkers.missing);
|
---|
| 108 | } else if (option === "consistent") {
|
---|
| 109 | const firstArgToken = sourceCode.getLastToken(node.arguments[0]);
|
---|
| 110 | const secondArgToken = sourceCode.getFirstToken(node.arguments[1]);
|
---|
| 111 |
|
---|
| 112 | if (firstArgToken.loc.end.line === secondArgToken.loc.start.line) {
|
---|
| 113 | checkArguments(node, checkers.unexpected);
|
---|
| 114 | } else {
|
---|
| 115 | checkArguments(node, checkers.missing);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | return {
|
---|
| 121 | CallExpression: check,
|
---|
| 122 | NewExpression: check
|
---|
| 123 | };
|
---|
| 124 | }
|
---|
| 125 | };
|
---|