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

Simplify ESLint Config #261

Merged
merged 8 commits into from
Oct 12, 2023
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
143 changes: 128 additions & 15 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,135 @@
/**
* This is intended to be a basic starting point for linting in the Indie Stack.
* It relies on recommended configs out of the box for simplicity, but you can
* and should modify this configuration to best suit your team's needs.
*/

/** @type {import('eslint').Linter.Config} */
module.exports = {
root: true,
extends: [
"@remix-run/eslint-config",
"@remix-run/eslint-config/node",
"@remix-run/eslint-config/jest-testing-library",
"prettier",
],
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
ecmaFeatures: {
jsx: true,
},
},
env: {
"cypress/globals": true,
browser: true,
commonjs: true,
es6: true,
},
plugins: ["cypress"],
// we're using vitest which has a very similar API to jest
// (so the linting plugins work nicely), but it means we have to explicitly
// set the jest version.
settings: {
jest: {
version: 28,

// Base config
extends: ["eslint:recommended"],

overrides: [
// React
{
files: ["**/*.{js,jsx,ts,tsx}"],
plugins: ["react", "jsx-a11y"],
extends: [
"plugin:react/recommended",
"plugin:react/jsx-runtime",
"plugin:jsx-a11y/recommended",
"prettier",
],
settings: {
react: {
version: "detect",
},
formComponents: ["Form"],
linkComponents: [
{ name: "Link", linkAttribute: "to" },
{ name: "NavLink", linkAttribute: "to" },
],
},
rules: {
"react/jsx-no-leaked-render": [
"warn",
{ validStrategies: ["ternary"] },
],
},
Comment on lines +47 to +52
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could go either way on this one-off rule. On one hand, if we truly want to stay out of the opinion game, then we technically should have zero rules or our own. But this one seems like it solves a common footgun, and TBH I was surprised it wasn't enable din the recommended plugin...so I added it. (It was also one we turned on via eslintrc.repo.js before.

},
},

// Typescript
{
files: ["**/*.{ts,tsx}"],
plugins: ["@typescript-eslint", "import"],
parser: "@typescript-eslint/parser",
settings: {
"import/internal-regex": "^~/",
"import/resolver": {
node: {
extensions: [".ts", ".tsx"],
},
typescript: {
alwaysTryTypes: true,
},
},
},
extends: [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/stylistic",
"plugin:import/recommended",
"plugin:import/typescript",
"prettier",
],
rules: {
"import/order": [
"error",
{
alphabetize: { caseInsensitive: true, order: "asc" },
groups: ["builtin", "external", "internal", "parent", "sibling"],
"newlines-between": "always",
},
],
},
Comment on lines +78 to +87
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same comment as above. I personally hate lack of consistency when it comes to imports - but again this is veering into "opinion" territory.

},

// Markdown
{
files: ["**/*.md"],
plugins: ["markdown"],
extends: ["plugin:markdown/recommended", "prettier"],
},

// Jest/Vitest
{
files: ["**/*.test.{js,jsx,ts,tsx}"],
plugins: ["jest", "jest-dom", "testing-library"],
extends: [
"plugin:jest/recommended",
"plugin:jest-dom/recommended",
"plugin:testing-library/react",
"prettier",
],
env: {
"jest/globals": true,
},
settings: {
jest: {
// we're using vitest which has a very similar API to jest
// (so the linting plugins work nicely), but it means we have to explicitly
// set the jest version.
version: 28,
},
},
},

// Cypress
{
files: ["cypress/**/*.ts"],
plugins: ["cypress"],
extends: ["plugin:cypress/recommended", "prettier"],
},

// Node
{
files: [".eslintrc.js", "mocks/**/*.js"],
env: {
node: true,
},
},
],
};
32 changes: 0 additions & 32 deletions .eslintrc.repo.js

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/lint-repo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
run: npm install

