Skip to content

Commit

Permalink
fix(eslint): fix async support for formatter option and TypeScript …
Browse files Browse the repository at this point in the history
…types (#1389)

* fix(eslint): make `formatter` support asynchronous function

* fix(eslint): Fix the deprecated `CLIEngine` error type

* fix(eslint): additional test cases for asynchronous formatter

* chore(eslint): add the correct function type

* test(eslint): adding TypeScript test cases

* docs(eslint): update formatter type
  • Loading branch information
surmon-china authored Jan 13, 2023
1 parent 6195a50 commit f39e8c9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/eslint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ If true, will auto fix source code.

### formatter

Type: `Function | String`<br>
Type: `Function<String> | Function<Promise<String>> | String`<br>
Default: `stylish`

Custom error formatter, the name of a built-in formatter, or the path to a custom formatter.
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"pretest": "pnpm build",
"release": "pnpm --workspace-root plugin:release --pkg $npm_package_name",
"test": "ava",
"test:ts": "tsc --noEmit"
"test:ts": "tsc types/index.d.ts test/types.ts --noEmit"
},
"files": [
"dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function eslint(options = {} as RollupEslintOptions): Plugin {
typeof formatter === 'string'
? await eslintInstance.loadFormatter(formatter)
: { format: formatter };
const output = eslintFormatter.format(results);
const output = await eslintFormatter.format(results);

if (output) {
// eslint-disable-next-line no-console
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint/test/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ test('should not fail with found formatter', async (t) => {
t.pass();
});

test('should not fail with asynchronous formatter function', async (t) => {
await rollup({
input: './test/fixtures/use-strict.js',
plugins: [
eslint({
formatter: async () => 'json'
})
]
});

t.pass();
});

test('should fix source code', async (t) => {
fs.writeFileSync(
'./test/fixtures/fixable-clone.js',
Expand Down
32 changes: 32 additions & 0 deletions packages/eslint/test/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { RollupOptions } from 'rollup';

import eslintPlugin from '../types';

const rollupConfig: RollupOptions = {
input: 'main.js',
output: {
file: 'bundle.js',
format: 'iife'
},
plugins: [
eslintPlugin(),
eslintPlugin({}),
eslintPlugin({
formatter: () => 'json'
}),
eslintPlugin({
formatter: async () => 'json'
}),
eslintPlugin({
fix: false,
throwOnWarning: true,
throwOnError: true,
include: 'node_modules/**',
exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/],
extensions: ['.js', '.coffee'],
formatter: 'json'
})
]
};

export default rollupConfig;
4 changes: 2 additions & 2 deletions packages/eslint/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Plugin } from 'rollup';
import type { CLIEngine, ESLint } from 'eslint';
import type { ESLint } from 'eslint';
import type { CreateFilter } from '@rollup/pluginutils';

export interface RollupEslintOptions extends ESLint.Options {
Expand Down Expand Up @@ -37,7 +37,7 @@ export interface RollupEslintOptions extends ESLint.Options {
* Custom error formatter or the name of a built-in formatter.
* @default stylish
*/
formatter?: CLIEngine.Formatter | string;
formatter?: Awaited<ReturnType<ESLint['loadFormatter']>>['format'] | string;
}

/**
Expand Down

0 comments on commit f39e8c9

Please sign in to comment.