-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathmarkdown_formatter.js
357 lines (344 loc) · 11.6 KB
/
markdown_formatter.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2017 TODO Group. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// eslint-disable-next-line no-unused-vars
const Result = require('../lib/result')
// eslint-disable-next-line no-unused-vars
const FormatResult = require('../lib/formatresult')
const slugger = require('../lib/github_slugger')
const ERROR_SYMBOL = '❗'
const FAIL_SYMBOL = '❌'
const WARN_SYMBOL = '⚠️'
const PASS_SYMBOL = '✅'
const FIX_SYMBOL = '🔨'
const SUGGESTED_FIX = `${FIX_SYMBOL} **Suggested Fix**:`
const APPLIED_FIX = `${PASS_SYMBOL} **Applied Fix**:`
const DISCLAIMER =
'*This report was generated automatically by the Repolinter.*'
const COLLAPSE_TOP = `<details>
<summary>Click to see rules</summary>`
const COLLAPSE_BOTTOM = '</details>'
/**
* Optionally add prefix or suffix to a string if it's truthy.
*
* @private
* @param {string?} pre The optional prefix
* @param {string?} base The base string
* @param {string?} [suf] The optional suffix
* @returns {string} The concatenated string or '' if base is falsey
*/
function opWrap(pre, base, suf) {
if (base) return (pre || '') + base + (suf || '')
return ''
}
/**
* A markdown formatter for Repolinter output, designed to be used with GH issues.
* Exported as markdownFormatter.
*
* @protected
*/
class MarkdownFormatter {
/**
* Creates a header for a rule-output block.
*
* @private
* @param {string} name The name of the rule
* @param {string} symbol The status symbol to use for the rule
* @returns {string} A formatted rule header (will not include ##)
*/
static formatRuleHeading(name, symbol) {
return `${opWrap(null, symbol, ' ')}\`${name}\``
}
/**
* Creates href tag allowing a header to be linked to in an issue or PR.
* You can append the output of this function to a header to make it linkable.
*
* @private
* @param {string} name The name of the rule (unslugged)
* @returns {string} A formatted header lint (ex. <a href="#user-content-some-heading" id="some-heading">#</a>)
*/
static makeHeaderLink(name) {
const slug = slugger.slug(name)
return `<a href="#user-content-${slug}" id="user-content-${slug}">#</a>`
}
/**
* Format a FormatResult object into a line of human-readable text.
*
* @param {FormatResult} result The result to format, must be valid
* @param {string} symbol The symbol to use at the start of the log line (ex. ✅)
* @param {boolean?} [dryRun] Whether or not to say the fix is "suggested" instead of "applied".
* @returns {string} The formatted string
*/
static formatResult(result, symbol, dryRun) {
const header = MarkdownFormatter.formatRuleHeading(
result.ruleInfo.name,
symbol
)
const formatBase = [
`### ${header} ${MarkdownFormatter.makeHeaderLink(header)}`
]
if (result.status === FormatResult.ERROR) {
// the rule failed to run for some reason?
const content = `\n\nThis rule failed to run with the following error: ${result.runMessage}. `
formatBase.push(content)
if (result.ruleInfo.policyInfo) {
formatBase.push(
`${result.ruleInfo.policyInfo}.${opWrap(
' For more information please visit: ',
result.ruleInfo.policyUrl,
'.'
)}`
)
}
} else if (result.status === FormatResult.IGNORED) {
// the rule was ignored
formatBase.push(
`\n\nThis rule was ignored for the following reason: ${result.runMessage}`
)
if (result.ruleInfo.policyInfo) {
formatBase.push(
`${result.ruleInfo.policyInfo}.${opWrap(
' For more information please visit: ',
result.ruleInfo.policyUrl,
'.'
)}`
)
}
} else if (result.lintResult.targets.length <= 1 && !result.fixResult) {
// the rule passed!
// condensed version for 0-1 targets and no fix
const body =
'\n\n' +
opWrap(null, result.lintResult.message, '. ') +
opWrap(
null,
result.lintResult.targets.length &&
result.lintResult.targets[0].message,
' '
) +
opWrap(
'(`',
result.lintResult.targets.length &&
(result.lintResult.targets[0].path ||
result.lintResult.targets[0].pattern),
'`). '
) +
opWrap(null, result.ruleInfo.policyInfo, '. ') +
opWrap(
'For more information please visit ',
result.ruleInfo.policyUrl,
'.'
)
formatBase.push(body)
} else {
// normal version with bulleted list for files
// start with policy information sentence
const start =
'\n\n' +
opWrap(null, result.ruleInfo.policyInfo, '. ') +
opWrap(
'For more information please visit ',
result.ruleInfo.policyUrl,
'. '
) +
opWrap(null, result.lintResult.message, '. ')
formatBase.push(start)
// create bulleted list, filter only failed targets
const failedList = result.lintResult.targets.filter(
t => t.passed === false
)
if (failedList.length === 0) {
formatBase.push('All files passed this test.')
} else {
formatBase.push('Below is a list of files or patterns that failed:\n\n')
// format the result based on these pieces of information
const list = failedList
// match each target to it's fix result, if one exists
.map(t =>
result.fixResult && t.path
? [
t,
result.fixResult.targets.find(f => f.path === t.path) || null
]
: [t, null]
)
.map(([lintTarget, fixTarget]) => {
const base = `- \`${
lintTarget.path || lintTarget.pattern
}\`${opWrap(': ', lintTarget.message, '.')}`
// no fix format
if (!fixTarget || !fixTarget.passed) {
return base
}
// with fix format
return (
base +
`\n - ${dryRun ? SUGGESTED_FIX : APPLIED_FIX} ${
fixTarget.message || result.fixResult.message
}`
)
})
.join('\n')
formatBase.push(list)
}
}
// suggested fix for overall rule/fix combo
if (result.fixResult && result.fixResult.passed) {
// find all fixes which didn't have a lint target (haven't been displayed yet)
const unassociatedFixList = result.fixResult.targets.filter(
t => !t.path || !result.lintResult.targets.find(l => l.path === t.path)
)
// break if there aren't any
if (result.fixResult.message || unassociatedFixList.length !== 0) {
const fixSuggest = `\n\n${dryRun ? SUGGESTED_FIX : APPLIED_FIX}${opWrap(
' ',
result.fixResult.message,
'.'
)}`
formatBase.push(fixSuggest)
const fixList = unassociatedFixList.map(
f => `\n- \`${f.path || f.pattern}\`${opWrap(': ', f.message, '.')}`
)
if (fixList.length) {
formatBase.push('\n')
}
formatBase.push(...fixList)
}
}
// return the created string!
return formatBase.join('')
}
/**
* Sort a list of FormatResults based on thier status, so it's easier to
* manipulate them. Returns an object with keys of FormatResult.<status name>
* and values of an array of results.
*
* @private
* @param {FormatResult[]} results
* @returns {Object.<string, FormatResult[]>} The object representing sorted results.
*/
static sortResults(results) {
/** @ignore @type {Object.<string, FormatResult[]>} */
const out = {}
for (const key of FormatResult.getAllStatus()) {
out[key] = []
}
return results.reduce((a, c) => {
a[c.status].push(c)
return a
}, out)
}
/**
* Creates a markdown section representing a type of rule result.
*
* @private
* @param {string} name What to name the markdown section.
* @param {string} body The content of the markdown section.
* @param {boolean?} [collapse] Whether or not to have the section be collapsed by default
* @returns {string} A fully formatted markdown section
*/
static createSection(name, body, collapse = false) {
const section = `\n\n## ${name} ${MarkdownFormatter.makeHeaderLink(name)}
${collapse ? `\n${COLLAPSE_TOP}\n` : ''}
${body}
${collapse ? `\n${COLLAPSE_BOTTOM}` : ''}`
return section
}
/**
*
* @param {LintResult} output The linter output to format
* @param {string} [output.formatOptions.disclaimer] A disclaimer to put at the top of the markdown document.
* @param {boolean?} [dryRun] Whether or not to print fix "suggested" or "applied"
* @returns {string} The formatted output
*/
static formatOutput(output, dryRun) {
const formatBase = [
`# Repolinter Report\n\n${
(output.formatOptions && output.formatOptions.disclaimer) || DISCLAIMER
}`
]
// count each type of format result in an object
const sorted = MarkdownFormatter.sortResults(output.results)
// create the summary block
const summary = `\n\nThis Repolinter run generated the following results:
| ${ERROR_SYMBOL} Error | ${FAIL_SYMBOL} Fail | ${WARN_SYMBOL} Warn | ${PASS_SYMBOL} Pass | Ignored | Total |
|---|---|---|---|---|---|
| ${sorted[FormatResult.ERROR].length} | ${
sorted[FormatResult.RULE_NOT_PASSED_ERROR].length
} | ${sorted[FormatResult.RULE_NOT_PASSED_WARN].length} | ${
sorted[FormatResult.RULE_PASSED].length
} | ${sorted[FormatResult.IGNORED].length} | ${output.results.length} |`
formatBase.push(summary)
// configure each section
const sectionConfig = [
{
type: FormatResult.ERROR,
name: 'Error',
symbol: ERROR_SYMBOL,
collapse: false
},
{
type: FormatResult.RULE_NOT_PASSED_ERROR,
name: 'Fail',
symbol: FAIL_SYMBOL,
collapse: false
},
{
type: FormatResult.RULE_NOT_PASSED_WARN,
name: 'Warning',
symbol: WARN_SYMBOL,
collapse: true
},
{
type: FormatResult.RULE_PASSED,
name: 'Passed',
symbol: PASS_SYMBOL,
collapse: true
},
{
type: FormatResult.IGNORED,
name: 'Ignored',
symbol: '',
collapse: true
}
]
// filter down to sections that have items
const relevantSections = sectionConfig.filter(
cfg => sorted[cfg.type].length > 0
)
// generate the TOC
formatBase.push('\n')
const toc = relevantSections.map(cfg => {
// generate rule-items
const subItems = sorted[cfg.type].map(r => {
const heading = MarkdownFormatter.formatRuleHeading(
r.ruleInfo.name,
cfg.symbol
)
return `\n - [${heading}](#user-content-${slugger.slug(heading)})`
})
// generate top level section
return `\n- [${cfg.name}](#user-content-${slugger.slug(
cfg.name
)})${subItems.join('')}`
})
formatBase.push(...toc)
// generate content sections
const allSections = relevantSections.map(cfg =>
MarkdownFormatter.createSection(
cfg.name,
sorted[cfg.type]
.map(r => MarkdownFormatter.formatResult(r, cfg.symbol, dryRun))
.join('\n\n'),
cfg.collapse
)
)
// generate TOC
// add it to the overall format
formatBase.push(...allSections)
// add final trailing newline
formatBase.push('\n')
// generate our finished markdown document, removing all trailing whitespace
return formatBase.join('').replace(/[^\S\r\n]+$/gm, '')
}
}
module.exports = MarkdownFormatter