Skip to content

Commit

Permalink
refactor: remove dup of code from linter.js (#202)
Browse files Browse the repository at this point in the history
* refactor: remove dup of code from linter.js

* refactor: removing unnecessary function

---------

Co-authored-by: Ricardo Gobbo de Souza <ricardogobbosouza@yahoo.com.br>
  • Loading branch information
Gabriel-Dias-Oliveira and ricardogobbosouza committed Feb 2, 2023
1 parent 204e3b8 commit ca1ae72
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ or
pnpm add -D eslint-webpack-plugin
```

> **Note**:
> **Note**:
>
> You also need to install `eslint >= 8` from npm, if you haven't already:
Expand Down
40 changes: 14 additions & 26 deletions src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { dirname, isAbsolute, join } = require('path');

const ESLintError = require('./ESLintError');
const { getESLint } = require('./getESLint');
const { arrify } = require('./utils');

/** @typedef {import('eslint').ESLint} ESLint */
/** @typedef {import('eslint').ESLint.Formatter} Formatter */
Expand Down Expand Up @@ -59,7 +60,7 @@ function linter(key, options, compilation) {
* @param {string | string[]} files
*/
function lint(files) {
for (const file of asList(files)) {
for (const file of arrify(files)) {
delete crossRunResultStorage[file];
}
rawResults.push(
Expand Down Expand Up @@ -192,10 +193,7 @@ function parseResults(options, results) {
);

if (messages.length > 0) {
errors.push({
...file,
messages,
});
errors.push({ ...file, messages });
}
}

Expand All @@ -205,10 +203,7 @@ function parseResults(options, results) {
);

if (messages.length > 0) {
warnings.push({
...file,
messages,
});
warnings.push({ ...file, messages });
}
}
});
Expand Down Expand Up @@ -267,20 +262,21 @@ async function removeIgnoredWarnings(eslint, results) {
// fatal is false for ignored file warnings.
// ruleId is unset for internal ESLint errors.
// line is unset for warnings not involving file contents.
const { messages, warningCount, errorCount, filePath } = result;
const [firstMessage] = messages;
const hasWarning = warningCount === 1 && errorCount === 0;
const ignored =
result.messages.length === 0 ||
(result.warningCount === 1 &&
result.errorCount === 0 &&
!result.messages[0].fatal &&
!result.messages[0].ruleId &&
!result.messages[0].line &&
(await eslint.isPathIgnored(result.filePath)));

messages.length === 0 ||
(hasWarning &&
!firstMessage.fatal &&
!firstMessage.ruleId &&
!firstMessage.line &&
(await eslint.isPathIgnored(filePath)));
return ignored ? false : result;
});

// @ts-ignore
return (await Promise.all(filterPromises)).filter((result) => !!result);
return (await Promise.all(filterPromises)).filter(Boolean);
}

/**
Expand Down Expand Up @@ -308,12 +304,4 @@ function getResultStorage({ compiler }) {
return storage;
}

/**
* @param {string | string[]} x
*/
function asList(x) {
/* istanbul ignore next */
return Array.isArray(x) ? x : [x];
}

module.exports = linter;

0 comments on commit ca1ae72

Please sign in to comment.