-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemstone-linter-html.js
57 lines (50 loc) · 2.01 KB
/
gemstone-linter-html.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
/*
** GemstoneJS -- Gemstone JavaScript Technology Stack
** Copyright (c) 2016-2019 Gemstone Project <http://gemstonejs.com>
** Licensed under Apache License 2.0 <https://spdx.org/licenses/Apache-2.0>
*/
/* load external requirements */
const path = require("path")
const fs = require("mz/fs")
const HTMLHint = require("htmlhint")
const HTMLHintGemstone = require("gemstone-config-htmlhint")
/* add custom Gemstone rules */
const htmlhint = new HTMLHint.HTMLHint()
const config = HTMLHintGemstone(htmlhint)
/* exported API function */
module.exports = async function (filenames, opts = {}, report = { sources: {}, findings: [] }) {
/* interate over all source files */
let passed = true
if (typeof opts.progress === "function")
opts.progress(0.0, "linting HTML: starting")
for (let i = 0; i < filenames.length; i++) {
/* indicate progress */
if (typeof opts.progress === "function")
opts.progress(i / filenames.length, `linting HTML: ${filenames[i]}`)
/* read source code */
const source = await fs.readFile(filenames[i], "utf8")
/* determine name */
const name = path.relative(process.cwd(), filenames[i])
/* lint the source code */
const rules = Object.assign({}, config, opts.rules)
const messages = htmlhint.verify(source, rules)
if (messages.length > 0) {
for (let j = 0; j < messages.length; j++) {
report.findings.push({
ctx: "HTML",
filename: name,
line: messages[j].line,
column: messages[j].col,
message: messages[j].message,
ruleProc: "htmlhint",
ruleId: messages[j].rule.id
})
}
report.sources[name] = source
passed = false
}
}
if (typeof opts.progress === "function")
opts.progress(1.0, "")
return passed
}