Skip to content

Commit

Permalink
feat!: migrate to ESLint flat config
Browse files Browse the repository at this point in the history
targos committed Jul 2, 2024

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent bed0203 commit 1e6be26
Showing 10 changed files with 459 additions and 452 deletions.
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -5,18 +5,22 @@ Shared ESLint config for cheminfo and ml.js projects.
## Installation

```console
npm i -D eslint-config-cheminfo eslint
npm i -D eslint-config-cheminfo eslint@^8.57.0
```

## Usage

Create a `.eslintrc.yml` with the following contents:
Create a `eslint.config.mjs` file with the following contents:

```yml
extends: [cheminfo, cheminfo/jsdoc, cheminfo/unicorn]
```js
import cheminfo from 'eslint-config-cheminfo';

export default [
...cheminfo,
];
```

You can then customize the config for your project by changing rules in this file.
You can then customize the config for your project.

Create ESLint scripts in your `package.json`:

@@ -43,8 +47,12 @@ https://github.com/cheminfo/eslint-config-cheminfo-react

You can extend both of the above configurations as they are compatible between each other:

```yml
extends:
- cheminfo-typescript
- cheminfo-react
```js
import typescript from 'eslint-config-cheminfo-typescript';
import react from 'eslint-config-cheminfo-react';

export default [
...typescript,
...react,
];
```
608 changes: 305 additions & 303 deletions base.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions cheminfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import base from './base.js';
import jsdoc from './jsdoc.js';
import unicorn from './unicorn.js';

export default [...base, ...jsdoc, ...unicorn];
1 change: 0 additions & 1 deletion eslintrc.test.yml

This file was deleted.

5 changes: 0 additions & 5 deletions index.js

This file was deleted.

57 changes: 28 additions & 29 deletions jsdoc.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
'use strict';
import jsdoc from 'eslint-plugin-jsdoc';

