[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview enforce the location of arrow function bodies
|
---|
| 3 | * @author Sharmila Jesupaul
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const { isCommentToken, isNotOpeningParenToken } = require("./utils/ast-utils");
|
---|
| 9 |
|
---|
| 10 | //------------------------------------------------------------------------------
|
---|
| 11 | // Rule Definition
|
---|
| 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 the location of arrow function bodies",
|
---|
| 22 | recommended: false,
|
---|
| 23 | url: "https://eslint.org/docs/latest/rules/implicit-arrow-linebreak"
|
---|
| 24 | },
|
---|
| 25 |
|
---|
| 26 | fixable: "whitespace",
|
---|
| 27 |
|
---|
| 28 | schema: [
|
---|
| 29 | {
|
---|
| 30 | enum: ["beside", "below"]
|
---|
| 31 | }
|
---|
| 32 | ],
|
---|
| 33 | messages: {
|
---|
| 34 | expected: "Expected a linebreak before this expression.",
|
---|
| 35 | unexpected: "Expected no linebreak before this expression."
|
---|
| 36 | }
|
---|
| 37 | },
|
---|
| 38 |
|
---|
| 39 | create(context) {
|
---|
| 40 | const sourceCode = context.sourceCode;
|
---|
| 41 | const option = context.options[0] || "beside";
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Validates the location of an arrow function body
|
---|
| 45 | * @param {ASTNode} node The arrow function body
|
---|
| 46 | * @returns {void}
|
---|
| 47 | */
|
---|
| 48 | function validateExpression(node) {
|
---|
| 49 | if (node.body.type === "BlockStatement") {
|
---|
| 50 | return;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | const arrowToken = sourceCode.getTokenBefore(node.body, isNotOpeningParenToken);
|
---|
| 54 | const firstTokenOfBody = sourceCode.getTokenAfter(arrowToken);
|
---|
| 55 |
|
---|
| 56 | if (arrowToken.loc.end.line === firstTokenOfBody.loc.start.line && option === "below") {
|
---|
| 57 | context.report({
|
---|
| 58 | node: firstTokenOfBody,
|
---|
| 59 | messageId: "expected",
|
---|
| 60 | fix: fixer => fixer.insertTextBefore(firstTokenOfBody, "\n")
|
---|
| 61 | });
|
---|
| 62 | } else if (arrowToken.loc.end.line !== firstTokenOfBody.loc.start.line && option === "beside") {
|
---|
| 63 | context.report({
|
---|
| 64 | node: firstTokenOfBody,
|
---|
| 65 | messageId: "unexpected",
|
---|
| 66 | fix(fixer) {
|
---|
| 67 | if (sourceCode.getFirstTokenBetween(arrowToken, firstTokenOfBody, { includeComments: true, filter: isCommentToken })) {
|
---|
| 68 | return null;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | return fixer.replaceTextRange([arrowToken.range[1], firstTokenOfBody.range[0]], " ");
|
---|
| 72 | }
|
---|
| 73 | });
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | //----------------------------------------------------------------------
|
---|
| 78 | // Public
|
---|
| 79 | //----------------------------------------------------------------------
|
---|
| 80 | return {
|
---|
| 81 | ArrowFunctionExpression: node => validateExpression(node)
|
---|
| 82 | };
|
---|
| 83 | }
|
---|
| 84 | };
|
---|