Skip to content

Commit

Permalink
feat: new eslint-config package
Browse files Browse the repository at this point in the history
  • Loading branch information
noahnu committed Mar 31, 2024
1 parent 4c2df9b commit 0cf6a70
Show file tree
Hide file tree
Showing 11 changed files with 890 additions and 561 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@
"@tophat/jest-resolver": "^0.1.0",
"@types/jest": "^29.5.8",
"@types/node": "^20",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.18.1",
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"@yarnpkg/core": "^4.0.3",
"@yarnpkg/sdks": "^3.1.0",
"@yarnpkg/types": "^4.0.0",
"commitizen": "^4.3.0",
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-node": "^0.3.9",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-jest": "^27.6.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^27.9.0",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.6.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
Expand Down
15 changes: 15 additions & 0 deletions packages/eslint-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @noahnu/eslint-config

## Usage

In your `.eslintrc.js` file:

```js
module.exports = {
extends: '@noahnu/eslint-config',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
};
```
89 changes: 89 additions & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"name": "@noahnu/eslint-config",
"version": "0.0.0",
"repository": {
"type": "git",
"url": "https://github.com/noahnu/nodejs-tools.git",
"directory": "packages/eslint-config"
},
"license": "MIT",
"author": {
"name": "noahnu",
"email": "[email protected]",
"url": "https://noahnu.com"
},
"scripts": {
"clean": "run workspace:clean \"$(pwd)\"",
"prepack": "run workspace:build \"$(pwd)\"",
"run-local": "run -T ts-node --transpileOnly ./src/bin.ts"
},
"main": "./src/index.ts",
"exports": {
".": {
"default": "./src/index.ts"
},
"./base": {
"default": "./src/base/index.ts"
},
"./jest": {
"default": "./src/jest/index.ts"
}
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public",
"bin": "./lib/bin.js",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"exports": {
".": {
"default": "./lib/index.js"
},
"./base": {
"default": "./lib/base/index.js"
},
"./jest": {
"default": "./lib/jest/index.js"
}
}
},
"files": [
"lib"
],
"devDependencies": {
"@types/debug": "^4.1.12",
"@types/eslint": "^8.56.6",
"@types/jest": "^29.5.12",
"@types/node": "^18.15.11",
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"@typescript-eslint/utils": "^7.4.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-node": "^0.3.9",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^27.9.0",
"eslint-plugin-prettier": "^5.1.3",
"prettier": "^3.0.3"
},
"dependencies": {
"@noahnu/eslint-import-resolver-require": "workspace:*",
"debug": "^4.3.4",
"eslint-import-resolver-typescript": "^3.6.1"
},
"peerDependencies": {
"@noahnu/eslint-import-resolver-require": "workspace:*",
"@typescript-eslint/eslint-plugin": ">=7.4.0",
"@typescript-eslint/parser": ">=7.4.0",
"eslint": ">=8.57.0",
"eslint-config-prettier": ">=9.1.0",
"eslint-import-resolver-node": ">=0.3.9",
"eslint-import-resolver-typescript": ">=3.6.1",
"eslint-plugin-import": ">=2.29.1",
"eslint-plugin-jest": ">=27.9.0",
"eslint-plugin-prettier": ">=5.1.3",
"prettier": ">=3.2.5",
"typescript": ">=5.4.3"
}
}
181 changes: 181 additions & 0 deletions packages/eslint-config/src/base/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { type ESLint } from 'eslint'