module.exports = {
plugins: ['jsdoc'],
extends: ['plugin:jsdoc/recommended'],
settings: {
jsdoc: {
ignoreInternal: true,
maxLines: 3,
},
},
rules: {
'jsdoc/require-jsdoc': [
'warn',
{
publicOnly: true,
export default [
jsdoc.configs['flat/recommended'],
{
settings: {
jsdoc: {
ignoreInternal: true,
maxLines: 3,
},
],
'jsdoc/require-asterisk-prefix': 'warn',
'jsdoc/require-description': 'warn',
'jsdoc/require-hyphen-before-param-description': 'warn',
},
rules: {
'jsdoc/require-jsdoc': [
'warn',
{
publicOnly: true,
},
],
'jsdoc/require-asterisk-prefix': 'warn',
'jsdoc/require-description': 'warn',
'jsdoc/require-hyphen-before-param-description': 'warn',
},
},
overrides: [
{
files: ['*.{ts,tsx,cts,mts}'],
rules: {
'jsdoc/require-param-type': 'off',
'jsdoc/require-returns-type': 'off',
'jsdoc/no-types': 'warn',
},
{
files: ['**/*.{ts,tsx,cts,mts}'],
rules: {
'jsdoc/require-param-type': 'off',
'jsdoc/require-returns-type': 'off',
'jsdoc/no-types': 'warn',
},
],
};
},
];
26 changes: 16 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -2,10 +2,16 @@
"name": "eslint-config-cheminfo",
"version": "10.0.0",
"description": "Shared ESLint config for cheminfo and ml.js projects",
"main": "index.js",
"type": "module",
"exports": {
".": "./cheminfo.js",
"./base": "./base.js",
"./jsdoc": "./jsdoc.js",
"./unicorn": "./unicorn.js"
},
"files": [
"cheminfo.js",
"base.js",
"index.js",
"jsdoc.js",
"unicorn.js"
],
@@ -23,19 +29,19 @@
"url": "https://github.com/cheminfo/eslint-config/issues"
},
"homepage": "https://github.com/cheminfo/eslint-config#readme",
"peerDependencies": {
"eslint": "^8.57.0",
"dependencies": {
"@eslint/compat": "^1.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsdoc": "^48.5.0",
"eslint-plugin-no-lookahead-lookbehind-regexp": "0.3.0",
"eslint-plugin-unicorn": "^54.0.0"
"eslint-plugin-no-lookahead-lookbehind-regexp": "^0.3.0",
"eslint-plugin-unicorn": "^54.0.0",
"globals": "^15.7.0"
},
"peerDependencies": {
"eslint": "^8.57.0"
},
"devDependencies": {
"eslint": "8.57.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-jsdoc": "48.5.0",
"eslint-plugin-no-lookahead-lookbehind-regexp": "0.3.0",
"eslint-plugin-unicorn": "54.0.0",
"prettier": "^3.3.2"
}
}
2 changes: 2 additions & 0 deletions test/not-ok.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

import '/absolute/path';

console.log('abc');

let a;
82 changes: 36 additions & 46 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,39 @@
'use strict';

const assert = require('assert');
const util = require('util');
const { ESLint } = require('eslint');

const eslint = new ESLint({ overrideConfigFile: 'eslintrc.test.yml' });

eslint
.lintFiles(['test/ok.js', 'test/not-ok.js'])
.then((results) => {
const [okResult, notOkResult] = results;

assert.strictEqual(
okResult.errorCount,
0,
'ok.js should have no error: ' + util.format(okResult.messages),
);

assert.strictEqual(
okResult.warningCount,
0,
'ok.js should have no warnings: ' + util.format(okResult.messages),
);

const errors = notOkResult.messages
.filter(isError)
.map((error) => error.ruleId)
.sort();
assert.deepStrictEqual(errors, [
'no-console',
'no-lookahead-lookbehind-regexp/no-lookahead-lookbehind-regexp',
'no-redeclare',
'no-unused-vars',
'no-unused-vars',
'no-var',
'no-var',
'one-var',
'strict',
'unicorn/no-array-reduce',
]);
})
.catch((error) => {
console.error(error);
process.exit(1);
});
import assert from 'node:assert';
import fs from 'node:fs';

import { Linter } from 'eslint';

import config from '../cheminfo.js';

function readTestFile(filename) {
return fs.readFileSync(new URL(`./${filename}`, import.meta.url), 'utf8');
}

const linter = new Linter({ configType: 'flat' });

const okResult = linter.verify(readTestFile('ok.js'), config);
assert.strictEqual(okResult.length, 0, 'ok.js should have no error');

const notOkResult = linter.verify(readTestFile('not-ok.js'), config);

const errors = notOkResult
.filter(isError)
.map((error) => error.ruleId)
.sort();

assert.deepStrictEqual(errors, [
'import/no-absolute-path',
'no-console',
'no-lookahead-lookbehind-regexp/no-lookahead-lookbehind-regexp',
'no-redeclare',
'no-unused-vars',
'no-unused-vars',
'no-var',
'no-var',
'one-var',
'strict',
'unicorn/no-array-reduce',
]);

function isError(message) {
return message.severity === 2;
99 changes: 50 additions & 49 deletions unicorn.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
'use strict';
import unicorn from 'eslint-plugin-unicorn';

module.exports = {
plugins: ['unicorn'],
extends: ['plugin:unicorn/recommended'],
rules: {
// We are not consistent enough to enable this rule.
'unicorn/filename-case': 'off',
// Flags i,j,k, etc.
'unicorn/prevent-abbreviations': 'off',
// TODO: create issue about risk (>32bit integers)
'unicorn/prefer-math-trunc': 'off',
// We prefer Array.from(x) over [...x]
'unicorn/prefer-spread': 'off',
// We prefer new Array(x).fill() over Array.from({ length: x })
'unicorn/no-new-array': 'off',
// We don't like separators after the comma
'unicorn/numeric-separators-style': 'off',
// For loop is used sometimes for performance.
'unicorn/no-for-loop': 'off',
// We use null in many places.
'unicorn/no-null': 'off',
'unicorn/switch-case-braces': 'off',
'unicorn/prefer-ternary': 'off',
'unicorn/prefer-type-error': 'off',
// Might want to enable in a future semver-major.
'unicorn/no-negated-condition': 'off',
'unicorn/no-useless-undefined': 'off',
// Conflicts with Prettier.
'unicorn/number-literal-case': 'off',
// Too early for full ESM
'unicorn/prefer-module': 'off',
// Conflicts with Prettier.
'unicorn/no-nested-ternary': 'off',
// Problematic with useOnOff
'unicorn/no-unreadable-array-destructuring': 'off',
'unicorn/prefer-regexp-test': 'off',
// Unfortunately too annoying on valid cases + conflicts with TS.
'unicorn/no-array-callback-reference': 'off',
// May conflict with other rule + ugly switch(0) auto-fix.
'unicorn/prefer-switch': 'off',
// Rare and problematic with APIs that have a find method.
'unicorn/no-array-method-this-argument': 'off',
// We don't use the feature.
'unicorn/expiring-todo-comments': 'off',
// Doesn't work with typescript-eslint v6 at the moment.
'unicorn/no-empty-file': 'off',
// We already have a rule for anonymous functions and tooling often does this with config.
'unicorn/no-anonymous-default-export': 'off',
export default [
unicorn.configs['flat/recommended'],
{
rules: {
// We are not consistent enough to enable this rule.
'unicorn/filename-case': 'off',
// Flags i,j,k, etc.
'unicorn/prevent-abbreviations': 'off',
// TODO: create issue about risk (>32bit integers)
'unicorn/prefer-math-trunc': 'off',
// We prefer Array.from(x) over [...x].
'unicorn/prefer-spread': 'off',
// We prefer new Array(x).fill() over Array.from({ length: x }).
'unicorn/no-new-array': 'off',
// We don't like separators after the comma.
'unicorn/numeric-separators-style': 'off',
// For loop is used sometimes for performance.
'unicorn/no-for-loop': 'off',
// We use null in many places.
'unicorn/no-null': 'off',
'unicorn/switch-case-braces': 'off',
'unicorn/prefer-ternary': 'off',
'unicorn/prefer-type-error': 'off',
// Might want to enable in a future semver-major.
'unicorn/no-negated-condition': 'off',
'unicorn/no-useless-undefined': 'off',
// Conflicts with Prettier.
'unicorn/number-literal-case': 'off',
// Too early for full ESM.
'unicorn/prefer-module': 'off',
// Conflicts with Prettier.
'unicorn/no-nested-ternary': 'off',
// Problematic with useOnOff.
'unicorn/no-unreadable-array-destructuring': 'off',
'unicorn/prefer-regexp-test': 'off',
// Unfortunately too annoying on valid cases + conflicts with TS.
'unicorn/no-array-callback-reference': 'off',
// May conflict with other rule + ugly switch(0) auto-fix.
'unicorn/prefer-switch': 'off',
// Rare and problematic with APIs that have a find method.
'unicorn/no-array-method-this-argument': 'off',
// We don't use the feature.
'unicorn/expiring-todo-comments': 'off',
// Doesn't work with typescript-eslint v6 at the moment.
'unicorn/no-empty-file': 'off',
// We already have a rule for anonymous functions and tooling often does this with config.
'unicorn/no-anonymous-default-export': 'off',
},
},
};
];

0 comments on commit 1e6be26

Please sign in to comment.