.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / newlineBeforeReturnRule.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 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);
26     function Rule() {
27         return _super !== null && _super.apply(this, arguments) || this;
28     }
29     Rule.prototype.apply = function (sourceFile) {
30         return this.applyWithWalker(new NewlineBeforeReturnWalker(sourceFile, this.ruleName, undefined));
31     };
32     /* tslint:disable:object-literal-sort-keys */
33     Rule.metadata = {
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.",
37         hasFix: true,
38         optionsDescription: "Not configurable.",
39         options: {},
40         optionExamples: [true],
41         type: "formatting",
42         typescriptOnly: false,
43     };
44     /* tslint:enable:object-literal-sort-keys */
45     Rule.FAILURE_STRING = "Missing blank line before return";
46     return Rule;
47 }(Lint.Rules.AbstractRule));
48 exports.Rule = Rule;
49 var NewlineBeforeReturnWalker = /** @class */ (function (_super) {
50     tslib_1.__extends(NewlineBeforeReturnWalker, _super);
51     function NewlineBeforeReturnWalker() {
52         return _super !== null && _super.apply(this, arguments) || this;
53     }
54     NewlineBeforeReturnWalker.prototype.walk = function (sourceFile) {
55         var _this = this;
56         var cb = function (node) {
57             if (node.kind === ts.SyntaxKind.ReturnStatement) {
58                 _this.visitReturnStatement(node);
59             }
60             return ts.forEachChild(node, cb);
61         };
62         return ts.forEachChild(sourceFile, cb);
63     };
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
69             return;
70         }
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)
78                     .line;
79                 if (endLine < line - 1) {
80                     return;
81                 }
82                 start = comments[i].pos;
83                 line = ts.getLineAndCharacterOfPosition(this.sourceFile, start).line;
84             }
85         }
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);
93         }
94     };
95     return NewlineBeforeReturnWalker;
96 }(Lint.AbstractWalker));