Skip to content

Commit

Permalink
Chore: make executeOnFile a pure function in CLIEngine (#9262)
Browse files Browse the repository at this point in the history
This will make it easier to accumulate results from multiple workers, which needs to happen for #3565.
  • Loading branch information
not-an-aardvark committed Sep 8, 2017
1 parent 5e0e579 commit b383d81
Showing 1 changed file with 32 additions and 64 deletions.
96 changes: 32 additions & 64 deletions lib/cli-engine.js
Expand Up @@ -493,8 +493,7 @@ class CLIEngine {
* @returns {Object} The results for all files that were linted.
*/
executeOnFiles(patterns) {
const results = [],
options = this.options,
const options = this.options,
fileCache = this._fileCache,
configHelper = this.config;
let prevConfig; // the previous configuration used
Expand Down Expand Up @@ -533,21 +532,11 @@ class CLIEngine {
return prevConfig.hash;
}

/**
* Executes the linter on a file defined by the `filename`. Skips
* unsupported file extensions and any files that are already linted.
* @param {string} filename The resolved filename of the file to be linted
* @param {boolean} warnIgnored always warn when a file is ignored
* @param {Linter} linter Linter context
* @returns {void}
*/
function executeOnFile(filename, warnIgnored, linter) {
let hashOfConfig,
descriptor;

if (warnIgnored) {
results.push(createIgnoreResult(filename, options.cwd));
return;
const startTime = Date.now();
const fileList = globUtil.listFilesToProcess(this.resolveFileGlobPatterns(patterns), options);
const results = fileList.map(fileInfo => {
if (fileInfo.ignored) {
return createIgnoreResult(fileInfo.filename, options.cwd);
}

if (options.cache) {
Expand All @@ -557,79 +546,58 @@ class CLIEngine {
* with the metadata and the flag that determines if
* the file has changed
*/
descriptor = fileCache.getFileDescriptor(filename);
const meta = descriptor.meta || {};

hashOfConfig = hashOfConfigFor(filename);

const changed = descriptor.changed || meta.hashOfConfig !== hashOfConfig;
const descriptor = fileCache.getFileDescriptor(fileInfo.filename);
const hashOfConfig = hashOfConfigFor(fileInfo.filename);
const changed = descriptor.changed || descriptor.meta.hashOfConfig !== hashOfConfig;

if (!changed) {
debug(`Skipping file since hasn't changed: ${filename}`);
debug(`Skipping file since hasn't changed: ${fileInfo.filename}`);

/*
* Add the the cached results (always will be 0 error and
* 0 warnings). We should not cache results for files that
* failed, in order to guarantee that next execution will
* process those files as well.
*/
results.push(descriptor.meta.results);

// move to the next file
return;
return descriptor.meta.results;
}
}

debug(`Processing ${filename}`);

const res = processFile(filename, configHelper, options, linter);
debug(`Processing ${fileInfo.filename}`);

if (options.cache) {
return processFile(fileInfo.filename, configHelper, options, this.linter);
});

/*
* if a file contains errors or warnings we don't want to
* store the file in the cache so we can guarantee that
* next execution will also operate on this file
*/
if (res.errorCount > 0 || res.warningCount > 0) {
debug(`File has problems, skipping it: ${filename}`);
if (options.cache) {
results.forEach(result => {
if (result.messages.length) {

// remove the entry from the cache
fileCache.removeEntry(filename);
/*
* if a file contains errors or warnings we don't want to
* store the file in the cache so we can guarantee that
* next execution will also operate on this file
*/
fileCache.removeEntry(result.filePath);
} else {

/*
* since the file passed we store the result here
* TODO: check this as we might not need to store the
* successful runs as it will always should be 0 errors and
* 0 warnings.
* TODO: it might not be necessary to store the results list in the cache,
* since it should always be 0 errors/warnings
*/
descriptor.meta.hashOfConfig = hashOfConfig;
descriptor.meta.results = res;
}
}
const descriptor = fileCache.getFileDescriptor(result.filePath);

results.push(res);
}

const startTime = Date.now();


patterns = this.resolveFileGlobPatterns(patterns);
const fileList = globUtil.listFilesToProcess(patterns, options);

fileList.forEach(fileInfo => {
executeOnFile(fileInfo.filename, fileInfo.ignored, this.linter);
});

const stats = calculateStatsPerRun(results);

if (options.cache) {
descriptor.meta.hashOfConfig = hashOfConfigFor(result.filePath);
descriptor.meta.results = result;
}
});

// persist the cache to disk
fileCache.reconcile();
}

const stats = calculateStatsPerRun(results);

debug(`Linting complete in: ${Date.now() - startTime}ms`);

return {
Expand Down

0 comments on commit b383d81

Please sign in to comment.