1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | class NormalizedMessage {
|
4 | constructor(data) {
|
5 | this.type = data.type;
|
6 | this.code = data.code;
|
7 | this.severity = data.severity;
|
8 | this.content = data.content;
|
9 | this.file = data.file;
|
10 | this.line = data.line;
|
11 | this.character = data.character;
|
12 | this.stack = data.stack;
|
13 | }
|
14 | static createFromJSON(json) {
|
15 | return new NormalizedMessage(json);
|
16 | }
|
17 | static compare(messageA, messageB) {
|
18 | if (!(messageA instanceof NormalizedMessage)) {
|
19 | return -1;
|
20 | }
|
21 | if (!(messageB instanceof NormalizedMessage)) {
|
22 | return 1;
|
23 | }
|
24 | return (NormalizedMessage.compareTypes(messageA.type, messageB.type) ||
|
25 | NormalizedMessage.compareOptionalStrings(messageA.file, messageB.file) ||
|
26 | NormalizedMessage.compareSeverities(messageA.severity, messageB.severity) ||
|
27 | NormalizedMessage.compareNumbers(messageA.line, messageB.line) ||
|
28 | NormalizedMessage.compareNumbers(messageA.character, messageB.character) ||
|
29 |
|
30 | NormalizedMessage.compareOptionalStrings(messageA.code, messageB.code) ||
|
31 | NormalizedMessage.compareOptionalStrings(messageA.content, messageB.content) ||
|
32 | NormalizedMessage.compareOptionalStrings(messageA.stack, messageB.stack) ||
|
33 | 0 );
|
34 | }
|
35 | static equals(messageA, messageB) {
|
36 | return this.compare(messageA, messageB) === 0;
|
37 | }
|
38 | static deduplicate(messages) {
|
39 | return messages.sort(NormalizedMessage.compare).filter((message, index) => {
|
40 | return (index === 0 || !NormalizedMessage.equals(message, messages[index - 1]));
|
41 | });
|
42 | }
|
43 | static compareTypes(typeA, typeB) {
|
44 | const priorities = [typeA, typeB].map(type => {
|
45 | return [
|
46 | NormalizedMessage.TYPE_LINT ,
|
47 | NormalizedMessage.TYPE_DIAGNOSTIC
|
48 | ].indexOf(type);
|
49 | });
|
50 | return priorities[0] - priorities[1];
|
51 | }
|
52 | static compareSeverities(severityA, severityB) {
|
53 | const priorities = [severityA, severityB].map(type => {
|
54 | return [
|
55 | NormalizedMessage.SEVERITY_WARNING ,
|
56 | NormalizedMessage.SEVERITY_ERROR
|
57 | ].indexOf(type);
|
58 | });
|
59 | return priorities[0] - priorities[1];
|
60 | }
|
61 | static compareOptionalStrings(stringA, stringB) {
|
62 | if (stringA === stringB) {
|
63 | return 0;
|
64 | }
|
65 | if (stringA === undefined || stringA === null) {
|
66 | return -1;
|
67 | }
|
68 | if (stringB === undefined || stringB === null) {
|
69 | return 1;
|
70 | }
|
71 | return stringA.toString().localeCompare(stringB.toString());
|
72 | }
|
73 | static compareNumbers(numberA, numberB) {
|
74 | if (numberA === numberB) {
|
75 | return 0;
|
76 | }
|
77 | if (numberA === undefined || numberA === null) {
|
78 | return -1;
|
79 | }
|
80 | if (numberB === undefined || numberB === null) {
|
81 | return 1;
|
82 | }
|
83 | return numberA - numberB;
|
84 | }
|
85 | toJSON() {
|
86 | return {
|
87 | type: this.type,
|
88 | code: this.code,
|
89 | severity: this.severity,
|
90 | content: this.content,
|
91 | file: this.file,
|
92 | line: this.line,
|
93 | character: this.character,
|
94 | stack: this.stack
|
95 | };
|
96 | }
|
97 | isDiagnosticType() {
|
98 | return NormalizedMessage.TYPE_DIAGNOSTIC === this.type;
|
99 | }
|
100 | isLintType() {
|
101 | return NormalizedMessage.TYPE_LINT === this.type;
|
102 | }
|
103 | getFormattedCode() {
|
104 | return this.isDiagnosticType() ? 'TS' + this.code : this.code;
|
105 | }
|
106 | isErrorSeverity() {
|
107 | return this.severity === NormalizedMessage.SEVERITY_ERROR;
|
108 | }
|
109 | isWarningSeverity() {
|
110 | return this.severity === NormalizedMessage.SEVERITY_WARNING;
|
111 | }
|
112 | }
|
113 | NormalizedMessage.TYPE_DIAGNOSTIC = 'diagnostic';
|
114 | NormalizedMessage.TYPE_LINT = 'lint';
|
115 |
|
116 | NormalizedMessage.SEVERITY_ERROR = 'error';
|
117 | NormalizedMessage.SEVERITY_WARNING = 'warning';
|
118 | NormalizedMessage.ERROR_CODE_INTERNAL = 'INTERNAL_ERROR';
|
119 | exports.NormalizedMessage = NormalizedMessage;
|
120 |
|
\ | No newline at end of file |