Skip to content

Commit

Permalink
refactor: reduce the readability complexity (#201)
Browse files Browse the repository at this point in the history
* Refactoring index.js to reduce the readability complexity

* refactor: index.js to reduce the readability complexity

---------

Co-authored-by: Alexander Akait <4567934+alexander-akait@users.noreply.github.com>
Co-authored-by: Ricardo Gobbo de Souza <ricardogobbosouza@yahoo.com.br>
  • Loading branch information
3 people committed Feb 2, 2023
1 parent ca1ae72 commit ebce9be
Showing 1 changed file with 59 additions and 60 deletions.
119 changes: 59 additions & 60 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const { arrify, parseFiles, parseFoldersToGlobs } = require('./utils');
/** @typedef {import('./options').Options} Options */

const ESLINT_PLUGIN = 'ESLintWebpackPlugin';
let counter = 0;
const DEFAULT_FOLDER_TO_EXCLUDE = '**/node_modules/**';

let compilerId = 0;

class ESLintWebpackPlugin {
/**
Expand All @@ -29,26 +31,30 @@ class ESLintWebpackPlugin {
apply(compiler) {
// Generate key for each compilation,
// this differentiates one from the other when being cached.
this.key = compiler.name || `${this.key}_${(counter += 1)}`;
this.key = compiler.name || `${this.key}_${(compilerId += 1)}`;

const excludedFiles = parseFiles(
this.options.exclude || [],
this.getContext(compiler)
);
const resourceQueries = arrify(this.options.resourceQueryExclude || []);
const excludedResourceQueries = resourceQueries.map((item) =>
item instanceof RegExp ? item : new RegExp(item)
);

const options = {
...this.options,
exclude: parseFiles(
this.options.exclude || [],
this.getContext(compiler)
),
exclude: excludedFiles,
resourceQueryExclude: excludedResourceQueries,
extensions: arrify(this.options.extensions),
resourceQueryExclude: arrify(this.options.resourceQueryExclude || []).map(
(item) => (item instanceof RegExp ? item : new RegExp(item))
),
files: parseFiles(this.options.files || '', this.getContext(compiler)),
};

const foldersToExclude = this.options.exclude
? options.exclude
: DEFAULT_FOLDER_TO_EXCLUDE;
const exclude = parseFoldersToGlobs(foldersToExclude);
const wanted = parseFoldersToGlobs(options.files, options.extensions);
const exclude = parseFoldersToGlobs(
this.options.exclude ? options.exclude : '**/node_modules/**',
[]
);

// If `lintDirtyModulesOnly` is disabled,
// execute the linter on the build
Expand All @@ -58,15 +64,15 @@ class ESLintWebpackPlugin {
);
}

let isFirstRun = this.options.lintDirtyModulesOnly;
let hasCompilerRunByDirtyModule = this.options.lintDirtyModulesOnly;

compiler.hooks.watchRun.tapPromise(this.key, (c) => {
if (isFirstRun) {
isFirstRun = false;
if (!hasCompilerRunByDirtyModule)
return this.run(c, options, wanted, exclude);

return Promise.resolve();
}
hasCompilerRunByDirtyModule = false;

return this.run(c, options, wanted, exclude);
return Promise.resolve();
});
}

Expand All @@ -77,13 +83,12 @@ class ESLintWebpackPlugin {
* @param {string[]} exclude
*/
async run(compiler, options, wanted, exclude) {
// Do not re-hook
if (
// @ts-ignore
compiler.hooks.compilation.taps.find(({ name }) => name === this.key)
) {
return;
}
// @ts-ignore
const isCompilerHooked = compiler.hooks.compilation.taps.find(
({ name }) => name === this.key
);

if (isCompilerHooked) return;

compiler.hooks.compilation.tap(this.key, (compilation) => {
/** @type {import('./linter').Linter} */
Expand All @@ -106,30 +111,27 @@ class ESLintWebpackPlugin {
// @ts-ignore
// Add the file to be linted
compilation.hooks.succeedModule.tap(this.key, ({ resource }) => {
if (resource) {
const [file, query] = resource.split('?');

if (
file &&
!files.includes(file) &&
isMatch(file, wanted, { dot: true }) &&
!isMatch(file, exclude, { dot: true }) &&
options.resourceQueryExclude.every((reg) => !reg.test(query))
) {
files.push(file);

if (threads > 1) {
lint(file);
}
}
if (!resource) return;

const [file, query] = resource.split('?');
const isFileNotListed = file && !files.includes(file);
const isFileWanted =
isMatch(file, wanted, { dot: true }) &&
!isMatch(file, exclude, { dot: true });
const isQueryNotExclude = options.resourceQueryExclude.every(
(reg) => !reg.test(query)
);

if (isFileNotListed && isFileWanted && isQueryNotExclude) {
files.push(file);

if (threads > 1) lint(file);
}
});

// Lint all files added
compilation.hooks.finishModules.tap(this.key, () => {
if (files.length > 0 && threads <= 1) {
lint(files);
}
if (files.length > 0 && threads <= 1) lint(files);
});

// await and interpret results
Expand All @@ -141,22 +143,20 @@ class ESLintWebpackPlugin {
if (warnings && !options.failOnWarning) {
// @ts-ignore
compilation.warnings.push(warnings);
} else if (warnings && options.failOnWarning) {
} else if (warnings) {
// @ts-ignore
compilation.errors.push(warnings);
}

if (errors && options.failOnError) {
// @ts-ignore
compilation.errors.push(errors);
} else if (errors && !options.failOnError) {
if (errors && !options.failOnError) {
// @ts-ignore
compilation.warnings.push(errors);
} else if (errors) {
// @ts-ignore
compilation.errors.push(errors);
}

if (generateReportAsset) {
await generateReportAsset(compilation);
}
if (generateReportAsset) await generateReportAsset(compilation);
}
});
}
Expand All @@ -167,15 +167,14 @@ class ESLintWebpackPlugin {
* @returns {string}
*/
getContext(compiler) {
if (!this.options.context) {
return String(compiler.options.context);
}
const compilerContext = String(compiler.options.context);
const optionContext = this.options.context;

if (!isAbsolute(this.options.context)) {
return join(String(compiler.options.context), this.options.context);
}
if (!optionContext) return compilerContext;

if (isAbsolute(optionContext)) return optionContext;

return this.options.context;
return join(compilerContext, optionContext);
}
}

Expand Down

0 comments on commit ebce9be

Please sign in to comment.