Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import and style sorting config + capabilities. #132

Merged
merged 14 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ check:
# Performs code formatting for the webapp files and contracts in their respective directories.
format:
cd packages/webapp && make format
cd packages/contract && make format
cd packages/contracts && make format
.PHONY: format

# ==================================================================================================
Expand Down
85 changes: 59 additions & 26 deletions packages/webapp/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,65 @@
/** @type {import("eslint").Linter.Config} */
const config = {
extends: ["next/core-web-vitals", "plugin:@typescript-eslint/recommended", "prettier"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.json",
},
plugins: ["@typescript-eslint"],
root: true,
ignorePatterns: ["node_modules", "src/hooks/useScrollBox.ts"],
rules: {
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"react/no-unescaped-entities": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-ts-comment": "off",
extends: ["next/core-web-vitals", "plugin:@typescript-eslint/recommended", "prettier"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.json",
sourceType: "module",
ecmaVersion: "latest",
},
plugins: ["@typescript-eslint", "simple-import-sort"],
root: true,
ignorePatterns: ["node_modules", "src/generated.ts"],
rules: {
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"react/no-unescaped-entities": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-ts-comment": "off",
"no-restricted-imports": "off",
"@typescript-eslint/no-restricted-imports": [
"error",
{
patterns: ["./*", "../*"],
},
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I was confused why this would work because this is the pattern used in the link I gave to ONLY ALLOW relative links.

Turns out:

  • make check does not actually run the eslint checks, it needs to be pnpm next lint --max-warnings 0 and not pnpm eslint (but let's actually call make lint from make check, that way we avoid duplication)

  • Even so, this should forbid every single one of our imports... turns out it does not work, probably ! is not a thing as an operator.

  • BUT, if you remove the !, then it does exactly what we want, and indeed catches a few relative imports that slipped through the cracks :)

Can you fix make check, this file and eliminate the remaining relative imports? And also add a comment in this file to explain we forbid relative imports and absolute imports starting with src/ should be used instead?

Oh, also you'll need to exclude generated.ts from eslint (ignorePatterns option).

I see we're also excluding src/hooks/useScrollBox.ts from linting, seems to be about React useEffect dependencies, would be good to remove it from the ignore list and either fix or simply add comments to disable eslint on those useEffect statements.


"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
{
// ignore unused args that start with underscore
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
},
],
},
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
{
// ignore unused args that start with underscore
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
},
],
"import/first": "error",
"import/newline-after-import": "error",
"import/no-duplicates": "error",
"simple-import-sort/imports": [
"error",
{
groups: [
// Packages. `react` related packages come first.
["^react", "^next"],
// External packages.
["^@?\\w"],
// Custom group for src/ prefixed imports
["^src/"],
// Parent imports. Put `..` last.
["^\\.\\.(?!/?$)", "^\\.\\./?$"],
// Other relative imports. Put same-folder imports and `.` last.
["^\\./(?=.*/)(?!/?$)", "^\\.(?!/?$)", "^\\./?$"],
// Style imports.
["^.+\\.s?css$"],
// Side effect imports.
["^\\u0000"],
],
},
],
},
}

module.exports = config
3 changes: 2 additions & 1 deletion packages/webapp/.prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"singleQuote": false,
"tabWidth": 4,
"useTabs": false,
"printWidth": 120
"printWidth": 120,
"plugins": ["prettier-plugin-tailwindcss"]
}
6 changes: 5 additions & 1 deletion packages/webapp/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"javascript.validate.enable": false,
"typescript.validate.enable": false
"typescript.validate.enable": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact"]
}
5 changes: 3 additions & 2 deletions packages/webapp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ lint:

# Runs code quality checks.
check:
pnpm eslint .
make lint
pnpm prettier --check "src/**/*.{js,jsx,ts,tsx,json,css}"
.PHONY: check

# Runs prettier formatting across webapp files with specified file extensions.
format:
pnpm prettier --write "**/*.{js,jsx,ts,tsx,json,css}"
pnpm eslint . --fix
pnpm prettier --write "**/*.{js,jsx,ts,tsx,json,css,cjs,mjs}"
.PHONY: format
98 changes: 49 additions & 49 deletions packages/webapp/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,57 @@ const require = createRequire(import.meta.url)

/** @type {import("next").NextConfig} */
const nextConfig = {
reactStrictMode: true,
reactStrictMode: true,

/**
* If you have the "experimental: { appDir: true }" setting enabled, then you
* must comment the below `i18n` config out.
*
* @see https://github.com/vercel/next.js/issues/41980
*/
i18n: {
locales: ["en"],
defaultLocale: "en",
},
eslint: {
// Warning: This allows production builds to successfully complete even if
// your project has ESLint errors.
ignoreDuringBuilds: true,
},
experimental: {
// Currently broken in Next 13.3.0: https://github.com/pmndrs/swc-jotai/issues/6
// Unlike what is suggested, also broken when I downgrade to Next 13.2.3 and Next 13.1.6.
swcPlugins: [
// ['@swc-jotai/react-refresh', {}],
// ["@swc-jotai/debug-label", {}]
]
},
webpack(config, { dev, isServer }) {
// prevent node-gyp from failing because "can't resolve fs"
config.resolve.fallback = {
...config.resolve.fallback,
fs: false,
net: false,
tls: false,
readline: false
}
/**
* If you have the "experimental: { appDir: true }" setting enabled, then you
* must comment the below `i18n` config out.
*
* @see https://github.com/vercel/next.js/issues/41980
*/
i18n: {
locales: ["en"],
defaultLocale: "en",
},
eslint: {
// Warning: This allows production builds to successfully complete even if
// your project has ESLint errors.
ignoreDuringBuilds: true,
},
experimental: {
// Currently broken in Next 13.3.0: https://github.com/pmndrs/swc-jotai/issues/6
// Unlike what is suggested, also broken when I downgrade to Next 13.2.3 and Next 13.1.6.
swcPlugins: [
// ['@swc-jotai/react-refresh', {}],
// ["@swc-jotai/debug-label", {}]
],
},
webpack(config, { dev, isServer }) {
// prevent node-gyp from failing because "can't resolve fs"
config.resolve.fallback = {
...config.resolve.fallback,
fs: false,
net: false,
tls: false,
readline: false,
}

config.experiments = {
...config.experiments,
topLevelAwait: true // enable await at top-level in modules
}
config.experiments = {
...config.experiments,
topLevelAwait: true, // enable await at top-level in modules
}

// This would be great, but is sadly disallowed by Next, because they hate freedom.
// https://nextjs.org/docs/messages/improper-devtool
// Having this would enable parsing hook names in the React DevTools.
// config.devtool = "cheap-module-source-map"
return config
},
// This hack makes it possible to use the Jotai devtools
// Sources:
// https://github.com/jotaijs/jotai-devtools/issues/47
// https://github.com/martpie/next-transpile-modules/releases/tag/the-end
transpilePackages: ['jotai-devtools']
// This would be great, but is sadly disallowed by Next, because they hate freedom.
// https://nextjs.org/docs/messages/improper-devtool
// Having this would enable parsing hook names in the React DevTools.
// config.devtool = "cheap-module-source-map"
return config
},
// This hack makes it possible to use the Jotai devtools
// Sources:
// https://github.com/jotaijs/jotai-devtools/issues/47
// https://github.com/martpie/next-transpile-modules/releases/tag/the-end
transpilePackages: ["jotai-devtools"],
}

// // This hack makes it possible to use the Jotai devtools
Expand All @@ -68,4 +68,4 @@ const nextConfig = {
// "jotai-devtools",
// ])
// export default withTranspileModules(nextConfig)
export default nextConfig
export default nextConfig
7 changes: 5 additions & 2 deletions packages/webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"connectkit": "^1.5.3",
"eslint-config-prettier": "^9.1.0",
"jotai": "^2.4.3",
"jotai-devtools": "^0.7.0",
"lodash": "^4.17.21",
"lucide-react": "^0.309.0",
"next": "^13.5.6",
"next-themes": "^0.2.1",
"next-transpile-modules": "^10.0.1",
"prettier": "^3.2.5",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-icons": "^4.11.0",
Expand All @@ -52,8 +52,11 @@
"autoprefixer": "^10.4.16",
"eslint": "^8.52.0",
"eslint-config-next": "^13.5.6",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-simple-import-sort": "^12.0.0",
"postcss": "^8.4.31",
"prettier": "2.8.8",
"prettier-plugin-tailwindcss": "^0.5.11",
ultraviolet10 marked this conversation as resolved.
Show resolved Hide resolved
"tailwindcss": "^3.3.3",
"typescript": "^5.2.2"
}
Expand Down
10 changes: 5 additions & 5 deletions packages/webapp/postcss.config.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const config = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}

