.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noTrailingWhitespaceRule.js
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 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);
31     function Rule() {
32         return _super !== null && _super.apply(this, arguments) || this;
33     }
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,
41         });
42     };
43     /* tslint:disable:object-literal-sort-keys */
44     Rule.metadata = {
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),
49         hasFix: true,
50         options: {
51             type: "array",
52             items: {
53                 type: "string",
54                 enum: [
55                     OPTION_IGNORE_COMMENTS,
56                     OPTION_IGNORE_JSDOC,
57                     OPTION_IGNORE_TEMPLATE_STRINGS,
58                     OPTION_IGNORE_BLANK_LINES,
59                 ],
60             },
61         },
62         optionExamples: [true, [true, OPTION_IGNORE_COMMENTS], [true, OPTION_IGNORE_JSDOC]],
63         type: "formatting",
64         typescriptOnly: false,
65     };
66     /* tslint:enable:object-literal-sort-keys */
67     Rule.FAILURE_STRING = "trailing whitespace";
68     return Rule;
69 }(Lint.Rules.AbstractRule));
70 exports.Rule = Rule;
71 function walk(ctx) {
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++) {
76         var line = _a[_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+$/);
79         if (match !== null &&
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,
85             });
86         }
87     }
88     if (possibleFailures.length === 0) {
89         return;
90     }
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)
97             : [];
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));
101         }
102     };
103     for (var _b = 0, possibleFailures_1 = possibleFailures; _b < possibleFailures_1.length; _b++) {
104         var possibleFailure = possibleFailures_1[_b];
105         _loop_1(possibleFailure);
106     }
107 }
108 function getExcludedRanges(sourceFile, options) {
109     var intervals = [];
110     tsutils_1.forEachTokenWithTrivia(sourceFile, function (text, kind, range) {
111         if (kind >= ts.SyntaxKind.FirstTemplateToken && kind <= ts.SyntaxKind.LastTemplateToken) {
112             intervals.push(range);
113         }
114         else if (options.ignoreComments) {
115             if (kind === ts.SyntaxKind.SingleLineCommentTrivia ||
116                 kind === ts.SyntaxKind.MultiLineCommentTrivia) {
117                 intervals.push(range);
118             }
119         }
120         else if (options.ignoreJsDoc) {
121             if (isJsDoc(text, kind, range)) {
122                 intervals.push(range);
123             }
124         }
125     });
126     return intervals;
127 }
128 function getExcludedComments(sourceFile, options) {
129     var intervals = [];
130     tsutils_1.forEachComment(sourceFile, function (text, comment) {
131         if (options.ignoreComments ||
132             (options.ignoreJsDoc && isJsDoc(text, comment.kind, comment))) {
133             intervals.push(comment);
134         }
135     });
136     return intervals;
137 }
138 function isJsDoc(sourceText, kind, range) {
139     return (kind === ts.SyntaxKind.MultiLineCommentTrivia &&
140         sourceText[range.pos + 2] === "*" &&
141         sourceText[range.pos + 3] !== "*");
142 }
143 var templateObject_1;