This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathstring-checker.js
332 lines (277 loc) · 9.14 KB
/
string-checker.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
var esprima = require('esprima');
var babelJscs = require('babel-jscs');
var Errors = require('./errors');
var JsFile = require('./js-file');
var Configuration = require('./config/configuration');
var MAX_FIX_ATTEMPTS = 5;
function getInternalErrorMessage(rule, e) {
return 'Error running rule ' + rule + ': ' +
'This is an issue with JSCS and not your codebase.\n' +
'Please file an issue (with the stack trace below) at: ' +
'https://github.com/jscs-dev/node-jscs/issues/new\n' + e;
}
/**
* Starts Code Style checking process.
*
* @name StringChecker
*/
var StringChecker = function() {
this._configuredRules = [];
this._errorsFound = 0;
this._maxErrorsExceeded = false;
// Need to be defined here because Configuration module can choose
// custom esprima or chose parsers based on "esnext" option
this._esprima = esprima;
this._configuration = this._createConfiguration();
this._configuration.registerDefaultPresets();
};
StringChecker.prototype = {
/**
* Registers single Code Style checking rule.
*
* @param {Rule} rule
*/
registerRule: function(rule) {
this._configuration.registerRule(rule);
},
/**
* Registers built-in Code Style checking rules.
*/
registerDefaultRules: function() {
this._configuration.registerDefaultRules();
},
/**
* Get processed config.
*
* @return {Object}
*/
getProcessedConfig: function() {
return this._configuration.getProcessedConfig();
},
/**
* Loads configuration from JS Object. Activates and configures required rules.
*
* @param {Object} config
*/
configure: function(config) {
this._configuration.load(config);
if (this._configuration.hasCustomEsprima()) {
this._esprima = this._configuration.getCustomEsprima();
} else if (this._configuration.isESNextEnabled()) {
this._esprima = babelJscs;
}
this._verbose = this._configuration.getVerbose();
this._configuredRules = this._configuration.getConfiguredRules();
this._maxErrors = this._configuration.getMaxErrors();
},
/**
* Checks file provided with a string.
*
* @param {String} source
* @param {String} [filename='input']
* @returns {Errors}
*/
checkString: function(source, filename) {
filename = filename || 'input';
var file = this._createJsFileInstance(filename, source);
var errors = new Errors(file, this._verbose);
file.getParseErrors().forEach(function(parseError) {
if (!this._maxErrorsExceeded) {
this._addParseError(errors, parseError);
}
}, this);
// Do not check empty strings
if (file.getFirstToken({includeComments: true}).type === 'EOF') {
return errors;
}
this._checkJsFile(file, errors);
return errors;
},
/**
* Fix provided error.
*
* @param {JsFile} file
* @param {Errors} errors
* @protected
*/
_fixJsFile: function(file, errors) {
var list = errors.getErrorList();
var configuration = this.getConfiguration();
list.forEach(function(error) {
if (error.fixed) {
return;
}
var instance = configuration.getConfiguredRule(error.rule);
if (instance && instance._fix) {
try {
// "error.fixed = true" should go first, so rule can
// decide for itself (with "error.fixed = false")
// if it can fix this particular error
error.fixed = true;
instance._fix(file, error);
} catch (e) {
error.fixed = undefined;
errors.add(getInternalErrorMessage(error.rule, e), 1, 0);
}
}
});
},
/**
* Checks a file specified using JsFile instance.
* Fills Errors instance with validation errors.
*
* @param {JsFile} file
* @param {Errors} errors
* @protected
*/
_checkJsFile: function(file, errors) {
if (this._maxErrorsExceeded) {
return;
}
var errorFilter = this._configuration.getErrorFilter();
this._configuredRules.forEach(function(rule) {
errors.setCurrentRule(rule.getOptionName());
try {
rule.check(file, errors);
} catch (e) {
errors.setCurrentRule('internalError');
errors.add(getInternalErrorMessage(rule.getOptionName(), e.stack), 1, 0);
}
}, this);
this._configuration.getUnsupportedRuleNames().forEach(function(rulename) {
errors.add('Unsupported rule: ' + rulename, 1, 0);
});
// sort errors list to show errors as they appear in source
errors.getErrorList().sort(function(a, b) {
return (a.line - b.line) || (a.column - b.column);
});
if (errorFilter) {
errors.filter(errorFilter);
}
if (this._maxErrorsEnabled()) {
global.s = this._maxErrors === -1 || this._maxErrors === null;
if (this._maxErrors === -1 || this._maxErrors === null) {
this._maxErrorsExceeded = false;
} else {
this._maxErrorsExceeded = this._errorsFound + errors.getErrorCount() > this._maxErrors;
errors.stripErrorList(Math.max(0, this._maxErrors - this._errorsFound));
}
}
this._errorsFound += errors.getErrorCount();
},
/**
* Adds parse error to the error list.
*
* @param {Errors} errors
* @param {Error} parseError
* @private
*/
_addParseError: function(errors, parseError) {
if (this._maxErrorsExceeded) {
return;
}
errors.setCurrentRule('parseError');
errors.add(parseError.description, parseError.lineNumber, parseError.column);
if (this._maxErrorsEnabled()) {
this._errorsFound += 1;
this._maxErrorsExceeded = this._errorsFound >= this._maxErrors;
}
},
/**
* Creates configured JsFile instance.
*
* @param {String} filename
* @param {String} source
* @private
*/
_createJsFileInstance: function(filename, source) {
return new JsFile({
filename: filename,
source: source,
esprima: this._esprima,
esprimaOptions: this._configuration.getEsprimaOptions(),
es3: this._configuration.isES3Enabled(),
es6: this._configuration.isESNextEnabled()
});
},
/**
* Checks file provided with a string.
*
* @param {String} source
* @param {String} [filename='input']
* @returns {{output: String, errors: Errors}}
*/
fixString: function(source, filename) {
filename = filename || 'input';
var file = this._createJsFileInstance(filename, source);
var errors = new Errors(file, this._verbose);
var parseErrors = file.getParseErrors();
if (parseErrors.length > 0) {
parseErrors.forEach(function(parseError) {
this._addParseError(errors, parseError);
}, this);
return {output: source, errors: errors};
} else {
var attempt = 0;
do {
// Changes to current sources are made in rules through assertions.
this._checkJsFile(file, errors);
// If assertions weren't used but rule has "fix" method,
// which we could use.
this._fixJsFile(file, errors);
var hasFixes = errors.getErrorList().some(function(err) {
return err.fixed;
});
if (!hasFixes) {
break;
}
file = this._createJsFileInstance(filename, file.render());
errors = new Errors(file, this._verbose);
attempt++;
} while (attempt < MAX_FIX_ATTEMPTS);
return {output: file.getSource(), errors: errors};
}
},
/**
* Returns `true` if max erros limit is enabled.
*
* @returns {Boolean}
*/
_maxErrorsEnabled: function() {
return this._maxErrors !== null;
},
/**
* Returns `true` if error count exceeded `maxErrors` option value.
*
* @returns {Boolean}
*/
maxErrorsExceeded: function() {
return this._maxErrorsExceeded;
},
/**
* Returns new configuration instance.
*
* @protected
* @returns {Configuration}
*/
_createConfiguration: function() {
return new Configuration();
},
/**
* Returns current configuration instance.
*
* @returns {Configuration}
*/
getConfiguration: function() {
return this._configuration;
},
/**
* Returns the current esprima parser
*
* @return {Esprima}
*/
getEsprima: function() {
return this._esprima || this._configuration.getCustomEsprima();
}
};
module.exports = StringChecker;