diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:38:50 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:40:43 +0200 |
commit | 7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027 (patch) | |
tree | 6de9a1aebd150a23b7f8c273ec657a5d0a18fe3e /node_modules/tslint/lib/rules/noTrailingWhitespaceRule.js | |
parent | 963b7a41feb29cc4be090a2446bdfe0c1f1bcd81 (diff) |
add linting (and some initial fixes)
Diffstat (limited to 'node_modules/tslint/lib/rules/noTrailingWhitespaceRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/noTrailingWhitespaceRule.js | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/node_modules/tslint/lib/rules/noTrailingWhitespaceRule.js b/node_modules/tslint/lib/rules/noTrailingWhitespaceRule.js new file mode 100644 index 000000000..56615b1b5 --- /dev/null +++ b/node_modules/tslint/lib/rules/noTrailingWhitespaceRule.js @@ -0,0 +1,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 = (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, + }); + }; + return Rule; +}(Lint.Rules.AbstractRule)); +/* 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"; +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; |