.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / arrowReturnShorthandRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 Palantir Technologies, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var utils_1 = require("../language/utils");
24 var arrowReturnShorthand_examples_1 = require("./code-examples/arrowReturnShorthand.examples");
25 var OPTION_MULTILINE = "multiline";
26 var Rule = /** @class */ (function (_super) {
27     tslib_1.__extends(Rule, _super);
28     function Rule() {
29         return _super !== null && _super.apply(this, arguments) || this;
30     }
31     /* tslint:enable:object-literal-sort-keys */
32     Rule.FAILURE_STRING = function (isObjectLiteral) {
33         var start = "This arrow function body can be simplified by omitting the curly braces and the keyword 'return'";
34         return (start + (isObjectLiteral ? ", and wrapping the object literal in parentheses." : "."));
35     };
36     Rule.prototype.apply = function (sourceFile) {
37         return this.applyWithFunction(sourceFile, walk, {
38             multiline: this.ruleArguments.indexOf(OPTION_MULTILINE) !== -1,
39         });
40     };
41     /* tslint:disable:object-literal-sort-keys */
42     Rule.metadata = {
43         ruleName: "arrow-return-shorthand",
44         description: "Suggests to convert `() => { return x; }` to `() => x`.",
45         hasFix: true,
46         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            If `", "` is specified, then this will warn even if the function spans multiple lines."], ["\n            If \\`", "\\` is specified, then this will warn even if the function spans multiple lines."])), OPTION_MULTILINE),
47         options: {
48             type: "string",
49             enum: [OPTION_MULTILINE],
50         },
51         optionExamples: [true, [true, OPTION_MULTILINE]],
52         rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            It's unnecessary to include `return` and `{}` brackets in arrow lambdas.\n            Leaving them out results in simpler and easier to read code.\n        "], ["\n            It's unnecessary to include \\`return\\` and \\`{}\\` brackets in arrow lambdas.\n            Leaving them out results in simpler and easier to read code.\n        "]))),
53         type: "style",
54         typescriptOnly: false,
55         codeExamples: arrowReturnShorthand_examples_1.codeExamples,
56     };
57     return Rule;
58 }(Lint.Rules.AbstractRule));
59 exports.Rule = Rule;
60 function walk(ctx) {
61     var sourceFile = ctx.sourceFile, multiline = ctx.options.multiline;
62     return ts.forEachChild(sourceFile, function cb(node) {
63         if (utils.isArrowFunction(node) && utils.isBlock(node.body)) {
64             var expr = getSimpleReturnExpression(node.body);
65             if (expr !== undefined &&
66                 (multiline ||
67                     utils.isSameLine(sourceFile, node.body.getStart(sourceFile), node.body.end))) {
68                 var isObjectLiteral = expr.kind === ts.SyntaxKind.ObjectLiteralExpression;
69                 ctx.addFailureAtNode(node.body, Rule.FAILURE_STRING(isObjectLiteral), createFix(node, node.body, expr, sourceFile.text));
70             }
71         }
72         return ts.forEachChild(node, cb);
73     });
74 }
75 function createFix(arrowFunction, body, expr, text) {
76     var statement = expr.parent;
77     var returnKeyword = utils.getChildOfKind(statement, ts.SyntaxKind.ReturnKeyword);
78     var arrow = utils.getChildOfKind(arrowFunction, ts.SyntaxKind.EqualsGreaterThanToken);
79     var openBrace = utils.getChildOfKind(body, ts.SyntaxKind.OpenBraceToken);
80     var closeBrace = utils.getChildOfKind(body, ts.SyntaxKind.CloseBraceToken);
81     var semicolon = utils.getChildOfKind(statement, ts.SyntaxKind.SemicolonToken);
82     var anyComments = hasComments(arrow) ||
83         hasComments(openBrace) ||
84         hasComments(statement) ||
85         hasComments(returnKeyword) ||
86         hasComments(expr) ||
87         (semicolon !== undefined && hasComments(semicolon)) ||
88         hasComments(closeBrace);
89     return anyComments
90         ? undefined
91         : (expr.kind === ts.SyntaxKind.ObjectLiteralExpression
92             ? [
93                 Lint.Replacement.appendText(expr.getStart(), "("),
94                 Lint.Replacement.appendText(expr.getEnd(), ")"),
95             ]
96             : []).concat([
97             // " {"
98             Lint.Replacement.deleteFromTo(arrow.end, openBrace.end),
99             // "return "
100             Lint.Replacement.deleteFromTo(statement.getStart(), expr.getStart()),
101             // " }" (may include semicolon)
102             Lint.Replacement.deleteFromTo(expr.end, closeBrace.end),
103         ]);
104     function hasComments(node) {
105         return utils_1.hasCommentAfterPosition(text, node.getEnd());
106     }
107 }
108 /** Given `{ return x; }`, return `x`. */
109 function getSimpleReturnExpression(block) {
110     return block.statements.length === 1 &&
111         block.statements[0].kind === ts.SyntaxKind.ReturnStatement
112         ? block.statements[0].expression
113         : undefined;
114 }
115 var templateObject_1, templateObject_2;