chore: move to eslint v9 (#7041)

- Upgrades eslint from v8 to v9
- Upgrades all other eslint packages. We will have to do a new
full-project lint, as new rules have been added
- Upgrades husky from v8 to v9
- Upgrades lint-staged from v14 to v15
- Moves the old .eslintrc.cjs file format to the new eslint.config.js
flat file format.

Previously, we were very specific regarding which rules are applied to
which files. Now that `extends` is no longer a thing, I have to use
deepMerge & imports instead.

This is rather uncommon and is not a documented pattern - e.g.
typescript-eslint docs want us to add the default typescript-eslint
rules to the top-level & then disable it in files using the
disable-typechecked config.

However, I hate this opt-out approach. The way I did it here adds a lot
of clarity as to which rules are applied to which files, and is pretty
easy to read. Much less black magic

## .eslintignore

These files are no longer supported (see
https://eslint.org/docs/latest/use/configure/migration-guide#ignoring-files).
I moved the entries to the ignores property in the eslint config. => one
less file in each package folder!
This commit is contained in:
Alessio Gravili
2024-07-09 09:50:37 -04:00
committed by GitHub
parent bd5f5a2d4b
commit 1038e1c228
238 changed files with 2915 additions and 1978 deletions

View File

@@ -0,0 +1,20 @@
import jestRules from './rules/jest.mjs'
import jestDomRules from './rules/jest-dom.mjs'
import jestDom from 'eslint-plugin-jest-dom'
import jest from 'eslint-plugin-jest'
import { deepMerge } from '../../deepMerge.js'
/** @type {import('eslint').Linter.FlatConfig} */
export const index = deepMerge(
jestRules,
jestDomRules,
{
plugins: {
jest,
'jest-dom': jestDom,
},
}
)
export default index

View File

@@ -0,0 +1,12 @@
/** @type {import('eslint').Linter.FlatConfig} */
export const index = {
rules: {
'jest-dom/prefer-checked': 'error',
'jest-dom/prefer-enabled-disabled': 'error',
'jest-dom/prefer-focus': 'error',
'jest-dom/prefer-required': 'error',
'jest-dom/prefer-to-have-attribute': 'error',
},
}
export default index

View File

@@ -0,0 +1,40 @@
/** @type {import('eslint').Linter.FlatConfig} */
export const index = {
rules: {
'jest/consistent-test-it': ['error', { fn: 'it' }],
'jest/expect-expect': 'error',
'jest/prefer-lowercase-title': ['error', { ignore: ['describe'] }],
'jest/no-alias-methods': 'error',
'jest/no-commented-out-tests': 'off',
'jest/no-disabled-tests': 'off',
'jest/no-duplicate-hooks': 'error',
'jest/no-export': 'error',
'jest/no-focused-tests': 'error',
'jest/no-hooks': 'off',
'jest/no-identical-title': 'error',
'jest/no-conditional-in-test': 'error',
'jest/no-jasmine-globals': 'error',
'jest/no-large-snapshots': 'error',
'jest/no-mocks-import': 'error',
'jest/no-standalone-expect': 'error',
'jest/no-done-callback': 'error',
'jest/no-test-prefixes': 'error',
'jest/no-test-return-statement': 'error',
'jest/prefer-called-with': 'error',
'jest/prefer-expect-assertions': 'off',
'jest/prefer-hooks-on-top': 'error',
'jest/prefer-spy-on': 'error',
'jest/prefer-strict-equal': 'error',
'jest/prefer-to-contain': 'error',
'jest/prefer-to-have-length': 'error',
'jest/prefer-todo': 'error',
'jest/require-top-level-describe': 'error',
'jest/require-to-throw-message': 'error',
'jest/valid-describe-callback': 'error',
'jest/valid-expect-in-promise': 'error',
'jest/valid-expect': 'error',
'jest/valid-title': 'error',
},
}
export default index

View File

@@ -0,0 +1,44 @@
import reactRules from './rules/react.mjs'
import reactA11yRules from './rules/react-a11y.mjs'
import jsxA11y from 'eslint-plugin-jsx-a11y'
import eslintPluginReactConfig from 'eslint-plugin-react/configs/recommended.js'
import eslintPluginReact from 'eslint-plugin-react'
import reactHooks from 'eslint-plugin-react-hooks'
import globals from 'globals';
import { fixupPluginRules } from '@eslint/compat'
import { deepMerge } from '../../deepMerge.js'
/** @type {import('eslint').Linter.FlatConfig} */
export const index = deepMerge(
{
rules: eslintPluginReact.configs.recommended.rules
},
{
rules: eslintPluginReactConfig.rules // Only take rules from the config, not plugins, as plugins there are on the old eslint v8 format => add react-hooks plugin myself below
},
reactRules,
reactA11yRules,
{
languageOptions: {
globals: {
...globals.browser,
},
parserOptions: {
ecmaFeatures: {
jsx: true,
}
}
},
plugins: {
'jsx-a11y': jsxA11y,
react: fixupPluginRules(eslintPluginReact),
'react-hooks': reactHooks,
},
settings: {
react: {
version: 'detect',
},
},
}
)
export default index

View File

@@ -0,0 +1,249 @@
// Sourced from https://github.com/airbnb/javascript/blob/master/packages/eslint-config-airbnb/rules/react-a11y.js
/** @type {import('eslint').Linter.FlatConfig} */
export const index = {
rules: {
// Enforce that anchors have content
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/anchor-has-content.md
'jsx-a11y/anchor-has-content': ['error', { components: [] }],
// Require ARIA roles to be valid and non-abstract
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/aria-role.md
'jsx-a11y/aria-role': ['error', { ignoreNonDom: false }],
// Enforce all aria-* props are valid.
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/aria-props.md
'jsx-a11y/aria-props': 'error',
// Enforce ARIA state and property values are valid.
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/aria-proptypes.md
'jsx-a11y/aria-proptypes': 'error',
// Enforce that elements that do not support ARIA roles, states, and
// properties do not have those attributes.
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/aria-unsupported-elements.md
'jsx-a11y/aria-unsupported-elements': 'error',
// Enforce that all elements that require alternative text have meaningful information
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/alt-text.md
'jsx-a11y/alt-text': [
'error',
{
elements: ['img', 'object', 'area', 'input[type="image"]'],
img: [],
object: [],
area: [],
'input[type="image"]': [],
},
],
// Prevent img alt text from containing redundant words like "image", "picture", or "photo"
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/img-redundant-alt.md
'jsx-a11y/img-redundant-alt': 'error',
// require that JSX labels use "htmlFor"
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/label-has-for.md
// deprecated: replaced by `label-has-associated-control` rule
'jsx-a11y/label-has-for': [
'off',
{
components: [],
required: {
every: ['nesting', 'id'],
},
allowChildren: false,
},
],
// Enforce that a label tag has a text label and an associated control.
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/b800f40a2a69ad48015ae9226fbe879f946757ed/docs/rules/label-has-associated-control.md
'jsx-a11y/label-has-associated-control': [
'error',
{
labelComponents: [],
labelAttributes: [],
controlComponents: [],
assert: 'both',
depth: 25,
},
],
// Enforce that a control (an interactive element) has a text label.
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/control-has-associated-label.md
'jsx-a11y/control-has-associated-label': [
'error',
{
labelAttributes: ['label'],
controlComponents: [],
ignoreElements: ['audio', 'canvas', 'embed', 'input', 'textarea', 'tr', 'video'],
ignoreRoles: [
'grid',
'listbox',
'menu',
'menubar',
'radiogroup',
'row',
'tablist',
'toolbar',
'tree',
'treegrid',
],
depth: 5,
},
],
// require that mouseover/out come with focus/blur, for keyboard-only users
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/mouse-events-have-key-events.md
'jsx-a11y/mouse-events-have-key-events': 'error',
// Prevent use of `accessKey`
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-access-key.md
'jsx-a11y/no-access-key': 'error',
// require onBlur instead of onChange
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-onchange.md
'jsx-a11y/no-onchange': 'off',
// Elements with an interactive role and interaction handlers must be focusable
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/interactive-supports-focus.md
'jsx-a11y/interactive-supports-focus': 'error',
// Enforce that elements with ARIA roles must have all required attributes
// for that role.
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/role-has-required-aria-props.md
'jsx-a11y/role-has-required-aria-props': 'error',
// Enforce that elements with explicit or implicit roles defined contain
// only aria-* properties supported by that role.
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/role-supports-aria-props.md
'jsx-a11y/role-supports-aria-props': 'error',
// Enforce tabIndex value is not greater than zero.
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/tabindex-no-positive.md
'jsx-a11y/tabindex-no-positive': 'error',
// ensure <hX> tags have content and are not aria-hidden
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/heading-has-content.md
'jsx-a11y/heading-has-content': ['error', { components: [''] }],
// require HTML elements to have a "lang" prop
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/html-has-lang.md
'jsx-a11y/html-has-lang': 'error',
// require HTML element's lang prop to be valid
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/lang.md
'jsx-a11y/lang': 'error',
// prevent distracting elements, like <marquee> and <blink>
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-distracting-elements.md
'jsx-a11y/no-distracting-elements': [
'error',
{
elements: ['marquee', 'blink'],
},
],
// only allow <th> to have the "scope" attr
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/scope.md
'jsx-a11y/scope': 'error',
// require onClick be accompanied by onKeyUp/onKeyDown/onKeyPress
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/click-events-have-key-events.md
'jsx-a11y/click-events-have-key-events': 'error',
// Enforce that DOM elements without semantic behavior not have interaction handlers
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-static-element-interactions.md
'jsx-a11y/no-static-element-interactions': [
'error',
{
handlers: ['onClick', 'onMouseDown', 'onMouseUp', 'onKeyPress', 'onKeyDown', 'onKeyUp'],
},
],
// A non-interactive element does not support event handlers (mouse and key handlers)
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-noninteractive-element-interactions.md
'jsx-a11y/no-noninteractive-element-interactions': [
'error',
{
handlers: ['onClick', 'onMouseDown', 'onMouseUp', 'onKeyPress', 'onKeyDown', 'onKeyUp'],
},
],
// ensure emoji are accessible
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/accessible-emoji.md
'jsx-a11y/accessible-emoji': 'error',
// elements with aria-activedescendant must be tabbable
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/aria-activedescendant-has-tabindex.md
'jsx-a11y/aria-activedescendant-has-tabindex': 'error',
// ensure iframe elements have a unique title
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/iframe-has-title.md
'jsx-a11y/iframe-has-title': 'error',
// prohibit autoFocus prop
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-autofocus.md
'jsx-a11y/no-autofocus': ['error', { ignoreNonDOM: true }],
// ensure HTML elements do not specify redundant ARIA roles
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-redundant-roles.md
'jsx-a11y/no-redundant-roles': 'error',
// media elements must have captions
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/media-has-caption.md
'jsx-a11y/media-has-caption': [
'error',
{
audio: [],
video: [],
track: [],
},
],
// WAI-ARIA roles should not be used to convert an interactive element to non-interactive
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-interactive-element-to-noninteractive-role.md
'jsx-a11y/no-interactive-element-to-noninteractive-role': [
'error',
{
tr: ['none', 'presentation'],
},
],
// WAI-ARIA roles should not be used to convert a non-interactive element to interactive
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-noninteractive-element-to-interactive-role.md
'jsx-a11y/no-noninteractive-element-to-interactive-role': [
'error',
{
ul: ['listbox', 'menu', 'menubar', 'radiogroup', 'tablist', 'tree', 'treegrid'],
ol: ['listbox', 'menu', 'menubar', 'radiogroup', 'tablist', 'tree', 'treegrid'],
li: ['menuitem', 'option', 'row', 'tab', 'treeitem'],
table: ['grid'],
td: ['gridcell'],
},
],
// Tab key navigation should be limited to elements on the page that can be interacted with.
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-noninteractive-tabindex.md
'jsx-a11y/no-noninteractive-tabindex': [
'error',
{
tags: [],
roles: ['tabpanel'],
},
],
// ensure <a> tags are valid
// https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/0745af376cdc8686d85a361ce36952b1fb1ccf6e/docs/rules/anchor-is-valid.md
'jsx-a11y/anchor-is-valid': [
'error',
{
components: ['Link'],
specialLink: ['to'],
aspects: ['noHref', 'invalidHref', 'preferButton'],
},
],
},
}
export default index

View File

@@ -0,0 +1,549 @@
/** @type {import('eslint').Linter.FlatConfig} */
export const index = {
rules: {
// View link below for react rules documentation
// https://github.com/yannickcr/eslint-plugin-react#list-of-supported-rules
// Specify whether double or single quotes should be used in JSX attributes
// https://eslint.org/docs/rules/jsx-quotes
'jsx-quotes': ['error', 'prefer-double'],
'class-methods-use-this': [
'error',
{
exceptMethods: [
'render',
'getInitialState',
'getDefaultProps',
'getChildContext',
'componentWillMount',
'UNSAFE_componentWillMount',
'componentDidMount',
'componentWillReceiveProps',
'UNSAFE_componentWillReceiveProps',
'shouldComponentUpdate',
'componentWillUpdate',
'UNSAFE_componentWillUpdate',
'componentDidUpdate',
'componentWillUnmount',
'componentDidCatch',
'getSnapshotBeforeUpdate',
],
},
],
// Prevent missing displayName in a React component definition
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/display-name.md
'react/display-name': ['off', { ignoreTranspilerName: false }],
// Forbid certain propTypes (any, array, object)
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/forbid-prop-types.md
'react/forbid-prop-types': [
'error',
{
forbid: ['any', 'array', 'object'],
checkContextTypes: true,
checkChildContextTypes: true,
},
],
// Forbid certain props on DOM Nodes
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/forbid-dom-props.md
'react/forbid-dom-props': ['off', { forbid: [] }],
// Enforce boolean attributes notation in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-boolean-value.md
'react/jsx-boolean-value': ['error', 'never', { always: [] }],
// Validate closing bracket location in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-bracket-location.md
'react/jsx-closing-bracket-location': ['error', 'line-aligned'],
// Validate closing tag location in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-closing-tag-location.md
'react/jsx-closing-tag-location': 'error',
// Enforce or disallow spaces inside of curly braces in JSX attributes
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-curly-spacing.md
'react/jsx-curly-spacing': ['error', 'never', { allowMultiline: true }],
// Enforce event handler naming conventions in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-handler-names.md
'react/jsx-handler-names': [
'off',
{
eventHandlerPrefix: 'handle',
eventHandlerPropPrefix: 'on',
},
],
// Validate props indentation in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-indent-props.md
'react/jsx-indent-props': ['error', 2],
// Validate JSX has key prop when in array or iterator
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-key.md
'react/jsx-key': 'off',
// Limit maximum of props on a single line in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-max-props-per-line.md
'react/jsx-max-props-per-line': ['error', { maximum: 1 }],
// Prevent usage of .bind() in JSX props
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md
'react/jsx-no-bind': [
'error',
{
ignoreRefs: true,
allowArrowFunctions: true,
allowFunctions: false,
allowBind: false,
ignoreDOMComponents: true,
},
],
// Prevent duplicate props in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-duplicate-props.md
'react/jsx-no-duplicate-props': ['error', { ignoreCase: true }],
// Prevent usage of unwrapped JSX strings
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-literals.md
'react/jsx-no-literals': ['off', { noStrings: true }],
// Disallow undeclared variables in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-undef.md
'react/jsx-no-undef': 'error',
// Enforce PascalCase for user-defined JSX components
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-pascal-case.md
'react/jsx-pascal-case': [
'error',
{
allowAllCaps: true,
ignore: [],
},
],
// Enforce propTypes declarations alphabetical sorting
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-prop-types.md
'react/sort-prop-types': [
'off',
{
ignoreCase: true,
callbacksLast: false,
requiredFirst: false,
sortShapeProp: true,
},
],
// Deprecated in favor of react/jsx-sort-props
'react/jsx-sort-prop-types': 'off',
// Enforce props alphabetical sorting
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-sort-props.md
'react/jsx-sort-props': [
'off',
{
ignoreCase: true,
callbacksLast: false,
shorthandFirst: false,
shorthandLast: false,
noSortAlphabetically: false,
reservedFirst: true,
},
],
// Enforce defaultProps declarations alphabetical sorting
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/jsx-sort-default-props.md
'react/jsx-sort-default-props': [
'off',
{
ignoreCase: true,
},
],
// Prevent React to be incorrectly marked as unused
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-uses-react.md
'react/jsx-uses-react': ['error'],
// Prevent variables used in JSX to be incorrectly marked as unused
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-uses-vars.md
'react/jsx-uses-vars': 'error',
// Prevent usage of dangerous JSX properties
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-danger.md
'react/no-danger': 'off',
// Prevent usage of deprecated methods
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-deprecated.md
'react/no-deprecated': ['error'],
// Prevent usage of setState in componentDidMount
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-did-mount-set-state.md
// this is necessary for server-rendering
'react/no-did-mount-set-state': 'off',
// Prevent usage of setState in componentDidUpdate
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-did-update-set-state.md
'react/no-did-update-set-state': 'off',
// Prevent usage of setState in componentWillUpdate
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-will-update-set-state.md
'react/no-will-update-set-state': 'error',
// Prevent direct mutation of this.state
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-direct-mutation-state.md
'react/no-direct-mutation-state': 'off',
// Prevent usage of isMounted
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-is-mounted.md
'react/no-is-mounted': 'error',
// Prevent multiple component definition per file
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-multi-comp.md
'react/no-multi-comp': 'off',
// Prevent usage of setState
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-set-state.md
'react/no-set-state': 'off',
// Prevent using string references
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md
'react/no-string-refs': 'error',
// Prevent usage of unknown DOM property
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-unknown-property.md
'react/no-unknown-property': 'error',
// Require ES6 class declarations over React.createClass
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-es6-class.md
'react/prefer-es6-class': ['error', 'always'],
// Require stateless functions when not using lifecycle methods, setState or ref
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-stateless-function.md
'react/prefer-stateless-function': ['error', { ignorePureComponents: true }],
// Prevent missing props validation in a React component definition
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prop-types.md
'react/prop-types': [
'error',
{
ignore: [],
customValidators: [],
skipUndeclared: false,
},
],
// Prevent missing React when using JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/react-in-jsx-scope.md
'react/react-in-jsx-scope': 'error',
// Require render() methods to return something
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/require-render-return.md
'react/require-render-return': 'error',
// Prevent extra closing tags for components without children
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/self-closing-comp.md
'react/self-closing-comp': 'error',
// Enforce component methods order
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/sort-comp.md
'react/sort-comp': [
'error',
{
order: [
'static-variables',
'static-methods',
'instance-variables',
'lifecycle',
'/^on.+$/',
'getters',
'setters',
'/^(get|set)(?!(InitialState$|DefaultProps$|ChildContext$)).+$/',
'instance-methods',
'everything-else',
'rendering',
],
groups: {
lifecycle: [
'displayName',
'propTypes',
'contextTypes',
'childContextTypes',
'mixins',
'statics',
'defaultProps',
'constructor',
'getDefaultProps',
'getInitialState',
'state',
'getChildContext',
'getDerivedStateFromProps',
'componentWillMount',
'UNSAFE_componentWillMount',
'componentDidMount',
'componentWillReceiveProps',
'UNSAFE_componentWillReceiveProps',
'shouldComponentUpdate',
'componentWillUpdate',
'UNSAFE_componentWillUpdate',
'getSnapshotBeforeUpdate',
'componentDidUpdate',
'componentDidCatch',
'componentWillUnmount',
],
rendering: ['/^render.+$/', 'render'],
},
},
],
// Prevent missing parentheses around multilines JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/jsx-wrap-multilines.md
'react/jsx-wrap-multilines': [
'error',
{
declaration: 'parens-new-line',
assignment: 'parens-new-line',
return: 'parens-new-line',
arrow: 'parens-new-line',
condition: 'parens-new-line',
logical: 'parens-new-line',
prop: 'parens-new-line',
},
],
// Require that the first prop in a JSX element be on a new line when the element is multiline
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-first-prop-new-line.md
'react/jsx-first-prop-new-line': ['error', 'multiline-multiprop'],
// Enforce spacing around jsx equals signs
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-equals-spacing.md
'react/jsx-equals-spacing': ['error', 'never'],
// Enforce JSX indentation
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-indent.md
'react/jsx-indent': ['error', 2],
// Disallow target="_blank" on links
// https://github.com/yannickcr/eslint-plugin-react/blob/ac102885765be5ff37847a871f239c6703e1c7cc/docs/rules/jsx-no-target-blank.md
'react/jsx-no-target-blank': ['error', { enforceDynamicLinks: 'always' }],
// only .jsx files may have JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-filename-extension.md
'react/jsx-filename-extension': ['error', { extensions: ['.js', '.jsx', '.ts', '.tsx'] }],
// prevent accidental JS comments from being injected into JSX as text
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-comment-textnodes.md
'react/jsx-no-comment-textnodes': 'error',
// disallow using React.render/ReactDOM.render's return value
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-render-return-value.md
'react/no-render-return-value': 'error',
// require a shouldComponentUpdate method, or PureRenderMixin
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/require-optimization.md
'react/require-optimization': ['off', { allowDecorators: [] }],
// warn against using findDOMNode()
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-find-dom-node.md
'react/no-find-dom-node': 'error',
// Forbid certain props on Components
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/forbid-component-props.md
'react/forbid-component-props': ['off', { forbid: [] }],
// Forbid certain elements
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/forbid-elements.md
'react/forbid-elements': ['off', { forbid: [] }],
// Prevent problem with children and props.dangerouslySetInnerHTML
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-danger-with-children.md
'react/no-danger-with-children': 'error',
// Prevent unused propType definitions
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-unused-prop-types.md
'react/no-unused-prop-types': [
'error',
{
customValidators: [],
skipShapeProps: true,
},
],
// Require style prop value be an object or var
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/style-prop-object.md
'react/style-prop-object': 'error',
// Prevent invalid characters from appearing in markup
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-unescaped-entities.md
'react/no-unescaped-entities': 'error',
// Prevent passing of children as props
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-children-prop.md
'react/no-children-prop': 'error',
// Validate whitespace in and around the JSX opening and closing brackets
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/jsx-tag-spacing.md
'react/jsx-tag-spacing': [
'error',
{
closingSlash: 'never',
beforeSelfClosing: 'always',
afterOpening: 'never',
beforeClosing: 'never',
},
],
// Enforce spaces before the closing bracket of self-closing JSX elements
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-space-before-closing.md
// Deprecated in favor of jsx-tag-spacing
'react/jsx-space-before-closing': ['off', 'always'],
// Prevent usage of Array index in keys
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-array-index-key.md
'react/no-array-index-key': 'off',
// Enforce a defaultProps definition for every prop that is not a required prop
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/require-default-props.md
'react/require-default-props': [
'error',
{
forbidDefaultForRequired: true,
},
],
// Forbids using non-exported propTypes
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/forbid-foreign-prop-types.md
// this is intentionally set to "warn". it would be "error",
// but it's only critical if you're stripping propTypes in production.
'react/forbid-foreign-prop-types': ['warn', { allowInPropTypes: true }],
// Prevent void DOM elements from receiving children
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/void-dom-elements-no-children.md
'react/void-dom-elements-no-children': 'error',
// Enforce all defaultProps have a corresponding non-required PropType
// https://github.com/yannickcr/eslint-plugin-react/blob/9e13ae2c51e44872b45cc15bf1ac3a72105bdd0e/docs/rules/default-props-match-prop-types.md
'react/default-props-match-prop-types': ['error', { allowRequiredDefaults: false }],
// Prevent usage of shouldComponentUpdate when extending React.PureComponent
// https://github.com/yannickcr/eslint-plugin-react/blob/9e13ae2c51e44872b45cc15bf1ac3a72105bdd0e/docs/rules/no-redundant-should-component-update.md
'react/no-redundant-should-component-update': 'error',
// Prevent unused state values
// https://github.com/yannickcr/eslint-plugin-react/pull/1103/
'react/no-unused-state': 'error',
// Enforces consistent naming for boolean props
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/boolean-prop-naming.md
'react/boolean-prop-naming': [
'off',
{
propTypeNames: ['bool', 'mutuallyExclusiveTrueProps'],
rule: '^(is|has)[A-Z]([A-Za-z0-9]?)+',
message: '',
},
],
// Prevents common casing typos
// https://github.com/yannickcr/eslint-plugin-react/blob/73abadb697034b5ccb514d79fb4689836fe61f91/docs/rules/no-typos.md
'react/no-typos': 'error',
// Enforce curly braces or disallow unnecessary curly braces in JSX props and/or children
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-curly-brace-presence.md
'react/jsx-curly-brace-presence': ['error', { props: 'never', children: 'never' }],
// One JSX Element Per Line
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/jsx-one-expression-per-line.md
'react/jsx-one-expression-per-line': ['error', { allow: 'single-child' }],
// Enforce consistent usage of destructuring assignment of props, state, and context
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/destructuring-assignment.md
'react/destructuring-assignment': ['error', 'always'],
// Prevent using this.state within a this.setState
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/no-access-state-in-setstate.md
'react/no-access-state-in-setstate': 'error',
// Prevent usage of button elements without an explicit type attribute
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/button-has-type.md
'react/button-has-type': [
'error',
{
button: true,
submit: true,
reset: false,
},
],
// Ensures inline tags are not rendered without spaces between them
'react/jsx-child-element-spacing': 'off',
// Prevent this from being used in stateless functional components
// https://github.com/yannickcr/eslint-plugin-react/blob/843d71a432baf0f01f598d7cf1eea75ad6896e4b/docs/rules/no-this-in-sfc.md
'react/no-this-in-sfc': 'error',
// Validate JSX maximum depth
// https://github.com/yannickcr/eslint-plugin-react/blob/abe8381c0d6748047224c430ce47f02e40160ed0/docs/rules/jsx-max-depth.md
'react/jsx-max-depth': 'off',
// Disallow multiple spaces between inline JSX props
// https://github.com/yannickcr/eslint-plugin-react/blob/ac102885765be5ff37847a871f239c6703e1c7cc/docs/rules/jsx-props-no-multi-spaces.md
'react/jsx-props-no-multi-spaces': 'error',
// Prevent usage of UNSAFE_ methods
// https://github.com/yannickcr/eslint-plugin-react/blob/157cc932be2cfaa56b3f5b45df6f6d4322a2f660/docs/rules/no-unsafe.md
'react/no-unsafe': 'off',
// Enforce shorthand or standard form for React fragments
// https://github.com/yannickcr/eslint-plugin-react/blob/bc976b837abeab1dffd90ac6168b746a83fc83cc/docs/rules/jsx-fragments.md
'react/jsx-fragments': ['error', 'element'],
// Enforce linebreaks in curly braces in JSX attributes and expressions.
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-curly-newline.md
'react/jsx-curly-newline': [
'error',
{
multiline: 'consistent',
singleline: 'consistent',
},
],
// Enforce state initialization style
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/state-in-constructor.md
// TODO: set to "never" once babel-preset-airbnb supports public class fields
'react/state-in-constructor': ['error', 'always'],
// Enforces where React component static properties should be positioned
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/static-property-placement.md
// TODO: set to "static public field" once babel-preset-airbnb supports public class fields
'react/static-property-placement': ['error', 'property assignment'],
// Disallow JSX props spreading
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-props-no-spreading.md
'react/jsx-props-no-spreading': 'off',
// Enforce that props are read-only
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-read-only-props.md
'react/prefer-read-only-props': 'off',
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.json'],
},
},
react: {
pragma: 'React',
version: 'detect',
},
propWrapperFunctions: [
'forbidExtraProps', // https://www.npmjs.com/package/airbnb-prop-types
'exact', // https://www.npmjs.com/package/prop-types-exact
'Object.freeze', // https://tc39.github.io/ecma262/#sec-object.freeze
],
},
}
export default index

