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 tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var utils_1 = require("../utils");
24 var Rule = /** @class */ (function (_super) {
25 tslib_1.__extends(Rule, _super);
27 return _super !== null && _super.apply(this, arguments) || this;
29 Rule.prototype.apply = function (sourceFile) {
30 return this.applyWithWalker(new NewlineBeforeReturnWalker(sourceFile, this.ruleName, undefined));
32 /* tslint:disable:object-literal-sort-keys */
34 ruleName: "newline-before-return",
35 description: "Enforces blank line before return when not the only line in the block.",
36 rationale: "Helps maintain a readable style in your codebase.",
38 optionsDescription: "Not configurable.",
40 optionExamples: [true],
42 typescriptOnly: false,
44 /* tslint:enable:object-literal-sort-keys */
45 Rule.FAILURE_STRING = "Missing blank line before return";
47 }(Lint.Rules.AbstractRule));
49 var NewlineBeforeReturnWalker = /** @class */ (function (_super) {
50 tslib_1.__extends(NewlineBeforeReturnWalker, _super);
51 function NewlineBeforeReturnWalker() {
52 return _super !== null && _super.apply(this, arguments) || this;
54 NewlineBeforeReturnWalker.prototype.walk = function (sourceFile) {
56 var cb = function (node) {
57 if (node.kind === ts.SyntaxKind.ReturnStatement) {
58 _this.visitReturnStatement(node);
60 return ts.forEachChild(node, cb);
62 return ts.forEachChild(sourceFile, cb);
64 NewlineBeforeReturnWalker.prototype.visitReturnStatement = function (node) {
65 var prev = tsutils_1.getPreviousStatement(node);
66 if (prev === undefined) {
67 // return is not within a block (e.g. the only child of an IfStatement) or the first statement of the block
68 // no need to check for preceding newline
71 var start = node.getStart(this.sourceFile);
72 var line = ts.getLineAndCharacterOfPosition(this.sourceFile, start).line;
73 var comments = ts.getLeadingCommentRanges(this.sourceFile.text, node.pos);
74 if (comments !== undefined) {
75 // check for blank lines between comments
76 for (var i = comments.length - 1; i >= 0; --i) {
77 var endLine = ts.getLineAndCharacterOfPosition(this.sourceFile, comments[i].end)
79 if (endLine < line - 1) {
82 start = comments[i].pos;
83 line = ts.getLineAndCharacterOfPosition(this.sourceFile, start).line;
86 var prevLine = ts.getLineAndCharacterOfPosition(this.sourceFile, prev.end).line;
87 if (prevLine >= line - 1) {
88 var fixer = Lint.Replacement.replaceFromTo(line === prevLine ? node.pos : node.pos + 1, start, line === prevLine
89 ? utils_1.newLineWithIndentation(prev, this.sourceFile, 2)
90 : utils_1.newLineWithIndentation(prev, this.sourceFile));
91 // Previous statement is on the same or previous line
92 this.addFailure(start, start, Rule.FAILURE_STRING, fixer);
95 return NewlineBeforeReturnWalker;
96 }(Lint.AbstractWalker));