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 noConsecutiveBlankLinesRule_1 = require("./noConsecutiveBlankLinesRule");
24 var ZERO_WIDTH_NO_BREAK_SPACE = 0xfeff;
25 var OPTION_IGNORE_COMMENTS = "ignore-comments";
26 var OPTION_IGNORE_JSDOC = "ignore-jsdoc";
27 var OPTION_IGNORE_TEMPLATE_STRINGS = "ignore-template-strings";
28 var OPTION_IGNORE_BLANK_LINES = "ignore-blank-lines";
29 var Rule = /** @class */ (function (_super) {
30 tslib_1.__extends(Rule, _super);
32 return _super !== null && _super.apply(this, arguments) || this;
34 Rule.prototype.apply = function (sourceFile) {
35 var ignoreComments = this.ruleArguments.indexOf(OPTION_IGNORE_COMMENTS) !== -1;
36 return this.applyWithFunction(sourceFile, walk, {
37 ignoreBlankLines: this.ruleArguments.indexOf(OPTION_IGNORE_BLANK_LINES) !== -1,
38 ignoreComments: ignoreComments,
39 ignoreJsDoc: ignoreComments || this.ruleArguments.indexOf(OPTION_IGNORE_JSDOC) !== -1,
40 ignoreTemplates: this.ruleArguments.indexOf(OPTION_IGNORE_TEMPLATE_STRINGS) !== -1,
43 /* tslint:disable:object-literal-sort-keys */
45 ruleName: "no-trailing-whitespace",
46 description: "Disallows trailing whitespace at the end of a line.",
47 rationale: "Keeps version control diffs clean as it prevents accidental whitespace from being committed.",
48 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),
55 OPTION_IGNORE_COMMENTS,
57 OPTION_IGNORE_TEMPLATE_STRINGS,
58 OPTION_IGNORE_BLANK_LINES,
62 optionExamples: [true, [true, OPTION_IGNORE_COMMENTS], [true, OPTION_IGNORE_JSDOC]],
64 typescriptOnly: false,
66 /* tslint:enable:object-literal-sort-keys */
67 Rule.FAILURE_STRING = "trailing whitespace";
69 }(Lint.Rules.AbstractRule));
72 var possibleFailures = [];
73 var sourceFile = ctx.sourceFile;
74 var text = sourceFile.text;
75 for (var _i = 0, _a = tsutils_1.getLineRanges(sourceFile); _i < _a.length; _i++) {
77 // \s matches any whitespace character (equal to [\r\n\t\f\v ])
78 var match = text.substr(line.pos, line.contentLength).match(/\s+$/);
80 !(ctx.options.ignoreBlankLines && match.index === 0) &&
81 match[0] !== String.fromCharCode(ZERO_WIDTH_NO_BREAK_SPACE)) {
82 possibleFailures.push({
83 end: line.pos + line.contentLength,
84 pos: line.pos + match.index,
88 if (possibleFailures.length === 0) {
91 var excludedRanges = ctx.options.ignoreTemplates
92 ? ctx.options.ignoreJsDoc
93 ? getExcludedRanges(sourceFile, ctx.options)
94 : noConsecutiveBlankLinesRule_1.getTemplateRanges(sourceFile)
95 : ctx.options.ignoreJsDoc
96 ? getExcludedComments(sourceFile, ctx.options)
98 var _loop_1 = function (possibleFailure) {
99 if (!excludedRanges.some(function (range) { return range.pos < possibleFailure.pos && possibleFailure.pos < range.end; })) {
100 ctx.addFailure(possibleFailure.pos, possibleFailure.end, Rule.FAILURE_STRING, Lint.Replacement.deleteFromTo(possibleFailure.pos, possibleFailure.end));
103 for (var _b = 0, possibleFailures_1 = possibleFailures; _b < possibleFailures_1.length; _b++) {
104 var possibleFailure = possibleFailures_1[_b];
105 _loop_1(possibleFailure);
108 function getExcludedRanges(sourceFile, options) {
110 tsutils_1.forEachTokenWithTrivia(sourceFile, function (text, kind, range) {
111 if (kind >= ts.SyntaxKind.FirstTemplateToken && kind <= ts.SyntaxKind.LastTemplateToken) {
112 intervals.push(range);
114 else if (options.ignoreComments) {
115 if (kind === ts.SyntaxKind.SingleLineCommentTrivia ||
116 kind === ts.SyntaxKind.MultiLineCommentTrivia) {
117 intervals.push(range);
120 else if (options.ignoreJsDoc) {
121 if (isJsDoc(text, kind, range)) {
122 intervals.push(range);
128 function getExcludedComments(sourceFile, options) {
130 tsutils_1.forEachComment(sourceFile, function (text, comment) {
131 if (options.ignoreComments ||
132 (options.ignoreJsDoc && isJsDoc(text, comment.kind, comment))) {
133 intervals.push(comment);
138 function isJsDoc(sourceText, kind, range) {
139 return (kind === ts.SyntaxKind.MultiLineCommentTrivia &&
140 sourceText[range.pos + 2] === "*" &&
141 sourceText[range.pos + 3] !== "*");
143 var templateObject_1;