From 70cd644802f44fb3ee9a54964d3c4035035f2b35 Mon Sep 17 00:00:00 2001 From: "Mauro E. Bender" Date: Fri, 30 Sep 2022 16:56:12 +0200 Subject: [PATCH] Fix ESLint config - Fix config so eslint checks all files in the project when run. - Add a library to check testing library common issues. - Fix typescript config issues when running eslint. --- .eslintrc.js | 174 +++++++++++++++++++++++++++------------ example/tsconfig.json | 29 +++++++ package.json | 4 + tsconfig.json | 1 + tsconfig.tests.json | 7 ++ web_example/package.json | 3 +- web_example/yarn.lock | 12 +-- yarn.lock | 59 ++++++++++++- 8 files changed, 226 insertions(+), 63 deletions(-) create mode 100644 example/tsconfig.json create mode 100644 tsconfig.tests.json diff --git a/.eslintrc.js b/.eslintrc.js index d9a63d4..06c6ee1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,5 +1,4 @@ module.exports = { - 'root': true, 'env': { 'browser': true, 'es2021': true, @@ -7,10 +6,13 @@ module.exports = { }, 'extends': [ 'airbnb-base', - 'plugin:@typescript-eslint/recommended' + 'plugin:@typescript-eslint/recommended', + 'plugin:react-hooks/recommended', + 'plugin:jest/recommended', + 'plugin:testing-library/react' ], 'parser': '@typescript-eslint/parser', - 'plugins': [ '@typescript-eslint' ], + 'plugins': [ '@typescript-eslint', 'jest', 'react', 'testing-library' ], 'parserOptions': { 'ecmaVersion': 12, 'sourceType': 'module' @@ -24,23 +26,83 @@ module.exports = { 'alwaysTryTypes': true, 'project': './tsconfig.json' } + }, + 'react': { + 'version': 'detect' } }, 'overrides': [ { - 'files': [ 'src/*.ts' ], + 'files': [ 'src/**/*.ts', 'src/**/*.tsx' ], 'parserOptions': { 'project': './tsconfig.json' }, 'excludedFiles': '*.js', 'extends': [ 'plugin:@typescript-eslint/recommended-requiring-type-checking' - ] + ], + 'rules': { + '@typescript-eslint/no-unsafe-assignment': 'off' + } + }, + { + 'files': [ 'tests/**/*.ts' ], + 'parserOptions': { + 'project': './tsconfig.tests.json' + }, + 'excludedFiles': '*.js', + 'extends': [ + 'plugin:@typescript-eslint/recommended-requiring-type-checking' + ], + 'rules': { + '@typescript-eslint/no-unsafe-assignment': 'off' + } + }, + { + 'files': [ 'example/src/**/*.ts', 'example/src/**/*.tsx' ], + 'parserOptions': { + 'project': './example/tsconfig.json' + }, + 'excludedFiles': '*.js', + 'extends': [ + 'plugin:@typescript-eslint/recommended-requiring-type-checking' + ], + 'rules': { + '@typescript-eslint/no-unsafe-assignment': 'off' + } + }, + { + 'files': [ 'web_example/src/**/*.ts', 'web_example/src/**/*.tsx' ], + 'parserOptions': { + 'project': './web_example/tsconfig.json' + }, + 'excludedFiles': '*.js', + 'extends': [ + 'plugin:@typescript-eslint/recommended-requiring-type-checking' + ], + 'rules': { + '@typescript-eslint/no-unsafe-assignment': 'off' + } + }, + { + 'files': [ 'docs/src/**/*.ts', 'docs/src/**/*.tsx' ], + 'parserOptions': { + 'project': './docs/tsconfig.json' + }, + 'excludedFiles': '*.js', + 'extends': [ + 'plugin:@typescript-eslint/recommended-requiring-type-checking' + ], + 'rules': { + '@typescript-eslint/no-unsafe-assignment': 'off', + 'react/jsx-filename-extension': [ 1, { extensions: [ '.ts', '.tsx', '.js', '.jsx' ] } ] + } }, { 'files': [ 'example/**/*', 'web_example/**/*', 'docs/**/*' ], 'rules': { - 'import/no-extraneous-dependencies': 'off' + 'import/no-extraneous-dependencies': 'off', + 'no-alert': 'off' } }, { @@ -54,16 +116,13 @@ module.exports = { { 'files': [ 'tests/**/*' ], 'rules': { - '@typescript-eslint/no-var-requires': 'off' + '@typescript-eslint/no-var-requires': 'off', + 'react/jsx-filename-extension': 'off' } } ], - 'ignorePatterns': [ 'dist/**/*' ], + 'ignorePatterns': [ 'build/**/*', 'node_modules/**/*', 'android/**/*', 'ios/**/*' ], 'rules': { - 'no-shadow': 'off', - '@typescript-eslint/no-shadow': [ 'error' ], - 'no-use-before-define': 'off', - '@typescript-eslint/no-use-before-define': [ 'error' ], 'import/extensions': [ 'error', 'ignorePackages', @@ -74,55 +133,27 @@ module.exports = { 'tsx': 'never' } ], - 'no-tabs': 'off', - 'indent': [ - 'error', - 'tab' - ], - 'comma-dangle': [ - 'error', - 'never' - ], 'import/prefer-default-export': 'off', - 'space-in-parens': [ - 'error', - 'always' - ], - 'array-bracket-spacing': [ + 'import/no-dynamic-require': 0, + 'import/no-extraneous-dependencies': [ 'error', - 'always' + { 'devDependencies': true } ], + 'import/no-named-as-default': 'off', + 'no-tabs': [ 'error', { allowIndentationTabs: true } ], + 'indent': [ 'error', 'tab' ], + 'comma-dangle': [ 'error', 'never' ], + 'space-in-parens': [ 'error', 'always' ], + 'array-bracket-spacing': [ 'error', 'always' ], 'no-underscore-dangle': [ 'error', - { - 'allowAfterThis': true - } - ], - 'computed-property-spacing': [ - 'error', - 'always' - ], - 'quote-props': [ - 'error', - 'consistent' - ], - 'import/no-dynamic-require': 0, - 'import/no-extraneous-dependencies': [ - 'error', - { - 'devDependencies': true - } + { 'allowAfterThis': true } ], + 'computed-property-spacing': [ 'error', 'always' ], + 'quote-props': [ 'error', 'consistent' ], 'prefer-const': 'off', 'no-param-reassign': 'off', - 'import/no-named-as-default': 'off', 'function-paren-newline': 'off', - 'no-restricted-imports': [ - 'error', - { - 'patterns': [ '@material-ui/*/*/*', '!@material-ui/core/test-utils/*' ] - } - ], 'arrow-parens': [ 2, 'as-needed', @@ -132,6 +163,43 @@ module.exports = { 'error', 'always', { 'exceptAfterSingleLine': true } - ] + ], + // Typescript + 'no-shadow': 'off', + '@typescript-eslint/no-shadow': [ 'error' ], + 'no-use-before-define': 'off', + '@typescript-eslint/no-use-before-define': [ 'error' ], + '@typescript-eslint/type-annotation-spacing': [ + 'error', + { + before: false, + after: true, + overrides: { arrow: { before: true, after: true } } + } + ], + '@typescript-eslint/no-unsafe-assignment': 'off', + // Jest + 'jest/expect-expect': [ + 'error', + { assertFunctionNames: [ 'expect' ] } + ], + 'jest/no-disabled-tests': 'error', + 'jest/no-focused-tests': 'error', + 'jest/no-identical-title': 'error', + 'jest/prefer-to-have-length': 'error', + 'jest/valid-expect': 'error', + // Testing library + 'testing-library/prefer-screen-queries': 'off', + // JSX + 'react/jsx-indent': 'off', + 'react/jsx-indent-props': [ 2, 'tab' ], + 'react/jsx-filename-extension': [ 1, { extensions: [ '.ts', '.tsx' ] } ], + 'react/function-component-definition': [ 2, { + namedComponents: 'arrow-function', + unnamedComponents: 'arrow-function' + } ], + 'react/jsx-props-no-spreading': 'off', + 'react/prop-types': 'off', + 'react/require-default-props': 'off' } }; diff --git a/example/tsconfig.json b/example/tsconfig.json new file mode 100644 index 0000000..c1b46dd --- /dev/null +++ b/example/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + "paths": { + "@amalgama/react-native-ui-kit": ["../src"] + }, + "jsx": "react-native", + "lib": ["esnext", "dom"], + "allowUnreachableCode": false, + "allowUnusedLabels": false, + "esModuleInterop": true, + "importsNotUsedAsValues": "error", + "forceConsistentCasingInFileNames": true, + "module": "esnext", + "moduleResolution": "node", + "noFallthroughCasesInSwitch": true, + "noImplicitReturns": true, + "noImplicitUseStrict": false, + "noStrictGenericChecks": false, + "noUnusedLocals": true, + "allowSyntheticDefaultImports": true, + "noUnusedParameters": true, + "resolveJsonModule": true, + "skipLibCheck": true, + "strict": true, + "target": "esnext" + }, + "include": ["src"], + "exclude": ["node_modules", "build"] +} diff --git a/package.json b/package.json index adf1c94..d3066ab 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "eslint-import-resolver-typescript": "^2.7.1", "eslint-plugin-import": "^2.26.0", "eslint-plugin-prettier": "^3.1.3", + "eslint-plugin-testing-library": "^5.7.0", "husky": "^6.0.0", "jest": "^26.0.1", "jest-junit": "^13.2.0", @@ -115,6 +116,9 @@ "testMatch": [ "/tests/**/?(*.)(spec|test).(js|ts|jsx|tsx)" ], + "transform": { + "^.+\\.[jt]sx?$": "babel-jest" + }, "modulePathIgnorePatterns": [ "/example/node_modules", "/dist/" diff --git a/tsconfig.json b/tsconfig.json index 663e6e3..5e3a2ae 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -26,5 +26,6 @@ "strict": true, "target": "esnext" }, + "include": [ "src" ], "exclude": ["node_modules", "tests"] } diff --git a/tsconfig.tests.json b/tsconfig.tests.json new file mode 100644 index 0000000..29251dd --- /dev/null +++ b/tsconfig.tests.json @@ -0,0 +1,7 @@ + +{ + "extends": "./tsconfig", + "include": [ "src", "tests" ], + "exclude": ["node_modules"] + } + \ No newline at end of file diff --git a/web_example/package.json b/web_example/package.json index 7847379..badd422 100644 --- a/web_example/package.json +++ b/web_example/package.json @@ -73,7 +73,8 @@ }, "resolutions": { "@types/react": "17.0.2", - "@types/react-dom": "17.0.2" + "@types/react-dom": "17.0.2", + "@types/react-native": "0.65" }, "browserslist": { "production": [ diff --git a/web_example/yarn.lock b/web_example/yarn.lock index b5a8bae..fa70032 100644 --- a/web_example/yarn.lock +++ b/web_example/yarn.lock @@ -1913,14 +1913,14 @@ "@types/react" "*" "@types/react-native" "*" -"@types/react-native@*": - version "0.69.2" - resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.69.2.tgz#cf6fe8c909093833579ff5956bc645d6e77de426" - integrity sha512-AVdWv4B8/++T5QQdnpveb2OVqD+0i8SOoT63ZRD+lNtQIYxzDak91V0k6olthy+H261d4t/MRBC1cfX1Rwsb6A== +"@types/react-native@*", "@types/react-native@0.65": + version "0.65.26" + resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.65.26.tgz#a1dc5a3a983d47eb61226a05221b2f7fd637539f" + integrity sha512-o/YpCvmLLH5uf89d9IiCXMRWEp0flIHBq7lVJq1najoF1aZgaAtmzdJhqPUcqxaetJNShnWMoajxL2dv7yjQJg== dependencies: - "@types/react" "*" + "@types/react" "^17" -"@types/react@*", "@types/react@17.0.2": +"@types/react@*", "@types/react@17.0.2", "@types/react@^17": version "17.0.2" resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.2.tgz#3de24c4efef902dd9795a49c75f760cbe4f7a5a8" integrity sha512-Xt40xQsrkdvjn1EyWe1Bc0dJLcil/9x2vAuW7ya+PuQip4UYUaXyhzWmAbwRsdMgwOFHpfp7/FFZebDU6Y8VHA== diff --git a/yarn.lock b/yarn.lock index f2b74b3..451ec04 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2127,6 +2127,14 @@ "@typescript-eslint/types" "5.23.0" "@typescript-eslint/visitor-keys" "5.23.0" +"@typescript-eslint/scope-manager@5.38.0": + version "5.38.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.38.0.tgz#8f0927024b6b24e28671352c93b393a810ab4553" + integrity sha512-ByhHIuNyKD9giwkkLqzezZ9y5bALW8VNY6xXcP+VxoH4JBDKjU5WNnsiD4HJdglHECdV+lyaxhvQjTUbRboiTA== + dependencies: + "@typescript-eslint/types" "5.38.0" + "@typescript-eslint/visitor-keys" "5.38.0" + "@typescript-eslint/type-utils@5.23.0": version "5.23.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-5.23.0.tgz#f852252f2fc27620d5bb279d8fed2a13d2e3685e" @@ -2146,6 +2154,11 @@ resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.23.0.tgz#8733de0f58ae0ed318dbdd8f09868cdbf9f9ad09" integrity sha512-NfBsV/h4dir/8mJwdZz7JFibaKC3E/QdeMEDJhiAE3/eMkoniZ7MjbEMCGXw6MZnZDMN3G9S0mH/6WUIj91dmw== +"@typescript-eslint/types@5.38.0": + version "5.38.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.38.0.tgz#8cd15825e4874354e31800dcac321d07548b8a5f" + integrity sha512-HHu4yMjJ7i3Cb+8NUuRCdOGu2VMkfmKyIJsOr9PfkBVYLYrtMCK/Ap50Rpov+iKpxDTfnqvDbuPLgBE5FwUNfA== + "@typescript-eslint/typescript-estree@3.10.1": version "3.10.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-3.10.1.tgz#fd0061cc38add4fad45136d654408569f365b853" @@ -2173,6 +2186,19 @@ semver "^7.3.5" tsutils "^3.21.0" +"@typescript-eslint/typescript-estree@5.38.0": + version "5.38.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.38.0.tgz#89f86b2279815c6fb7f57d68cf9b813f0dc25d98" + integrity sha512-6P0RuphkR+UuV7Avv7MU3hFoWaGcrgOdi8eTe1NwhMp2/GjUJoODBTRWzlHpZh6lFOaPmSvgxGlROa0Sg5Zbyg== + dependencies: + "@typescript-eslint/types" "5.38.0" + "@typescript-eslint/visitor-keys" "5.38.0" + debug "^4.3.4" + globby "^11.1.0" + is-glob "^4.0.3" + semver "^7.3.7" + tsutils "^3.21.0" + "@typescript-eslint/utils@5.23.0": version "5.23.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.23.0.tgz#4691c3d1b414da2c53d8943310df36ab1c50648a" @@ -2185,6 +2211,18 @@ eslint-scope "^5.1.1" eslint-utils "^3.0.0" +"@typescript-eslint/utils@^5.13.0": + version "5.38.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.38.0.tgz#5b31f4896471818153790700eb02ac869a1543f4" + integrity sha512-6sdeYaBgk9Fh7N2unEXGz+D+som2QCQGPAf1SxrkEr+Z32gMreQ0rparXTNGRRfYUWk/JzbGdcM8NSSd6oqnTA== + dependencies: + "@types/json-schema" "^7.0.9" + "@typescript-eslint/scope-manager" "5.38.0" + "@typescript-eslint/types" "5.38.0" + "@typescript-eslint/typescript-estree" "5.38.0" + eslint-scope "^5.1.1" + eslint-utils "^3.0.0" + "@typescript-eslint/visitor-keys@3.10.1": version "3.10.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-3.10.1.tgz#cd4274773e3eb63b2e870ac602274487ecd1e931" @@ -2200,6 +2238,14 @@ "@typescript-eslint/types" "5.23.0" eslint-visitor-keys "^3.0.0" +"@typescript-eslint/visitor-keys@5.38.0": + version "5.38.0" + resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.38.0.tgz#60591ca3bf78aa12b25002c0993d067c00887e34" + integrity sha512-MxnrdIyArnTi+XyFLR+kt/uNAcdOnmT+879os7qDRI+EYySR4crXJq9BXPfRzzLGq0wgxkwidrCJ9WCAoacm1w== + dependencies: + "@typescript-eslint/types" "5.38.0" + eslint-visitor-keys "^3.3.0" + JSONStream@^1.0.4: version "1.3.5" resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0" @@ -4124,6 +4170,13 @@ eslint-plugin-react@^7.20.0: semver "^6.3.0" string.prototype.matchall "^4.0.6" +eslint-plugin-testing-library@^5.7.0: + version "5.7.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-testing-library/-/eslint-plugin-testing-library-5.7.0.tgz#08046bce93c63c0d4bf01ec62550130237bb1575" + integrity sha512-pI8LKtFiAflBpN4h14vTtfhKqLwtIW40TNhWyw0ckqHm0W/J0VmYtThoxpTAdHrvEWnkALSG1Z8ABBkIncMIHA== + dependencies: + "@typescript-eslint/utils" "^5.13.0" + eslint-scope@^5.0.0, eslint-scope@^5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c" @@ -4156,7 +4209,7 @@ eslint-visitor-keys@^2.0.0: resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.1.0.tgz#f65328259305927392c938ed44eb0a5c9b2bd303" integrity sha512-0rSmRBzXgDzIsD6mGdJgevzgezI534Cer5L/vyMX0kHzT/jiB43jRhd9YUlMGYLQy2zprNmoT8qasCGtY+QaKw== -eslint-visitor-keys@^3.0.0: +eslint-visitor-keys@^3.0.0, eslint-visitor-keys@^3.3.0: version "3.3.0" resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-3.3.0.tgz#f6480fa6b1f30efe2d1968aa8ac745b862469826" integrity sha512-mQ+suqKJVyeuwGYHAdjMFqjCyfl8+Ldnxuyp3ldiMBFKkvytrXUZWaiPCEav8qDHKty44bD+qV1IP4T+w+xXRA== @@ -4835,7 +4888,7 @@ globby@11.0.4: merge2 "^1.3.0" slash "^3.0.0" -globby@^11.0.1, globby@^11.0.4: +globby@^11.0.1, globby@^11.0.4, globby@^11.1.0: version "11.1.0" resolved "https://registry.yarnpkg.com/globby/-/globby-11.1.0.tgz#bd4be98bb042f83d796f7e3811991fbe82a0d34b" integrity sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g== @@ -8433,7 +8486,7 @@ semver@^6.0.0, semver@^6.1.1, semver@^6.1.2, semver@^6.2.0, semver@^6.3.0: resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d" integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw== -semver@^7.2.1, semver@^7.3.2, semver@^7.3.4, semver@^7.3.5: +semver@^7.2.1, semver@^7.3.2, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7: version "7.3.7" resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f" integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==