ruleWalker.js 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 rule_1 = require("../rule/rule");
  21. var syntaxWalker_1 = require("./syntaxWalker");
  22. var RuleWalker = /** @class */ (function (_super) {
  23. tslib_1.__extends(RuleWalker, _super);
  24. function RuleWalker(sourceFile, options) {
  25. var _this = _super.call(this) || this;
  26. _this.sourceFile = sourceFile;
  27. _this.failures = [];
  28. _this.options = options.ruleArguments;
  29. _this.limit = _this.sourceFile.getFullWidth();
  30. _this.ruleName = options.ruleName;
  31. return _this;
  32. }
  33. RuleWalker.prototype.getSourceFile = function () {
  34. return this.sourceFile;
  35. };
  36. RuleWalker.prototype.getLineAndCharacterOfPosition = function (position) {
  37. return this.sourceFile.getLineAndCharacterOfPosition(position);
  38. };
  39. RuleWalker.prototype.getFailures = function () {
  40. return this.failures;
  41. };
  42. RuleWalker.prototype.getLimit = function () {
  43. return this.limit;
  44. };
  45. RuleWalker.prototype.getOptions = function () {
  46. return this.options;
  47. };
  48. RuleWalker.prototype.hasOption = function (option) {
  49. if (this.options !== undefined) {
  50. return this.options.indexOf(option) !== -1;
  51. }
  52. else {
  53. return false;
  54. }
  55. };
  56. /** @deprecated Prefer `addFailureAt` and its variants. */
  57. RuleWalker.prototype.createFailure = function (start, width, failure, fix) {
  58. var from = (start > this.limit) ? this.limit : start;
  59. var to = ((start + width) > this.limit) ? this.limit : (start + width);
  60. return new rule_1.RuleFailure(this.sourceFile, from, to, failure, this.ruleName, fix);
  61. };
  62. /** @deprecated Prefer `addFailureAt` and its variants. */
  63. RuleWalker.prototype.addFailure = function (failure) {
  64. this.failures.push(failure);
  65. };
  66. /** Add a failure with any arbitrary span. Prefer `addFailureAtNode` if possible. */
  67. RuleWalker.prototype.addFailureAt = function (start, width, failure, fix) {
  68. // tslint:disable-next-line deprecation
  69. this.addFailure(this.createFailure(start, width, failure, fix));
  70. };
  71. /** Like `addFailureAt` but uses start and end instead of start and width. */
  72. RuleWalker.prototype.addFailureFromStartToEnd = function (start, end, failure, fix) {
  73. this.addFailureAt(start, end - start, failure, fix);
  74. };
  75. /** Add a failure using a node's span. */
  76. RuleWalker.prototype.addFailureAtNode = function (node, failure, fix) {
  77. this.addFailureAt(node.getStart(this.sourceFile), node.getWidth(this.sourceFile), failure, fix);
  78. };
  79. RuleWalker.prototype.createReplacement = function (start, length, text) {
  80. return new rule_1.Replacement(start, length, text);
  81. };
  82. RuleWalker.prototype.appendText = function (start, text) {
  83. return this.createReplacement(start, 0, text);
  84. };
  85. RuleWalker.prototype.deleteText = function (start, length) {
  86. return this.createReplacement(start, length, "");
  87. };
  88. RuleWalker.prototype.deleteFromTo = function (start, end) {
  89. return this.createReplacement(start, end - start, "");
  90. };
  91. RuleWalker.prototype.getRuleName = function () {
  92. return this.ruleName;
  93. };
  94. return RuleWalker;
  95. }(syntaxWalker_1.SyntaxWalker));
  96. exports.RuleWalker = RuleWalker;