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

feat(eslint): add flat configs #114

Open
wants to merge 14 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .eslintrc.js

This file was deleted.

26 changes: 26 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import globals from 'globals';
import flat from './eslint/flat/index.js';

/** @type {import('eslint').Linter.Config[]} */
export default [
...flat.configs.recommended,
{
languageOptions: {
globals: {
...globals.node,
},
},
},
{
files: ['eslint/rules/**'],
rules: {
'sort-keys': 'error',
},
},
{
files: ['*.config.mjs'],
rules: {
'import/no-default-export': 'off',
},
},
];
4 changes: 2 additions & 2 deletions eslint/_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ module.exports = {
'plugin:import/recommended',
'prettier',
require.resolve('./rules/best-practice'),
require.resolve('./rules/comments'),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comments config and a unicorn config to ensure that only pure rules, excluding plugins, are in the rules directory.

require.resolve('./comments'),
require.resolve('./rules/es6'),
require.resolve('./rules/import'),
require.resolve('./rules/possible-errors'),
require.resolve('./rules/stylistic'),
require.resolve('./rules/unicorn'),
require.resolve('./unicorn'),
require.resolve('./rules/variables'),
],
env: {
Expand Down
2 changes: 1 addition & 1 deletion eslint/comments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
extends: [
'plugin:eslint-comments/recommended',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced eslint-comment to @eslint-community/eslint-comments cause eslint-comment is not maintained for a while

'plugin:@eslint-community/eslint-comments/recommended',
require.resolve('./rules/comments'),
],
};
48 changes: 48 additions & 0 deletions eslint/flat/_base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const babelParser = require('@babel/eslint-parser');
const eslint = require('@eslint/js');
const prettier = require('eslint-config-prettier');
const commentsConfig = require('./comments');
const importConfig = require('./import');
const unicornConfig = require('./unicorn');
const { ECMA_VERSION, JAVASCRIPT_FILES } = require('./constants');

/**
* This is the base for both our browser and Node ESLint config files.
* @type {import('eslint').Linter.Config[]}
*/
module.exports = [
eslint.configs.recommended,
prettier,
commentsConfig,
importConfig,
unicornConfig,
{
rules: {
...require('../rules/best-practice').rules,
...require('../rules/es6').rules,
...require('../rules/possible-errors').rules,
...require('../rules/stylistic').rules,
...require('../rules/variables').rules,
},
settings: {
'import/resolver': {
typescript: true,
},
},
},
{
files: JAVASCRIPT_FILES,
ignores: ['!.*.js'],
linterOptions: {
reportUnusedDisableDirectives: 'warn',
},
languageOptions: {
ecmaVersion: ECMA_VERSION,
sourceType: 'module',
parser: babelParser,
parserOptions: {
requireConfigFile: false,
},
},
},
];
10 changes: 10 additions & 0 deletions eslint/flat/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const comments = require('@eslint-community/eslint-plugin-eslint-comments/configs');

/** @type {import('eslint').Linter.Config} */
module.exports = {
...comments.recommended,
rules: {
...comments.recommended.rules,
...require('../rules/comments').rules,
},
};
5 changes: 5 additions & 0 deletions eslint/flat/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
ECMA_VERSION: 2021,
JAVASCRIPT_FILES: ['**/*.js?(x)', '**/*.(c|m)?js'],
TYPESCRIPT_FILES: ['**/*.ts?(x)'],
};
10 changes: 10 additions & 0 deletions eslint/flat/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const importPlugin = require('eslint-plugin-import');

/** @type {improt('eslint').Linter.Config} */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a typo here. import instead of improt

module.exports = {
...importPlugin.flatConfigs.recommended,
rules: {
...importPlugin.flatConfigs.recommended.rules,
...require('../rules/import').rules,
},
};
13 changes: 13 additions & 0 deletions eslint/flat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
configs: {
recommended: require('./_base'),
jest: require('./jest'),
'jest-typescript': require('./jest-typescript'),
next: require('./next'),
'playwright-test': require('./playwright-test'),
react: require('./react'),
'testing-library': require('./testing-library'),
typescript: require('./typescript'),
vitest: require('./vitest'),
},
};
10 changes: 10 additions & 0 deletions eslint/flat/jest-typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { TYPESCRIPT_FILES } = require('./constants');

/** @type {import('eslint').Linter.Config} */
module.exports = {
files: TYPESCRIPT_FILES,
rules: {
'@typescript-eslint/unbound-method': 'off',
'jest/unbound-method': 'error',
},
};
10 changes: 10 additions & 0 deletions eslint/flat/jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const jest = require('eslint-plugin-jest');

/** @type {import('eslint').Linter.Config} */
module.exports = {
...jest.configs['flat/recommended'],
rules: {
...jest.configs['flat/recommended'].rules,
...require('../rules/jest').rules,
},
};
42 changes: 42 additions & 0 deletions eslint/flat/next.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const next = require('@next/eslint-plugin-next');
const requirePackage = require('../utils/require-package');
const { JAVASCRIPT_FILES } = require('./constants');

requirePackage('next', '@next/eslint-plugin-next');

const babelOptions = {
presets: (() => {
try {
require.resolve('next/babel');
return ['next/babel'];
} catch {
return [];
}
})(),
};

