-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjs.js
executable file
·181 lines (166 loc) · 4.51 KB
/
js.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
#!/usr/bin/env node
var args = (function() {
var args = {};
process.argv.slice(2).forEach(function(arg) {
if (arg.indexOf('--') === 0) {
arg = arg.replace(/^--/, '');
if (arg.indexOf('=') > 0) {
args[arg.split('=')[0]] = arg.split('=')[1];
} else {
args[arg] = true;
}
}
});
return args;
})();
// ignore files we don't have control over
var excluded = [
'rendertools',
'_preview',
'ckeditor',
'jquery-ui',
'jquery',
'compiled',
'greenfield',
'jwplayer',
'swfobject',
'WriteCapture',
'jmol',
'jsmol',
'raphael',
'yui',
'min.js',
'ie6'
];
//ignore varnames we don't have control over
var excludedVarNames = {
'uglifyJS': true
};
(function(jshint, finder, fs, reporter) {
var paths = [],
status = 0,
count = 0;
var validate = function() {
setTimeout(function() {
if (reporter.hasLoaded(paths.length)) {
paths.map(function(path) {
fs.readFile(path, 'utf-8', function(err, data) {
// sniff for an environment hint on the first non-empty line
var rx = /^[\s\n\r]*[^\n\r]*\b(node|phantom)\b/g;
envhint = (null !== (rxRes = rx.exec(data))) ? rxRes[1] : null;
jshint.JSHINT(data, {
curly: true,
eqeqeq: true,
forin: true,
immed: true,
newcap: true,
undef: true,
latedef: true,
trailing: true,
white: true,
/* now suppress some errors */
shadow: true,
sub: true,
scripturl: true,
/* environment */
browser: true,
jquery: true,
node: ('node' === envhint),
// phantom: ('phantom' === envhint), // doesn't seem to understand 'phantom' in node v0.10.18
wsh: true,
nonstandard: true
});
var data = jshint.JSHINT.data(),
types = ['closure', 'outer', 'param', 'var'],
varNames = {};
data.functions.forEach(function(fn) {
types.forEach(function(type) {
if (fn[type] && fn[type].length) {
fn[type].forEach(function(name) {
varNames[name] = 'appears in ' + fn.name + ' at around line ' + fn.line;
});
}
});
});
var invalidVarNames = Object.keys(varNames).filter(function(name) {
if (name === '$') { // jQuery
return false;
}
if (name.match(/^[A-Z][A-Z_]+$/)) { // SOME_CONSTANT_VALUE
return false;
}
if (name.match(/[A-Z]{2,}/)) { // disallow someHTML should be someHtml
return (true !== excludedVarNames[name]) ? true : false;
}
if (name.match(/^_?\$?[a-z][a-zA-Z0-9]*$/)) { // $myVar, myVar, _myVar or _$myVar
return false;
}
if (name.match(/^[A-Z][a-z0-9][a-zA-Z0-9]*$/)) { // MyClass
return false;
}
return true;
});
if (invalidVarNames.length) {
if (!jshint.JSHINT.errors) {
jshint.JSHINT.errors = [];
}
invalidVarNames.forEach(function(name) {
jshint.JSHINT.errors.push({
reason: 'Invalid variable name',
evidence: name + ' ' + varNames[name],
character: 'Unknown',
line: 'Unknown'
});
});
}
if (jshint.JSHINT.errors && jshint.JSHINT.errors.length) {
jshint.JSHINT.errors.forEach(function(err) {
if (err) {
var message = {
type: 'error',
message: err.reason + ' ' + ((typeof err.evidence !== 'undefined') ? err.evidence.replace(/^\s+/, '') : ''),
col: err.character,
line: err.line
};
status = reporter.record(message, path) || status;
}
});
}
if (++count === paths.length) {
reporter.summarise();
process.exit(status);
}
});
});
} else {
setTimeout(arguments.callee, 100);
}
}, 100);
};
var isValidPath = function(path) {
return excluded.reduce(function(prev, current) {
return prev && path.indexOf(current) === -1;
}, path.match(/\.js$/));
};
if (args.files) {
paths = args.files.split(',').map(function(val) {
return process.cwd() + '/' + val;
}).filter(function(path) {
return isValidPath(path);
});
paths.forEach(function(path) {
reporter.loadFile(path)
});
validate();
} else {
var emitter = finder(args.path || process.cwd());
// get a list of all files that don't match the excluded list
emitter.on('file', function(path) {
if (isValidPath(path)) {
paths.push(path);
reporter.loadFile(path);
}
});
emitter.on('end', validate);
}
})(require('jshint'), require('walkdir'), require('fs'), require('./lib/reporter')(args));