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:
124
packages/eslint-plugin/customRules/no-flaky-assertions.js
Normal file
124
packages/eslint-plugin/customRules/no-flaky-assertions.js
Normal file
@@ -0,0 +1,124 @@
|
||||
/** @type {import('eslint').Rule.RuleModule} */
|
||||
export const rule = {
|
||||
meta: {
|
||||
type: 'problem',
|
||||
docs: {
|
||||
description:
|
||||
'Disallow non-retryable assertions in Playwright E2E tests unless they are wrapped in an expect.poll() or expect().toPass()',
|
||||
category: 'Best Practices',
|
||||
recommended: true,
|
||||
},
|
||||
schema: [],
|
||||
},
|
||||
create: function (context) {
|
||||
const nonRetryableAssertions = [
|
||||
'toBe',
|
||||
'toBeCloseTo',
|
||||
'toBeDefined',
|
||||
'toBeFalsy',
|
||||
'toBeGreaterThan',
|
||||
'toBeGreaterThanOrEqual',
|
||||
'toBeInstanceOf',
|
||||
'toBeLessThan',
|
||||
'toBeLessThanOrEqual',
|
||||
'toBeNaN',
|
||||
'toBeNull',
|
||||
'toBeTruthy',
|
||||
'toBeUndefined',
|
||||
'toContain',
|
||||
'toContainEqual',
|
||||
'toEqual',
|
||||
'toHaveLength',
|
||||
'toHaveProperty',
|
||||
'toMatch',
|
||||
'toMatchObject',
|
||||
'toStrictEqual',
|
||||
'toThrow',
|
||||
'any',
|
||||
'anything',
|
||||
'arrayContaining',
|
||||
'closeTo',
|
||||
'objectContaining',
|
||||
'stringContaining',
|
||||
'stringMatching',
|
||||
]
|
||||
|
||||
function isNonRetryableAssertion(node) {
|
||||
return (
|
||||
node.type === 'MemberExpression' &&
|
||||
node.property.type === 'Identifier' &&
|
||||
nonRetryableAssertions.includes(node.property.name)
|
||||
)
|
||||
}
|
||||
|
||||
function isExpectPollOrToPass(node) {
|
||||
if (
|
||||
node.type === 'MemberExpression' &&
|
||||
(node?.property?.name === 'poll' || node?.property?.name === 'toPass')
|
||||
) {
|
||||
return true
|
||||
}
|
||||
|
||||
return (
|
||||
node.type === 'CallExpression' &&
|
||||
node.callee.type === 'MemberExpression' &&
|
||||
((node.callee.object.type === 'CallExpression' &&
|
||||
node.callee.object.callee.type === 'MemberExpression' &&
|
||||
node.callee.object.callee.property.name === 'poll') ||
|
||||
node.callee.property.name === 'toPass')
|
||||
)
|
||||
}
|
||||
|
||||
function hasExpectPollOrToPassInChain(node) {
|
||||
let ancestor = node
|
||||
|
||||
while (ancestor) {
|
||||
if (isExpectPollOrToPass(ancestor)) {
|
||||
return true
|
||||
}
|
||||
ancestor = 'object' in ancestor ? ancestor.object : ancestor.callee
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
function hasExpectPollOrToPassInParentChain(node) {
|
||||
let ancestor = node
|
||||
|
||||
while (ancestor) {
|
||||
if (isExpectPollOrToPass(ancestor)) {
|
||||
return true
|
||||
}
|
||||
ancestor = ancestor.parent
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return {
|
||||
CallExpression(node) {
|
||||
// node.callee is MemberExpressiom
|
||||
if (isNonRetryableAssertion(node.callee)) {
|
||||
if (hasExpectPollOrToPassInChain(node.callee)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (hasExpectPollOrToPassInParentChain(node)) {
|
||||
return
|
||||
}
|
||||
|
||||
context.report({
|
||||
node: node.callee.property,
|
||||
message:
|
||||
'Non-retryable, flaky assertion used in Playwright test: "{{ assertion }}". Those need to be wrapped in expect.poll() or expect().toPass().',
|
||||
data: {
|
||||
assertion: node.callee.property.name,
|
||||
},
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export default rule
|
||||
@@ -0,0 +1,32 @@
|
||||
/** @type {import('eslint').Rule.RuleModule} */
|
||||
export const rule = {
|
||||
meta: {
|
||||
type: 'problem',
|
||||
docs: {
|
||||
description: 'Disallow imports from an exports directory',
|
||||
category: 'Best Practices',
|
||||
recommended: true,
|
||||
},
|
||||
schema: [],
|
||||
},
|
||||
create: function (context) {
|
||||
return {
|
||||
ImportDeclaration(node) {
|
||||
const importPath = node.source.value
|
||||
|
||||
// Match imports starting with any number of "../" followed by "exports/"
|
||||
const regex = /^(\.?\.\/)*exports\//
|
||||
|
||||
if (regex.test(importPath)) {
|
||||
context.report({
|
||||
node: node.source,
|
||||
message:
|
||||
'Import from relative "exports/" is not allowed. Import directly to the source instead.',
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export default rule
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Disallows imports from .jsx extensions. Auto-fixes to .js.
|
||||
*/
|
||||
|
||||
/** @type {import('eslint').Rule.RuleModule} */
|
||||
export const rule = {
|
||||
meta: {
|
||||
type: 'problem',
|
||||
docs: {
|
||||
description: 'Disallow imports from .jsx extensions',
|
||||
},
|
||||
fixable: 'code',
|
||||
schema: [],
|
||||
},
|
||||
create: function (context) {
|
||||
return {
|
||||
ImportDeclaration(node) {
|
||||
const importPath = node.source.value
|
||||
|
||||
if (!importPath.endsWith('.jsx')) return
|
||||
|
||||
context.report({
|
||||
node: node.source,
|
||||
message: 'JSX imports are invalid. Use .js instead.',
|
||||
fix: (fixer) => {
|
||||
return fixer.removeRange([node.source.range[1] - 2, node.source.range[1] - 1])
|
||||
},
|
||||
})
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export default rule
|
||||
@@ -0,0 +1,67 @@
|
||||
/** @type {import('eslint').Rule.RuleModule} */
|
||||
export const rule = {
|
||||
meta: {
|
||||
type: 'problem',
|
||||
docs: {
|
||||
description: 'Disallow non-retryable assertions in Playwright E2E tests',
|
||||
category: 'Best Practices',
|
||||
recommended: true,
|
||||
},
|
||||
schema: [],
|
||||
},
|
||||
create: function (context) {
|
||||
const nonRetryableAssertions = [
|
||||
'toBe',
|
||||
'toBeCloseTo',
|
||||
'toBeDefined',
|
||||
'toBeFalsy',
|
||||
'toBeGreaterThan',
|
||||
'toBeGreaterThanOrEqual',
|
||||
'toBeInstanceOf',
|
||||
'toBeLessThan',
|
||||
'toBeLessThanOrEqual',
|
||||
'toBeNaN',
|
||||
'toBeNull',
|
||||
'toBeTruthy',
|
||||
'toBeUndefined',
|
||||
'toContain',
|
||||
'toContainEqual',
|
||||
'toEqual',
|
||||
'toHaveLength',
|
||||
'toHaveProperty',
|
||||
'toMatch',
|
||||
'toMatchObject',
|
||||
'toStrictEqual',
|
||||
'toThrow',
|
||||
'any',
|
||||
'anything',
|
||||
'arrayContaining',
|
||||
'closeTo',
|
||||
'objectContaining',
|
||||
'stringContaining',
|
||||
'stringMatching',
|
||||
]
|
||||
|
||||
return {
|
||||
CallExpression(node) {
|
||||
if (
|
||||
node.callee.type === 'MemberExpression' &&
|
||||
//node.callee.object.name === 'expect' &&
|
||||
node.callee.property.type === 'Identifier' &&
|
||||
nonRetryableAssertions.includes(node.callee.property.name)
|
||||
) {
|
||||
context.report({
|
||||
node: node.callee.property,
|
||||
message:
|
||||
'Non-retryable, flaky assertion used in Playwright test: "{{ assertion }}". Those need to be wrapped in expect.poll or expect.toPass.',
|
||||
data: {
|
||||
assertion: node.callee.property.name,
|
||||
},
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export default rule
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Disallows imports from relative monorepo package paths.
|
||||
*
|
||||
* ie. `import { mongooseAdapter } from '../../../packages/mongoose-adapter/src'`
|
||||
*/
|
||||
|
||||
/** @type {import('eslint').Rule.RuleModule} */
|
||||
export const rule = {
|
||||
meta: {
|
||||
type: 'problem',
|
||||
docs: {
|
||||
description: 'Disallow imports from relative monorepo packages/*/src',
|
||||
category: 'Best Practices',
|
||||
recommended: true,
|
||||
},
|
||||
schema: [],
|
||||
},
|
||||
create: function (context) {
|
||||
return {
|
||||
ImportDeclaration(node) {
|
||||
const importPath = node.source.value
|
||||
|
||||
// Match imports starting with any number of "../" followed by "packages/"
|
||||
const regex = /^(\.\.\/)*packages\/[^/]+\/src/
|
||||
|
||||
if (regex.test(importPath)) {
|
||||
context.report({
|
||||
node: node.source,
|
||||
message: 'Import from relative "packages/*/src" is not allowed',
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export default rule
|
||||
37
packages/eslint-plugin/index.mjs
Normal file
37
packages/eslint-plugin/index.mjs
Normal file
@@ -0,0 +1,37 @@
|
||||
import noJsxImportStatements from './customRules/no-jsx-import-statements.js'
|
||||
import noNonRetryableAssertions from './customRules/no-non-retryable-assertions.js'
|
||||
import noRelativeMonorepoImports from './customRules/no-relative-monorepo-imports.js'
|
||||
import noImportsFromExportsDir from './customRules/no-imports-from-exports-dir.js'
|
||||
import noFlakyAssertions from './customRules/no-flaky-assertions.js'
|
||||
|
||||
|
||||
/**
|
||||
* @type {import('eslint').ESLint.Plugin}
|
||||
*/
|
||||
const index = {
|
||||
rules: {
|
||||
'no-jsx-import-statements': noJsxImportStatements,
|
||||
'no-non-retryable-assertions': noNonRetryableAssertions,
|
||||
'no-relative-monorepo-imports': noRelativeMonorepoImports,
|
||||
'no-imports-from-exports-dir': noImportsFromExportsDir,
|
||||
'no-flaky-assertions': noFlakyAssertions,
|
||||
'no-wait-function': {
|
||||
create: function (context) {
|
||||
return {
|
||||
CallExpression(node) {
|
||||
// Check if the function being called is named "wait"
|
||||
if (node.callee.name === 'wait') {
|
||||
context.report({
|
||||
node,
|
||||
message:
|
||||
'Usage of "wait" function is discouraged as it\'s flaky. Proper assertions should be used instead.',
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
export default index
|
||||
39
packages/eslint-plugin/package.json
Normal file
39
packages/eslint-plugin/package.json
Normal file
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"name": "@payloadcms/eslint-plugin",
|
||||
"version": "1.0.0",
|
||||
"description": "Payload plugins for ESLint",
|
||||
"keywords": [],
|
||||
"homepage": "https://payloadcms.com",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/payloadcms/payload.git",
|
||||
"directory": "packages/eslint-plugin"
|
||||
},
|
||||
"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",
|
||||
"@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"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user