4 * Copyright 2013 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 OPTION_USE_TABS = "tabs";
24 var OPTION_USE_SPACES = "spaces";
25 var OPTION_INDENT_SIZE_2 = 2;
26 var OPTION_INDENT_SIZE_4 = 4;
27 var Rule = /** @class */ (function (_super) {
28 tslib_1.__extends(Rule, _super);
30 return _super !== null && _super.apply(this, arguments) || this;
32 /* tslint:enable:object-literal-sort-keys */
33 Rule.FAILURE_STRING = function (expected) {
34 return expected + " indentation expected";
36 Rule.prototype.apply = function (sourceFile) {
37 var options = parseOptions(this.ruleArguments);
38 return options === undefined ? [] : this.applyWithFunction(sourceFile, walk, options);
40 /* tslint:disable:object-literal-sort-keys */
43 description: "Enforces indentation with tabs or spaces.",
44 rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\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."], ["\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."]))),
45 optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\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\n **NOTE**: auto-fixing will only convert invalid indent whitespace to the desired type, it will not fix invalid whitespace sizes.\n "], ["\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\n **NOTE**: auto-fixing will only convert invalid indent whitespace to the desired type, it will not fix invalid whitespace sizes.\n "])), OPTION_USE_SPACES, OPTION_USE_TABS, OPTION_INDENT_SIZE_2.toString(), OPTION_INDENT_SIZE_4.toString()),
51 enum: [OPTION_USE_TABS, OPTION_USE_SPACES],
55 enum: [OPTION_INDENT_SIZE_2, OPTION_INDENT_SIZE_4],
62 [true, OPTION_USE_SPACES],
63 [true, OPTION_USE_SPACES, OPTION_INDENT_SIZE_4],
64 [true, OPTION_USE_TABS, OPTION_INDENT_SIZE_2],
68 typescriptOnly: false,
71 }(Lint.Rules.AbstractRule));
73 function parseOptions(ruleArguments) {
74 var type = ruleArguments[0];
75 if (type !== OPTION_USE_TABS && type !== OPTION_USE_SPACES) {
78 var size = ruleArguments[1];
80 size: size === OPTION_INDENT_SIZE_2 || size === OPTION_INDENT_SIZE_4 ? size : undefined,
81 tabs: type === OPTION_USE_TABS,
85 var sourceFile = ctx.sourceFile, _a = ctx.options, tabs = _a.tabs, size = _a.size;
86 var regExp = tabs ? new RegExp(" ".repeat(size === undefined ? 1 : size)) : /\t/;
87 var failure = Rule.FAILURE_STRING(tabs ? "tab" : size === undefined ? "space" : size + " space");
88 for (var _i = 0, _b = tsutils_1.getLineRanges(sourceFile); _i < _b.length; _i++) {
89 var _c = _b[_i], pos = _c.pos, contentLength = _c.contentLength;
90 if (contentLength === 0) {
93 var line = sourceFile.text.substr(pos, contentLength);
94 var indentEnd = line.search(/\S/);
95 if (indentEnd === 0) {
98 if (indentEnd === -1) {
99 indentEnd = contentLength;
101 var whitespace = line.slice(0, indentEnd);
102 if (!regExp.test(whitespace)) {
105 var token = tsutils_1.getTokenAtPosition(sourceFile, pos);
106 if (token.kind !== ts.SyntaxKind.JsxText &&
107 (pos >= token.getStart(sourceFile) || tsutils_1.isPositionInComment(sourceFile, pos, token))) {
110 ctx.addFailureAt(pos, indentEnd, failure, createFix(pos, whitespace, tabs, size));
113 function createFix(lineStart, fullLeadingWhitespace, tabs, size) {
114 if (size === undefined) {
117 var replaceRegExp = tabs
118 ? // we want to find every group of `size` spaces, plus up to one 'incomplete' group
119 new RegExp("^( {" + size + "})+( {1," + (size - 1) + "})?", "g")
121 var replacement = fullLeadingWhitespace.replace(replaceRegExp, function (match) {
122 return (tabs ? "\t" : " ".repeat(size)).repeat(Math.ceil(match.length / size));
124 return new Lint.Replacement(lineStart, fullLeadingWhitespace.length, replacement);
126 var templateObject_1, templateObject_2;