Skip to content

Commit

Permalink
fix: types (#66)
Browse files Browse the repository at this point in the history
* fix: types

* fix: remove `allowSyntheticDefaultImports`

* fix: types

* fix: types
  • Loading branch information
ricardogobbosouza authored Feb 1, 2021
1 parent cef4f74 commit 4daddf5
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 50 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"checkJs": true,
"strict": true,
"types": ["node"],
"esModuleInterop": true,
"resolveJsonModule": true
},
"include": ["./src/**/*"]
Expand Down

0 comments on commit 4daddf5

Please sign in to comment.