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 utils_1 = require("../../utils");
20 function isTypedRule(rule) {
21 return "applyWithProgram" in rule;
23 exports.isTypedRule = isTypedRule;
24 var Replacement = /** @class */ (function () {
25 function Replacement(start, length, text) {
30 Replacement.applyFixes = function (content, fixes) {
31 return Replacement.applyAll(content, utils_1.flatMap(fixes, utils_1.arrayify));
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);
38 Replacement.replaceNode = function (node, text, sourceFile) {
39 return Replacement.replaceFromTo(node.getStart(sourceFile), node.getEnd(), text);
41 Replacement.replaceFromTo = function (start, end, text) {
42 return new Replacement(start, end - start, text);
44 Replacement.deleteText = function (start, length) {
45 return new Replacement(start, length, "");
47 Replacement.deleteFromTo = function (start, end) {
48 return new Replacement(start, end - start, "");
50 Replacement.appendText = function (start, text) {
51 return new Replacement(start, 0, text);
53 Object.defineProperty(Replacement.prototype, "end", {
55 return this.start + this.length;
60 Replacement.prototype.apply = function (content) {
61 return (content.substring(0, this.start) +
63 content.substring(this.start + this.length));
65 Replacement.prototype.toJson = function () {
66 // tslint:disable object-literal-sort-keys
68 innerStart: this.start,
69 innerLength: this.length,
72 // tslint:enable object-literal-sort-keys
76 exports.Replacement = Replacement;
77 var RuleFailurePosition = /** @class */ (function () {
78 function RuleFailurePosition(position, lineAndCharacter) {
79 this.position = position;
80 this.lineAndCharacter = lineAndCharacter;
82 RuleFailurePosition.prototype.getPosition = function () {
85 RuleFailurePosition.prototype.getLineAndCharacter = function () {
86 return this.lineAndCharacter;
88 RuleFailurePosition.prototype.toJson = function () {
90 character: this.lineAndCharacter.character,
91 line: this.lineAndCharacter.line,
92 position: this.position,
95 RuleFailurePosition.prototype.equals = function (ruleFailurePosition) {
96 var ll = this.lineAndCharacter;
97 var rr = ruleFailurePosition.lineAndCharacter;
98 return (this.position === ruleFailurePosition.position &&
99 ll.line === rr.line &&
100 ll.character === rr.character);
102 return RuleFailurePosition;
104 exports.RuleFailurePosition = RuleFailurePosition;
105 var RuleFailure = /** @class */ (function () {
106 function RuleFailure(sourceFile, start, end, failure, ruleName, fix) {
107 this.sourceFile = sourceFile;
108 this.failure = failure;
109 this.ruleName = ruleName;
111 this.fileName = sourceFile.fileName;
112 this.startPosition = this.createFailurePosition(start);
113 this.endPosition = this.createFailurePosition(end);
114 this.rawLines = sourceFile.text;
115 this.ruleSeverity = "error";
117 RuleFailure.compare = function (a, b) {
118 if (a.fileName !== b.fileName) {
119 return a.fileName < b.fileName ? -1 : 1;
121 return a.startPosition.getPosition() - b.startPosition.getPosition();
123 RuleFailure.prototype.getFileName = function () {
124 return this.fileName;
126 RuleFailure.prototype.getRuleName = function () {
127 return this.ruleName;
129 RuleFailure.prototype.getStartPosition = function () {
130 return this.startPosition;
132 RuleFailure.prototype.getEndPosition = function () {
133 return this.endPosition;
135 RuleFailure.prototype.getFailure = function () {
138 RuleFailure.prototype.hasFix = function () {
139 return this.fix !== undefined;
141 RuleFailure.prototype.getFix = function () {
144 RuleFailure.prototype.getRawLines = function () {
145 return this.rawLines;
147 RuleFailure.prototype.getRuleSeverity = function () {
148 return this.ruleSeverity;
150 RuleFailure.prototype.setRuleSeverity = function (value) {
151 this.ruleSeverity = value;
153 RuleFailure.prototype.toJson = function () {
155 endPosition: this.endPosition.toJson(),
156 failure: this.failure,
157 fix: this.fix === undefined
159 : Array.isArray(this.fix)
160 ? this.fix.map(function (r) { return r.toJson(); })
163 ruleName: this.ruleName,
164 ruleSeverity: this.ruleSeverity.toUpperCase(),
165 startPosition: this.startPosition.toJson(),
168 RuleFailure.prototype.equals = function (ruleFailure) {
169 return (this.failure === ruleFailure.getFailure() &&
170 this.fileName === ruleFailure.getFileName() &&
171 this.startPosition.equals(ruleFailure.getStartPosition()) &&
172 this.endPosition.equals(ruleFailure.getEndPosition()));
174 RuleFailure.prototype.createFailurePosition = function (position) {
175 var lineAndCharacter = this.sourceFile.getLineAndCharacterOfPosition(position);
176 return new RuleFailurePosition(position, lineAndCharacter);
180 exports.RuleFailure = RuleFailure;