forked from moddular/css-linter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
executable file
·167 lines (154 loc) · 4.01 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
#!/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',
'raphael',
'yui',
'min.js',
'ie6'
];
(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) {
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,
/* environment */
browser: true,
jquery: 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;
}
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 {
finder = finder.find(args.path || process.cwd());
// get a list of all files that don't match the excluded list
finder.on('file', function(path) {
if (isValidPath(path)) {
paths.push(path);
reporter.loadFile(path);
}
});
finder.on('end', validate);
}
})(require('jshint'), require('findit'), require('fs'), require('./lib/reporter')(args));