rule.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 utils_1 = require("../../utils");
  20. function isTypedRule(rule) {
  21. return "applyWithProgram" in rule;
  22. }
  23. exports.isTypedRule = isTypedRule;
  24. var Replacement = /** @class */ (function () {
  25. function Replacement(start, length, text) {
  26. this.start = start;
  27. this.length = length;
  28. this.text = text;
  29. }
  30. Replacement.applyFixes = function (content, fixes) {
  31. return this.applyAll(content, utils_1.flatMap(fixes, utils_1.arrayify));
  32. };
  33. Replacement.applyAll = function (content, replacements) {
  34. // sort in reverse so that diffs are properly applied
  35. replacements.sort(function (a, b) { return b.end !== a.end ? b.end - a.end : b.start - a.start; });
  36. return replacements.reduce(function (text, r) { return r.apply(text); }, content);
  37. };
  38. Replacement.replaceNode = function (node, text, sourceFile) {
  39. return this.replaceFromTo(node.getStart(sourceFile), node.getEnd(), text);
  40. };
  41. Replacement.replaceFromTo = function (start, end, text) {
  42. return new Replacement(start, end - start, text);
  43. };
  44. Replacement.deleteText = function (start, length) {
  45. return new Replacement(start, length, "");
  46. };
  47. Replacement.deleteFromTo = function (start, end) {
  48. return new Replacement(start, end - start, "");
  49. };
  50. Replacement.appendText = function (start, text) {
  51. return new Replacement(start, 0, text);
  52. };
  53. Object.defineProperty(Replacement.prototype, "end", {
  54. get: function () {
  55. return this.start + this.length;
  56. },
  57. enumerable: true,
  58. configurable: true
  59. });
  60. Replacement.prototype.apply = function (content) {
  61. return content.substring(0, this.start) + this.text + content.substring(this.start + this.length);
  62. };
  63. Replacement.prototype.toJson = function () {
  64. // tslint:disable object-literal-sort-keys
  65. return {
  66. innerStart: this.start,
  67. innerLength: this.length,
  68. innerText: this.text,
  69. };
  70. // tslint:enable object-literal-sort-keys
  71. };
  72. return Replacement;
  73. }());
  74. exports.Replacement = Replacement;
  75. var RuleFailurePosition = /** @class */ (function () {
  76. function RuleFailurePosition(position, lineAndCharacter) {
  77. this.position = position;
  78. this.lineAndCharacter = lineAndCharacter;
  79. }
  80. RuleFailurePosition.prototype.getPosition = function () {
  81. return this.position;
  82. };
  83. RuleFailurePosition.prototype.getLineAndCharacter = function () {
  84. return this.lineAndCharacter;
  85. };
  86. RuleFailurePosition.prototype.toJson = function () {
  87. return {
  88. character: this.lineAndCharacter.character,
  89. line: this.lineAndCharacter.line,
  90. position: this.position,
  91. };
  92. };
  93. RuleFailurePosition.prototype.equals = function (ruleFailurePosition) {
  94. var ll = this.lineAndCharacter;
  95. var rr = ruleFailurePosition.lineAndCharacter;
  96. return this.position === ruleFailurePosition.position
  97. && ll.line === rr.line
  98. && ll.character === rr.character;
  99. };
  100. return RuleFailurePosition;
  101. }());
  102. exports.RuleFailurePosition = RuleFailurePosition;
  103. var RuleFailure = /** @class */ (function () {
  104. function RuleFailure(sourceFile, start, end, failure, ruleName, fix) {
  105. this.sourceFile = sourceFile;
  106. this.failure = failure;
  107. this.ruleName = ruleName;
  108. this.fix = fix;
  109. this.fileName = sourceFile.fileName;
  110. this.startPosition = this.createFailurePosition(start);
  111. this.endPosition = this.createFailurePosition(end);
  112. this.rawLines = sourceFile.text;
  113. this.ruleSeverity = "error";
  114. }
  115. RuleFailure.compare = function (a, b) {
  116. if (a.fileName !== b.fileName) {
  117. return a.fileName < b.fileName ? -1 : 1;
  118. }
  119. return a.startPosition.getPosition() - b.startPosition.getPosition();
  120. };
  121. RuleFailure.prototype.getFileName = function () {
  122. return this.fileName;
  123. };
  124. RuleFailure.prototype.getRuleName = function () {
  125. return this.ruleName;
  126. };
  127. RuleFailure.prototype.getStartPosition = function () {
  128. return this.startPosition;
  129. };
  130. RuleFailure.prototype.getEndPosition = function () {
  131. return this.endPosition;
  132. };
  133. RuleFailure.prototype.getFailure = function () {
  134. return this.failure;
  135. };
  136. RuleFailure.prototype.hasFix = function () {
  137. return this.fix !== undefined;
  138. };
  139. RuleFailure.prototype.getFix = function () {
  140. return this.fix;
  141. };
  142. RuleFailure.prototype.getRawLines = function () {
  143. return this.rawLines;
  144. };
  145. RuleFailure.prototype.getRuleSeverity = function () {
  146. return this.ruleSeverity;
  147. };
  148. RuleFailure.prototype.setRuleSeverity = function (value) {
  149. this.ruleSeverity = value;
  150. };
  151. RuleFailure.prototype.toJson = function () {
  152. return {
  153. endPosition: this.endPosition.toJson(),
  154. failure: this.failure,
  155. fix: this.fix === undefined ? undefined : Array.isArray(this.fix) ? this.fix.map(function (r) { return r.toJson(); }) : this.fix.toJson(),
  156. name: this.fileName,
  157. ruleName: this.ruleName,
  158. ruleSeverity: this.ruleSeverity.toUpperCase(),
  159. startPosition: this.startPosition.toJson(),
  160. };
  161. };
  162. RuleFailure.prototype.equals = function (ruleFailure) {
  163. return this.failure === ruleFailure.getFailure()
  164. && this.fileName === ruleFailure.getFileName()
  165. && this.startPosition.equals(ruleFailure.getStartPosition())
  166. && this.endPosition.equals(ruleFailure.getEndPosition());
  167. };
  168. RuleFailure.prototype.createFailurePosition = function (position) {
  169. var lineAndCharacter = this.sourceFile.getLineAndCharacterOfPosition(position);
  170. return new RuleFailurePosition(position, lineAndCharacter);
  171. };
  172. return RuleFailure;
  173. }());
  174. exports.RuleFailure = RuleFailure;