Skip to content

Commit

Permalink
feat(eslint-plugin-result)!: update to eslint v9 flat config
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This plugin now requires eslint v9 or higher. For more information see the [eslint migration guide](https://eslint.org/blog/2024/04/eslint-v9.0.0-released) and [flat config guide](https://eslint.org/blog/2022/08/new-config-system-part-2/). The README of this package has instructions for how to use this package with the new flat config system.
  • Loading branch information
favna committed May 28, 2024
1 parent a21a59e commit ea428f6
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 63 deletions.
48 changes: 35 additions & 13 deletions packages/eslint-plugin-result/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,46 @@ You can use the following command to install this package, or replace `npm insta
npm install @sapphire/eslint-plugin-result
```

It is important to note that this package only exports [ESLint Flat Config][]! This means that you _have_ to use `eslint.config.mjs` to use this package. See the ESLint documentation on flat config for more information.

## Usage

Somewhere in your ESLint configuration:
1. Create a file `eslint.config.mjs` in the root of your project.
2. Add the following content to the file:

```json
{
"extends": ["plugin:@sapphire/result/recommended"]
}
```
```js
import sapphireEslintPluginResult from '@sapphire/eslint-plugin-result/recommended';

Or
/**
* @type {import('@typescript-eslint/utils').TSESLint.FlatConfig.ConfigArray}
*/
const config = [
// Additional config here
sapphireEslintPluginResult
// or here
];

```json
{
"plugins": ["@sapphire/result"],
"rules": {
"@sapphire/result/no-discard-result": "error"
export default config;
```

```js
import sapphireEslintPluginResult from '@sapphire/eslint-plugin-result';

/**
* @type {import('@typescript-eslint/utils').TSESLint.FlatConfig.ConfigArray}
*/
const config = [
// Additional config here
{
plugins: { result: sapphireEslintPluginResult },
rules: {
'@sapphire/result/no-discard-result': 'error'
}
}
}
// or here
];

export default config;
```

## Buy us some doughnuts
Expand All @@ -75,3 +96,4 @@ Thank you to all the people who already contributed to Sapphire!
</a>

[contributing]: https://github.com/sapphiredev/.github/blob/main/.github/CONTRIBUTING.md
[ESLint Flat Config]: https://eslint.org/blog/2022/08/new-config-system-part-2/
31 changes: 24 additions & 7 deletions packages/eslint-plugin-result/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,36 @@
"license": "MIT",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"types": "dist/cjs/index.d.cts",
"exports": {
"import": {
"default": "./dist/esm/index.mjs"
".": {
"import": {
"types": "./dist/esm/index.d.mts",
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
},
"require": {
"default": "./dist/cjs/index.cjs"
"./recommended": {
"import": {
"types": "./dist/esm/configs/recommended.d.mts",
"default": "./dist/esm/configs/recommended.mjs"
},
"require": {
"types": "./dist/cjs/configs/recommended.d.cts",
"default": "./dist/configs/recommended.cjs"
}
}
},
"sideEffects": false,
"homepage": "https://github.com/sapphiredev/utilities/tree/main/packages/eslint-plugin-result",
"scripts": {
"test": "vitest run",
"lint": "eslint src tests --fix -c ../../eslint.config.mjs",
"build": "tsup",
"build": "tsup && yarn build:rename-cjs-index",
"build:rename-cjs-index": "tsx --tsconfig ../../scripts/tsconfig.json ../../scripts/rename-cjs-index.cts",
"prepack": "yarn build",
"bump": "cliff-jumper",
"check-update": "cliff-jumper --dry-run"
Expand All @@ -33,8 +49,8 @@
"dist/"
],
"engines": {
"node": ">=v14.0.0",
"npm": ">=7.0.0"
"node": "^18.18.0 || ^20.9.0 || >=21.1.0",
"npm": ">=10.0.0"
},
"keywords": [
"@sapphire/eslint-plugin-result",
Expand All @@ -57,6 +73,7 @@
"@typescript-eslint/typescript-estree": "^7.10.0",
"@vitest/coverage-v8": "^1.6.0",
"tsup": "^8.0.2",
"tsx": "^4.7.2",
"typedoc-json-parser": "^10.0.0",
"vitest": "^1.6.0"
},
Expand Down
11 changes: 9 additions & 2 deletions packages/eslint-plugin-result/src/configs/recommended.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
export const recommendedConfig = {
plugins: ['@sapphire/eslint-plugin-result'],
import type { TSESLint } from '@typescript-eslint/utils';
import sapphireEslintPluginResult from '../index';

const recommendedConfig: TSESLint.FlatConfig.Config = {
plugins: {
result: sapphireEslintPluginResult
},
rules: {
'@sapphire/result/no-discard-result': 'error'
}
};

export default recommendedConfig;
51 changes: 36 additions & 15 deletions packages/eslint-plugin-result/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
import { recommendedConfig } from './configs/recommended';
import recommendedConfig from './configs/recommended';
import { noDiscardResult } from './rules/no-discard-result';
import type { TSESLint } from '@typescript-eslint/utils';

/**
* ESLint plugin result for @sapphire/result package
* @example
* ```json
* {
* "extends": "plugin:@sapphire/result/recommended"
* }
* file: `eslint.config.mjs`
* ```typescript
* import sapphireEslintPluginResult from '@sapphire/eslint-plugin-result/recommended';
*
* const config = [
* sapphireEslintPluginResult,
* ];
*
* export default config;
* ```
*
* @example
* ```json
* {
* "plugins": ["@sapphire/result"],
* "rules": {
* "@sapphire/result/no-discard-result": "error"
* }
*}
*```
* file: `eslint.config.mjs`
* ```typescript
* import sapphireEslintPluginResult from '@sapphire/eslint-plugin-result';
*
* const config = [
* {
* plugins: { result: sapphireEslintPluginResult },
* rules: { '@sapphire/result/no-discard-result': 'error' }
* },
* ];
*
* export default config;
* ```
*
* Optionally, you can type the config object as
* ```
* import('@typescript-eslint/utils').TSESLint.FlatConfig.ConfigArray
* ```
*/
const eslintPluginResult = {
const eslintPluginResult: TSESLint.FlatConfig.Plugin = {
meta: {
name: '@sapphire/eslint-plugin-result',
version: '[VI]{{inject}}[/VI]'
},
rules: {
'no-discard-result': noDiscardResult
},
Expand All @@ -28,4 +49,4 @@ const eslintPluginResult = {
}
};

module.exports = eslintPluginResult;
export default eslintPluginResult;
6 changes: 5 additions & 1 deletion packages/eslint-plugin-result/src/rules/no-discard-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ export const noDiscardResult: RuleModule<'discardedResult', [], RuleListener> =
meta: {
messages,
type: 'problem',
schema: []
schema: [],
docs: {
description: 'Disallow discarding the result of a function returning a Result.',
url: 'https://github.com/sapphiredev/utilities/blob/main/packages/eslint-plugin-result/README.md'
}
},
defaultOptions: [],
create: (context) => {
Expand Down
2 changes: 0 additions & 2 deletions packages/eslint-plugin-result/src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"compilerOptions": {
"rootDir": "./",
"outDir": "../dist",
"moduleResolution": "Node16",
"verbatimModuleSyntax": false,
"incremental": false
},
"include": ["."]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { fileURLToPath } from 'url';
import { noDiscardResult } from '../../src/rules/no-discard-result';
import { ruleTester } from '../shared';
import { RuleTester } from '@typescript-eslint/rule-tester';

describe.skip('ESLint plugin result', () => {
export const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
tsconfigRootDir: fileURLToPath(new URL('..', import.meta.url)),
project: '../tsconfig.json'
},
parser: '@typescript-eslint/parser'
});

describe('ESLint plugin result', () => {
ruleTester.run('no-discard-result', noDiscardResult, {
valid: [
{
Expand Down Expand Up @@ -35,7 +45,7 @@ describe.skip('ESLint plugin result', () => {
{
code: `import { Result } from '@sapphire/result';
function foo(): Result<string, string> {}
foo();`,
name: 'simple discard',
errors: [
Expand All @@ -48,7 +58,7 @@ describe.skip('ESLint plugin result', () => {
{
code: `import { Result } from '@sapphire/result';
async function foo(): Promise<Result<string, string>> {}
foo();`,
name: 'unawaited async function discarded',
errors: [
Expand All @@ -61,7 +71,7 @@ describe.skip('ESLint plugin result', () => {
{
code: `import { Result } from '@sapphire/result';
async function foo(): Promise<Result<string, string>> {}
await foo();`,
name: 'awaited async function discarded',
errors: [
Expand All @@ -74,7 +84,7 @@ describe.skip('ESLint plugin result', () => {
{
code: `import { Result } from '@sapphire/result';
function foo(): Promise<Result<string, string>> {}
(
foo(),
await foo()
Expand All @@ -94,7 +104,7 @@ describe.skip('ESLint plugin result', () => {
{
code: `import { Result } from '@sapphire/result';
function foo(): Promise<Result<string, string>> {}
null ?? foo();
`,
name: 'potential discard (??)',
Expand Down
10 changes: 0 additions & 10 deletions packages/eslint-plugin-result/tests/shared.ts

This file was deleted.

4 changes: 4 additions & 0 deletions packages/eslint-plugin-result/tests/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as vitest from 'vitest';
import { RuleTester } from '@typescript-eslint/rule-tester';

RuleTester.afterAll = vitest.afterAll;
11 changes: 6 additions & 5 deletions packages/eslint-plugin-result/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { esbuildPluginVersionInjector } from 'esbuild-plugin-version-injector';
import { Options } from 'tsup';
import { createTsupConfig } from '../../scripts/tsup.config';

const options: Options = {
sourcemap: false,
dts: false
const defaultOptions: Options = {
entry: ['src/index.ts', 'src/configs/recommended.ts'],
esbuildPlugins: [esbuildPluginVersionInjector()]
};

export default createTsupConfig({
cjsOptions: options,
esmOptions: options,
cjsOptions: defaultOptions,
esmOptions: defaultOptions,
iifeOptions: { disabled: true }
});
6 changes: 5 additions & 1 deletion packages/eslint-plugin-result/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { createVitestConfig } from '../../scripts/vitest.config';

export default createVitestConfig();
export default createVitestConfig({
test: {
setupFiles: ['./tests/vitest.setup.ts']
}
});

0 comments on commit ea428f6

Please sign in to comment.