123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 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 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);
  29. function Rule() {
  30. return _super !== null && _super.apply(this, arguments) || this;
  31. }
  32. /* tslint:enable:object-literal-sort-keys */
  33. Rule.FAILURE_STRING = function (expected) {
  34. return expected + " indentation expected";
  35. };
  36. Rule.prototype.apply = function (sourceFile) {
  37. var options = parseOptions(this.ruleArguments);
  38. return options === undefined ? [] : this.applyWithFunction(sourceFile, walk, options);
  39. };
  40. /* tslint:disable:object-literal-sort-keys */
  41. Rule.metadata = {
  42. ruleName: "indent",
  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()),
  46. options: {
  47. type: "array",
  48. items: [
  49. {
  50. type: "string",
  51. enum: [OPTION_USE_TABS, OPTION_USE_SPACES],
  52. },
  53. {
  54. type: "number",
  55. enum: [OPTION_INDENT_SIZE_2, OPTION_INDENT_SIZE_4],
  56. },
  57. ],
  58. minLength: 0,
  59. maxLength: 5,
  60. },
  61. optionExamples: [
  62. [true, OPTION_USE_SPACES],
  63. [true, OPTION_USE_SPACES, OPTION_INDENT_SIZE_4],
  64. [true, OPTION_USE_TABS, OPTION_INDENT_SIZE_2],
  65. ],
  66. hasFix: true,
  67. type: "maintainability",
  68. typescriptOnly: false,
  69. };
  70. return Rule;
  71. }(Lint.Rules.AbstractRule));
  72. exports.Rule = Rule;
  73. function parseOptions(ruleArguments) {
  74. var type = ruleArguments[0];
  75. if (type !== OPTION_USE_TABS && type !== OPTION_USE_SPACES) {
  76. return undefined;
  77. }
  78. var size = ruleArguments[1];
  79. return {
  80. size: size === OPTION_INDENT_SIZE_2 || size === OPTION_INDENT_SIZE_4 ? size : undefined,
  81. tabs: type === OPTION_USE_TABS,
  82. };
  83. }
  84. function walk(ctx) {
  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) {
  91. continue;
  92. }
  93. var line = sourceFile.text.substr(pos, contentLength);
  94. var indentEnd = line.search(/\S/);
  95. if (indentEnd === 0) {
  96. continue;
  97. }
  98. if (indentEnd === -1) {
  99. indentEnd = contentLength;
  100. }
  101. var whitespace = line.slice(0, indentEnd);
  102. if (!regExp.test(whitespace)) {
  103. continue;
  104. }
  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))) {
  108. continue;
  109. }
  110. ctx.addFailureAt(pos, indentEnd, failure, createFix(pos, whitespace, tabs, size));
  111. }
  112. }
  113. function createFix(lineStart, fullLeadingWhitespace, tabs, size) {
  114. if (size === undefined) {
  115. return undefined;
  116. }
  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")
  120. : /\t/g;
  121. var replacement = fullLeadingWhitespace.replace(replaceRegExp, function (match) {
  122. return (tabs ? "\t" : " ".repeat(size)).repeat(Math.ceil(match.length / size));
  123. });
  124. return new Lint.Replacement(lineStart, fullLeadingWhitespace.length, replacement);
  125. }
  126. var templateObject_1, templateObject_2;