4 * Copyright 2017 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
29 return _super !== null && _super.apply(this, arguments) || this;
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." : "."));
36 Rule.prototype.apply = function (sourceFile) {
37 return this.applyWithFunction(sourceFile, walk, {
38 multiline: this.ruleArguments.indexOf(OPTION_MULTILINE) !== -1,
41 /* tslint:disable:object-literal-sort-keys */
43 ruleName: "arrow-return-shorthand",
44 description: "Suggests to convert `() => { return x; }` to `() => x`.",
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),
49 enum: [OPTION_MULTILINE],
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 "]))),
54 typescriptOnly: false,
55 codeExamples: arrowReturnShorthand_examples_1.codeExamples,
58 }(Lint.Rules.AbstractRule));
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 &&
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));
72 return ts.forEachChild(node, cb);
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) ||
87 (semicolon !== undefined && hasComments(semicolon)) ||
88 hasComments(closeBrace);
91 : (expr.kind === ts.SyntaxKind.ObjectLiteralExpression
93 Lint.Replacement.appendText(expr.getStart(), "("),
94 Lint.Replacement.appendText(expr.getEnd(), ")"),
98 Lint.Replacement.deleteFromTo(arrow.end, openBrace.end),
100 Lint.Replacement.deleteFromTo(statement.getStart(), expr.getStart()),
101 // " }" (may include semicolon)
102 Lint.Replacement.deleteFromTo(expr.end, closeBrace.end),
104 function hasComments(node) {
105 return utils_1.hasCommentAfterPosition(text, node.getEnd());
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
115 var templateObject_1, templateObject_2;