Skip to content

Commit

Permalink
Merge pull request #633 from devoxa/update-rules
Browse files Browse the repository at this point in the history
Update rules for new major version
  • Loading branch information
kodiakhq[bot] authored Dec 1, 2024
2 parents 98e560c + ca247fa commit f6427fc
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
43 changes: 43 additions & 0 deletions eslint-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const tsEslint = require('typescript-eslint')
const parseGitignore = require('parse-gitignore')
const eslintConfigPrettier = require('eslint-config-prettier')
const eslintPluginReact = require('eslint-plugin-react')
const eslintPluginStylistic = require('@stylistic/eslint-plugin')

module.exports = function (options) {
const { ignoreFiles = [], configs = [] } = options
Expand All @@ -30,6 +31,15 @@ module.exports = function (options) {
eslintPluginReact.configs.flat.recommended,
{ settings: { react: { version: 'detect' } } },

// Custom stylistic rules that Prettier doesn't handle
{
plugins: { '@stylistic': eslintPluginStylistic },
rules: {
// Require single quotes for strings, except when using templating or to prevent escaping
'@stylistic/quotes': ['error', 'single', { avoidEscape: true }],
},
},

// Disable rules that conflict with Prettier
eslintConfigPrettier,

Expand All @@ -51,6 +61,16 @@ module.exports = function (options) {
// Make sure we are awaiting or explicitly voiding all Promises
'@typescript-eslint/no-floating-promises': 'error',

// Don't check void returns for JSX attributes, because it's a common React pattern
// (e.g. `onClick={() => returnsPromiseVoid()}`)
'@typescript-eslint/no-misused-promises': [
'error',
{ checksVoidReturn: { attributes: false } },
],

// Require functions to define their return types for clearer contracts
'@typescript-eslint/explicit-function-return-type': 'error',

// Allow using functions that are defined later in the file (fine thanks to hoisting)
'@typescript-eslint/no-use-before-define': [
'error',
Expand All @@ -63,6 +83,9 @@ module.exports = function (options) {
// Disable validation of prop types, because we use TS instead and it gets confused
'react/prop-types': 'off',

// Make sure we're not using any deprecated APIs
'@typescript-eslint/no-deprecated': 'error',

// Make sure comments are starting with an uppercase letter, to encourage correct grammar
'capitalized-comments': [
'warn',
Expand All @@ -78,10 +101,30 @@ module.exports = function (options) {
selector: 'TSEnumDeclaration',
message: 'Unexpected enum. Use a literal string union or a const object instead.',
},
// Don't allow the `ReturnType<T>` generic, since this is nearly always a bad pattern and
// indicates that something is missing proper typing
{
selector: "TSTypeReference[typeName.name='ReturnType']",
message: "Unexpected 'ReturnType<T>'. Use an explicit type instead.",
},
],
},
},

// Custom rule overrides for test files
{
files: ['**/*.spec.ts', '**/*.spec.tsx'],
rules: {
// Allow `any` type usage in tests (for less frustrating mocking of complex types)
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
},
},

// Any additional configs that are passed in
...configs
)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"prettier": "@devoxa/prettier-config",
"dependencies": {
"@eslint/js": "9.16.0",
"@stylistic/eslint-plugin": "2.11.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-react": "7.37.2",
"parse-gitignore": "2.0.0",
Expand Down
10 changes: 10 additions & 0 deletions tests/basicFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ interface Foo {
}

export const foo: Foo = { bar: 1 }

export function stringChecks(): void {
console.log('This should be fine')
console.log("This should be 'fine'")
console.log(`This should be fine ${1 + 1}`)

console.log(`This should be an error`) // eslint-disable-line @stylistic/quotes
console.log(`This should be an "error"`) // eslint-disable-line @stylistic/quotes
console.log(`This should be an 'error'`) // eslint-disable-line @stylistic/quotes
}
4 changes: 2 additions & 2 deletions tests/reactFile.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import React, { ReactElement } from 'react'

// This is a Button component
export const Button = () => {
export const Button = (): ReactElement => {
return <div>Foo Bar</div>
}
18 changes: 17 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@
"@nodelib/fs.scandir" "2.1.4"
fastq "^1.6.0"

"@stylistic/[email protected]":
version "2.11.0"
resolved "https://registry.yarnpkg.com/@stylistic/eslint-plugin/-/eslint-plugin-2.11.0.tgz#50d0289f36f7201055b7fa1729fdc1d8c46e93fa"
integrity sha512-PNRHbydNG5EH8NK4c+izdJlxajIR6GxcUhzsYNRsn6Myep4dsZt0qFCz3rCPnkvgO5FYibDcMqgNHUT+zvjYZw==
dependencies:
"@typescript-eslint/utils" "^8.13.0"
eslint-visitor-keys "^4.2.0"
espree "^10.3.0"
estraverse "^5.3.0"
picomatch "^4.0.2"

"@types/estree@^1.0.6":
version "1.0.6"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.6.tgz#628effeeae2064a1b4e79f78e81d87b7e5fc7b50"
Expand Down Expand Up @@ -226,7 +237,7 @@
semver "^7.6.0"
ts-api-utils "^1.3.0"

"@typescript-eslint/[email protected]":
"@typescript-eslint/[email protected]", "@typescript-eslint/utils@^8.13.0":
version "8.16.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-8.16.0.tgz#c71264c437157feaa97842809836254a6fc833c3"
integrity sha512-C1zRy/mOL8Pj157GiX4kaw7iyRLKfJXBR3L82hk5kS/GyHcOFmy4YUq/zfZti72I9wnuQtA/+xzft4wCC8PJdA==
Expand Down Expand Up @@ -1813,6 +1824,11 @@ picomatch@^2.3.1:
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==

picomatch@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-4.0.2.tgz#77c742931e8f3b8820946c76cd0c1f13730d1dab"
integrity sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==

possible-typed-array-names@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/possible-typed-array-names/-/possible-typed-array-names-1.0.0.tgz#89bb63c6fada2c3e90adc4a647beeeb39cc7bf8f"
Expand Down

0 comments on commit f6427fc

Please sign in to comment.