Skip to content

Commit e92626a

Browse files
author
Robert Jackson
committed
Introduce rule visitor based fixing support.
1 parent a310a44 commit e92626a

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

lib/linter.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ const MAX_AUTOFIX_PASSES = 10;
99
const WARNING_SEVERITY = 1;
1010
const ERROR_SEVERITY = 2;
1111

12-
/**
13-
* @param {Object} options
14-
* @param {string} options.source - The source code to fix.
15-
* @param {Object[]} messages - The lint messages.
16-
* @returns {string} output - The fixed source.
17-
*/
18-
function applyFixes(options /* messages */) {
19-
return options.source;
20-
}
21-
2212
function buildErrorMessage(filePath, moduleId, error) {
2313
let message = {
2414
fatal: true,
@@ -225,7 +215,7 @@ class Linter {
225215
shouldVerify = false;
226216

227217
if (iterations < MAX_AUTOFIX_PASSES) {
228-
const output = applyFixes(options, messages);
218+
const output = this._applyFixes(options, messages);
229219

230220
// let's lint one more time if source was modified
231221
shouldVerify = output !== currentSource;
@@ -241,6 +231,43 @@ class Linter {
241231
};
242232
}
243233

234+
/**
235+
* @param {Object} options
236+
* @param {string} options.source - The source code to verify.
237+
* @param {string} options.filePath
238+
* @param {string} options.moduleId
239+
* @param {Object[]} results - The lint messages.
240+
*
241+
* @returns {string} output - The fixed source.
242+
*/
243+
_applyFixes(options, results) {
244+
let fixableIssues = results.filter(r => r.isFixable);
245+
246+
// nothing to do, bail out
247+
if (fixableIssues.length === 0) {
248+
return options.source;
249+
}
250+
251+
let currentSource = options.source;
252+
253+
let pending = this.statusForModule('pending', options.moduleId);
254+
let ruleNames = new Set(fixableIssues.map(r => r.rule));
255+
for (let ruleName of ruleNames) {
256+
let rule = this._buildRule(ruleName, {
257+
pending,
258+
shouldFix: true,
259+
filePath: options.filePath,
260+
rawSource: currentSource,
261+
fileConfig: getConfigForFile(this.config, options.filePath),
262+
});
263+
264+
let { code } = transform(currentSource, () => rule.getVisitor());
265+
currentSource = code;
266+
}
267+
268+
return currentSource;
269+
}
270+
244271
/**
245272
* The main function for the Linter class. It takes the source code to lint
246273
* and returns the results.

lib/rules/base.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = class BaseRule {
4545

4646
this.severity = options.defaultSeverity;
4747
this.results = [];
48+
this.mode = options.shouldFix ? 'fix' : 'report';
4849

4950
// To support DOM-scoped configuration instructions, we need to maintain
5051
// a stack of our configurations so we can restore the previous one when

0 commit comments

Comments
 (0)