aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tslint/lib/rules/indentRule.js
blob: 7d133a13b32054a7e6a0924b7f0e9d986b41237f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"use strict";
/**
 * @license
 * Copyright 2013 Palantir Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ts = require("typescript");
var Lint = require("../index");
var OPTION_USE_TABS = "tabs";
var OPTION_USE_SPACES = "spaces";
var OPTION_INDENT_SIZE_2 = 2;
var OPTION_INDENT_SIZE_4 = 4;
var Rule = (function (_super) {
    tslib_1.__extends(Rule, _super);
    function Rule() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Rule.prototype.apply = function (sourceFile) {
        return this.applyWithWalker(new IndentWalker(sourceFile, this.getOptions()));
    };
    return Rule;
}(Lint.Rules.AbstractRule));
/* tslint:disable:object-literal-sort-keys */
Rule.metadata = {
    ruleName: "indent",
    description: "Enforces indentation with tabs or spaces.",
    rationale: (_a = ["\n            Using only one of tabs or spaces for indentation leads to more consistent editor behavior,\n            cleaner diffs in version control, and easier programmatic manipulation."], _a.raw = ["\n            Using only one of tabs or spaces for indentation leads to more consistent editor behavior,\n            cleaner diffs in version control, and easier programmatic manipulation."], Lint.Utils.dedent(_a)),
    optionsDescription: (_b = ["\n            One of the following arguments must be provided:\n\n            * `", "` enforces consistent spaces.\n            * `", "` enforces consistent tabs.\n\n            A second optional argument specifies indentation size:\n\n            * `", "` enforces 2 space indentation.\n            * `", "` enforces 4 space indentation.\n\n            Indentation size is required for auto-fixing, but not for rule checking.\n            "], _b.raw = ["\n            One of the following arguments must be provided:\n\n            * \\`", "\\` enforces consistent spaces.\n            * \\`", "\\` enforces consistent tabs.\n\n            A second optional argument specifies indentation size:\n\n            * \\`", "\\` enforces 2 space indentation.\n            * \\`", "\\` enforces 4 space indentation.\n\n            Indentation size is required for auto-fixing, but not for rule checking.\n            "], Lint.Utils.dedent(_b, OPTION_USE_SPACES, OPTION_USE_TABS, OPTION_INDENT_SIZE_2.toString(), OPTION_INDENT_SIZE_4.toString())),
    options: {
        type: "array",
        items: [
            {
                type: "string",
                enum: [OPTION_USE_TABS, OPTION_USE_SPACES],
            },
            {
                type: "number",
                enum: [OPTION_INDENT_SIZE_2, OPTION_INDENT_SIZE_4],
            },
        ],
        minLength: 0,
        maxLength: 5,
    },
    optionExamples: [
        [true, OPTION_USE_SPACES],
        [true, OPTION_USE_SPACES, OPTION_INDENT_SIZE_4],
        [true, OPTION_USE_TABS, OPTION_INDENT_SIZE_2],
    ],
    type: "maintainability",
    typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */
Rule.FAILURE_STRING_TABS = "tab indentation expected";
Rule.FAILURE_STRING_SPACES = "space indentation expected";
exports.Rule = Rule;
// visit every token and enforce that only the right character is used for indentation
var IndentWalker = (function (_super) {
    tslib_1.__extends(IndentWalker, _super);
    function IndentWalker(sourceFile, options) {
        var _this = _super.call(this, sourceFile, options) || this;
        // fixer is only provided with the indent size arg
        if (_this.getOptions().length === 2 && typeof _this.getOptions()[1] === "number"
            && (_this.getOptions()[1] === OPTION_INDENT_SIZE_2 || _this.getOptions()[1] === OPTION_INDENT_SIZE_4)) {
            // tslint:disable-next-line:no-unsafe-any
            var size_1 = _this.getOptions()[1];
            var replaceRegExp_1;
            var replaceIndent_1;
            if (_this.hasOption(OPTION_USE_TABS)) {
                _this.regExp = new RegExp(" ".repeat(size_1));
                _this.failureString = Rule.FAILURE_STRING_TABS;
                // we want to find every group of `size` spaces, plus up to one 'incomplete' group
                replaceRegExp_1 = new RegExp("^( {" + size_1 + "})+( {1," + (size_1 - 1) + "})?", "g");
                replaceIndent_1 = "\t";
            }
            else if (_this.hasOption(OPTION_USE_SPACES)) {
                _this.regExp = new RegExp("\t");
                _this.failureString = size_1 + " " + Rule.FAILURE_STRING_SPACES;
                replaceRegExp_1 = new RegExp("\t", "g");
                replaceIndent_1 = " ".repeat(size_1);
            }
            _this.replacementFactory = function (lineStart, fullLeadingWhitespace) {
                return new Lint.Replacement(lineStart, fullLeadingWhitespace.length, fullLeadingWhitespace.replace(replaceRegExp_1, function (match) { return replaceIndent_1.repeat(Math.ceil(match.length / size_1)); }));
            };
        }
        else {
            if (_this.hasOption(OPTION_USE_TABS)) {
                _this.regExp = new RegExp(" ");
                _this.failureString = Rule.FAILURE_STRING_TABS;
            }
            else if (_this.hasOption(OPTION_USE_SPACES)) {
                _this.regExp = new RegExp("\t");
                _this.failureString = Rule.FAILURE_STRING_SPACES;
            }
            _this.replacementFactory = function () { return undefined; };
        }
        return _this;
    }
    IndentWalker.prototype.visitSourceFile = function (node) {
        if (!this.hasOption(OPTION_USE_TABS) && !this.hasOption(OPTION_USE_SPACES)) {
            // if we don't have either option, no need to check anything, and no need to call super, so just return
            return;
        }
        var endOfComment = -1;
        var endOfTemplateString = -1;
        var scanner = ts.createScanner(ts.ScriptTarget.ES5, false, ts.LanguageVariant.Standard, node.text);
        for (var _i = 0, _a = node.getLineStarts(); _i < _a.length; _i++) {
            var lineStart = _a[_i];
            if (lineStart < endOfComment || lineStart < endOfTemplateString) {
                // skip checking lines inside multi-line comments or template strings
                continue;
            }
            scanner.setTextPos(lineStart);
            var currentScannedType = scanner.scan();
            var fullLeadingWhitespace = "";
            var lastStartPos = -1;
            while (currentScannedType === ts.SyntaxKind.WhitespaceTrivia) {
                var startPos = scanner.getStartPos();
                if (startPos === lastStartPos) {
                    break;
                }
                lastStartPos = startPos;
                fullLeadingWhitespace += scanner.getTokenText();
                currentScannedType = scanner.scan();
            }
            var commentRanges = ts.getTrailingCommentRanges(node.text, lineStart);
            if (commentRanges !== undefined) {
                endOfComment = commentRanges[commentRanges.length - 1].end;
            }
            else {
                var scanType = currentScannedType;
                // scan until we reach end of line, skipping over template strings
                while (scanType !== ts.SyntaxKind.NewLineTrivia && scanType !== ts.SyntaxKind.EndOfFileToken) {
                    if (scanType === ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
                        // template string without expressions - skip past it
                        endOfTemplateString = scanner.getStartPos() + scanner.getTokenText().length;
                    }
                    else if (scanType === ts.SyntaxKind.TemplateHead) {
                        // find end of template string containing expressions...
                        while (scanType !== ts.SyntaxKind.TemplateTail && scanType !== ts.SyntaxKind.EndOfFileToken) {
                            scanType = scanner.scan();
                            if (scanType === ts.SyntaxKind.CloseBraceToken) {
                                scanType = scanner.reScanTemplateToken();
                            }
                        }
                        // ... and skip past it
                        endOfTemplateString = scanner.getStartPos() + scanner.getTokenText().length;
                    }
                    scanType = scanner.scan();
                }
            }
            switch (currentScannedType) {
                case ts.SyntaxKind.SingleLineCommentTrivia:
                case ts.SyntaxKind.MultiLineCommentTrivia:
                case ts.SyntaxKind.NewLineTrivia:
                    // ignore lines that have comments before the first token
                    continue;
            }
            if (this.regExp.test(fullLeadingWhitespace)) {
                this.addFailureAt(lineStart, fullLeadingWhitespace.length, this.failureString, this.replacementFactory(lineStart, fullLeadingWhitespace));
            }
        }
        // no need to call super to visit the rest of the nodes, so don't call super here
    };
    return IndentWalker;
}(Lint.RuleWalker));
var _a, _b;