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 Sep 12, 2017
1 parent 61f1093 commit 120beda
Show file tree
Hide file tree
Showing 2 changed files with 47 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 @@ -143,10 +143,8 @@ function calculateStatsPerRun(results) {
*/
function processText(text, configHelper, filename, fix, allowInlineConfig, linter) {
let filePath,
messages,
fileExtension,
processor,
fixedResult;
processor;

if (filename) {
filePath = path.resolve(filename);
Expand All @@ -170,51 +168,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
38 changes: 36 additions & 2 deletions lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
const EventEmitter = require("events").EventEmitter,
eslintScope = require("eslint-scope"),
levn = require("levn"),
lodash = require("lodash"),
blankScriptAST = require("../conf/blank-script.json"),
defaultConfig = require("../conf/default-config-options.js"),
CodePathAnalyzer = require("./code-path-analysis/code-path-analyzer"),
Expand Down Expand Up @@ -721,7 +722,7 @@ module.exports = class Linter {
*/

/**
* 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 @@ -731,7 +732,7 @@ module.exports = class Linter {
* 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) {
_verifyWithoutProcessors(textOrSourceCode, config, filenameOrOptions) {
let text,
parserServices,
allowInlineConfig,
Expand Down Expand Up @@ -965,6 +966,37 @@ module.exports = class Linter {
});
}

/**
* 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) {
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)
),
filename
);
}

/**
* Gets the SourceCode object representing the parsed source.
* @returns {SourceCode} The SourceCode object.
Expand Down Expand Up @@ -1012,6 +1044,8 @@ module.exports = class Linter {
* @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 120beda

Please sign in to comment.