update dependencies and devDependencies, fix lint issues
[squeep-html-template-helper] / test / lint-html.js
1 'use strict';
2
3 /**
4 * A brief assertion wrapper around html-validate.
5 */
6
7 const assert = require('node:assert');
8
9 /**
10 * @typedef {object} ConsoleLike
11 * @property {Function} debug debug
12 */
13
14 /**
15 * @typedef {import('html-validate')} HtmlValidateModule
16 */
17
18 /**
19 * Given an instance of html-validate, returns a function which asserts validity of some HTML.
20 * @param {ConsoleLike} logger logger
21 * @param {HtmlValidateModule.HtmlValidate} htmlValidate html-validate instance
22 * @returns {(html: string) => Promise<void>} function which lints html
23 */
24 function makeHtmlLint(logger, htmlValidate) {
25 /**
26 *
27 * @param {any[]} violations array of violations to add to
28 * @param {any} message violation to add
29 */
30 function note(violations, message) {
31 violations.push(message.ruleId);
32 logger.debug('HtmlLint', message);
33 }
34 return async function HtmlLint(html) {
35 const violations = [];
36 const report = await htmlValidate.validateString(html);
37 report.results.forEach((r) => {
38 if (Array.isArray(r)) {
39 r.forEach((m) => {
40 note(violations, m);
41 });
42 } else {
43 note(violations, r);
44 }
45 });
46 assert(report.valid, 'HTML violations: ' + violations.join(', '));
47 };
48 }
49
50 module.exports = {
51 makeHtmlLint,
52 };