-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
272 lines (228 loc) · 7.16 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
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
const fs = require('fs');
const os = require('os');
const beep = require('beeper');
const c = require('ansi-colors');
const flog = require('fancy-log');
const through2 = require('through2');
const PluginError = require('plugin-error');
const stripJsonComments = require('strip-json-comments');
const {HTMLHint} = require('htmlhint');
const formatOutput = function (report, file, options) {
'use strict';
if (report.length === 0) {
return {
success: true
};
}
const filePath = (file.path || 'stdin');
// Handle errors
const messages = report.filter(err => {
return err;
}).map(err => {
return {
file: filePath,
error: err
};
});
const output = {
errorCount: messages.length,
success: false,
messages,
options
};
return output;
};
const htmlhintPlugin = function (options, customRules) {
'use strict';
const ruleset = {};
if (!customRules && options && Array.isArray(options) && options.length > 0) {
customRules = options;
options = {};
}
if (!options) {
options = {};
}
// Read Htmlhint options from a specified htmlhintrc file.
if (typeof options === 'string') {
// Don't catch readFile errors, let them bubble up
options = {
htmlhintrc: options
};
}
// If necessary check for required param(s), e.g. options hash, etc.
// read config file for htmlhint if available
if (options.htmlhintrc) {
try {
const externalOptions = fs.readFileSync(options.htmlhintrc, 'utf-8');
options = JSON.parse(stripJsonComments(externalOptions));
} catch (error) {
throw new Error('gulp-htmlhint: Cannot parse .htmlhintrc ' + (error.message || error));
}
}
if (Object.keys(options).length > 0) {
// Build a list of all available rules
for (const key in HTMLHint.defaultRuleset) {
if (HTMLHint.defaultRuleset.hasOwnProperty(key)) { // eslint-disable-line no-prototype-builtins
ruleset[key] = 1;
}
}
// Normalize htmlhint options
// htmlhint only checks for rulekey, so remove rule if set to false
for (const rule in options) {
if (options[rule]) {
ruleset[rule] = options[rule];
} else {
delete ruleset[rule];
}
}
}
// Add the defined custom rules
// This will not require adding the costume rule id to the .htmlhintrc file
if (customRules !== null && Array.isArray(customRules) && customRules.length > 0) {
const has = Object.prototype.hasOwnProperty;
for (const rule of customRules) {
if (typeof rule === 'object') {
HTMLHint.addRule(rule);
if (has.call(rule, 'id')) {
ruleset[rule.id] = true;
}
}
}
}
return through2.obj((file, enc, cb) => {
const report = HTMLHint.verify(file.contents.toString(), ruleset);
// Send status down-stream
file.htmlhint = formatOutput(report, file, options);
cb(null, file);
});
};
function getMessagesForFile(file) {
'use strict';
return file.htmlhint.messages.map(message_ => {
const {error: message} = message_;
let {evidence} = message;
const {line, col} = message;
const detail = line ? c.yellow('L' + line) + c.red(':') + c.yellow('C' + col) : c.yellow('GENERAL');
if (col === 0) {
evidence = c.red('?') + evidence;
} else if (col > evidence.length) {
evidence = c.red(evidence + ' ');
} else {
evidence = evidence.slice(0, col - 1) + c.red(evidence[col - 1]) + evidence.slice(col);
}
return {
message: c.red('[') + detail + c.red(']') + c.yellow(' ' + message.message) + ' (' + message.rule.id + ')',
evidence
};
});
}
const defaultReporter = function (file) {
'use strict';
const {errorCount} = file.htmlhint;
const plural = errorCount === 1 ? '' : 's';
beep();
flog(c.cyan(errorCount) + ' error' + plural + ' found in ' + c.magenta(file.path));
getMessagesForFile(file).forEach(data => {
flog(data.message);
flog(data.evidence);
});
};
htmlhintPlugin.addRule = function (rule) {
'use strict';
return HTMLHint.addRule(rule);
};
htmlhintPlugin.reporter = function (customReporter, options) {
'use strict';
let reporter = defaultReporter;
if (typeof customReporter === 'function') {
reporter = customReporter;
}
if (typeof customReporter === 'string') {
if (customReporter === 'fail' || customReporter === 'failOn') {
return htmlhintPlugin.failOnError();
}
if (customReporter === 'failAfter') {
return htmlhintPlugin.failAfterError();
}
reporter = require(customReporter);
}
if (typeof reporter === 'undefined') {
throw new TypeError('Invalid reporter');
}
return through2.obj((file, enc, cb) => {
// Only report if HTMLHint ran and errors were found
if (file.htmlhint && !file.htmlhint.success) {
reporter(file, file.htmlhint.messages, options);
}
cb(null, file);
});
};
htmlhintPlugin.failOnError = function (options) {
'use strict';
options = options || {};
return through2.obj((file, enc, cb) => {
// Something to report and has errors
let error;
if (file.htmlhint && !file.htmlhint.success) {
if (options.suppress === true) {
error = new PluginError('gulp-htmlhint', {
message: 'HTMLHint failed.',
showStack: false
});
} else {
const {errorCount} = file.htmlhint;
const plural = errorCount === 1 ? '' : 's';
const message = c.cyan(errorCount) + ' error' + plural + ' found in ' + c.magenta(file.path);
const messages = [message].concat(getMessagesForFile(file).map(m => {
return m.message;
}));
error = new PluginError('gulp-htmlhint', {
message: messages.join(os.EOL),
showStack: false
});
}
}
cb(error, file);
});
};
htmlhintPlugin.failAfterError = function (options) {
'use strict';
options = options || {};
let globalErrorCount = 0;
let globalErrorMessage = '';
return through2.obj(check, summarize);
function check(file, enc, cb) {
if (file.htmlhint && !file.htmlhint.success) {
if (options.suppress === true) {
globalErrorCount += file.htmlhint.errorCount;
} else {
globalErrorCount += file.htmlhint.errorCount;
const plural = file.htmlhint.errorCount === 1 ? '' : 's';
const message = c.cyan(file.htmlhint.errorCount) + ' error' + plural + ' found in ' + c.magenta(file.path);
const messages = [message].concat(getMessagesForFile(file).map(m => {
return m.message;
}));
globalErrorMessage += messages.join(os.EOL) + os.EOL;
}
}
cb(null, file);
}
function summarize(cb) {
if (!globalErrorCount) {
cb();
return;
}
const plural = globalErrorCount === 1 ? '' : 's';
const message = globalErrorMessage ?
c.cyan(globalErrorCount) + ' error' + plural + ' overall:' + os.EOL + globalErrorMessage :
c.cyan(globalErrorCount) + ' error' + plural + ' overall.';
const error = new PluginError('gulp-htmlhint', {
message: 'HTMLHint failed. ' + message,
showStack: false
});
cb(error);
}
};
// Backward compatibility
htmlhintPlugin.failReporter = htmlhintPlugin.failOnError;
module.exports = htmlhintPlugin;