const rules: ESLint.ConfigData['rules'] = {
/* Prettier Overrides */
'prettier/prettier': [
'error',
{
printWidth: 100,
tabWidth: 4,
semi: false,
trailingComma: 'all' /* Reduces git diff. */,
singleQuote: true,
arrowParens: 'always', // Reduces character diff when adding Typescript types.
},
],

/* Variables */
'no-var': 'error',
'prefer-const': 'error',
'no-unused-vars': 'off', // Prefer equivalent rule from @typescript-eslint */
'@typescript-eslint/no-unused-vars': [
'error',
{
/* By ignoring rest siblings, we support using `...rest` as an alternative to
* a 3rd party omit function, to omit keys from an object. */
ignoreRestSiblings: true,
// The following allows us to maintain args for type purposes without needing to use them,
// for example, a reducer in which the action is typed but we don't use the action in the
// reducer
argsIgnorePattern: '^_',
// The alternative to this is to use empty array syntax, such as const [, , thing] = someArr
// this syntax lets you at least name the unused parameters which reads a bit nicer, i.e.
// const [_first, _second, third] = someArr
destructuredArrayIgnorePattern: '^_',
},
],

/* Strings */
quotes: ['error', 'single', { avoidEscape: true }],
'prefer-template': 'error',

/* Naming Convention */
camelcase: 'off', // disabled in favour of @typescript-eslint's version
'@typescript-eslint/naming-convention': 'off', // to enable this, we require a deeper exploration of our naming patterns

/* Object Properties */
'dot-notation': 'error',
'no-useless-computed-key': 'error',
'no-import-assign': 'warn',

/* Conditionals */
eqeqeq: 'error',
'no-nested-ternary': 'error',

/* Behaviours */
/*
* Rule: Disallow empty function blocks.
* Reason Disabled: Useful for try..catch{} where we don't care about failure.
*/
'@typescript-eslint/no-empty-function': 'off',
'no-empty': ['error', { allowEmptyCatch: true }],

/* Broken Rules */
/*
* Rule: Disallow assignments that can lead to race conditions.
* Reason Disabled: The rule is broken and leads to many false positives.
* See: https://github.com/eslint/eslint/issues/11899
*/
'require-atomic-updates': 'off',

/* Imports */
'no-duplicate-imports': 'error',
'sort-imports': [
'error',
{
/* Prefer import/order declaration sort due to autofixer */
ignoreDeclarationSort: true,
},
],
'import/default': 'error',
'import/export': 'error',
'import/named': 'error',
'import/newline-after-import': 'error',
'import/no-absolute-path': 'error',
'import/no-duplicates': 'error',
'import/no-mutable-exports': 'error',
'import/no-self-import': 'error',
'import/no-useless-path-segments': 'error',
'import/no-unresolved': 'error',
'import/order': [
'error',
{
alphabetize: { order: 'asc' },
'newlines-between': 'always',
groups: ['unknown', 'builtin', 'external', 'internal', 'parent', 'sibling', 'index'],
},
],

/* Typescript Types */
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/ban-ts-comment': 'warn', // Discourage disabling static analysis.
'@typescript-eslint/ban-types': 'warn', // Discourage disabling static analysis.
'@typescript-eslint/consistent-type-imports': [
'error',
{ prefer: 'type-imports', fixStyle: 'inline-type-imports' },
],
'@typescript-eslint/no-duplicate-enum-values': 'error',
'@typescript-eslint/no-for-in-array': 'error',
'@typescript-eslint/no-non-null-asserted-nullish-coalescing': 'error',
'@typescript-eslint/prefer-includes': 'error',
'@typescript-eslint/prefer-for-of': 'error',
'@typescript-eslint/prefer-optional-chain': 'error',
'@typescript-eslint/prefer-ts-expect-error': 'error',

// no-use-before-define can cause errors with typescript concepts, like types or enums
'no-use-before-define': 'off',
// functions can be called before they are defined because function declarations are hoisted
'@typescript-eslint/no-use-before-define': ['error', { functions: false }],
}

const jsIncompatibleRules: ESLint.ConfigData['rules'] = {
/*
* Rule: Disallow require statements in favour of import statements.
* Reason Disabled: We don't know if JS files are transpiled, so don't bother enforcing TS import syntax.
*/
'@typescript-eslint/no-var-requires': 'off',
}

const config: ESLint.ConfigData = {
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: ['import', 'prettier', '@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/stylistic',
// prettier must be the last item in this list to prevent conflicts
'prettier',
],
env: {
node: true,
es2024: true,
},
rules,
overrides: [
{
files: ['**/*.js', '**/*.cjs', '**/*.mjs'],
rules: jsIncompatibleRules,
},
],
settings: {
'import/external-module-folders': ['node_modules', '.yarn'],
'import/parsers': {
'@typescript-eslint/parser': [
'.ts',
'.tsx',
'.mts',
'.cts',
'.js',
'.mjs',
'.cjs',
'.jsx',
],
},
'import/resolver': [
{
[require.resolve('eslint-import-resolver-typescript')]: {
alwaysTryTypes: true,
},
},
{ [require.resolve('@noahnu/eslint-import-resolver-require')]: {} },
],
},
}

export = config
14 changes: 14 additions & 0 deletions packages/eslint-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type ESLint } from 'eslint'

const config: ESLint.ConfigData = {
extends: ['@noahnu/eslint-config/base'],
rules: {},
overrides: [
{
files: ['**/*.test.*'],
extends: ['@noahnu/eslint-config/jest'],
},
],
}

export = config
12 changes: 12 additions & 0 deletions packages/eslint-config/src/jest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { type ESLint } from 'eslint'

const config: ESLint.ConfigData = {
env: {
jest: true,
},
plugins: ['jest'],
extends: ['plugin:jest/recommended', 'plugin:jest/style'],
rules: {},
}

export = config
9 changes: 9 additions & 0 deletions packages/eslint-config/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"tsBuildInfoFile": "./.tmp/.tsbuildinfo",
},
"include": ["./src"],
"exclude": ["./src/**/*.test.ts"]
}
8 changes: 8 additions & 0 deletions packages/eslint-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"declarationDir": "./lib",
"outDir": "./lib"
},
"include": ["./src"],
}
2 changes: 1 addition & 1 deletion packages/eslint-import-resolver-require/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@types/eslint": "^8.44.3",
"@types/jest": "^29.5.5",
"@types/node": "^18.15.11",
"eslint": "^8.50.0",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/unused-files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
],
"dependencies": {
"@types/debug": "^4.1.12",
"@typescript-eslint/typescript-estree": "^6.18.1",
"@typescript-eslint/typescript-estree": "^7.4.0",
"clipanion": "4.0.0-rc.2",
"debug": "^4.3.4",
"fast-glob": "^3.3.2",
Expand Down
Loading

0 comments on commit 0cf6a70

Please sign in to comment.