Skip to content

Instantly share code, notes, and snippets.

@nurmdrafi
Last active November 14, 2024 11:17
Show Gist options
  • Save nurmdrafi/524aca647b447ef5f5ca1c6c4f430518 to your computer and use it in GitHub Desktop.
Save nurmdrafi/524aca647b447ef5f5ca1c6c4f430518 to your computer and use it in GitHub Desktop.
typescript eslint config for shadcn UI

tsconfig.json

{
{
	"compilerOptions": {
		"target": "es2022",
		"lib": ["dom", "dom.iterable", "esnext"],
		"allowJs": true,
		"skipLibCheck": true,
		"strict": true,
		"forceConsistentCasingInFileNames": true,
		"noEmit": true,
		"esModuleInterop": true,
		"module": "esnext",
		"moduleResolution": "node",
		"resolveJsonModule": true,
		"isolatedModules": true,
		"jsx": "preserve",
		"incremental": true,
		"typeRoots": ["./src/types"],
		"plugins": [
			{
				"name": "next"
			}
		],
		"baseUrl": ".",
		"paths": {
			"@/*": ["./*"]
		}
	},
	"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "*.ts", ".next/types/**/*.ts", "next.config.mjs"],
	"exclude": ["node_modules"]
}

.eslintrc

{
  "env": {
    "browser": true,
    "commonjs": true,
    "es2022": true,
    "node": true
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  },
    "parser": "@typescript-eslint/parser",
    "plugins": ["@typescript-eslint"],
    "parserOptions": {
      "parser": "@typescript-eslint/parser",
        "project": "./tsconfig.json"
    },
    "extends": [
        "next/core-web-vitals",
        "airbnb",
        "airbnb-typescript",
        "airbnb/hooks"
    ],
    "rules": {
            // === React === /
            "react/forbid-prop-types": "error",
            "react/default-props-match-prop-types": "error",
            "react/self-closing-comp": "error",
            "react/no-unused-prop-types": "error",
            "react/jsx-key": "error",
            "react/no-unused-state": "error",
            "react/state-in-constructor": "error",
            "react/function-component-definition": "off",
            "react/require-default-props": "off",
            "react/no-array-index-key": "off",
            "react/no-unescaped-entities": "off",
            "react/no-unstable-nested-components": "off",
            "react/no-danger": "off", // allow for next.js
            "react/prop-types": "off", // shadcn
            // === React Hooks === //
            "react-hooks/rules-of-hooks": "error",
            "react-hooks/exhaustive-deps": "error",
            // === JSX === //
            "jsx-a11y/img-redundant-alt": "error",
            "jsx-a11y/anchor-is-valid": "error",
            "jsx-a11y/alt-text": "error",
            "jsx-a11y/mouse-events-have-key-events": "error",
            "jsx-a11y/no-static-element-interactions": "off",
            "jsx-a11y/no-noninteractive-element-interactions": "off",
            "jsx-a11y/click-events-have-key-events": "off",
            // === React-JSX === //
            "react/jsx-indent-props": "error",
            "react/jsx-curly-newline": "error",
            "react/jsx-equals-spacing": "error",
            "react/jsx-indent": "warn",
            "react/jsx-props-no-multi-spaces": "warn",
            "react/jsx-curly-brace-presence": "warn",
            "react/jsx-closing-bracket-location": "warn",
            "react/jsx-tag-spacing": "warn",
            "react/jsx-props-no-spreading": "off",
            "react/react-in-jsx-scope": "off",
            "react/jsx-closing-tag-location": "off",
            "react/jsx-boolean-value": "off",
            "react/jsx-wrap-multilines": "off",
            "react/jsx-no-useless-fragment": "off",
            "react/jsx-one-expression-per-line": "off",
            "react/jsx-max-props-per-line": "off",
            "react/jsx-curly-spacing": [
              2,
              "always",
              {
                "allowMultiline": true,
                "spacing": {
                  "objectLiterals": "never"
                }
              }
            ],
            // === TypeScript === //
            "@typescript-eslint/return-await": "error",
            "@typescript-eslint/restrict-plus-operands": "error",
            "@typescript-eslint/no-shadow": "error", // *** recommanded ***
            "@typescript-eslint/object-curly-spacing": "warn",
            "@typescript-eslint/no-misused-promises": [
              "error",
              {
                "checksVoidReturn": false
              }
            ],
            "@typescript-eslint/no-unused-expressions": "warn",
            "@typescript-eslint/no-unused-vars": "off", // use "warn" when code
            "@typescript-eslint/indent": "warn",
            "@typescript-eslint/space-before-blocks": "warn",
            "@typescript-eslint/keyword-spacing": "warn",
            "@typescript-eslint/comma-spacing": "warn",
            "@typescript-eslint/space-infix-ops": "warn",
            "@typescript-eslint/prefer-as-const": "warn",
            "@typescript-eslint/no-explicit-any": "off",
            "@typescript-eslint/no-unsafe-member-access": "off",
            "@typescript-eslint/no-unsafe-argument": "off",
            "@typescript-eslint/no-unsafe-call": "off",
            "@typescript-eslint/no-unsafe-assignment": "off",
            "@typescript-eslint/no-unsafe-return": "off",
            "@typescript-eslint/restrict-template-expressions": "off",
            "@typescript-eslint/no-floating-promises": "off",
            "@typescript-eslint/ban-types": "off",
            "@typescript-eslint/no-use-before-define": "off",
            "@typescript-eslint/semi": "off",
            "@typescript-eslint/comma-dangle": "off",
            "@typescript-eslint/quotes": "off",
            "@typescript-eslint/naming-convention": "off", // *** recommanded ***
            "@typescript-eslint/no-var-requires": "off",
            // === Import === //
            "import/first": "error",
            "import/no-mutable-exports": "error",
            "import/no-useless-path-segments": "error",
            "import/no-named-as-default": "error",
            "import/no-duplicates": "error",
            "import/newline-after-import": "error",
            "import/no-extraneous-dependencies": "off",
            "import/order": "off",
            "import/no-cycle": "off",
            "import/extensions": "off", // shadcn
            "import/prefer-default-export": "off", // shadcn
            // === Others === //
            "function-paren-newline": "error",
            "function-call-argument-newline": "error",
            "spaced-comment": "error",
            "operator-linebreak": "error",
            "computed-property-spacing": "error",
            "array-callback-return": "error",
            "space-unary-ops": "error",
            "object-shorthand": "error",
            "key-spacing": "error",
            "quote-props": "error",
            "prefer-const": "error",
            "prefer-destructuring": "error",
            "prefer-template": "error",
            "prefer-regex-literals": "error",
            "prefer-promise-reject-errors": "error",
            "guard-for-in": "error",
            "one-var": "error",
            "no-cond-assign": "error",
            "no-sequences": "error",
            "no-unneeded-ternary": "error",
            "no-extra-boolean-cast": "error",
            "no-lonely-if": "error",
            "no-unsafe-optional-chaining": "error",
            "no-mixed-operators": "error",
            "no-confusing-arrow": "error",
            "no-plusplus": "error",
            "no-constant-condition": "error",
            "no-floating-decimal": "error",
            "eol-last": "error",
            "array-bracket-spacing": "error", // *** recommanded ***
            "space-in-parens": "error", // *** recommanded ***
            "template-curly-spacing": [
              "error",
              "always"
            ], // *** recommanded ***
            "no-tabs": [
              "error",
              {
                "allowIndentationTabs": true
              }
            ],
            "dot-notation": "warn",
            "implicit-arrow-linebreak": "warn",
            "padded-blocks": "warn",
            "no-multiple-empty-lines": "warn",
            "no-multi-spaces": "warn",
            "no-else-return": "off",
            "indent": [
              "warn",
              2
            ],
            "linebreak-style": [
              "warn",
              "unix"
            ],
            "semi": [
              "warn",
              "never"
            ],
            "no-param-reassign": [
              "warn",
              {
                "props": true,
                "ignorePropertyModificationsFor": [
                  "state"
                ]
              }
            ],
            "jsx-quotes": "off",
            "max-len": "off",
            "object-curly-newline": "off",
            "arrow-body-style": "off",
            "arrow-parens": "off",
            "consistent-return": "off",
            "eqeqeq": "off",
            "no-console": ["error", { "allow": ["warn", "error"] }], // *** recommanded ***
            "no-bitwise": "off",
            "no-trailing-spaces": "off",
            "no-useless-return": "off",
            "no-underscore-dangle": "off",
            "no-nested-ternary": "off",
            "no-restricted-syntax": "off",
            "no-unused-vars": "off", // "@typescript-eslint/no-unused-vars"
            "no-return-await": "off" // "@typescript-eslint/return-await"
    }
}

.vscode/settings.json

{
    "editor.codeActionsOnSave": {
      "source.fixAll.eslint": "explicit"
    },  
    // "editor.formatOnSave": false,
    "eslint.format.enable": true, // on save fix format
    "eslint.validate": [
      "javascript",
      "javascriptreact",
      "typescript",
      "typescriptreact"
    ],
    "eslint.workingDirectories": [
      {
        "mode": "auto"
      }
    ],
    "javascript.preferences.importModuleSpecifier": "non-relative", // import alias
    "typescript.preferences.importModuleSpecifier": "non-relative", // import alias
  }

components.json

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "default",
  "rsc": true,
  "tsx": true,
  "tailwind": {
    "config": "tailwind.config.ts",
    "css": "app/globals.css",
    "baseColor": "slate",
    "cssVariables": true,
    "prefix": ""
  },
  "aliases": {
    "components": "@/components",
    "utils": "@/lib/utils",
    "ui": "@/components/ui",
    "lib": "@/lib",
    "hooks": "@/hooks"
  },
  "iconLibrary": "lucide"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment