Example eslint config

Home   »   Example eslint config

const path = require('path')
const { readFileSync } = require('fs')

const schemaString = readFileSync(`${__dirname}/data/schema.graphql`, 'utf8')

module.exports = {
  root: true,
  plugins: [
    // 'jest',
    '@typescript-eslint',
    'react-hooks',
    'react',
    'graphql',
    'simple-import-sort',
    'import',
    'jsx-a11y',
    'json',
    'jsdoc',
    'workspaces',
  ],
  extends: [
    'airbnb',
    'airbnb/hooks',
    // 'plugin:react/recommended',
    // 'plugin:react-hooks/recommended',
    // 'plugin:jsx-a11y/recommended',
    'eslint:recommended',
    'plugin:import/errors',
    'plugin:json/recommended',
    'plugin:jsdoc/recommended',
    'plugin:workspaces/recommended',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
      // experimentalDecorators: true,
    },
  },
  overrides: [
    {
      files: ['packages/e2e/cypress/**'],
      plugins: ['cypress'],
      env: {
        'cypress/globals': true,
      },
    },
    {
      files: ['**/*.ts', '**/*.tsx'],
      extends: [
        'airbnb-typescript',
        'airbnb/hooks',
        // 'plugin:react/recommended',
        // 'plugin:react-hooks/recommended',
        // 'plugin:jsx-a11y/recommended',
        'eslint:recommended',
        'plugin:@typescript-eslint/recommended',
        'plugin:import/errors',
        'plugin:import/typescript',
        'plugin:workspaces/recommended',
      ],
      // parserOptions: {
      //   project: './tsconfig.eslint.json',
      // },
    },
  ],
  settings: {
    react: {
      version: 'detect',
    },
    // 'import/resolver': {
    //   node: {
    //     extensions: ['.mjs', '.js', '.jsx', '.ts', '.tsx', '.android.js', '.ios.js'],
    //   },
    // },
  },
  env: {
    // Please override the env in each workspace package
    browser: false,
    node: true,
    es6: true,
    commonjs: true, // support nodejs polyfill such as global.fetch
    // mocha: true,
    // jest: true,
  },
  globals: {
    __VERSION__: true,
    __ENV__: true,
    __BUILD_TARGET__: true,
  },
  rules: {
    // note you must disable the base rule as it can report incorrect errors
    'no-unused-vars': 'off',
    '@typescript-eslint/no-unused-vars': ['error'],
    // note you must disable the base rule as it can report incorrect errors
    'no-use-before-define': 'off',
    '@typescript-eslint/no-use-before-define': ['error'],

    // JSDoc linting
    'jsdoc/require-jsdoc': 'off',
    'jsdoc/require-returns': 'off',
    'jsdoc/require-param-description': 'off',

    indent: ['error', 2],
    quotes: ['error', 'single'],
    semi: ['error', 'never'],
    camelcase: 0,
    'arrow-body-style': 0,
    'arrow-parens': 0,
    'comma-dangle': ['error', 'always-multiline'],
    'func-names': ['error', 'never'],
    'linebreak-style': ['error', 'unix'],
    'lines-between-class-members': 0,
    'max-len': 0,
    'no-console': 0,
    'no-nested-ternary': 1,
    'no-param-reassign': ['error', { props: false }],
    'no-plusplus': ['error', { allowForLoopAfterthoughts: true }],
    'no-underscore-dangle': 0,
    'no-unused-vars': [
      'error',
      {
        vars: 'all',
        args: 'after-used',
        ignoreRestSiblings: true,
      },
    ],
    'object-curly-newline': [0],
    'object-curly-spacing': ['error', 'always'],
    'operator-linebreak': ['error', 'after'],
    'prefer-destructuring': [
      'error',
      {
        array: false,
        object: true,
      },
    ],

    // import
    'import/named': 2,
    'import/no-cycle': 1,
    'import/no-extraneous-dependencies': [
      'error',
      {
        packageDir: [
          path.resolve(__dirname),
          path.resolve(__dirname, 'packages/gateway'),
          path.resolve(__dirname, 'packages/components'),
          path.resolve(__dirname, 'packages/client_landing'),
          path.resolve(__dirname, 'packages/cloud_aws_s3'),
          path.resolve(__dirname, 'packages/codemirror-svelte'),
        ],
      },
    ],
    // 'import/prefer-default-export': 'off',
    'import/no-deprecated': 'warn',

    /*
     * simple-import-sort seems to be the most stable import sorting currently,
     * disable others
     * See https://github.com/lydell/eslint-plugin-simple-import-sort/blob/1280c2ed6e3c0c76dad55d5fbec685fa18f299d1/examples/.eslintrc.js#L74-L92
     */
    'simple-import-sort/imports': [
      'warn',
      {
        // See: https://github.com/lydell/eslint-plugin-simple-import-sort/blob/main/examples/.eslintrc.js
        groups: [
          // Node.js builtins. You could also generate this regex if you use a `.js` config.
          // For example: `^(${require('module').builtinModules.join('|')})(/|$)`
          [
            '^(assert|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|https|module|net|os|path|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|tty|url|util|vm|zlib|freelist|v8|process|async_hooks|http2|perf_hooks)(/.*|$)',
          ],
          // Packages. `react` related packages come first.
          ['^react', '^@?\\w'],
          // Internal packages.
          ['^(@|@company|@ui|components|utils|config|vendored-lib)(/.*|$)'],
          // Side effect imports.
          ['^\\u0000'],
          // Parent imports. Put `..` last.
          ['^\\.\\.(?!/?$)', '^\\.\\./?$'],
          // Other relative imports. Put same-folder imports and `.` last.
          ['^\\./(?=.*/)(?!/?$)', '^\\.(?!/?$)', '^\\./?$'],
          // Style imports.
          ['^.+\\.s?css$'],
        ],
      },
    ],
    'simple-import-sort/exports': 'warn',
    'sort-imports': 'off',
    'import/order': 'off',

    // react
    // 'react/destructuring-assignment': 0,
    // 'react/forbid-prop-types': [2, { forbid: ['any'] }],
    // 'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx'] }],
    // 'react/jsx-fragments': [1],
    // 'react/jsx-key': 1,
    // 'react/jsx-props-no-spreading': 0,
    // 'react/jsx-one-expression-per-line': 0,
    // 'react/no-array-index-key': 1, // warning for now, don't know how to fix
    // 'react/no-unescaped-entities': 0,
    // 'react/prop-types': 0,
    // 'react/prefer-stateless-function': 0,
    // 'react/sort-comp': 0,

    'jsx-a11y/anchor-is-valid': [
      'error',
      {
        components: ['Link'],
        specialLink: ['to'],
        aspects: ['noHref', 'invalidHref', 'preferButton'],
      },
    ],
    'jsx-a11y/click-events-have-key-events': 0,
    'jsx-a11y/label-has-for': 0,
    'jsx-a11y/no-static-element-interactions': 0,
    'jsx-a11y/no-noninteractive-element-interactions': 0,

    'graphql/template-strings': [
      'error',
      {
        env: 'literal',
        schemaString,
        validators: [
          'ExecutableDefinitions',
          'FieldsOnCorrectType',
          'FragmentsOnCompositeTypes',
          'KnownArgumentNames',
          'KnownDirectives', // disabled by default in relay
          // 'KnownFragmentNames', // disabled by default in all envs
          'KnownTypeNames',
          'LoneAnonymousOperation',
          'NoFragmentCycles',
          'NoUndefinedVariables', // disabled by default in relay
          // 'NoUnusedFragments' // disabled by default in all envs
          // 'NoUnusedVariables' throws even when fragments use the variable
          'OverlappingFieldsCanBeMerged',
          'PossibleFragmentSpreads',
          'ProvidedRequiredArguments', // disabled by default in relay
          'ScalarLeafs', // disabled by default in relay
          'SingleFieldSubscriptions',
          'UniqueArgumentNames',
          'UniqueDirectivesPerLocation',
          'UniqueFragmentNames',
          'UniqueInputFieldNames',
          'UniqueOperationNames',
          'UniqueVariableNames',
          'ValuesOfCorrectType',
          'VariablesAreInputTypes',
          // 'VariablesDefaultValueAllowed',
          'VariablesInAllowedPosition',
        ],
      },
    ],
    'graphql/named-operations': [
      'error',
      {
        schemaString,
      },
    ],
    'graphql/required-fields': [
      'error',
      {
        env: 'literal',
        schemaString,
        requiredFields: ['_id', 'id'],
      },
    ],
  },
}

Leave a Reply

Your email address will not be published. Required fields are marked *