Skip to content

Commit

Permalink
Update: allow autofixing when using processors (fixes #7510)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Aug 30, 2017
1 parent 2d90030 commit b070907
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 40 deletions.
49 changes: 11 additions & 38 deletions lib/cli-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ function processText(text, configHelper, filename, fix, allowInlineConfig, linte
linter.reset();

let filePath,
messages,
fileExtension,
processor,
fixedResult;
processor;

if (filename) {
filePath = path.resolve(filename);
Expand All @@ -174,51 +172,26 @@ function processText(text, configHelper, filename, fix, allowInlineConfig, linte
}
}

if (processor) {
debug("Using processor");
const parsedBlocks = processor.preprocess(text, filename);
const unprocessedMessages = [];

parsedBlocks.forEach(block => {
unprocessedMessages.push(linter.verify(block, config, {
filename,
allowInlineConfig
}));
});

// TODO(nzakas): Figure out how fixes might work for processors

messages = processor.postprocess(unprocessedMessages, filename);

} else {

if (fix) {
fixedResult = linter.verifyAndFix(text, config, {
filename,
allowInlineConfig,
fix
});
messages = fixedResult.messages;
} else {
messages = linter.verify(text, config, {
filename,
allowInlineConfig
});
}
}
const fixedResult = linter.verifyAndFix(text, config, {
filename,
allowInlineConfig,
fix: typeof fix !== "undefined" && fix,
preprocess: processor && processor.preprocess,
postprocess: processor && processor.postprocess
});

const stats = calculateStatsPerFile(messages);
const stats = calculateStatsPerFile(fixedResult.messages);

const result = {
filePath: filename,
messages,
messages: fixedResult.messages,
errorCount: stats.errorCount,
warningCount: stats.warningCount,
fixableErrorCount: stats.fixableErrorCount,
fixableWarningCount: stats.fixableWarningCount
};

if (fixedResult && fixedResult.fixed) {
if (fixedResult.fixed) {
result.output = fixedResult.output;
}

Expand Down
37 changes: 35 additions & 2 deletions lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ class Linter extends EventEmitter {
*/

/**
* Verifies the text against the rules specified by the second argument.
* Same as linter.verify, except without support for processors.
* @param {string|SourceCode} textOrSourceCode The text to parse or a SourceCode object.
* @param {ESLintConfig} config An ESLintConfig instance to configure everything.
* @param {(string|Object)} [filenameOrOptions] The optional filename of the file being checked.
Expand All @@ -791,7 +791,7 @@ class Linter extends EventEmitter {
* Useful if you want to validate JS without comments overriding rules.
* @returns {Object[]} The results as an array of messages or null if no messages.
*/
verify(textOrSourceCode, config, filenameOrOptions, saveState) {
_verifyWithoutProcessors(textOrSourceCode, config, filenameOrOptions, saveState) {
const text = (typeof textOrSourceCode === "string") ? textOrSourceCode : null;
let ast,
parseResult,
Expand Down Expand Up @@ -1007,6 +1007,37 @@ class Linter extends EventEmitter {
return this.messages;
}

/**
* Verifies the text against the rules specified by the second argument.
* @param {string|SourceCode} textOrSourceCode The text to parse or a SourceCode object.
* @param {ESLintConfig} config An ESLintConfig instance to configure everything.
* @param {(string|Object)} [filenameOrOptions] The optional filename of the file being checked.
* If this is not set, the filename will default to '<input>' in the rule context. If
* an object, then it has "filename", "saveState", and "allowInlineConfig" properties.
* @param {boolean} [saveState] Indicates if the state from the last run should be saved.
* Mostly useful for testing purposes.
* @param {boolean} [filenameOrOptions.allowInlineConfig] Allow/disallow inline comments' ability to change config once it is set. Defaults to true if not supplied.
* Useful if you want to validate JS without comments overriding rules.
* @param {function(string): string[]} [filenameOrOptions.preprocess] preprocessor for source text. If provided, this should
* this should accept a string of source text, and return an array of code blocks to lint.
* @param {function(Array<Object[]>): Object[]} [filenameOrOptions.postprocess] postprocessor for report messages. If provided,
* this should accept an array of the message lists for each code block returned from the preprocessor,
* apply a mapping to the messages as appropriate, and return a one-dimensional array of messages
* @returns {Object[]} The results as an array of messages or null if no messages.
*/
verify(textOrSourceCode, config, filenameOrOptions, saveState) {
const preprocess = filenameOrOptions && filenameOrOptions.preprocess || (rawText => [rawText]);
const postprocess = filenameOrOptions && filenameOrOptions.postprocess || lodash.flatten;
const filename = typeof filenameOrOptions === "object" ? filenameOrOptions.filename : filenameOrOptions;

return postprocess(
preprocess(textOrSourceCode, filename).map(
textBlock => this._verifyWithoutProcessors(textBlock, config, filenameOrOptions, saveState)
),
filename
);
}

/**
* Gets the SourceCode object representing the parsed source.
* @returns {SourceCode} The SourceCode object.
Expand Down Expand Up @@ -1182,6 +1213,8 @@ class Linter extends EventEmitter {
* @param {boolean} options.allowInlineConfig Flag indicating if inline comments
* should be allowed.
* @param {boolean|Function} options.fix Determines whether fixes should be applied
* @param {Function} options.preprocess preprocessor
* @param {Function} options.postprocess postprocessor
* @returns {Object} The result of the fix operation as returned from the
* SourceCodeFixer.
*/
Expand Down

0 comments on commit b070907

Please sign in to comment.