UNPKG

8.77 kBJavaScriptView Raw
1"use strict";
2// Please don't have includes in here that aren't inside the DSL folder, or the d.ts/flow defs break
3var __spreadArrays = (this && this.__spreadArrays) || function () {
4 for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
5 for (var r = Array(s), k = 0, i = 0; i < il; i++)
6 for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
7 r[k] = a[j];
8 return r;
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.inlineResultsIntoResults = exports.resultsIntoInlineResults = exports.isMarkdownOnlyResults = exports.isEmptyResults = exports.emptyResults = exports.sortResults = exports.sortInlineResults = exports.mergeResults = exports.regularResults = exports.inlineResults = exports.validateResults = exports.emptyDangerResults = void 0;
12var Violation_1 = require("../dsl/Violation");
13/// End of Danger DSL definition
14exports.emptyDangerResults = {
15 fails: [],
16 warnings: [],
17 messages: [],
18 markdowns: [],
19};
20function validateResults(results) {
21 // The data we get back is JSON sent by STDIN that can come from many
22 // consumers, let's take the time to ensure the data is how we think it is.
23 var fails = results.fails, warnings = results.warnings, messages = results.messages, markdowns = results.markdowns;
24 var props = { fails: fails, warnings: warnings, messages: messages, markdowns: markdowns };
25 Object.keys(props).forEach(function (name) {
26 //
27 // Must include the key 4 types
28 if (!props[name]) {
29 throw new Error("Results passed to Danger JS did not include " + name + ".\n\n" + JSON.stringify(results, null, " "));
30 }
31 var violations = props[name];
32 violations.forEach(function (v) {
33 // They should always have a message
34 if (!v.message) {
35 throw new Error("A violation passed to Danger JS in " + name + " did not include `message`.\n\n" + JSON.stringify(v, null, " "));
36 }
37 // Warn if anything other than the initial API is on a violation
38 var officialAPI = ["message", "line", "file", "icon"];
39 var keys = Object.keys(v).filter(function (f) { return !officialAPI.includes(f); });
40 if (keys.length) {
41 console.warn("Received unexpected key in Violation, expected only " + officialAPI + " but got " + Object.keys(v));
42 }
43 });
44 });
45}
46exports.validateResults = validateResults;
47/** Returns only the inline violations from Danger results */
48function inlineResults(results) {
49 return {
50 fails: results.fails.filter(function (m) { return Violation_1.isInline(m); }),
51 warnings: results.warnings.filter(function (m) { return Violation_1.isInline(m); }),
52 messages: results.messages.filter(function (m) { return Violation_1.isInline(m); }),
53 markdowns: results.markdowns.filter(function (m) { return Violation_1.isInline(m); }),
54 };
55}
56exports.inlineResults = inlineResults;
57/** Returns only the main-comment comments violations from Danger results */
58function regularResults(results) {
59 return {
60 fails: results.fails.filter(function (m) { return !Violation_1.isInline(m); }),
61 warnings: results.warnings.filter(function (m) { return !Violation_1.isInline(m); }),
62 messages: results.messages.filter(function (m) { return !Violation_1.isInline(m); }),
63 markdowns: results.markdowns.filter(function (m) { return !Violation_1.isInline(m); }),
64 meta: results.meta,
65 };
66}
67exports.regularResults = regularResults;
68/** Concat all the violations into a new results */
69function mergeResults(results1, results2) {
70 return {
71 fails: results1.fails.concat(results2.fails),
72 warnings: results1.warnings.concat(results2.warnings),
73 messages: results1.messages.concat(results2.messages),
74 markdowns: results1.markdowns.concat(results2.markdowns),
75 meta: results1.meta || results2.meta,
76 };
77}
78exports.mergeResults = mergeResults;
79/** Sorts all of the results according to their files and lines */
80function sortInlineResults(inlineResults) {
81 // First sort messages in every inline result
82 var sortedInlineResults = inlineResults.map(function (i) {
83 return {
84 file: i.file,
85 line: i.line,
86 fails: i.fails.sort(),
87 warnings: i.warnings.sort(),
88 messages: i.messages.sort(),
89 markdowns: i.markdowns.sort(),
90 };
91 });
92 // Then sort a whole array of inline results based on file/line
93 return sortedInlineResults.sort(function (a, b) {
94 if (a.file < b.file) {
95 return -1;
96 }
97 else if (a.file > b.file) {
98 return 1;
99 }
100 else if (a.line < b.line) {
101 return -1;
102 }
103 else if (a.line > b.line) {
104 return 1;
105 }
106 else {
107 // both file & line are the same
108 return 0;
109 }
110 });
111}
112exports.sortInlineResults = sortInlineResults;
113function sortResults(results) {
114 var sortByFile = function (a, b) {
115 if (a.file === undefined) {
116 return -1;
117 }
118 if (b.file === undefined) {
119 return 1;
120 }
121 if (a.file == b.file) {
122 if (a.line == undefined) {
123 return -1;
124 }
125 if (b.line == undefined) {
126 return 1;
127 }
128 if (a.line < b.line) {
129 return -1;
130 }
131 else if (a.line > b.line) {
132 return 1;
133 }
134 else {
135 return 0;
136 }
137 }
138 if (a.file < b.file) {
139 return -1;
140 }
141 else {
142 return 1;
143 }
144 };
145 return {
146 fails: results.fails.sort(sortByFile),
147 warnings: results.warnings.sort(sortByFile),
148 messages: results.messages.sort(sortByFile),
149 markdowns: results.markdowns.sort(sortByFile),
150 meta: results.meta,
151 };
152}
153exports.sortResults = sortResults;
154exports.emptyResults = function () { return ({ fails: [], markdowns: [], warnings: [], messages: [] }); };
155exports.isEmptyResults = function (results) {
156 return __spreadArrays(results.fails, results.warnings, results.messages, results.markdowns).length === 0;
157};
158exports.isMarkdownOnlyResults = function (results) {
159 return results.markdowns.length > 0 && __spreadArrays(results.fails, results.warnings, results.messages).length === 0;
160};
161function resultsIntoInlineResults(results) {
162 // Here we iterate through all keys ("fails", "warnings", "messages", "markdowns") and for each violation
163 // in given kind we produce new DangerInlineResult or append a violation to existing result. This is all
164 // happening in a `violationsIntoInlineResults` function that mutates an out-of-scope variable `dangerInlineResults`.
165 var dangerInlineResults = [];
166 var violationsIntoInlineResults = function (kind) {
167 var _loop_1 = function (violation) {
168 if (violation.file && violation.line) {
169 var findInlineResult = dangerInlineResults.find(function (r) { return r.file == violation.file && r.line == violation.line; });
170 if (findInlineResult) {
171 findInlineResult[kind].push(violation.message);
172 }
173 else {
174 var inlineResult = {
175 file: violation.file,
176 line: violation.line,
177 fails: [],
178 warnings: [],
179 messages: [],
180 markdowns: [],
181 };
182 inlineResult[kind].push(violation.message);
183 dangerInlineResults.push(inlineResult);
184 }
185 }
186 };
187 for (var _i = 0, _a = results[kind]; _i < _a.length; _i++) {
188 var violation = _a[_i];
189 _loop_1(violation);
190 }
191 };
192 Object.keys(results).forEach(violationsIntoInlineResults);
193 return dangerInlineResults;
194}
195exports.resultsIntoInlineResults = resultsIntoInlineResults;
196function inlineResultsIntoResults(inlineResults) {
197 var messageToViolation = function (message) {
198 return { message: message, file: inlineResults.file, line: inlineResults.line };
199 };
200 return {
201 fails: inlineResults.fails.map(messageToViolation),
202 warnings: inlineResults.warnings.map(messageToViolation),
203 messages: inlineResults.messages.map(messageToViolation),
204 markdowns: inlineResults.markdowns.map(messageToViolation),
205 };
206}
207exports.inlineResultsIntoResults = inlineResultsIntoResults;
208//# sourceMappingURL=DangerResults.js.map
\No newline at end of file