- name: 🔬 Lint
run: npm run lint:repo
run: npm run lint
1 change: 1 addition & 0 deletions app/models/user.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export async function verifyLogin(
return null;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { password: _password, ...userWithoutPassword } = userWithPassword;

return userWithoutPassword;
Expand Down
1 change: 1 addition & 0 deletions app/routes/join.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default function Join() {
ref={emailRef}
id="email"
required
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={true}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These feel like they're an OK exception to the rule as this form is the sole purpose of the page

name="email"
type="email"
Expand Down
3 changes: 2 additions & 1 deletion app/routes/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export default function LoginPage() {
ref={emailRef}
id="email"
required
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={true}
name="email"
type="email"
Expand Down Expand Up @@ -160,7 +161,7 @@ export default function LoginPage() {
</label>
</div>
<div className="text-center text-sm text-gray-500">
Don't have an account?{" "}
Don&apos;t have an account?{" "}
<Link
className="text-blue-500 underline"
to={{
Expand Down
4 changes: 2 additions & 2 deletions app/singleton.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const singleton = <Value>(
name: string,
valueFactory: () => Value,
): Value => {
const g = global as any;
const g = global as unknown as { __singletons: Record<string, unknown> };
g.__singletons ??= {};
g.__singletons[name] ??= valueFactory();
return g.__singletons[name];
return g.__singletons[name] as Value;
};
9 changes: 7 additions & 2 deletions app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ export function useMatchesData(
return route?.data as Record<string, unknown>;
}

function isUser(user: any): user is User {
return user && typeof user === "object" && typeof user.email === "string";
function isUser(user: unknown): user is User {
return (
user != null &&
typeof user === "object" &&
"email" in user &&
typeof user.email === "string"
);
}

export function useOptionalUser(): User | undefined {
Expand Down
3 changes: 2 additions & 1 deletion cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { faker } from "@faker-js/faker";

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't know the best way to do this otherwise so I just disabled it.

namespace Cypress {
interface Chainable {
/**
Expand Down Expand Up @@ -85,7 +86,7 @@ function deleteUserByEmail(email: string) {
// Also added custom types to avoid getting detached
// https://github.com/cypress-io/cypress/issues/7306#issuecomment-1152752612
// ===========================================================
function visitAndCheck(url: string, waitTime: number = 1000) {
function visitAndCheck(url: string, waitTime = 1000) {
cy.visit(url);
cy.location("pathname").should("contain", url).wait(waitTime);
}
Expand Down
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
"dev": "remix dev -c \"npm run dev:serve\"",
"dev:serve": "binode --require ./mocks -- @remix-run/serve:remix-serve ./build/index.js",
"format": "prettier --write .",
"format:repo": "npm run format && npm run lint:repo -- --fix",
"format:repo": "npm run format && npm run lint -- --fix",
"lint": "eslint --cache --cache-location ./node_modules/.cache/eslint .",
"lint:repo": "npm run lint -- --config .eslintrc.repo.js",
"setup": "prisma generate && prisma migrate deploy && prisma db seed",
"start": "remix-serve ./build/index.js",
"start:mocks": "binode --require ./mocks -- @remix-run/serve:remix-serve ./build/index.js",
Expand Down Expand Up @@ -41,26 +40,33 @@
"devDependencies": {
"@faker-js/faker": "^8.0.2",
"@remix-run/dev": "*",
"@remix-run/eslint-config": "*",
"@testing-library/cypress": "^9.0.0",
"@testing-library/jest-dom": "^5.17.0",
"@types/bcryptjs": "^2.4.2",
"@types/eslint": "^8.44.2",
"@types/node": "^18.17.6",
"@types/react": "^18.2.20",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"@typescript-eslint/parser": "^6.7.3",
"@vitejs/plugin-react": "^4.0.4",
"@vitest/coverage-v8": "^0.34.2",
"autoprefixer": "^10.4.15",
"binode": "^1.0.5",
"cookie": "^0.5.0",
"cross-env": "^7.0.3",
"cypress": "12.17.3",
"eslint": "^8.47.0",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-cypress": "^2.14.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-cypress": "^2.15.1",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jest": "^27.4.0",
"eslint-plugin-jest-dom": "^5.1.0",
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-markdown": "^3.0.1",
"eslint-plugin-prefer-let": "^3.0.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-testing-library": "^6.0.2",
"happy-dom": "^10.10.4",
"msw": "^1.2.3",
"npm-run-all": "^4.1.5",
Expand All @@ -72,7 +78,7 @@
"tailwindcss": "^3.3.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.6",
"typescript": "^5.2.2",
"vite": "^4.4.9",
"vite-tsconfig-paths": "^3.6.0",
"vitest": "^0.34.2"
Expand Down
18 changes: 2 additions & 16 deletions remix.init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,17 @@ const getPackageManagerVersion = (packageManager) =>

const getRandomString = (length) => crypto.randomBytes(length).toString("hex");

const removeUnusedDependencies = (dependencies, unusedDependencies) =>
Object.fromEntries(
Object.entries(dependencies).filter(
([key]) => !unusedDependencies.includes(key),
),
);

const updatePackageJson = ({ APP_NAME, packageJson }) => {
const {
devDependencies,
scripts: {
// eslint-disable-next-line no-unused-vars
"format:repo": _repoFormatScript,
"lint:repo": _repoLintScript,
...scripts
},
} = packageJson.content;

packageJson.update({
name: APP_NAME,
devDependencies: removeUnusedDependencies(
devDependencies,
// packages that are only used for linting the repo
["eslint-plugin-markdown", "eslint-plugin-prefer-let"],
),
scripts,
});
};
Expand Down Expand Up @@ -190,13 +177,12 @@ const main = async ({ packageManager, rootDirectory }) => {
fs.rm(path.join(rootDirectory, ".github", "workflows", "no-response.yml")),
fs.rm(path.join(rootDirectory, ".github", "dependabot.yml")),
fs.rm(path.join(rootDirectory, ".github", "PULL_REQUEST_TEMPLATE.md")),
fs.rm(path.join(rootDirectory, ".eslintrc.repo.js")),
fs.rm(path.join(rootDirectory, "LICENSE.md")),
]);

execSync(pm.run("setup"), { cwd: rootDirectory, stdio: "inherit" });

execSync(pm.run("format", "--loglevel warn"), {
execSync(pm.run("format", "--log-level warn"), {
cwd: rootDirectory,
stdio: "inherit",
});
Expand Down