-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
175 lines (152 loc) · 5.2 KB
/
index.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
'use strict';
import path from 'path';
import fs from 'fs';
import {HTMLHint} from 'htmlhint';
import loaderUtils from 'loader-utils';
import chalk from 'chalk';
import stripBom from 'strip-bom';
import glob from 'glob';
function formatMessage(message) {
let {evidence} = message;
const {line} = message;
const {col} = message;
const detail = typeof message.line === 'undefined' ?
chalk.yellow('GENERAL') : `${chalk.yellow('L' + line)}${chalk.red(':')}${chalk.yellow('C' + col)}`;
if (col === 0) {
evidence = chalk.red('?') + evidence;
} else if (col > evidence.length) {
evidence = chalk.red(evidence + ' ');
} else {
evidence = `${evidence.slice(0, col - 1)}${chalk.red(evidence[col - 1])}${evidence.slice(col)}`;
}
return {
message: `${chalk.red('[')}${detail}${chalk.red(']')}${chalk.yellow(' ' + message.message)} (${message.rule.id})`,
evidence: evidence // eslint-disable-line object-shorthand
};
}
function defaultFormatter(messages) {
let output = '';
messages.forEach(message => {
const formatted = formatMessage(message);
output += formatted.message + '\n';
output += formatted.evidence + '\n';
});
return output.trim();
}
function loadCustomRules(options) {
let rulesDir = options.rulesDir.replace(/\\/g, '/');
if (fs.existsSync(rulesDir)) {
if (fs.statSync(rulesDir).isDirectory()) {
rulesDir += /\/$/.test(rulesDir) ? '' : '/';
rulesDir += '**/*.js';
glob.sync(rulesDir, {
dot: false,
nodir: true,
strict: false,
silent: true
}).forEach(file => {
loadRule(file, options);
});
} else {
loadRule(rulesDir, options);
}
}
}
function loadRule(filepath, options) {
options = options || {};
filepath = path.resolve(filepath);
const ruleObj = require(filepath); // eslint-disable-line import/no-dynamic-require
const ruleOption = options[ruleObj.id]; // We can pass a value to the rule
ruleObj.rule(HTMLHint, ruleOption);
}
function lint(source, options, webpack, done) {
try {
if (options.customRules) {
options.customRules.forEach(rule => HTMLHint.addRule(rule));
}
if (options.rulesDir) {
loadCustomRules(options);
}
const report = HTMLHint.verify(source, options);
if (report.length > 0) {
const reportByType = {
warning: report.filter(message => message.type === 'warning'),
error: report.filter(message => message.type === 'error')
};
// Add filename for each results so formatter can have relevant filename
report.forEach(r => {
r.filePath = webpack.resourcePath;
});
const messages = options.formatter(report);
if (options.outputReport && options.outputReport.filePath) {
let reportOutput;
// If a different formatter is passed in as an option use that
if (options.outputReport.formatter) {
reportOutput = options.outputReport.formatter(report);
} else {
reportOutput = messages;
}
const filePath = loaderUtils.interpolateName(webpack, options.outputReport.filePath, {
content: report.map(r => r.filePath).join('\n')
});
webpack.emitFile(filePath, reportOutput);
}
let emitter = reportByType.error.length > 0 ? webpack.emitError : webpack.emitWarning;
if (options.emitAs === 'error') {
emitter = webpack.emitError;
} else if (options.emitAs === 'warning') {
emitter = webpack.emitWarning;
}
emitter(new Error(options.formatter(report)));
if (reportByType.error.length > 0 && options.failOnError) {
throw new Error('Module failed because of a htmlhint error.');
}
if (reportByType.warning.length > 0 && options.failOnWarning) {
throw new Error('Module failed because of a htmlhint warning.');
}
}
done(null, source);
} catch (error) {
done(error);
}
}
module.exports = function (source) {
const DEFAULT_CONFIG_FILE = '.htmlhintrc';
const options = Object.assign(
{ // Loader defaults
formatter: defaultFormatter,
emitAs: null, // Can be either warning or error
failOnError: false,
failOnWarning: false,
customRules: [],
configFile: DEFAULT_CONFIG_FILE
},
this.options && this.options.htmlhint ? this.options.htmlhint : {}, // User defaults
loaderUtils.getOptions(this) // Loader query string
);
this.cacheable();
const done = this.async();
let configFilePath = options.configFile;
if (!path.isAbsolute(configFilePath)) {
configFilePath = path.join(process.cwd(), configFilePath);
}
if (fs.existsSync(configFilePath)) {
fs.readFile(configFilePath, 'utf8', (err, configString) => {
if (err) {
done(err);
} else {
try {
const htmlHintConfig = JSON.parse(stripBom(configString));
lint(source, Object.assign(options, htmlHintConfig), this, done);
} catch (error) {
done(new Error('Could not parse the htmlhint config file'));
}
}
});
} else {
if (configFilePath !== path.join(process.cwd(), DEFAULT_CONFIG_FILE)) {
console.warn(`Could not find htmlhint config file in ${configFilePath}`);
}
lint(source, options, this, done);
}
};