module.exports = config
module.exports = config
8 changes: 4 additions & 4 deletions packages/webapp/postcss.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
2 changes: 1 addition & 1 deletion packages/webapp/src/actions/concede.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defaultErrorHandling } from "src/actions/errors"
import { checkFresh, freshWrap } from "src/store/checkFresh"
import { contractWriteThrowing } from "src/actions/libContractWrite"
import { Address } from "src/chain"
import { deployment } from "src/deployment"
import { gameABI } from "src/generated"
import { checkFresh, freshWrap } from "src/store/checkFresh"

// =================================================================================================

Expand Down
2 changes: 1 addition & 1 deletion packages/webapp/src/actions/defend.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defaultErrorHandling } from "src/actions/errors"
import { checkFresh, freshWrap } from "src/store/checkFresh"
import { contractWriteThrowing } from "src/actions/libContractWrite"
import { Address } from "src/chain"
import { deployment } from "src/deployment"
import { gameABI } from "src/generated"
import { checkFresh, freshWrap } from "src/store/checkFresh"

// =================================================================================================

Expand Down
12 changes: 6 additions & 6 deletions packages/webapp/src/actions/drawCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import { defaultErrorHandling } from "src/actions/errors"
import { contractWriteThrowing } from "src/actions/libContractWrite"
import { Address, type HexString } from "src/chain"
import { CancellationHandler } from "src/components/modals/loadingModal"
import { DRAW_CARD_PROOF_TIMEOUT } from "src/constants"
import { deployment } from "src/deployment"
import { packCards } from "src/game/fableProofs"
import { gameABI } from "src/generated"
import { getOrInitPrivateInfo, setPrivateInfo } from "src/store/write"
import { checkFresh, freshWrap } from "src/store/checkFresh"
import {
getCards,
getCurrentPlayerAddress,
Expand All @@ -21,12 +23,10 @@ import {
getPlayerAddress,
} from "src/store/read"
import { GameStep, PrivateInfo } from "src/store/types"
import { FAKE_PROOF, proveInWorker, SHOULD_GENERATE_PROOFS } from "src/utils/zkproofs"
import { bigintToHexString } from "src/utils/js-utils"
import { getOrInitPrivateInfo, setPrivateInfo } from "src/store/write"
import { mimcHash } from "src/utils/hashing"
import { DRAW_CARD_PROOF_TIMEOUT } from "src/constants"
import { CancellationHandler } from "src/components/modals/loadingModal"
import { checkFresh, freshWrap } from "src/store/checkFresh"
import { bigintToHexString } from "src/utils/js-utils"
import { FAKE_PROOF, proveInWorker, SHOULD_GENERATE_PROOFS } from "src/utils/zkproofs"

// =================================================================================================

Expand Down
2 changes: 1 addition & 1 deletion packages/webapp/src/actions/endTurn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { contractWriteThrowing } from "src/actions/libContractWrite"
import { Address } from "src/chain"
import { deployment } from "src/deployment"
import { gameABI } from "src/generated"
import { checkFresh, freshWrap } from "src/store/checkFresh"
import { getCurrentPlayerAddress, getGameData, getGameID, getPlayerAddress } from "src/store/read"
import { GameStep } from "src/store/types"
import { checkFresh, freshWrap } from "src/store/checkFresh"

// =================================================================================================

Expand Down
3 changes: 1 addition & 2 deletions packages/webapp/src/actions/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import { ContractFunctionRevertedError, UserRejectedRequestError } from "viem"

import { ContractWriteError } from "src/actions/libContractWrite"
import { GIT_ISSUES } from "src/constants"
import { setError } from "src/store/write"

import { StaleError } from "src/store/checkFresh"
import { setError } from "src/store/write"
import { TimeoutError } from "src/utils/errors"
import { ProofCancelled, ProofError, ProofTimeoutError } from "src/utils/zkproofs/proofs"

Expand Down
Loading