Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: types #66

Merged
merged 5 commits into from Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -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`

Expand Down
5 changes: 2 additions & 3 deletions 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';
2 changes: 1 addition & 1 deletion declarations/cjs.d.ts
@@ -1,2 +1,2 @@
declare const _exports: typeof import('.').ESLintWebpackPlugin;
declare const _exports: typeof import('.').default;
export = _exports;
10 changes: 5 additions & 5 deletions 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
*/
Expand All @@ -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;
4 changes: 2 additions & 2 deletions src/getESLint.js
@@ -1,4 +1,4 @@
import os from 'os';
import { cpus } from 'os';

import JestWorker from 'jest-worker';

Expand Down Expand Up @@ -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;
Expand Down
11 changes: 4 additions & 7 deletions 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';
Expand All @@ -13,7 +14,7 @@ import { parseFiles, parseFoldersToGlobs } from './utils';
const ESLINT_PLUGIN = 'ESLintWebpackPlugin';
let counter = 0;

export class ESLintWebpackPlugin {
class ESLintWebpackPlugin {
/**
* @param {Options} options
*/
Expand Down Expand Up @@ -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);
}
Expand Down
32 changes: 12 additions & 20 deletions src/linter.js
Expand Up @@ -112,24 +112,26 @@ 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<void>} */ (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 */
if (err2) bail(err2);
else finish();
});
});
});
}));

if (!outputReport || !outputReport.filePath) {
return;
Expand Down Expand Up @@ -185,14 +187,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({
Expand All @@ -203,14 +200,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({
Expand Down
4 changes: 3 additions & 1 deletion 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 */
Expand Down Expand Up @@ -47,6 +48,8 @@ import schema from './options.json';
export function getOptions(pluginOptions) {
const options = {
extensions: 'js',
emitError: true,
emitWarning: true,
...pluginOptions,
...(pluginOptions.quiet ? { emitError: true, emitWarning: false } : {}),
};
Expand Down Expand Up @@ -77,6 +80,5 @@ export function getESLintOptions(loaderOptions) {
delete eslintOptions[option];
}

// @ts-ignore
return eslintOptions;
}
4 changes: 2 additions & 2 deletions src/options.json
Expand Up @@ -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": {
Expand Down
9 changes: 5 additions & 4 deletions src/utils.js
@@ -1,5 +1,6 @@
import { statSync } from 'fs';

// @ts-ignore
import arrify from 'arrify';

const UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()*?[\]{|}]|^!|[!+@](?=\())/g;
Expand All @@ -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'
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Expand Up @@ -6,7 +6,6 @@
"checkJs": true,
"strict": true,
"types": ["node"],
"esModuleInterop": true,
"resolveJsonModule": true
},
"include": ["./src/**/*"]
Expand Down