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