diff --git a/README.md b/README.md index 1b3db50..b25634a 100644 --- a/README.md +++ b/README.md @@ -174,16 +174,16 @@ You can still force this behavior by using `emitError` **or** `emitWarning` opti #### `emitError` - Type: `Boolean` -- Default: `false` +- Default: `true` -Will always return errors, if set to `true`. +The errors found will always be emitted, to disable set to `false`. #### `emitWarning` - Type: `Boolean` -- Default: `false` +- Default: `true` -Will always return warnings, if set to `true`. +The warnings found will always be emitted, to disable set to `false`. #### `failOnError` diff --git a/declarations/ESLintError.d.ts b/declarations/ESLintError.d.ts index 2d533bf..a38d07e 100644 --- a/declarations/ESLintError.d.ts +++ b/declarations/ESLintError.d.ts @@ -1,8 +1,7 @@ -export default class ESLintError { +export default class ESLintError extends WebpackError { /** * @param {string=} messages */ constructor(messages?: string | undefined); - name: string; - stack: string; } +import { WebpackError } from 'webpack'; diff --git a/declarations/cjs.d.ts b/declarations/cjs.d.ts index 428d31f..e9ca127 100644 --- a/declarations/cjs.d.ts +++ b/declarations/cjs.d.ts @@ -1,2 +1,2 @@ -declare const _exports: typeof import('.').ESLintWebpackPlugin; +declare const _exports: typeof import('.').default; export = _exports; diff --git a/declarations/index.d.ts b/declarations/index.d.ts index 7ae2022..b7bf9b6 100644 --- a/declarations/index.d.ts +++ b/declarations/index.d.ts @@ -1,4 +1,8 @@ -export class ESLintWebpackPlugin { +export default ESLintWebpackPlugin; +export type Compiler = import('webpack').Compiler; +export type Options = import('./options').PluginOptions & + import('eslint').ESLint.Options; +declare class ESLintWebpackPlugin { /** * @param {Options} options */ @@ -21,7 +25,3 @@ export class ESLintWebpackPlugin { */ getContext(compiler: Compiler): string; } -export default ESLintWebpackPlugin; -export type Compiler = import('webpack').Compiler; -export type Options = import('./options').PluginOptions & - import('eslint').ESLint.Options; diff --git a/src/getESLint.js b/src/getESLint.js index fe27f1b..92a6b66 100644 --- a/src/getESLint.js +++ b/src/getESLint.js @@ -1,4 +1,4 @@ -import os from 'os'; +import { cpus } from 'os'; import JestWorker from 'jest-worker'; @@ -96,7 +96,7 @@ export default function getESLint(key, { threads, ...options }) { const max = typeof threads !== 'number' ? threads - ? os.cpus().length - 1 + ? cpus().length - 1 : 1 : /* istanbul ignore next */ threads; diff --git a/src/index.js b/src/index.js index e68bbcc..4a8c232 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,8 @@ import { isAbsolute, join } from 'path'; +// @ts-ignore import arrify from 'arrify'; -import micromatch from 'micromatch'; +import { isMatch } from 'micromatch'; import { getOptions } from './options'; import linter from './linter'; @@ -13,7 +14,7 @@ import { parseFiles, parseFoldersToGlobs } from './utils'; const ESLINT_PLUGIN = 'ESLintWebpackPlugin'; let counter = 0; -export class ESLintWebpackPlugin { +class ESLintWebpackPlugin { /** * @param {Options} options */ @@ -99,11 +100,7 @@ export class ESLintWebpackPlugin { if (module.resource) { const [file] = module.resource.split('?'); - if ( - file && - micromatch.isMatch(file, wanted) && - !micromatch.isMatch(file, exclude) - ) { + if (file && isMatch(file, wanted) && !isMatch(file, exclude)) { // Queue file for linting. lint(file); } diff --git a/src/linter.js b/src/linter.js index f8d7967..efb5884 100644 --- a/src/linter.js +++ b/src/linter.js @@ -106,16 +106,18 @@ export default function linter(key, options, compilation) { */ async function generateReportAsset({ compiler }) { const { outputReport } = options; - // @ts-ignore + /** + * @param {string} name + * @param {string | Buffer} content + */ const save = (name, content) => - new Promise((finish, bail) => { + /** @type {Promise} */ (new Promise((finish, bail) => { const { mkdir, writeFile } = compiler.outputFileSystem; // ensure directory exists // @ts-ignore - the types for `outputFileSystem` are missing the 3 arg overload mkdir(dirname(name), { recursive: true }, (err) => { /* istanbul ignore if */ if (err) bail(err); - // @ts-ignore else writeFile(name, content, (err2) => { /* istanbul ignore if */ @@ -123,7 +125,7 @@ export default function linter(key, options, compilation) { else finish(); }); }); - }); + })); if (!outputReport || !outputReport.filePath) { return; @@ -179,14 +181,9 @@ function parseResults(options, results) { results.forEach((file) => { if (fileHasErrors(file)) { - const messages = file.messages.filter((message) => { - if (options.emitError === undefined) { - return true; - } else if (options.emitError) { - return message.severity === 2; - } - return false; - }); + const messages = file.messages.filter( + (message) => options.emitError && message.severity === 2 + ); if (messages.length > 0) { errors.push({ @@ -197,14 +194,9 @@ function parseResults(options, results) { } if (fileHasWarnings(file)) { - const messages = file.messages.filter((message) => { - if (options.emitWarning === undefined) { - return true; - } else if (options.emitWarning) { - return message.severity === 1; - } - return false; - }); + const messages = file.messages.filter( + (message) => options.emitWarning && message.severity === 1 + ); if (messages.length > 0) { warnings.push({ diff --git a/src/options.js b/src/options.js index b235ad6..2bc449e 100644 --- a/src/options.js +++ b/src/options.js @@ -1,5 +1,6 @@ import { validate } from 'schema-utils'; +// @ts-ignore import schema from './options.json'; /** @typedef {import("eslint").ESLint.Options} ESLintOptions */ @@ -47,6 +48,8 @@ import schema from './options.json'; export function getOptions(pluginOptions) { const options = { extensions: 'js', + emitError: true, + emitWarning: true, failOnError: true, ...pluginOptions, ...(pluginOptions.quiet ? { emitError: true, emitWarning: false } : {}), @@ -78,6 +81,5 @@ export function getESLintOptions(loaderOptions) { delete eslintOptions[option]; } - // @ts-ignore return eslintOptions; } diff --git a/src/options.json b/src/options.json index 68f77fc..b006321 100644 --- a/src/options.json +++ b/src/options.json @@ -7,11 +7,11 @@ "type": "string" }, "emitError": { - "description": "Will always return errors, if set to `true`.", + "description": "The errors found will always be emitted, to disable set to `false`.", "type": "boolean" }, "emitWarning": { - "description": "Will always return warnings, if set to `true`.", + "description": "The warnings found will always be emitted, to disable set to `false`.", "type": "boolean" }, "eslintPath": { diff --git a/src/utils.js b/src/utils.js index 03a35ae..2fe85d2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,6 @@ import { statSync } from 'fs'; +// @ts-ignore import arrify from 'arrify'; const UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()*?[\]{|}]|^!|[!+@](?=\())/g; @@ -11,7 +12,7 @@ const UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()*?[\]{|}]|^!|[!+@](?=\())/g; */ export function parseFiles(files, context) { return arrify(files).map( - (file) => + (/** @type {string} */ file) => `${replaceBackslashes(context).replace( UNESCAPED_GLOB_SYMBOLS_RE, '\\$2' @@ -36,12 +37,12 @@ export function parseFoldersToGlobs(patterns, extensions = []) { const extensionsList = arrify(extensions); const [prefix, postfix] = extensionsList.length > 1 ? ['{', '}'] : ['', '']; const extensionsGlob = extensionsList - .map((extension) => extension.replace(/^\./u, '')) + .map((/** @type {string} */ extension) => extension.replace(/^\./u, '')) .join(','); return arrify(patterns) - .map((pattern) => replaceBackslashes(pattern)) - .map((pattern) => { + .map((/** @type {string} */ pattern) => replaceBackslashes(pattern)) + .map((/** @type {string} */ pattern) => { try { // The patterns are absolute because they are prepended with the context. const stats = statSync(pattern); diff --git a/tsconfig.json b/tsconfig.json index 4c6551b..30a40b7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,6 @@ "checkJs": true, "strict": true, "types": ["node"], - "esModuleInterop": true, "resolveJsonModule": true }, "include": ["./src/**/*"]