-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathformatresult.js
142 lines (131 loc) · 4.65 KB
/
formatresult.js
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
// Copyright 2017 TODO Group. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// eslint-disable-next-line no-unused-vars
const Result = require('./result')
// eslint-disable-next-line no-unused-vars
const RuleInfo = require('./ruleinfo')
/**
* @typedef {Object} FormatResultBase
* @property {string} status status of the rule execution, either FormatResult.OK, FormatResult.IGNORED, or FormatResult.ERROR
* @property {string?} [runMessage] a message why the rule was ignored or failed, or undefined if the rule ran successfully
* @property {Result?} [lintResult] the linter result object, or undefined if the rule was ignored
* @property {Result?} [fixResult] the fix result object, or undefined if no fix was present or the rule was ignored
* @property {RuleInfo} ruleInfo the rule metadata object
*/
/**
* A class representing the information produces by a single rule/fix
* check. Do not use the constructor for this class, prefer the static
* factory functions instead.
*
* @memberof repolinter
* @extends FormatResultBase
* @param {RuleInfo} ruleInfo Information about the rule
* @param {string?} message Message from the engine indicating why the rule may have been excluded. must be null if lintRes is present.
* @param {string} status The "status" (error, ignored, ok) code, based on static values in FormatResult
* @param {Result?} lintRes The linter rule output
* @param {Result?} fixRes The fixer rule output
*/
class FormatResult {
constructor(ruleInfo, message, status, lintRes, fixRes) {
this.ruleInfo = ruleInfo
if (message) this.runMessage = message
this.status = status
if (lintRes) this.lintResult = lintRes
if (fixRes) this.fixResult = fixRes
}
/**
* Get the status of the lint result assuming it passed.
*
* @private
* @param {RuleInfo} ruleInfo The rule information to check
* @param {Result} lintRes The result that passed to check the status of.
* @returns {string} one of FormatResult.RULE_PASSED, FormatResult.RULE_NOT_PASSED_ERROR, FormatResult.RULE_NOT_PASSED_WARN
*/
static getStatus(ruleInfo, lintRes) {
if (lintRes.passed) {
return FormatResult.RULE_PASSED
}
if (ruleInfo.level === 'warning') {
return FormatResult.RULE_NOT_PASSED_WARN
}
if (ruleInfo.level === 'error') {
return FormatResult.RULE_NOT_PASSED_ERROR
}
return FormatResult.ERROR
}
/**
* Get a list of all possible status codes supported by FormatResult.
*
* @returns {string[]} A list of FormatResult.ERROR, FormatResult.IGNORED, and so on.
*/
static getAllStatus() {
return [
FormatResult.RULE_PASSED,
FormatResult.RULE_NOT_PASSED_WARN,
FormatResult.RULE_NOT_PASSED_ERROR,
FormatResult.ERROR,
FormatResult.IGNORED
]
}
/**
* Create a FormatResult for an ignored rule
*
* @param {RuleInfo} ruleInfo Information about the rule
* @param {string} message Why the rule was ignored
* @returns {FormatResult} A FormatResult object
*/
static CreateIgnored(ruleInfo, message) {
return new FormatResult(ruleInfo, message, FormatResult.IGNORED, null, null)
}
/**
* Create a FormatResult for a rule that threw an error
*
* @param {RuleInfo} ruleInfo Information about the rule
* @param {string} message Why the rule errored
* @returns {FormatResult} A FormatResult object
*/
static CreateError(ruleInfo, message) {
return new FormatResult(ruleInfo, message, FormatResult.ERROR, null, null)
}
/**
* Create a FormatResult for a rule that only contains
* output from a lint rule
*
* @param {RuleInfo} ruleInfo Information about the rule
* @param {Result} lintRes The result from the linter rule
* @returns {FormatResult} A FormatResult object
*/
static CreateLintOnly(ruleInfo, lintRes) {
return new FormatResult(
ruleInfo,
null,
FormatResult.getStatus(ruleInfo, lintRes),
lintRes,
null
)
}
/**
* Create a FormatResult for a rule that contains output
* from both a lint and fix job.
*
* @param {RuleInfo} ruleInfo Information about the rule
* @param {Result} lintRes The result from the lint rule
* @param {Result} fixRes The result from the fix rule
* @returns {FormatResult} A FormatResult object
*/
static CreateLintAndFix(ruleInfo, lintRes, fixRes) {
return new FormatResult(
ruleInfo,
null,
FormatResult.getStatus(ruleInfo, lintRes),
lintRes,
fixRes
)
}
}
FormatResult.RULE_PASSED = 'PASSED'
FormatResult.RULE_NOT_PASSED_ERROR = 'NOT_PASSED_ERROR'
FormatResult.RULE_NOT_PASSED_WARN = 'NOT_PASSED_WARN'
FormatResult.IGNORED = 'IGNORED'
FormatResult.ERROR = 'ERROR'
module.exports = FormatResult