|
| 1 | +/** |
| 2 | + * This reporter is adapted from the CLI reporter built into the pa11y library, |
| 3 | + * with some small differences for performance reasons. |
| 4 | + * |
| 5 | + * @see https://github.com/pa11y/pa11y/blob/6.1.1/lib/reporters/cli.js |
| 6 | + */ |
| 7 | +// @ts-check |
| 8 | + |
| 9 | +'use strict' |
| 10 | + |
| 11 | +const { cyan, green, gray, red, underline, yellow } = require('picocolors') |
| 12 | + |
| 13 | +// Pa11y version support |
| 14 | +const PA11Y_SUPPORTS = '^6.0.0 || ^6.0.0-alpha || ^6.0.0-beta' |
| 15 | + |
| 16 | +const DUPLICATE_WHITESPACE_EXP = /\s+/g |
| 17 | +const EMPTY_SPACE = ' ' |
| 18 | +const NEWLINE_LITERAL = '\n' |
| 19 | + |
| 20 | +const rootFilePath = 'file://' + process.cwd() |
| 21 | + |
| 22 | +// Helper strings for use in reporter methods |
| 23 | +const start = cyan(' >') |
| 24 | +const typeIndicators = { |
| 25 | + error: red(' • Error:'), |
| 26 | + notice: cyan(' • Notice:'), |
| 27 | + unknown: gray(' •'), |
| 28 | + warning: yellow(' • Warning:'), |
| 29 | +} |
| 30 | + |
| 31 | +function renderIssue(issue) { |
| 32 | + const code = issue.code |
| 33 | + const selector = issue.selector.replace(DUPLICATE_WHITESPACE_EXP, EMPTY_SPACE) |
| 34 | + const context = issue.context ? issue.context.replace(DUPLICATE_WHITESPACE_EXP, EMPTY_SPACE) : '[no context]' |
| 35 | + |
| 36 | + return cleanWhitespace(` |
| 37 | +
|
| 38 | + ${typeIndicators[issue.type]} ${issue.message} |
| 39 | + ${gray(`├── ${code}`)} |
| 40 | + ${gray(`├── ${selector}`)} |
| 41 | + ${gray(`└── ${context}`)} |
| 42 | + `) |
| 43 | +} |
| 44 | + |
| 45 | +// Output formatted results |
| 46 | +function renderResults(results) { |
| 47 | + const relativeFilePath = results.pageUrl.replace(rootFilePath, '.') |
| 48 | + if (results.issues.length) { |
| 49 | + const totals = { |
| 50 | + error: 0, |
| 51 | + notice: 0, |
| 52 | + warning: 0, |
| 53 | + } |
| 54 | + const issues = [] |
| 55 | + const summary = [] |
| 56 | + |
| 57 | + for (const issue of results.issues) { |
| 58 | + issues.push(renderIssue(issue)) |
| 59 | + totals[issue.type] = totals[issue.type] + 1 |
| 60 | + } |
| 61 | + |
| 62 | + if (totals.error > 0) { |
| 63 | + summary.push(red(`${totals.error} ${pluralize('Error', totals.error)}`)) |
| 64 | + } |
| 65 | + if (totals.warning > 0) { |
| 66 | + summary.push(yellow(`${totals.warning} ${pluralize('Warning', totals.warning)}`)) |
| 67 | + } |
| 68 | + if (totals.notice > 0) { |
| 69 | + summary.push(cyan(`${totals.notice} ${pluralize('Notice', totals.notice)}`)) |
| 70 | + } |
| 71 | + |
| 72 | + return cleanWhitespace(` |
| 73 | +
|
| 74 | + ${underline(`Results for file: ${relativeFilePath}`)} |
| 75 | + ${issues.join(NEWLINE_LITERAL)} |
| 76 | +
|
| 77 | + ${summary.join(NEWLINE_LITERAL)} |
| 78 | +
|
| 79 | + `) |
| 80 | + } |
| 81 | + return cleanWhitespace(` |
| 82 | + ${green('No issues found!')} |
| 83 | + `) |
| 84 | +} |
| 85 | + |
| 86 | +// Output the welcome message once Pa11y begins testing |
| 87 | +function renderBegin() { |
| 88 | + return cleanWhitespace(` |
| 89 | + ${cyan(underline('Welcome to Pa11y'))} |
| 90 | + `) |
| 91 | +} |
| 92 | + |
| 93 | +// Output debug messages |
| 94 | +function renderDebug(message) { |
| 95 | + message = `Debug: ${message}` |
| 96 | + return cleanWhitespace(` |
| 97 | + ${start} ${gray(message)} |
| 98 | + `) |
| 99 | +} |
| 100 | + |
| 101 | +// Output information messages |
| 102 | +function renderInfo(message) { |
| 103 | + return cleanWhitespace(` |
| 104 | + ${start} ${message} |
| 105 | + `) |
| 106 | +} |
| 107 | + |
| 108 | +function renderError(message) { |
| 109 | + if (!/^error:/i.test(message)) { |
| 110 | + message = `Error: ${message}` |
| 111 | + } |
| 112 | + return cleanWhitespace(` |
| 113 | + ${red(message)} |
| 114 | + `) |
| 115 | +} |
| 116 | + |
| 117 | +// Clean whitespace from output. This function is used to keep |
| 118 | +// the reporter code a little cleaner |
| 119 | +function cleanWhitespace(string) { |
| 120 | + return string.replace(/\t+|^\t*\n|\n\t*$/g, '') |
| 121 | +} |
| 122 | + |
| 123 | +function pluralize(noun, count) { |
| 124 | + return count === 1 ? noun : noun + 's' |
| 125 | +} |
| 126 | + |
| 127 | +module.exports = { |
| 128 | + begin: renderBegin, |
| 129 | + debug: renderDebug, |
| 130 | + info: renderInfo, |
| 131 | + error: renderError, |
| 132 | + results: renderResults, |
| 133 | + supports: PA11Y_SUPPORTS, |
| 134 | +} |
0 commit comments