1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var jshint = require('jshint').JSHINT;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | task.registerBasicTask('lint', 'Validate files with JSHint.', function(data, name) {
|
17 |
|
18 | var options = config('jshint.options');
|
19 | var globals = config('jshint.globals');
|
20 |
|
21 |
|
22 | verbose.writeflags(options, 'Options');
|
23 | verbose.writeflags(globals, 'Globals');
|
24 |
|
25 |
|
26 | file.expand(data).forEach(function(filepath) {
|
27 | task.helper('lint', file.read(filepath), options, globals, filepath);
|
28 | });
|
29 |
|
30 |
|
31 | if (task.hadErrors()) { return false; }
|
32 |
|
33 |
|
34 | log.writeln('Lint free.');
|
35 | });
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | task.registerHelper('lint', function(src, options, globals, extraMsg) {
|
43 |
|
44 | options = underscore.clone(options);
|
45 | globals = underscore.clone(globals);
|
46 |
|
47 | if (option('debug') !== undefined) {
|
48 | options.devel = options.debug = option('debug');
|
49 | }
|
50 | var msg = 'Linting' + (extraMsg ? ' ' + extraMsg : '') + '...';
|
51 | verbose.write(msg);
|
52 |
|
53 | var result = jshint(src, options || {}, globals || {});
|
54 |
|
55 | if (!result) {
|
56 | jshint.errors = jshint.errors.filter(function(o) {
|
57 |
|
58 |
|
59 | return o && !(o.reason === 'Read only.' && /\bexports\s*[=]/.test(o.evidence));
|
60 | });
|
61 |
|
62 | result = jshint.errors.length === 0;
|
63 | }
|
64 | if (result) {
|
65 |
|
66 | verbose.ok();
|
67 | } else {
|
68 |
|
69 | verbose.or.write(msg);
|
70 | log.error();
|
71 |
|
72 | jshint.errors.forEach(function(e) {
|
73 |
|
74 | if (!e) { return; }
|
75 | var pos;
|
76 | if (e.evidence) {
|
77 |
|
78 | fail.errorcount++;
|
79 |
|
80 | pos = '['.red + ('L' + e.line).yellow + ':'.red + ('C' + e.character).yellow + ']'.red;
|
81 | log.writeln(pos + ' ' + e.reason.yellow).writeln(e.evidence.inverse);
|
82 | } else {
|
83 |
|
84 | log.error(e.reason);
|
85 | }
|
86 | });
|
87 | log.writeln();
|
88 | }
|
89 | });
|