From dcad5ac591869ddb692a9b2596b777382bcb0311 Mon Sep 17 00:00:00 2001 From: devgioele Date: Fri, 14 Jul 2023 22:39:44 +0200 Subject: [PATCH 1/5] upgrade tsconfig and fix new errors --- package.json | 10 ++++++---- src/nextRouterPagination.tsx | 3 +-- src/reactRouterPagination.tsx | 2 +- tsconfig.cjs.json | 13 ++++++++++++ tsconfig.esm.json | 13 ++++++++++++ tsconfig.esnext.json | 16 --------------- tsconfig.json | 37 +++++++++++++++++++++++------------ tsconfig.types.json | 20 +++++++++++++++++++ 8 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 tsconfig.cjs.json create mode 100644 tsconfig.esm.json delete mode 100644 tsconfig.esnext.json create mode 100644 tsconfig.types.json diff --git a/package.json b/package.json index c792573..0aa0284 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,10 @@ "node": "^16" }, "scripts": { - "build": "npm run build:node && npm run build:esm", - "build:node": "tsc", - "build:esm": "tsc --project tsconfig.esnext.json", + "build": "rimraf dist && npm run build:types && npm run build:esm && npm run build:cjs", + "build:esm": "tsc -p tsconfig.esm.json", + "build:cjs": "tsc -p tsconfig.cjs.json", + "build:types": "tsc -p tsconfig.types.json", "lint": "eslint --ext js,ts,tsx src", "lint:fix": "npm run lint -- --fix", "typecheck": "tsc --noEmit", @@ -65,8 +66,9 @@ "react": "^18.0.0", "react-dom": "^18.0.0", "react-router-dom": "^6.7.0", + "rimraf": "^5.0.1", "ts-jest": "^28.0.5", - "typescript": "^4.7.4" + "typescript": "^5.1.6" }, "peerDependencies": { "next": "^12.0.0 || ^13.0.0", diff --git a/src/nextRouterPagination.tsx b/src/nextRouterPagination.tsx index 0c36b77..6cc551c 100644 --- a/src/nextRouterPagination.tsx +++ b/src/nextRouterPagination.tsx @@ -1,7 +1,6 @@ import { ParsedUrlQuery } from 'querystring' import { useRouter } from 'next/router' import { useCallback } from 'react' - import { IndexType, IUseQueryAndPagination, QueryParameters } from './types' import { convert } from './utils' @@ -64,7 +63,7 @@ export const useQueryAndPagination: IUseQueryAndPagination = function (config) { ) { delete params[parameter] } else { - params[parameter] = queryParameters[parameter].toString() + params[parameter] = queryParameters[parameter]?.toString() } } diff --git a/src/reactRouterPagination.tsx b/src/reactRouterPagination.tsx index c2e800d..9ad96bc 100644 --- a/src/reactRouterPagination.tsx +++ b/src/reactRouterPagination.tsx @@ -42,7 +42,7 @@ export const useQueryAndPagination: IUseQueryAndPagination = function (config) { ) { params.delete(parameter) } else { - params.set(parameter, queryParameters[parameter].toString()) + params.set(parameter, queryParameters[parameter] as string) } } diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..07f9e19 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,13 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "./dist/cjs", + "declaration": false, + "declarationMap": false, + "sourceMap": false, + "removeComments": true + }, + "exclude": ["node_modules", "**/__test__/*", "**/__tests__/*"], + "include": ["src"] +} diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..d6a606e --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,13 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "es2020", + "outDir": "./dist/esm", + "declaration": false, + "declarationMap": false, + "sourceMap": false, + "removeComments": true + }, + "exclude": ["node_modules", "**/__test__/*", "**/__tests__/*"], + "include": ["src"] +} diff --git a/tsconfig.esnext.json b/tsconfig.esnext.json deleted file mode 100644 index 074e202..0000000 --- a/tsconfig.esnext.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "compilerOptions": { - "module": "esnext", - "target": "es5", - "jsx": "react", - "moduleResolution": "Node", - "allowSyntheticDefaultImports": true, - "esModuleInterop": true, - "outDir": "./dist/esm", - "declaration": true, - "strict": true, - "skipLibCheck": true - }, - "include": ["src"], - "exclude": ["node_modules", "**/__test__/*", "**/__tests__/*"] -} diff --git a/tsconfig.json b/tsconfig.json index fc22be6..4fc47fe 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,27 @@ { - "compilerOptions": { - "target": "es5", - "jsx": "react", - "esModuleInterop": true, - "outDir": "./dist", - "declaration": true, - "strict": true, - "sourceMap": true, - "skipLibCheck": true - }, - "include": ["src"], - "exclude": ["node_modules", "**/__test__/*", "**/__tests__/*"] + "compilerOptions": { + "target": "es2020", + "moduleResolution": "node", + "esModuleInterop": true, + "resolveJsonModule": true, + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noImplicitOverride": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedIndexedAccess": true, + "forceConsistentCasingInFileNames": true, + "allowUnreachableCode": false, + "downlevelIteration": true, + "isolatedModules": true, + "jsx": "react-jsx", + "importHelpers": true, + "outDir": "./dist", + "declaration": true, + "sourceMap": true, + "skipLibCheck": true + }, + "include": ["src"], + "exclude": ["node_modules", "**/__test__/*", "**/__tests__/*"] } diff --git a/tsconfig.types.json b/tsconfig.types.json new file mode 100644 index 0000000..5185a65 --- /dev/null +++ b/tsconfig.types.json @@ -0,0 +1,20 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "emitDeclarationOnly": true, + "outDir": "./dist/types", + "declaration": true, + "declarationMap": true, + "removeComments": false + }, + "exclude": [ + "src/vitest.ts", + "**/__test__/*", + "**/__tests__/*", + "**/*.stories.tsx", + "**/*.stories.ts", + "src/examples/*", + ".storybook/*" + ], + "include": ["src"] +} From 7511dc16deb23da5c4daa2f68ea9bddd852e0ab0 Mon Sep 17 00:00:00 2001 From: devgioele Date: Sun, 16 Jul 2023 16:31:45 +0200 Subject: [PATCH 2/5] do not make typescript find unused symbols --- tsconfig.json | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 4fc47fe..27e4d23 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,8 +5,6 @@ "esModuleInterop": true, "resolveJsonModule": true, "strict": true, - "noUnusedLocals": true, - "noUnusedParameters": true, "noImplicitReturns": true, "noImplicitOverride": true, "noFallthroughCasesInSwitch": true, @@ -22,6 +20,12 @@ "sourceMap": true, "skipLibCheck": true }, - "include": ["src"], - "exclude": ["node_modules", "**/__test__/*", "**/__tests__/*"] + "include": [ + "src" + ], + "exclude": [ + "node_modules", + "**/__test__/*", + "**/__tests__/*" + ] } From 2788a0eabefee99d56caab1cb7c19c6b6b65e25a Mon Sep 17 00:00:00 2001 From: devgioele Date: Sun, 16 Jul 2023 16:32:51 +0200 Subject: [PATCH 3/5] update eslint config to be more comprehensive --- .eslintrc.json | 82 ++++++++++++++++++++++++++++++-------------------- package.json | 2 ++ 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 8c47821..dff6579 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,36 +1,54 @@ { - "parser": "@typescript-eslint/parser", - "env": { - "browser": true, - "node": true - }, - "plugins": [ - "prettier", - "@typescript-eslint", - "jest", - "import" + "parser": "@typescript-eslint/parser", + "env": { + "browser": true, + "node": true + }, + "plugins": ["@typescript-eslint", "react", "react-hooks", "jest"], + "extends": [ + "plugin:@typescript-eslint/recommended", + "plugin:import/recommended", + "plugin:import/typescript", + "plugin:jest/recommended", + "plugin:prettier/recommended" + ], + "parserOptions": { + "sourceType": "module", + "ecmaVersion": 2018 + }, + "rules": { + "react-hooks/rules-of-hooks": "error", + "react-hooks/exhaustive-deps": "error", + "@typescript-eslint/no-unused-vars": [ + "error", + { + "ignoreRestSiblings": true, + "varsIgnorePattern": "^[iI]gnored", + "argsIgnorePattern": "^_", + "caughtErrorsIgnorePattern": "^ignore" + } ], - "extends": [ - "prettier", - "plugin:prettier/recommended", - "plugin:@typescript-eslint/recommended", - "plugin:import/typescript", - "plugin:jest/recommended" + "@typescript-eslint/member-delimiter-style": "off", + "@typescript-eslint/explicit-function-return-type": "off", + "@next/next/no-img-element": "off", + "import/order": [ + "error", + { + "groups": [ + "builtin", + "external", + "unknown", + "parent", + "sibling", + "index" + ] + } ], - "parserOptions": { - "sourceType": "module", - "ecmaVersion": 2018 - }, - "rules": { - "import/namespace": "off", - "import/order": [ - "error" - ], - "no-unused-vars": "off", - "@typescript-eslint/no-unused-vars": "error", - "@typescript-eslint/member-delimiter-style": "off", - "@typescript-eslint/no-var-requires": "off", - "@typescript-eslint/no-extra-semi": "off", - "@typescript-eslint/no-explicit-any": "off" - } + "no-console": [ + "error", + { + "allow": ["warn", "error", "assert"] + } + ] + } } diff --git a/package.json b/package.json index 0aa0284..47be365 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,8 @@ "eslint-plugin-import": "^2.26.0", "eslint-plugin-jest": "^26.5.3", "eslint-plugin-prettier": "^4.1.0", + "eslint-plugin-react": "^7.32.2", + "eslint-plugin-react-hooks": "^4.6.0", "jest": "^28.1.1", "jest-environment-jsdom": "^28.1.1", "next": "^13.1.3", From 18d6c79f747410f4dfc27f3cb60be9f64772388e Mon Sep 17 00:00:00 2001 From: devgioele Date: Mon, 17 Jul 2023 08:45:31 +0200 Subject: [PATCH 4/5] make eslint ignore non-source files --- .eslintignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..f06235c --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +node_modules +dist From 23c4f6240b69ae43c822c5a6b7481cadf5126877 Mon Sep 17 00:00:00 2001 From: devgioele Date: Mon, 17 Jul 2023 08:48:37 +0200 Subject: [PATCH 5/5] fix eslint violations --- src/inMemoryPagination.tsx | 49 +++++++++++++++++++---------------- src/nextRouterPagination.tsx | 4 +-- src/reactRouterPagination.tsx | 6 ++--- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/inMemoryPagination.tsx b/src/inMemoryPagination.tsx index a31d8fd..003d84b 100644 --- a/src/inMemoryPagination.tsx +++ b/src/inMemoryPagination.tsx @@ -10,36 +10,39 @@ export const useQueryAndPagination: IUseQueryAndPagination = function (config) { size: pageSize, queryParameters: config?.defaultQueryParameters || {}, }), - [] + [firstPage, pageSize, config?.defaultQueryParameters] ) const [state, setState] = useState(initialState) - const updateQuery = useCallback((queryParameters: QueryParameters) => { - setState((currentState) => { - const updatedQueryParameters = { - ...currentState.queryParameters, - ...queryParameters, - } + const updateQuery = useCallback( + (queryParameters: QueryParameters) => { + setState((currentState) => { + const updatedQueryParameters = { + ...currentState.queryParameters, + ...queryParameters, + } - for (const parameter in queryParameters) { - if ( - !!config?.defaultQueryParameters && - (config.defaultQueryParameters[parameter] === undefined || - config.defaultQueryParameters[parameter] === - queryParameters[parameter]) - ) { - delete updatedQueryParameters[parameter] + for (const parameter in queryParameters) { + if ( + !!config?.defaultQueryParameters && + (config.defaultQueryParameters[parameter] === undefined || + config.defaultQueryParameters[parameter] === + queryParameters[parameter]) + ) { + delete updatedQueryParameters[parameter] + } } - } - return { - ...currentState, - page: 0, - queryParameters: updatedQueryParameters, - } - }) - }, []) + return { + ...currentState, + page: 0, + queryParameters: updatedQueryParameters, + } + }) + }, + [config?.defaultQueryParameters] + ) const clear = useCallback(() => { setState(() => { diff --git a/src/nextRouterPagination.tsx b/src/nextRouterPagination.tsx index 6cc551c..721e7dc 100644 --- a/src/nextRouterPagination.tsx +++ b/src/nextRouterPagination.tsx @@ -74,7 +74,7 @@ export const useQueryAndPagination: IUseQueryAndPagination = function (config) { query: params, }) }, - [router] + [router, config?.defaultQueryParameters] ) const clear = useCallback(() => { @@ -92,7 +92,7 @@ export const useQueryAndPagination: IUseQueryAndPagination = function (config) { router.push({ query: params, }) - }, [router]) + }, [router, config?.defaultQueryParameters]) return { queryParameters: extractCurrentQueryParameters( diff --git a/src/reactRouterPagination.tsx b/src/reactRouterPagination.tsx index 9ad96bc..7e78ba0 100644 --- a/src/reactRouterPagination.tsx +++ b/src/reactRouterPagination.tsx @@ -54,7 +54,7 @@ export const useQueryAndPagination: IUseQueryAndPagination = function (config) { search: params.toString(), }) }, - [navigate, params] + [navigate, params, config?.defaultQueryParameters, routerUrl] ) const clear = useCallback(() => { @@ -69,7 +69,7 @@ export const useQueryAndPagination: IUseQueryAndPagination = function (config) { pathname: routerUrl, search: params.toString(), }) - }, [navigate, params]) + }, [navigate, params, config?.defaultQueryParameters, routerUrl]) const setPage = useCallback( (page: number) => { @@ -79,7 +79,7 @@ export const useQueryAndPagination: IUseQueryAndPagination = function (config) { search: params.toString(), }) }, - [navigate, params] + [navigate, params, routerUrl] ) return {