This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
148 lines (122 loc) · 3.24 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
'use strict';
var fs = require('fs');
var path = require('path');
var strip = require('strip-json-comments');
var utils = require('loader-utils');
var RcFinder = require('rcfinder');
var Checker = require('jscs');
var extend = function (obj) {
var source, prop, i, l;
for (i = 1, l = arguments.length; i < l; i += 1) {
source = arguments[i];
for (prop in source) {
if (hasOwnProperty.call(source, prop)) {
obj[prop] = source[prop];
}
}
}
return obj;
};
var rcFinder = new RcFinder('.jscsrc', {
loader: function (rcpath) {
return rcpath;
}
});
var loadConfigSync = function () {
var folder = path.dirname(this.resourcePath);
var rcpath = rcFinder.find(folder);
if (typeof rcpath !== 'string') {
// No .jscsrc found.
return {};
}
this.addDependency(rcpath);
var file = fs.readFileSync(rcpath, 'utf8');
return JSON.parse(strip(file));
};
var loadConfigAsync = function (callback) {
var folder = path.dirname(this.resourcePath);
rcFinder.find(folder, function (err, rcpath) {
if (typeof rcpath !== 'string') {
// No .jscsrc found.
return callback(null, {});
}
this.addDependency(rcpath);
fs.readFile(rcpath, 'utf8', function (err, file) {
var options;
if (!err) {
try {
options = JSON.parse(strip(file));
}
catch (e) {
err = e;
}
}
callback(err, options);
});
}.bind(this));
};
var checkSource = function (source, config) {
// Copy options to own object.
var options = this.options.jscs;
extend(config, options);
// Copy query to own object.
var query = utils.getOptions(this) || {};
extend(config, query);
// Move flags.
var emitErrors = config.emitErrors;
delete config.emitErrors;
var failOnHint = config.failOnHint;
delete config.failOnHint;
// Custom reporter.
var reporter = config.reporter;
delete config.reporter;
var checker = new Checker();
checker.registerDefaultRules();
checker.configure(config);
var result = checker.checkString(source);
if (!result.isEmpty()) {
if (reporter) {
reporter.call(this, result);
}
else {
var message = result.getErrorList().map(function (error) {
return ' ' + error.rule +
' @ line ' + error.line +
' char ' + error.column +
'\n ' + error.message;
}).join('\n\n');
var emitter = emitErrors ? this.emitError : this.emitWarning;
if (emitter) {
emitter('jscs results in errors\n' + message);
}
else {
throw new Error('Your module system doesn\'t support emitWarning.' +
' Update available? \n' + message);
}
}
if (failOnHint) {
throw new Error('Module failed in cause of jscs error.');
}
}
};
module.exports = function (source) {
this.cacheable();
var callback = this.async();
if (!callback) {
var config = loadConfigSync.call(this);
checkSource.call(this, source, config);
return source;
}
loadConfigAsync.call(this, function (err, config) {
if (err) {
return callback(err);
}
try {
checkSource.call(this, source, config);
}
catch (e) {
return callback(e);
}
callback(null, source);
}.bind(this));
};