aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tslint/lib/rules/noTrailingWhitespaceRule.js
blob: 3531bc9ae3e8bb21026e0a485efb55a35b2ffce9 (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
"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 tsutils_1 = require("tsutils");
var ts = require("typescript");
var Lint = require("../index");
var noConsecutiveBlankLinesRule_1 = require("./noConsecutiveBlankLinesRule");
var OPTION_IGNORE_COMMENTS = "ignore-comments";
var OPTION_IGNORE_JSDOC = "ignore-jsdoc";
var OPTION_IGNORE_TEMPLATE_STRINGS = "ignore-template-strings";
var Rule = /** @class */ (function (_super) {
    tslib_1.__extends(Rule, _super);
    function Rule() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Rule.prototype.apply = function (sourceFile) {
        var ignoreComments = this.ruleArguments.indexOf(OPTION_IGNORE_COMMENTS) !== -1;
        return this.applyWithFunction(sourceFile, walk, {
            ignoreComments: ignoreComments,
            ignoreJsDoc: ignoreComments || this.ruleArguments.indexOf(OPTION_IGNORE_JSDOC) !== -1,
            ignoreTemplates: this.ruleArguments.indexOf(OPTION_IGNORE_TEMPLATE_STRINGS) !== -1,
        });
    };
    /* tslint:disable:object-literal-sort-keys */
    Rule.metadata = {
        ruleName: "no-trailing-whitespace",
        description: "Disallows trailing whitespace at the end of a line.",
        rationale: "Keeps version control diffs clean as it prevents accidental whitespace from being committed.",
        optionsDescription: (_a = ["\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."], _a.raw = ["\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."], Lint.Utils.dedent(_a, OPTION_IGNORE_TEMPLATE_STRINGS, OPTION_IGNORE_COMMENTS, OPTION_IGNORE_JSDOC)),
        hasFix: true,
        options: {
            type: "array",
            items: {
                type: "string",
                enum: [OPTION_IGNORE_COMMENTS, OPTION_IGNORE_JSDOC, OPTION_IGNORE_TEMPLATE_STRINGS],
            },
        },
        optionExamples: [
            true,
            [true, OPTION_IGNORE_COMMENTS],
            [true, OPTION_IGNORE_JSDOC],
        ],
        type: "style",
        typescriptOnly: false,
    };
    /* tslint:enable:object-literal-sort-keys */
    Rule.FAILURE_STRING = "trailing whitespace";
    return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
function walk(ctx) {
    var possibleFailures = [];
    var sourceFile = ctx.sourceFile;
    var text = sourceFile.text;
    for (var _i = 0, _a = tsutils_1.getLineRanges(sourceFile); _i < _a.length; _i++) {
        var line = _a[_i];
        var match = text.substr(line.pos, line.contentLength).match(/\s+$/);
        if (match !== null) {
            possibleFailures.push({
                end: line.pos + line.contentLength,
                pos: line.pos + match.index,
            });
        }
    }
    if (possibleFailures.length === 0) {
        return;
    }
    var excludedRanges = ctx.options.ignoreTemplates
        ? ctx.options.ignoreJsDoc ? getExcludedRanges(sourceFile, ctx.options) : noConsecutiveBlankLinesRule_1.getTemplateRanges(sourceFile)
        : ctx.options.ignoreJsDoc ? getExcludedComments(sourceFile, ctx.options) : [];
    var _loop_1 = function (possibleFailure) {
        if (!excludedRanges.some(function (range) { return range.pos < possibleFailure.pos && possibleFailure.pos < range.end; })) {
            ctx.addFailure(possibleFailure.pos, possibleFailure.end, Rule.FAILURE_STRING, Lint.Replacement.deleteFromTo(possibleFailure.pos, possibleFailure.end));
        }
    };
    for (var _b = 0, possibleFailures_1 = possibleFailures; _b < possibleFailures_1.length; _b++) {
        var possibleFailure = possibleFailures_1[_b];
        _loop_1(possibleFailure);
    }
}
function getExcludedRanges(sourceFile, options) {
    var intervals = [];
    tsutils_1.forEachTokenWithTrivia(sourceFile, function (text, kind, range) {
        if (kind >= ts.SyntaxKind.FirstTemplateToken && kind <= ts.SyntaxKind.LastTemplateToken) {
            intervals.push(range);
        }
        else if (options.ignoreComments) {
            if (kind === ts.SyntaxKind.SingleLineCommentTrivia || kind === ts.SyntaxKind.MultiLineCommentTrivia) {
                intervals.push(range);
            }
        }
        else if (options.ignoreJsDoc) {
            if (isJsDoc(text, kind, range)) {
                intervals.push(range);
            }
        }
    });
    return intervals;
}
function getExcludedComments(sourceFile, options) {
    var intervals = [];
    tsutils_1.forEachComment(sourceFile, function (text, comment) {
        if (options.ignoreComments ||
            options.ignoreJsDoc && isJsDoc(text, comment.kind, comment)) {
            intervals.push(comment);
        }
    });
    return intervals;
}
function isJsDoc(sourceText, kind, range) {
    return kind === ts.SyntaxKind.MultiLineCommentTrivia && sourceText[range.pos + 2] === "*" && sourceText[range.pos + 3] !== "*";
}
var _a;