Skip to content

Instantly share code, notes, and snippets.

@jakobo
Created August 5, 2022 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakobo/98606b83e9586e0ea623c03c5e782305 to your computer and use it in GitHub Desktop.
Save jakobo/98606b83e9586e0ea623c03c5e782305 to your computer and use it in GitHub Desktop.
monorepo eslint config
/**
* Create a set of default eslint configs for a typescript project
* @param {string} dir
* @returns
*/
const tsProject = (dir) => ({
plugins: ["@typescript-eslint", "import", "node"],
parser: "@typescript-eslint/parser",
parserOptions: {
tsconfigRootDir: __dirname,
project: [`${dir}/tsconfig.json`],
},
settings: {
"import/parsers": {
"@typescript-eslint/parser": [".ts", ".tsx"],
},
"import/resolver": {
typescript: {
alwaysTryTypes: true,
project: [`${dir}/tsconfig.json`],
},
},
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier",
],
rules: {
// False positives. There be dragons here. Most common culprits are
// optional chaining and nullish coalesce.
// Using it causes typescript-eslint to believe it is
// of type "any" and trigger these eslint errors.
// https://github.com/typescript-eslint/typescript-eslint/issues/2728
// https://github.com/typescript-eslint/typescript-eslint/issues/4912
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/restrict-template-expressions": "off",
// https://typescript-eslint.io/rules/no-unused-vars/
// https://eslint.org/docs/rules/no-unused-vars
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{ ignoreRestSiblings: true },
],
},
});
/**
* Get a list of all lintable files in a directory
* @param {string} dir
* @returns
*/
const lintableFiles = (dir) => [
`${dir}/**/*.+(t|j)s?(x)`,
`${dir}/**/*.+(c|m)js`,
];
/** A set of import rules that ensure files are imported in a way webpack and vscode understand */
const nextJsImportRules = {
"import/extensions": ["error", "never", { json: "always", css: "always" }],
"node/file-extension-in-import": [
"error",
"never",
{ ".json": "always", ".css": "always" },
],
"node/no-missing-import": "off",
};
module.exports = {
root: true,
rules: {},
// baseline js parsing options set env for CJS files and skip known builds
env: {
es6: true,
es2018: true,
node: true,
},
ignorePatterns: ["**/build/*", "**/.swc/*", "**/.next/*"],
// migration prep for eslint 9
overrides: [
// check yourself before you wreck yourself
{
files: ["**/.eslintrc.cjs", ".lintstagedrc.cjs", "commitlint.config.js"],
extends: ["eslint:recommended", "prettier"],
},
// api
{
files: lintableFiles("./services/api"),
excludedFiles: ["**/__generated__/*", "**.d.ts"],
...tsProject("./services/api"),
extends: [
...tsProject("./services/api").extends,
"next/core-web-vitals",
],
settings: {
...tsProject("./services/api").settings,
next: {
rootDir: "services/api/",
},
},
rules: {
...tsProject("./services/api").rules,
...nextJsImportRules,
},
},
// site
{
files: lintableFiles("./services/site"),
excludedFiles: ["**/__generated__/*", "**.d.ts"],
...tsProject("./services/site"),
extends: [
...tsProject("./services/site").extends,
"next/core-web-vitals",
],
settings: {
...tsProject("./services/site").settings,
next: {
rootDir: "services/site/",
},
},
rules: {
...tsProject("./services/site").rules,
...nextJsImportRules,
},
},
// worker
{
files: lintableFiles("./services/worker"),
excludedFiles: ["**/__generated__/*", "**/*.d.ts"],
...tsProject("./services/worker"),
rules: {
...tsProject("./services/worker").rules,
"no-restricted-imports": [
"error",
{
patterns: ["src/*"],
},
],
"node/file-extension-in-import": ["error", "always"],
},
},
],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment