forked from Jack-Works/pug-lint-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (45 loc) · 1.52 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
'use strict';
var PugLint = require('pug-lint');
var parse = require('./parse.js');
var webpack_util = require('loader-utils');
var util = require('util');
var linter = new PugLint();
var configured = false;
var cheerio = require('cheerio');
var htmlparser = require('htmlparser2');
module.exports = function (content) {
if (this.resourceQuery) return content;
this.cacheable();
var config = webpack_util.getOptions(this);
if(!configured) linter.configure(parse(config));
var template;
var handler = new htmlparser.DefaultHandler((error, dom) => {
if (error) {
return console.error(error);
}
var $ = cheerio.load(dom);
template = $('template[lang="pug"]').text();
})
var parser = new htmlparser.Parser(handler)
parser.parseComplete(content)
var result = linter.checkString(template);
if(result.length) {
this.emitError(result.sort(function (a, b) {
var line = a.line - b.line;
if(line == 0) return (a.column || 0) - (b.column || 0);
else return line;
}).map(function (problem) {
let message = problem.message.split('\n')
message.splice(0, 1)
message.reverse()
message.splice(0, 2)
message.reverse()
return util.format('\n%d:%d\t\x1b[31merror\x1b[0m\t%s\n%s',
problem.line,
problem.column || 0,
problem.msg,
message.join('\n'));
}).join('\n\n'));
}
return content
}