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 rule_1 = require("../rule/rule");
21 var syntaxWalker_1 = require("./syntaxWalker");
24 * RuleWalker-based rules are slow,
25 * so it's generally preferable to use applyWithFunction instead of applyWithWalker.
26 * @see https://github.com/palantir/tslint/issues/2522
28 var RuleWalker = /** @class */ (function (_super) {
29 tslib_1.__extends(RuleWalker, _super);
30 function RuleWalker(sourceFile, options) {
31 var _this = _super.call(this) || this;
32 _this.sourceFile = sourceFile;
34 _this.options = options.ruleArguments;
35 _this.limit = _this.sourceFile.getFullWidth();
36 _this.ruleName = options.ruleName;
39 RuleWalker.prototype.getSourceFile = function () {
40 return this.sourceFile;
42 RuleWalker.prototype.getLineAndCharacterOfPosition = function (position) {
43 return this.sourceFile.getLineAndCharacterOfPosition(position);
45 RuleWalker.prototype.getFailures = function () {
48 RuleWalker.prototype.getLimit = function () {
51 RuleWalker.prototype.getOptions = function () {
54 RuleWalker.prototype.hasOption = function (option) {
55 if (this.options !== undefined) {
56 return this.options.indexOf(option) !== -1;
62 /** @deprecated Prefer `addFailureAt` and its variants. */
63 RuleWalker.prototype.createFailure = function (start, width, failure, fix) {
64 var from = start > this.limit ? this.limit : start;
65 var to = start + width > this.limit ? this.limit : start + width;
66 return new rule_1.RuleFailure(this.sourceFile, from, to, failure, this.ruleName, fix);
68 /** @deprecated Prefer `addFailureAt` and its variants. */
69 RuleWalker.prototype.addFailure = function (failure) {
70 this.failures.push(failure);
72 /** Add a failure with any arbitrary span. Prefer `addFailureAtNode` if possible. */
73 RuleWalker.prototype.addFailureAt = function (start, width, failure, fix) {
74 // tslint:disable-next-line deprecation
75 this.addFailure(this.createFailure(start, width, failure, fix));
77 /** Like `addFailureAt` but uses start and end instead of start and width. */
78 RuleWalker.prototype.addFailureFromStartToEnd = function (start, end, failure, fix) {
79 this.addFailureAt(start, end - start, failure, fix);
81 /** Add a failure using a node's span. */
82 RuleWalker.prototype.addFailureAtNode = function (node, failure, fix) {
83 this.addFailureAt(node.getStart(this.sourceFile), node.getWidth(this.sourceFile), failure, fix);
85 RuleWalker.prototype.createReplacement = function (start, length, text) {
86 return new rule_1.Replacement(start, length, text);
88 RuleWalker.prototype.appendText = function (start, text) {
89 return this.createReplacement(start, 0, text);
91 RuleWalker.prototype.deleteText = function (start, length) {
92 return this.createReplacement(start, length, "");
94 RuleWalker.prototype.deleteFromTo = function (start, end) {
95 return this.createReplacement(start, end - start, "");
97 RuleWalker.prototype.getRuleName = function () {
101 }(syntaxWalker_1.SyntaxWalker));
102 exports.RuleWalker = RuleWalker;