View File

@@ -0,0 +1,58 @@
/**
* obj2 has priority over obj1
*
* Merges obj2 into obj1
*/
export function _deepMerge(obj1, obj2, doNotMergeInNulls = true) {
const output = { ...obj1 }
for (const key in obj2) {
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
if (doNotMergeInNulls) {
if (
(obj2[key] === null || obj2[key] === undefined) &&
obj1[key] !== null &&
obj1[key] !== undefined
) {
continue
}
}
// Check if both are arrays
if (Array.isArray(obj1[key]) && Array.isArray(obj2[key])) {
// Merge each element in the arrays
// We need the Array.from, map rather than a normal map because this handles holes in arrays properly. A simple .map would skip holes.
output[key] = Array.from(obj2[key], (item, index) => {
if (doNotMergeInNulls) {
if (
(item === undefined || item === null) &&
obj1[key][index] !== null &&
obj1[key][index] !== undefined
) {
return obj1[key][index]
}
}
if (typeof item === 'object' && !Array.isArray(item) && obj1[key][index]) {
// Deep merge for objects in arrays
return deepMerge(obj1[key][index], item, doNotMergeInNulls)
}
return item
})
} else if (typeof obj2[key] === 'object' && !Array.isArray(obj2[key]) && obj1[key]) {
// Existing behavior for objects
output[key] = deepMerge(obj1[key], obj2[key], doNotMergeInNulls)
} else {
// Direct assignment for values
output[key] = obj2[key]
}
}
}
return output
}
export function deepMerge(...objs) {
return objs.reduce((acc, obj) => _deepMerge(acc, obj), {})
}

View File

@@ -0,0 +1,209 @@
import js from '@eslint/js'
import tseslint from 'typescript-eslint'
import perfectionistNatural from 'eslint-plugin-perfectionist/configs/recommended-natural'
import { configs as regexpPluginConfigs } from 'eslint-plugin-regexp'
import eslintConfigPrettier from 'eslint-config-prettier';
import payloadPlugin from '@payloadcms/eslint-plugin'
import reactExtends from './configs/react/index.mjs'
import jestExtends from './configs/jest/index.mjs'
import globals from 'globals';
import importX from 'eslint-plugin-import-x'
import typescriptParser from '@typescript-eslint/parser'
import { deepMerge } from './deepMerge.js'
const baseRules = {
// This rule makes no sense when overriding class methods. This is used a lot in richtext-lexical.
'class-methods-use-this': 'off',
'arrow-body-style': 0,
'import-x/prefer-default-export': 'off',
'no-restricted-exports': ['warn', { restrictDefaultExports: { direct: true } }],
'no-console': 'warn',
'no-sparse-arrays': 'off',
'no-underscore-dangle': 'off',
'no-use-before-define': 'off',
'object-shorthand': 'warn',
'no-useless-escape': 'warn',
'import-x/no-duplicates': 'warn',
'perfectionist/sort-objects': [
'error',
{
type: 'natural',
order: 'asc',
'partition-by-comment': true,
'partition-by-new-line': true,
groups: ['top', 'unknown'],
'custom-groups': {
top: ['_id', 'id', 'name', 'slug', 'type'],
},
},
],
/*'perfectionist/sort-object-types': [
'error',
{
'partition-by-new-line': true,
},
],
'perfectionist/sort-interfaces': [
'error',
{
'partition-by-new-line': true,
},
],*/
'payload/no-jsx-import-statements': 'error',
}
const reactRules = {
'react/no-unused-prop-types': 'off',
'react/prop-types': 'off',
'react/require-default-props': 'off',
'react/destructuring-assignment': 'warn',
'react/no-unescaped-entities': 'warn',
'jsx-a11y/anchor-is-valid': 'warn',
'jsx-a11y/control-has-associated-label': 'warn',
'jsx-a11y/no-static-element-interactions': 'warn',
'jsx-a11y/label-has-associated-control': 'warn',
}
const typescriptRules = {
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
// Type-aware any rules:
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/unbound-method': 'warn',
// This rule doesn't work well in .tsx files
'@typescript-eslint/no-misused-promises': 'off',
'@typescript-eslint/consistent-type-imports': 'warn',
'@typescript-eslint/no-explicit-any': 'warn',
// Type-aware any rules end
// ts-expect preferred over ts-ignore. It will error if the expected error is no longer present.
'@typescript-eslint/prefer-ts-expect-error': 'error',
// By default, it errors for unused variables. This is annoying, warnings are enough.
'@typescript-eslint/no-unused-vars': [
'warn',
{
vars: 'all',
args: 'after-used',
ignoreRestSiblings: false,
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
destructuredArrayIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^ignore',
},
],
'@typescript-eslint/no-base-to-string': 'warn',
'@typescript-eslint/restrict-template-expressions': 'warn',
'@typescript-eslint/no-redundant-type-constituents': 'warn',
'@typescript-eslint/no-unnecessary-type-constraint': 'warn',
'@typescript-eslint/ban-types': 'warn',
}
/** @typedef {import('eslint').Linter.FlatConfig} */
let FlatConfig
/** @type {FlatConfig} */
const baseExtends = deepMerge(js.configs.recommended, perfectionistNatural , regexpPluginConfigs['flat/recommended'])
/** @type {FlatConfig[]} */
export const rootEslintConfig = [
{
name: 'Settings',
languageOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
ecmaVersion: 'latest',
sourceType: 'module',
globals: {
...globals.node,
},
parser: typescriptParser,
},
plugins: {
'import-x': importX,
},
},
{
name: 'TypeScript',
// has 3 entries: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/typescript-eslint/src/configs/recommended-type-checked.ts
...deepMerge(
baseExtends,
tseslint.configs.recommendedTypeChecked[0],
tseslint.configs.recommendedTypeChecked[1],
tseslint.configs.recommendedTypeChecked[2],
eslintConfigPrettier,
{
plugins: {
payload: payloadPlugin,
},
rules: {
...baseRules,
...typescriptRules,
},
}
),
files: ['**/*.ts'],
},
{
name: 'TypeScript-React',
...deepMerge(
baseExtends,
tseslint.configs.recommendedTypeChecked[0],
tseslint.configs.recommendedTypeChecked[1],
tseslint.configs.recommendedTypeChecked[2],
reactExtends,
eslintConfigPrettier,
{
plugins: {
payload: payloadPlugin,
},
rules: {
...baseRules,
...typescriptRules,
...reactRules,
},
}
),
files: ['**/*.tsx'],
},
{
name: 'Unit Tests',
...deepMerge(
jestExtends,
{
plugins: {
payload: payloadPlugin
},
rules: {
...baseRules,
...typescriptRules,
'@typescript-eslint/unbound-method': 'off',
},
}
),
files: ['**/*.spec.ts'],
},
{
name: 'Payload Config',
plugins: {
payload: payloadPlugin
},
rules: {
...baseRules,
...typescriptRules,
'no-restricted-exports': 'off',
},
files: ['*.config.ts', 'config.ts'],
},
]
export default rootEslintConfig

View File

@@ -0,0 +1,40 @@
{
"name": "@payloadcms/eslint-config",
"version": "1.1.1",
"description": "Payload styles for ESLint and Prettier",
"keywords": [],
"homepage": "https://payloadcms.com",
"repository": {
"type": "git",
"url": "https://github.com/payloadcms/payload.git",
"directory": "packages/eslint-config"
},
"license": "MIT",
"author": "Payload <dev@payloadcms.com> (https://payloadcms.com)",
"type": "module",
"main": "index.mjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@eslint/compat": "1.1.0",
"@eslint/js": "9.6.0",
"@payloadcms/eslint-plugin": "workspace:*",
"@types/eslint": "8.56.10",
"@types/eslint__js": "8.42.3",
"@typescript-eslint/parser": "7.15.0",
"eslint": "9.6.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import-x": "0.5.3",
"eslint-plugin-jest": "28.6.0",
"eslint-plugin-jest-dom": "5.4.0",
"eslint-plugin-jsx-a11y": "6.9.0",
"eslint-plugin-perfectionist": "2.11.0",
"eslint-plugin-react": "7.34.3",
"eslint-plugin-react-hooks": "5.1.0-rc-f38c22b244-20240704",
"eslint-plugin-regexp": "2.6.0",
"globals": "15.8.0",
"typescript": "5.5.3",
"typescript-eslint": "7.15.0"
}
}