From 8fb16da6282f4ed1d5128d5cda6180499b07a487 Mon Sep 17 00:00:00 2001 From: Pete F Date: Wed, 29 Jan 2025 14:54:18 +0000 Subject: [PATCH] Eslint 9 fixes --- .gitignore | 1 + newswires/client/.eslintignore | 2 - newswires/client/.eslintrc.cjs | 51 ------- newswires/client/eslint.config.js | 144 ++++++++++++++++++ newswires/client/package-lock.json | 21 ++- newswires/client/package.json | 6 +- newswires/client/src/Item.tsx | 2 +- newswires/client/src/catcodes-lookup.ts | 2 +- .../client/src/context/SearchContext.tsx | 1 - newswires/client/src/panda-session.ts | 8 +- 10 files changed, 172 insertions(+), 66 deletions(-) delete mode 100644 newswires/client/.eslintignore delete mode 100644 newswires/client/.eslintrc.cjs create mode 100644 newswires/client/eslint.config.js diff --git a/.gitignore b/.gitignore index 43d04f47..e51eb8b3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ !jest.config.js !jest.setup.js !.eslintrc.js +!eslint.config.js node_modules dist out diff --git a/newswires/client/.eslintignore b/newswires/client/.eslintignore deleted file mode 100644 index 06d792df..00000000 --- a/newswires/client/.eslintignore +++ /dev/null @@ -1,2 +0,0 @@ -*.css -*.svg diff --git a/newswires/client/.eslintrc.cjs b/newswires/client/.eslintrc.cjs deleted file mode 100644 index 724d535a..00000000 --- a/newswires/client/.eslintrc.cjs +++ /dev/null @@ -1,51 +0,0 @@ -module.exports = { - root: true, - parser: '@typescript-eslint/parser', - extends: [ - '@guardian/eslint-config-typescript', - 'prettier', - 'plugin:react/recommended', - 'plugin:react/jsx-runtime', - 'plugin:react-hooks/recommended', - ], - plugins: ['@typescript-eslint', 'prettier'], - env: { - browser: true, - es6: true, - node: true, - }, - overrides: [ - { - parserOptions: { - project: ['./tsconfig.json', './tsconfig.*.json'], - }, - files: ['*.ts', '*.tsx'], - }, - ], - rules: { - 'no-unused-vars': 'off', - '@typescript-eslint/no-unused-vars': [ - 'warn', - { - argsIgnorePattern: '^_', - varsIgnorePattern: '^_', - caughtErrorsIgnorePattern: '^_', - }, - ], - 'prettier/prettier': 'warn', - 'react/no-unknown-property': ['error', { ignore: ['css'] }], - 'no-restricted-syntax': [ - 'error', - { - message: - "Don't use `fetch` directly, please use the `pandaFetch` abstraction, to get automatic session refreshes", - selector: "CallExpression[callee.name='fetch']", - }, - ], - }, - settings: { - react: { - version: 'detect', - }, - }, -}; diff --git a/newswires/client/eslint.config.js b/newswires/client/eslint.config.js new file mode 100644 index 00000000..b1c77f45 --- /dev/null +++ b/newswires/client/eslint.config.js @@ -0,0 +1,144 @@ +import guardian from '@guardian/eslint-config'; + +export default [ + { + ignores: [ + 'dist', + 'jest.dist.*', // depends on build output, so don't lint it + '.wireit', + 'storybook-static', + ], + }, + ...guardian.configs.recommended, + ...guardian.configs.jest, + ...guardian.configs.react, + { + rules: { + 'no-unused-vars': 'off', + + '@typescript-eslint/no-unused-vars': [ + 'warn', + { + argsIgnorePattern: '^_', + varsIgnorePattern: '^_', + caughtErrorsIgnorePattern: '^_', + }, + ], + + // 'prettier/prettier': 'warn', + + 'react/no-unknown-property': [ + 'error', + { + ignore: ['css'], + }, + ], + + 'no-restricted-syntax': [ + 'error', + { + message: + "Don't use `fetch` directly, please use the `pandaFetch` abstraction, to get automatic session refreshes", + selector: "CallExpression[callee.name='fetch']", + }, + ], + }, + }, +]; + +// import { fixupConfigRules } from '@eslint/compat'; +// import typescriptEslint from '@typescript-eslint/eslint-plugin'; +// import prettier from 'eslint-plugin-prettier'; +// import globals from 'globals'; +// import tsParser from '@typescript-eslint/parser'; +// import path from 'node:path'; +// import { fileURLToPath } from 'node:url'; +// import js from '@eslint/js'; +// import { FlatCompat } from '@eslint/eslintrc'; + +// const __filename = fileURLToPath(import.meta.url); +// const __dirname = path.dirname(__filename); +// const compat = new FlatCompat({ +// baseDirectory: __dirname, +// recommendedConfig: js.configs.recommended, +// allConfig: js.configs.all, +// }); + +// export default [ +// { +// ignores: ['**/*.css', '**/*.svg'], +// }, +// ...fixupConfigRules( +// compat.extends( +// '@guardian/eslint-config-typescript', +// 'prettier', +// 'plugin:react/recommended', +// 'plugin:react/jsx-runtime', +// 'plugin:react-hooks/recommended', +// ), +// ), +// { +// plugins: { +// '@typescript-eslint': typescriptEslint, +// prettier, +// }, + +// languageOptions: { +// globals: { +// ...globals.browser, +// ...globals.node, +// }, + +// parser: tsParser, +// }, + +// settings: { +// react: { +// version: 'detect', +// }, +// }, + +// rules: { +// 'no-unused-vars': 'off', + +// '@typescript-eslint/no-unused-vars': [ +// 'warn', +// { +// argsIgnorePattern: '^_', +// varsIgnorePattern: '^_', +// caughtErrorsIgnorePattern: '^_', +// }, +// ], + +// 'prettier/prettier': 'warn', + +// 'react/no-unknown-property': [ +// 'error', +// { +// ignore: ['css'], +// }, +// ], + +// 'no-restricted-syntax': [ +// 'error', +// { +// message: +// "Don't use `fetch` directly, please use the `pandaFetch` abstraction, to get automatic session refreshes", +// selector: "CallExpression[callee.name='fetch']", +// }, +// ], +// }, +// }, +// { +// files: ['**/*.ts', '**/*.tsx'], + +// languageOptions: { +// ecmaVersion: 5, +// sourceType: 'script', + +// parserOptions: { +// project: ['./tsconfig.json', './tsconfig.*.json'], +// }, +// }, +// }, +// ]; diff --git a/newswires/client/package-lock.json b/newswires/client/package-lock.json index 4bd4dc84..427020d5 100644 --- a/newswires/client/package-lock.json +++ b/newswires/client/package-lock.json @@ -32,7 +32,7 @@ "eslint-plugin-react": "^7.35.0", "eslint-plugin-react-hooks": "^5.1.0", "eslint-plugin-react-refresh": "^0.4.18", - "globals": "^15.9.0", + "globals": "15.14.0", "jest": "29.7.0", "jest-environment-jsdom": "^29.7.0", "prettier": "^3.4.2", @@ -1606,6 +1606,19 @@ "eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0-0 || ^9.0.0" } }, + "node_modules/@guardian/eslint-config/node_modules/globals": { + "version": "15.13.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-15.13.0.tgz", + "integrity": "sha512-49TewVEz0UxZjr1WYYsWpPrhyC/B/pA8Bq0fUmet2n+eR7yn0IvNzNaoBwnK6mdkzcN+se7Ez9zUgULTz2QH4g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/@guardian/prettier": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/@guardian/prettier/-/prettier-8.0.1.tgz", @@ -7006,9 +7019,9 @@ } }, "node_modules/globals": { - "version": "15.13.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-15.13.0.tgz", - "integrity": "sha512-49TewVEz0UxZjr1WYYsWpPrhyC/B/pA8Bq0fUmet2n+eR7yn0IvNzNaoBwnK6mdkzcN+se7Ez9zUgULTz2QH4g==", + "version": "15.14.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-15.14.0.tgz", + "integrity": "sha512-OkToC372DtlQeje9/zHIo5CT8lRP/FUgEOKBEhU4e0abL7J7CD24fD9ohiLN5hagG/kWCYj4K5oaxxtj2Z0Dig==", "dev": true, "license": "MIT", "engines": { diff --git a/newswires/client/package.json b/newswires/client/package.json index cf0d3788..7646c381 100644 --- a/newswires/client/package.json +++ b/newswires/client/package.json @@ -6,8 +6,8 @@ "scripts": { "dev": "vite", "build": "tsc -b && vite build", - "lint": "eslint 'src/**' --ext ts --no-error-on-unmatched-pattern --fix", - "lint:ci": "eslint src/** --ext ts --no-error-on-unmatched-pattern", + "lint": "eslint 'src/**' -c 'eslint.config.js' --no-error-on-unmatched-pattern --fix", + "lint:ci": "eslint src/** -c 'eslint.config.js' --no-error-on-unmatched-pattern", "format": "prettier --write \"src/**/*.ts{,x}\"", "format:ci": "prettier --check \"src/**/*.ts{,x}\"", "preview": "vite preview", @@ -39,7 +39,7 @@ "eslint-plugin-react": "^7.35.0", "eslint-plugin-react-hooks": "^5.1.0", "eslint-plugin-react-refresh": "^0.4.18", - "globals": "^15.9.0", + "globals": "15.14.0", "jest": "29.7.0", "jest-environment-jsdom": "^29.7.0", "prettier": "^3.4.2", diff --git a/newswires/client/src/Item.tsx b/newswires/client/src/Item.tsx index 3aa6a12d..93b0c9fd 100644 --- a/newswires/client/src/Item.tsx +++ b/newswires/client/src/Item.tsx @@ -71,7 +71,7 @@ export const Item = ({ id }: { id: string }) => { : 'unknown error', ); }); - }, [id]); + }, [id, maybeSearchParams]); return ( diff --git a/newswires/client/src/catcodes-lookup.ts b/newswires/client/src/catcodes-lookup.ts index 023b7f29..7d805d01 100644 --- a/newswires/client/src/catcodes-lookup.ts +++ b/newswires/client/src/catcodes-lookup.ts @@ -1,6 +1,6 @@ import { lookupTables } from './category-code-lookup-tables'; -const qCodePrefixesLookup = { +const _qCodePrefixesLookup = { medtop: ['IPTC MediaTopics'], subj: ['IPTC NewsCodes', 'IPTC MediaTopics'], a1312cat: ['A1312 CatCodes'], diff --git a/newswires/client/src/context/SearchContext.tsx b/newswires/client/src/context/SearchContext.tsx index 69a0388c..a6e0198f 100644 --- a/newswires/client/src/context/SearchContext.tsx +++ b/newswires/client/src/context/SearchContext.tsx @@ -5,7 +5,6 @@ import { useContext, useEffect, useReducer, - useRef, useState, } from 'react'; import { z } from 'zod'; diff --git a/newswires/client/src/panda-session.ts b/newswires/client/src/panda-session.ts index 04cfb942..591a9fce 100644 --- a/newswires/client/src/panda-session.ts +++ b/newswires/client/src/panda-session.ts @@ -1,16 +1,18 @@ -/* eslint-disable no-restricted-syntax -- this file provides the abstraction over `fetch`, so is allowed to call it directly */ - /** * A customised version of `fetch`, that will attempt to re-auth the user on login failure. */ export const pandaFetch: typeof fetch = async (...args) => { + /* eslint-disable-next-line no-restricted-syntax -- this is the definition of 'pandaFetch' that we're asking people to use instead of fetch, but it needs to use fetch itself*/ const response = await fetch(...args); - if (response.status !== 419) + if (response.status !== 419) { // succeeded; return the response return response; + } // refresh the auth session + /* eslint-disable-next-line no-restricted-syntax -- this is the definition of 'pandaFetch' that we're asking people to use instead of fetch, but it needs to use fetch itself*/ await fetch('/', { mode: 'no-cors', credentials: 'include' }); // reattempt the fetch, return the result no matter the expiry + /* eslint-disable-next-line no-restricted-syntax -- this is the definition of 'pandaFetch' that we're asking people to use instead of fetch, but it needs to use fetch itself*/ return fetch(...args); };