Skip to content

Commit

Permalink
chore: implement ESLint rulset as a package in the workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-sanderson committed Nov 1, 2024
1 parent 63384f5 commit d95ea66
Show file tree
Hide file tree
Showing 47 changed files with 325 additions and 594 deletions.
192 changes: 2 additions & 190 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,191 +1,3 @@
import path from 'path';
import { fileURLToPath } from 'url';
import { eslint } from '@trezor/eslint';

import globals from 'globals';
import pluginJs from '@eslint/js';
import tseslint from 'typescript-eslint';
import pluginReact from 'eslint-plugin-react';
import pluginJest from 'eslint-plugin-jest';
import pluginReactHooks from 'eslint-plugin-react-hooks';
import pluginImport from 'eslint-plugin-import';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default [
{
ignores: [
'**/.nx/*',
'**/lib/*',
'**/libDev/*',
'**/dist/*',
'**/coverage/*',
'**/build/*',
'**/build-electron/*',
'**/node_modules/*',
'**/public/*',
'packages/suite-data/files/*',
'packages/protobuf/scripts/protobuf-patches/*',
'packages/address-validator',
'packages/connect-examples',
'**/ci/',
'eslint-local-rules/*',
],
},
{ files: ['**/*.{js,mjs,cjs,ts,jsx,tsx}'] },
{ languageOptions: { globals: globals.browser } },
{
languageOptions: {
globals: {
...globals.serviceworker,
...globals.browser,
},
},
},

// JS
pluginJs.configs.recommended,
{
rules: {
// Offs
'no-undef': 'off', // Todo: write description

// Additional rules
'no-console': ['error', { allow: ['warn', 'error'] }],
'require-await': ['error'],
},
},
{
files: ['**/*.js'], // Usually config files
rules: {
'no-console': 'off',
},
},

// React
pluginReact.configs.flat.recommended,
{
languageOptions: {
...pluginReact.configs.flat.recommended.languageOptions,
},
settings: { react: { version: 'detect' } },
rules: {
// Offs
'react/react-in-jsx-scope': 'off', // We are not importing React in every file
'react/prop-types': 'off', // This rule is not needed when using TypeScript
'react/display-name': 'off', // This is annoying for stuff like `forwardRef`. Todo: reconsider
'no-prototype-builtins': 'off', // Todo: just temporary, reconsider to remove it
},
},

// React Hooks
{
plugins: { 'react-hooks': pluginReactHooks },
rules: pluginReactHooks.configs.recommended.rules,
},

// Typescript
...tseslint.configs.recommended,
{
rules: {
// Offs
'@typescript-eslint/no-require-imports': 'off', // We just use require a lot (mostly for dynamic imports)
'@typescript-eslint/no-explicit-any': 'off', // Todo: write description
'@typescript-eslint/ban-ts-comment': 'off', // Todo: just temporary, reconsider to remove it
'@typescript-eslint/no-empty-object-type': 'off', // Todo: we shall solve this, this is bad practice

// Additional rules
'@typescript-eslint/no-unused-vars': [
'error',
{
vars: 'all',
args: 'none',
ignoreRestSiblings: true,
varsIgnorePattern: '^_',
},
],
'@typescript-eslint/no-shadow': [
'off',
{
builtinGlobals: false,
allow: ['_', 'error', 'resolve', 'reject', 'fetch'],
},
],
'@typescript-eslint/no-use-before-define': ['error'],
},
},

// Import
pluginImport.flatConfigs.recommended,
{
settings: {
'import/ignore': ['node_modules', '\\.(coffee|scss|css|less|hbs|svg|json)$'],
'import/resolver': {
node: {
paths: [path.resolve(__dirname, 'eslint-rules')],
},
},
},
rules: {
// Offs
'import/no-unresolved': 'off', // Does not work with Babel react-native to react-native-web
'import/no-default-export': 'error',
'import/no-anonymous-default-export': [
'error',
{
allowArray: true,
allowLiteral: true,
allowObject: true,
},
],
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: [
'**/*fixtures*/**',
'**/*.test.{tsx,ts,js}',
'**/blockchain-link/tests/**',
'**/blockchain-link/webpack/**',
'**/suite-desktop-core/**',
'**/*e2e/**',
'**/suite/src/support/tests/**',
'**/suite-data/**',
'**/*.stories.*',
'**/*webpack.config*',
'**/webpack/**',
],

includeTypes: true,
},
],
},
},

// Tests
pluginJest.configs['flat/recommended'],
{
rules: {
'jest/valid-title': 'off', // This rule does not use Typescript and produces false positives
'jest/valid-describe-callback': 'off', // This rule does not use Typescript and produces false positives
'jest/no-disabled-tests': 'off', // Well, what can I say... ¯\_(ツ)_/¯ We skip tests sometimes.
'jest/no-focused-tests': 'off', // Same as above, but // Todo: shall be easy to get rid of this
'jest/no-conditional-expect': 'off', // Todo: we shall solve this, this is bad practice
'jest/expect-expect': 'off', // Todo: we have test with no assertions, this may be legit but it needs to be checked
},
},
{
files: ['**/__fixtures__/**/*'],
rules: {
'import/no-default-export': 'off', // Todo: we have many default exports in fixtures, we shall get rid of them
},
},

// ESLint config
{
files: ['**/eslint.config.mjs'],
rules: {
'import/no-default-export': 'off',
'import/no-extraneous-dependencies': 'off',
},
},
];
export default [...eslint];
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,15 @@
"@babel/preset-react": "^7.24.7",
"@babel/preset-typescript": "^7.24.7",
"@babel/runtime": "^7.23.9",
"@eslint/js": "^9.13.0",
"@suite-common/wallet-types": "workspace:*",
"@trezor/eslint": "workspace:*",
"@types/jest": "29.5.12",
"@types/node": "20.12.7",
"@types/prettier": "^3.0.0",
"@types/semver": "^7.5.6",
"@types/tar": "^6.1.11",
"babel-jest": "29.7.0",
"depcheck": "^1.4.7",
"eslint": "^9.13.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-jest": "^28.8.3",
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-hooks": "^5.0.0",
"globals": "^15.11.0",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
Expand All @@ -150,7 +145,6 @@
"tslib": "^2.6.2",
"tsx": "^4.16.3",
"typescript": "5.5.4",
"typescript-eslint": "^8.12.2",
"version-bump-prompt": "^6.1.0"
},
"dependenciesMeta": {
Expand Down
4 changes: 2 additions & 2 deletions packages/blockchain-link-utils/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parentConfig from '../../eslint.config.mjs';
import { eslint } from '@trezor/eslint';

export default [
...parentConfig,
...eslint,
{
rules: {
'import/no-extraneous-dependencies': [
Expand Down
1 change: 1 addition & 0 deletions packages/blockchain-link-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"devDependencies": {
"@trezor/blockchain-link-types": "workspace:*",
"@trezor/eslint": "workspace:*",
"@trezor/type-utils": "workspace:*",
"tsx": "^4.16.3"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/blockchain-link/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parentConfig from '../../eslint.config.mjs';
import { eslint } from '@trezor/eslint';

export default [
...parentConfig,
...eslint,
{
rules: {
camelcase: 'off',
Expand Down
1 change: 1 addition & 0 deletions packages/blockchain-link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
},
"devDependencies": {
"@trezor/e2e-utils": "workspace:*",
"@trezor/eslint": "workspace:*",
"@trezor/type-utils": "workspace:*",
"fs-extra": "^11.2.0",
"html-webpack-plugin": "^5.6.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/coinjoin/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parentConfig from '../../eslint.config.mjs';
import { eslint } from '@trezor/eslint';

export default [
...parentConfig,
...eslint,
{
rules: {
'no-bitwise': 'off',
Expand Down
1 change: 1 addition & 0 deletions packages/coinjoin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@trezor/blockchain-link": "workspace:*",
"@trezor/blockchain-link-types": "workspace:*",
"@trezor/blockchain-link-utils": "workspace:*",
"@trezor/eslint": "workspace:*",
"@trezor/utils": "workspace:*",
"@trezor/utxo-lib": "workspace:*",
"cross-fetch": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Todo: figure out the better way, if this would be package, we would use "@trezor/eslint": "workspace:*" in package.json
import parentConfig from '../../../eslint.config.mjs';

export default [
Expand Down
4 changes: 2 additions & 2 deletions packages/connect-explorer/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as mdx from 'eslint-plugin-mdx';
import parentConfig from '../../eslint.config.mjs';
import { eslint } from '@trezor/eslint';

export default [
...parentConfig,
...eslint,
// Mdx
{
...mdx.flat,
Expand Down
1 change: 1 addition & 0 deletions packages/connect-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"swr": "^2.2.5"
},
"devDependencies": {
"@trezor/eslint": "workspace:*",
"@types/redux-logger": "^3.0.11",
"babel-plugin-styled-components": "^2.1.4",
"concurrently": "^8.2.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/connect-web/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parentConfig from '../../eslint.config.mjs';
import { eslint } from '@trezor/eslint';

export default [
...parentConfig,
...eslint,
{
rules: {
'no-underscore-dangle': 'off', // underscore is used
Expand Down
1 change: 1 addition & 0 deletions packages/connect-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@playwright/browser-firefox": "^1.46.1",
"@playwright/browser-webkit": "^1.46.1",
"@playwright/test": "^1.46.1",
"@trezor/eslint": "workspace:*",
"@types/chrome": "^0.0.278",
"@types/w3c-web-usb": "^1.0.10",
"babel-loader": "^9.1.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/connect-webextension/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parentConfig from '../../eslint.config.mjs';
import { eslint } from '@trezor/eslint';

export default [
...parentConfig,
...eslint,
{
rules: {
'no-underscore-dangle': 'off', // underscore is used
Expand Down
1 change: 1 addition & 0 deletions packages/connect-webextension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"devDependencies": {
"@babel/preset-typescript": "^7.24.7",
"@trezor/eslint": "workspace:*",
"@trezor/node-utils": "workspace:*",
"@trezor/trezor-user-env-link": "workspace:*",
"@types/chrome": "^0.0.278",
Expand Down
4 changes: 2 additions & 2 deletions packages/connect/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parentConfig from '../../eslint.config.mjs';
import { eslint } from '@trezor/eslint';

export default [
...parentConfig,
...eslint,
{
rules: {
'no-bitwise': 'off', // airbnb-base: used in hardending
Expand Down
1 change: 1 addition & 0 deletions packages/connect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"cross-fetch": "^4.0.0"
},
"devDependencies": {
"@trezor/eslint": "workspace:*",
"@trezor/trezor-user-env-link": "workspace:*",
"@types/karma": "^6.3.8",
"babel-loader": "^9.1.3",
Expand Down
22 changes: 22 additions & 0 deletions packages/eslint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@trezor/eslint",
"version": "1.0.0",
"license": "See LICENSE.md in repo root",
"private": true,
"sideEffects": false,
"main": "src/index.mjs",
"scripts": {
"depcheck": "yarn g:depcheck",
"type-check": "yarn g:tsc --build",
"lint:js": "yarn g:eslint '**/*.{ts,tsx,js}'"
},
"dependencies": {
"@eslint/js": "^9.13.0",
"eslint": "^9.13.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-jest": "^28.8.3",
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-hooks": "^5.0.0",
"typescript-eslint": "^8.12.2"
}
}
Loading

0 comments on commit d95ea66

Please sign in to comment.