-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
190 lines (150 loc) · 4.95 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
var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var findUp = require('find-up');
var mkdirp = require('mkdirp');
var JSHINT = require('jshint').JSHINT;
var Filter = require('broccoli-persistent-filter');
var crypto = require('crypto');
var stringify = require('json-stable-stringify');
JSHinter.prototype = Object.create(Filter.prototype);
JSHinter.prototype.constructor = JSHinter;
function JSHinter (inputNode, options) {
if (!(this instanceof JSHinter)) return new JSHinter(inputNode, options);
options = options || {};
if (!options.hasOwnProperty('persist')) {
options.persist = true;
}
Filter.call(this, inputNode, {
annotation: options.annotation,
persist: options.persist
});
this.log = true;
this.options = options;
this.console = console;
for (var key in options) {
if (options.hasOwnProperty(key)) {
this[key] = options[key]
}
}
}
JSHinter.prototype.extensions = ['js'];
JSHinter.prototype.targetExtension = 'lint.js';
JSHinter.prototype.baseDir = function() {
return __dirname;
};
JSHinter.prototype.build = function () {
var self = this;
self._errors = [];
if (!self.jshintrc) {
var jshintPath = self.jshintrcPath || path.join(this.inputPaths[0], self.jshintrcRoot || '');
self.jshintrc = self.getConfig(jshintPath);
}
return Filter.prototype.build.call(this)
.finally(function() {
if (self._errors.length > 0) {
var label = ' JSHint Error' + (self._errors.length > 1 ? 's' : '');
self.console.log('\n' + self._errors.join('\n'));
self.console.log(chalk.yellow('===== ' + self._errors.length + label + '\n'));
}
});
};
JSHinter.prototype.processString = function (content, relativePath) {
var passed = JSHINT(content, this.jshintrc);
var errors = this.processErrors(relativePath, JSHINT.errors);
var output = '';
if (!this.disableTestGenerator) {
output = this.testGenerator(relativePath, passed, errors);
}
return {
output: output,
passed: passed,
errors: errors
};
};
JSHinter.prototype.postProcess = function(results) {
var errors = results.errors;
var passed = results.passed;
if (this.failOnAnyError && errors.length > 0){
var generalError = new Error('JSHint failed');
generalError.jshintErrors = errors;
throw generalError;
}
if (!passed && this.log) {
this.logError(errors);
}
return results;
};
JSHinter.prototype.processErrors = function (file, errors) {
if (!errors) { return ''; }
var len = errors.length,
str = '',
error, idx;
if (len === 0) { return ''; }
for (idx=0; idx<len; idx++) {
error = errors[idx];
if (error !== null) {
str += file + ': line ' + error.line + ', col ' +
error.character + ', ' + error.reason + '\n';
}
}
return str + "\n" + len + ' error' + ((len === 1) ? '' : 's');
};
JSHinter.prototype.testGenerator = function(relativePath, passed, errors) {
if (errors) {
errors = "\\n" + this.escapeErrorString(errors);
} else {
errors = "";
}
return "" +
"QUnit.module('JSHint - " + path.dirname(relativePath) + "');\n" +
"QUnit.test('" + relativePath + " should pass jshint', function(assert) { \n" +
" assert.expect(1);\n" +
" assert.ok(" + !!passed + ", '" + relativePath + " should pass jshint." + errors + "'); \n" +
"});\n";
};
JSHinter.prototype.logError = function(message, color) {
color = color || 'red';
this._errors.push(chalk[color](message) + "\n");
};
JSHinter.prototype.getConfig = function(rootPath) {
if (!rootPath) { rootPath = process.cwd(); }
var jshintrcPath = findUp.sync('.jshintrc', {cwd: rootPath});
if (jshintrcPath) {
var config = fs.readFileSync(jshintrcPath, {encoding: 'utf8'});
try {
return JSON.parse(this.stripComments(config));
} catch (e) {
this.console.error(chalk.red('Error occured parsing .jshintrc.'));
this.console.error(e.stack);
return null;
}
}
};
JSHinter.prototype.stripComments = function(string) {
string = string || "";
string = string.replace(/\/\*(?:(?!\*\/)[\s\S])*\*\//g, "");
string = string.replace(/\/\/[^\n\r]*/g, ""); // Everything after '//'
return string;
};
JSHinter.prototype.escapeErrorString = function(string) {
string = string.replace(/\n/gi, "\\n");
string = string.replace(/'/gi, "\\'");
return string;
};
JSHinter.prototype.optionsHash = function() {
if (!this._optionsHash) {
this._optionsHash = crypto.createHash('md5')
.update(stringify(this.options), 'utf8')
.update(stringify(this.jshintrc) || '', 'utf8')
.update(this.testGenerator.toString(), 'utf8')
.update(this.logError.toString(), 'utf8')
.update(this.escapeErrorString.toString(), 'utf8')
.digest('hex');
}
return this._optionsHash;
};
JSHinter.prototype.cacheKeyProcessString = function(string, relativePath) {
return this.optionsHash() + Filter.prototype.cacheKeyProcessString.call(this, string, relativePath);
};
module.exports = JSHinter;