noTrailingWhitespaceRule.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 noConsecutiveBlankLinesRule_1 = require("./noConsecutiveBlankLinesRule");
  24. var OPTION_IGNORE_COMMENTS = "ignore-comments";
  25. var OPTION_IGNORE_JSDOC = "ignore-jsdoc";
  26. var OPTION_IGNORE_TEMPLATE_STRINGS = "ignore-template-strings";
  27. var OPTION_IGNORE_BLANK_LINES = "ignore-blank-lines";
  28. var Rule = /** @class */ (function (_super) {
  29. tslib_1.__extends(Rule, _super);
  30. function Rule() {
  31. return _super !== null && _super.apply(this, arguments) || this;
  32. }
  33. Rule.prototype.apply = function (sourceFile) {
  34. var ignoreComments = this.ruleArguments.indexOf(OPTION_IGNORE_COMMENTS) !== -1;
  35. return this.applyWithFunction(sourceFile, walk, {
  36. ignoreBlankLines: this.ruleArguments.indexOf(OPTION_IGNORE_BLANK_LINES) !== -1,
  37. ignoreComments: ignoreComments,
  38. ignoreJsDoc: ignoreComments || this.ruleArguments.indexOf(OPTION_IGNORE_JSDOC) !== -1,
  39. ignoreTemplates: this.ruleArguments.indexOf(OPTION_IGNORE_TEMPLATE_STRINGS) !== -1,
  40. });
  41. };
  42. /* tslint:disable:object-literal-sort-keys */
  43. Rule.metadata = {
  44. ruleName: "no-trailing-whitespace",
  45. description: "Disallows trailing whitespace at the end of a line.",
  46. rationale: "Keeps version control diffs clean as it prevents accidental whitespace from being committed.",
  47. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Possible settings are:\n\n * `\"", "\"`: Allows trailing whitespace in template strings.\n * `\"", "\"`: Allows trailing whitespace in comments.\n * `\"", "\"`: Allows trailing whitespace only in JSDoc comments.\n * `\"", "\"`: Allows trailing whitespace on empty lines."], ["\n Possible settings are:\n\n * \\`\"", "\"\\`: Allows trailing whitespace in template strings.\n * \\`\"", "\"\\`: Allows trailing whitespace in comments.\n * \\`\"", "\"\\`: Allows trailing whitespace only in JSDoc comments.\n * \\`\"", "\"\\`: Allows trailing whitespace on empty lines."])), OPTION_IGNORE_TEMPLATE_STRINGS, OPTION_IGNORE_COMMENTS, OPTION_IGNORE_JSDOC, OPTION_IGNORE_BLANK_LINES),
  48. hasFix: true,
  49. options: {
  50. type: "array",
  51. items: {
  52. type: "string",
  53. enum: [OPTION_IGNORE_COMMENTS, OPTION_IGNORE_JSDOC, OPTION_IGNORE_TEMPLATE_STRINGS, OPTION_IGNORE_BLANK_LINES],
  54. },
  55. },
  56. optionExamples: [
  57. true,
  58. [true, OPTION_IGNORE_COMMENTS],
  59. [true, OPTION_IGNORE_JSDOC],
  60. ],
  61. type: "style",
  62. typescriptOnly: false,
  63. };
  64. /* tslint:enable:object-literal-sort-keys */
  65. Rule.FAILURE_STRING = "trailing whitespace";
  66. return Rule;
  67. }(Lint.Rules.AbstractRule));
  68. exports.Rule = Rule;
  69. function walk(ctx) {
  70. var possibleFailures = [];
  71. var sourceFile = ctx.sourceFile;
  72. var text = sourceFile.text;
  73. for (var _i = 0, _a = tsutils_1.getLineRanges(sourceFile); _i < _a.length; _i++) {
  74. var line = _a[_i];
  75. // \s matches any whitespace character (equal to [\r\n\t\f\v ])
  76. var match = text.substr(line.pos, line.contentLength).match(/\s+$/);
  77. if (match !== null && !(ctx.options.ignoreBlankLines && match.index === 0)) {
  78. possibleFailures.push({
  79. end: line.pos + line.contentLength,
  80. pos: line.pos + match.index,
  81. });
  82. }
  83. }
  84. if (possibleFailures.length === 0) {
  85. return;
  86. }
  87. var excludedRanges = ctx.options.ignoreTemplates
  88. ? ctx.options.ignoreJsDoc ? getExcludedRanges(sourceFile, ctx.options) : noConsecutiveBlankLinesRule_1.getTemplateRanges(sourceFile)
  89. : ctx.options.ignoreJsDoc ? getExcludedComments(sourceFile, ctx.options) : [];
  90. var _loop_1 = function (possibleFailure) {
  91. if (!excludedRanges.some(function (range) { return range.pos < possibleFailure.pos && possibleFailure.pos < range.end; })) {
  92. ctx.addFailure(possibleFailure.pos, possibleFailure.end, Rule.FAILURE_STRING, Lint.Replacement.deleteFromTo(possibleFailure.pos, possibleFailure.end));
  93. }
  94. };
  95. for (var _b = 0, possibleFailures_1 = possibleFailures; _b < possibleFailures_1.length; _b++) {
  96. var possibleFailure = possibleFailures_1[_b];
  97. _loop_1(possibleFailure);
  98. }
  99. }
  100. function getExcludedRanges(sourceFile, options) {
  101. var intervals = [];
  102. tsutils_1.forEachTokenWithTrivia(sourceFile, function (text, kind, range) {
  103. if (kind >= ts.SyntaxKind.FirstTemplateToken && kind <= ts.SyntaxKind.LastTemplateToken) {
  104. intervals.push(range);
  105. }
  106. else if (options.ignoreComments) {
  107. if (kind === ts.SyntaxKind.SingleLineCommentTrivia || kind === ts.SyntaxKind.MultiLineCommentTrivia) {
  108. intervals.push(range);
  109. }
  110. }
  111. else if (options.ignoreJsDoc) {
  112. if (isJsDoc(text, kind, range)) {
  113. intervals.push(range);
  114. }
  115. }
  116. });
  117. return intervals;
  118. }
  119. function getExcludedComments(sourceFile, options) {
  120. var intervals = [];
  121. tsutils_1.forEachComment(sourceFile, function (text, comment) {
  122. if (options.ignoreComments ||
  123. options.ignoreJsDoc && isJsDoc(text, comment.kind, comment)) {
  124. intervals.push(comment);
  125. }
  126. });
  127. return intervals;
  128. }
  129. function isJsDoc(sourceText, kind, range) {
  130. return kind === ts.SyntaxKind.MultiLineCommentTrivia && sourceText[range.pos + 2] === "*" && sourceText[range.pos + 3] !== "*";
  131. }
  132. var templateObject_1;