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