All files / eslint-config-eloquence node.js

0% Statements 0/12
0% Branches 0/2
100% Functions 0/0
0% Lines 0/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                                                                                                                                                                                                                                                                                                                                 
/**
 * Module notes: This config handles two types of Node projects: ESM and
 * CommonJS.
 * - ESM projects will have a type of `module` in their package.json and are
 *   expected to still be using .js file extensions. These projects may have
 *   scripts or configuration files run by CommonJS tools (like Jest and
 *   ESLint), which will have .cjs file extensions. Overrides
 * - CommonJS projects are expected to use .js file extensions for all files,
 *   and all files will use commonJS modules
 */
 
'use strict'
 
const path = require('path')
 
const envRuleSeverities = require('./src/rule-severities')
const coreBestPractices = require('./src/rules/core-best-practices')
const coreEcmaScript = require('./src/rules/core-ecma-script')
const corePossibleErrors = require('./src/rules/core-possible-errors')
const coreStylisticIssues = require('./src/rules/core-stylistic-issues')
const coreVariables = require('./src/rules/core-variables')
const pluginImport = require('./src/rules/plugin-import')
const pluginNode = require('./src/rules/plugin-node')
const pluginTypescript = require('./src/rules/plugin-typescript')
 
const { NODE_ENV } = process.env
 
/**
 * Base configs for Node.js projects
 */
module.exports = {
  // Default expectation is a single config at root of project, with overrides
  // for directory and file customizations
  root: true,
 
  // Project custom ignore patterns, defaults to ignoring build directories
  // and forcing linting of dot files and directories
  ignorePatterns: ['!.*', 'coverage', 'node_modules', 'public', 'dist'],
 
  // Provides warnings for eslint-disable directives that aren't necessary
  reportUnusedDisableDirectives: true,
 
  // Node plugin will check package.json type field and set correct Node
  // globals, sourceType, and overrides for .cjs files
  extends: ['plugin:node/recommended'],
 
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'latest',
    ecmaFeatures: {},
  },
 
  plugins: ['@typescript-eslint', 'import', 'prettier'],
 
  settings: {
    // Assumes tools like eslint-loader aren't used
    'import/cache': Infinity,
    // Use Node resolver upgraded with `@` alias support
    'import/resolver': path.resolve(__dirname, 'src/resolver'),
    // TS Support - Extensions that plugin-import will be parse to check for exports
    'import/extensions': ['.cjs', '.js', '.mjs', '.ts'],
    // TS Support - Ensure that modules resolved with type definitions are considered external imports
    'import/external-module-folders': ['node_modules', 'node_modules/@types'],
  },
 
  env: {
    es6: true,
  },
 
  rules: envRuleSeverities(NODE_ENV, {
    // --- ESLint core rules configuration ---
    ...coreBestPractices,
    ...coreEcmaScript,
    ...corePossibleErrors,
    ...coreStylisticIssues,
    ...coreVariables,
 
    // --- Plugin rules configuration ---
    ...pluginImport,
    ...pluginNode,
 
    // Prettier formatting enforcement enabled by Prettier *plugin*
    'prettier/prettier': 'error',
 
    // Node ESM projects must specify all import extensions
    'import/extensions': ['error', 'ignorePackages'],
    // Node ESM requires full import paths (override default noUselessIndex)
    'import/no-useless-path-segments': 'error',
    // Core rules that conflict with Prettier not disabled by eslint-config-prettier
    'arrow-body-style': 'off',
    'prefer-arrow-callback': 'off',
  }),
 
  // --------------------------------------------------------
  // File overrides (Override directories, then extensions, then files)
  overrides: [
    // --- 💡 TypeScript files --------------------------
    {
      files: ['*.ts', '*.tsx'],
      parserOptions: {
        tsconfigRootDir: __dirname,
        // Project required for type-aware rules - include only for src otherwise any
        // file not in tsconfig include will break linting
        project: ['./tsconfig.json'],
 
        rules: {
          ...envRuleSeverities(NODE_ENV, pluginTypescript),
        },
      },
    },
    // --- 1️⃣ Source directory --------------------------
    {
      files: ['src/**/*'],
 
      rules: {
        // ℹ️ Prevent forgotten console.logs only needed in project source
        // code
        'no-console': NODE_ENV === 'test' ? 'error' : 'warn',
 
        // ℹ️ In project source code ensure that access to process.env is
        // controlled.
        'node/no-process-env': 'error',
 
        // ℹ️ Imported modules in project source need to be declared as
        // dependencies to ensure they're available in production
        'import/no-extraneous-dependencies': [
          'error',
          // Allow imports from devDependencies in story and test files
          { devDependencies: ['**/*.{spec}.{cjs,mjs,js}'] },
        ],
      },
    },
 
    // --- ✅ Test files --------------------------
    {
      files: ['*.spec.js'],
      extends: [
        'plugin:jest-formatting/strict',
        'plugin:jest/all',
        'plugin:jest-extended/all',
      ],
      plugins: ['jest', 'jest-extended', 'jest-formatting'],
      rules: {
        'jest/unbound-method': 'off', // requires addl ts configs to enable
        'jest/prefer-expect-assertions': 'off', // just a little toooo opinionated
      },
 
      env: { jest: true },
    },
 
    // --- 💾 CommonJS files --------------------------
    {
      files: ['*.cjs'], // Only for files *opted* into CJS *inside* an ESM project
      rules: {
        '@typescript-eslint/no-var-requires': 'off',
        'import/extensions': ['error', 'ignorePackages'],
      },
    },
  ],
}