/** @type {import('eslint').Linter.Config[]} */
module.exports = [
{
plugins: {
'@next/next': next,
},
rules: {
...next.configs.recommended.rules,
},
languageOptions: {
parserOptions: {
babelOptions,
},
},
ignores: ['**/.next/**'],
},
{
files: JAVASCRIPT_FILES,
languageOptions: {
parserOptions: {
babelOptions,
},
},
},
];
10 changes: 10 additions & 0 deletions eslint/flat/playwright-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const playwright = require('eslint-plugin-playwright');

/** @type {import('eslint').Linter} */
module.exports = {
...playwright.configs['flat/recommended'],
rules: {
...playwright.configs['flat/recommended'].rules,
...require('../rules/playwright-test').rules,
},
};
33 changes: 33 additions & 0 deletions eslint/flat/react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const react = require('eslint-plugin-react');
const reactHooks = require('eslint-plugin-react-hooks');
const jsxA11y = require('eslint-plugin-jsx-a11y');
const importPlugin = require('eslint-plugin-import');
const prettier = require('eslint-config-prettier');

/** @type {import('eslint').Linter.Config[]} */
module.exports = [
react.configs.flat.recommended,
react.configs.flat['jsx-runtime'],
{
plugins: {
'react-hooks': reactHooks,
},
rules: {
...reactHooks.configs.recommended.rules,
},
},
jsxA11y.flatConfigs.recommended,
{
rules: {
...require('../rules/react').rules,
...require('../rules/jsx-a11y').rules,
},
settings: {
react: {
version: 'detect',
},
},
},
importPlugin.flatConfigs.react,
prettier,
];
6 changes: 6 additions & 0 deletions eslint/flat/testing-library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const testingLibrary = require('eslint-plugin-testing-library');

/** @type {import('eslint').Linter.Config} */
module.exports = {
...testingLibrary.configs['flat/react'],
};
11 changes: 11 additions & 0 deletions eslint/flat/tsdoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const tsdoc = require('eslint-plugin-tsdoc');

/** @type {import('eslint').Linter.Config} */
module.exports = {
plugins: {
tsdoc,
},
rules: {
...require('../rules/tsdoc').rules,
},
};
30 changes: 30 additions & 0 deletions eslint/flat/typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const tseslint = require('typescript-eslint');
const importPlugin = require('eslint-plugin-import');
const prettier = require('eslint-config-prettier');
const requirePackage = require('../utils/require-package');
const { TYPESCRIPT_FILES } = require('./constants');
const tsdoc = require('./tsdoc');

requirePackage('typescript', 'typescript');

/** @type {import('eslint').Linter.Config[]} */
module.exports = [
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
importPlugin.flatConfigs.typescript,
prettier,
tsdoc,
{
files: TYPESCRIPT_FILES,
rules: {
...require('../rules/typescript').rules,
...require('../rules/typescript/extension').rules,
...require('../rules/typescript/import').rules,
},
settings: {
'import/resolver': {
typescript: true,
},
},
},
];
11 changes: 11 additions & 0 deletions eslint/flat/unicorn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const unicorn = require('eslint-plugin-unicorn');

/** @type {import('eslint').Linter.Config} */
module.exports = {
plugins: {
unicorn,
},
rules: {
...require('../rules/unicorn').rules,
},
};
12 changes: 12 additions & 0 deletions eslint/flat/vitest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const vitest = require('@vitest/eslint-plugin');

/** @type {import('eslint').Linter.Config} */
module.exports = {
plugins: {
vitest,
},
rules: {
...vitest.configs.recommended.rules,
...require('../rules/vitest').rules,
},
};
2 changes: 1 addition & 1 deletion eslint/next.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const babelOptions = {
try {
require.resolve('next/babel');
return ['next/babel'];
} catch (e) {
} catch {
return [];
}
})(),
Expand Down
3 changes: 1 addition & 2 deletions eslint/rules/comments.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module.exports = {
plugins: ['eslint-comments'],
rules: {
/**
* Require comments on ESlint disable directives.
*
* 🚫 Not fixable - https://mysticatea.github.io/eslint-plugin-eslint-comments/rules/require-description.html
*/
'eslint-comments/require-description': 'error',
'@eslint-community/eslint-comments/require-description': 'error',
},
};
1 change: 0 additions & 1 deletion eslint/rules/tsdoc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {
plugins: ['eslint-plugin-tsdoc'],
rules: {
/**
* Require TSDoc comments conform to the TSDoc specification.
Expand Down
1 change: 0 additions & 1 deletion eslint/rules/unicorn.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {
plugins: ['unicorn'],
rules: {
/**
* Require consistent filename case for all linted files.
Expand Down
4 changes: 4 additions & 0 deletions eslint/tsdoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
plugins: ['eslint-plugin-tsdoc'],
extends: [require.resolve('./rules/tsdoc')],
};
2 changes: 1 addition & 1 deletion eslint/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
require.resolve('./rules/typescript'),
require.resolve('./rules/typescript/extension'),
require.resolve('./rules/typescript/import'),
require.resolve('./rules/tsdoc'),
require.resolve('./tsdoc'),
],
},
],
Expand Down
4 changes: 4 additions & 0 deletions eslint/unicorn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
plugins: ['unicorn'],
extends: [require.resolve('./rules/unicorn.js')],
};
2 changes: 1 addition & 1 deletion eslint/utils/require-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function readPackageManager() {
module.exports = (configName, packageName) => {
try {
require.resolve(packageName);
} catch (e) {
} catch {
const packageManager = readPackageManager();
const command = packageManager === 'yarn' ? 'add' : 'install';

Expand Down
Loading