All files / standard-engine/lib resolve-eslint-config.js

93.16% Statements 218/234
69.76% Branches 30/43
100% Functions 7/7
93.16% Lines 218/234

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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 2352x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 15x 15x 15x 2x 2x 2x 2x 2x 2x 25x 20x 25x 20x 20x 25x 25x 25x 25x 2x 2x 2x 2x 2x 2x 10x 6x 10x 6x 6x 10x 2x 2x 2x 2x 2x 2x 10x 5x 10x 5x 5x 10x 10x 10x 10x 2x 2x 2x 2x 2x 2x 10x 5x 5x 5x 5x 10x             10x 5x 5x 5x 10x 5x 5x 10x 2x 2x 2x 2x 2x 2x 5x 1x 4x 1x 1x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 10x 10x 10x     5x 5x 5x 10x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x                 5x 5x 5x 5x 2x 2x  
/*! standard-engine. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
 
const fs = require('fs')
const path = require('path')
 
const pkgConf = require('pkg-conf')
 
const {
  ensureArray,
  ensureStringArrayValue,
  stringArrayToObj
} = require('./utils')
 
/** @typedef {import('../').EslintOptions} EslintOptions */
 
const DEFAULT_EXTENSIONS = [
  '.js',
  '.jsx',
  '.mjs',
  '.cjs'
]
 
const DEFAULT_IGNORE = [
  '**/*.min.js',
  'coverage/**',
  'node_modules/**',
  'vendor/**'
]
 
/**
 * @param {EslintOptions} eslintConfig
 * @param {string|string[]} [extensions]
 */
const addExtensions = (eslintConfig, extensions) => {
  if (!extensions) return
  eslintConfig.extensions = (eslintConfig.extensions || []).concat(extensions)
}
 
/**
 * @param {EslintOptions} eslintConfig
 * @param {string|string[]} [ignore]
 */
const addIgnore = (eslintConfig, ignore) => {
  if (!ignore) return
 
  if (!eslintConfig.baseConfig) eslintConfig.baseConfig = {}
 
  eslintConfig.baseConfig.ignorePatterns = [
    ...ensureArray(eslintConfig.baseConfig.ignorePatterns || []),
    ...ensureArray(ignore)
  ]
}
 
/**
 * @param {EslintOptions} eslintConfig
 * @param {string|string[]} [globals]
 */
const addGlobals = (eslintConfig, globals) => {
  if (!globals) return
 
  if (!eslintConfig.baseConfig) eslintConfig.baseConfig = {}
 
  eslintConfig.baseConfig.globals = stringArrayToObj(globals, eslintConfig.baseConfig.globals)
}
 
/**
 * @param {EslintOptions} eslintConfig
 * @param {string|string[]} [plugins]
 */
const addPlugins = (eslintConfig, plugins) => {
  if (!plugins) return
 
  if (!eslintConfig.baseConfig) eslintConfig.baseConfig = {}
 
  eslintConfig.baseConfig.plugins = [
    ...ensureArray(eslintConfig.baseConfig.plugins || []),
    ...ensureArray(plugins)
  ]
}
 
/**
 * @param {EslintOptions} eslintConfig
 * @param {string|string[]|{[key: string]: string}} [envs]
 */
const addEnvs = (eslintConfig, envs) => {
  if (!envs) return
 
  /** @type {string[]} */
  let values
 
  if (!Array.isArray(envs) && typeof envs !== 'string') {
    values = []
    // envs can be an object in `package.json`
    for (const key in envs) {
      const value = envs[key]
      if (value) values.push(value)
    }
  } else {
    values = ensureArray(envs)
  }
 
  if (!eslintConfig.baseConfig) eslintConfig.baseConfig = {}
 
  eslintConfig.baseConfig.env = stringArrayToObj(values, eslintConfig.baseConfig.env)
}
 
/**
 * @param {EslintOptions} eslintConfig
 * @param {string} [parser]
 */
const setParser = (eslintConfig, parser) => {
  if (!parser) return
 
  if (!eslintConfig.baseConfig) eslintConfig.baseConfig = {}
 
  eslintConfig.baseConfig.parser = parser
}
 
/**
 * @typedef ResolveOptions
 * @property {string} cmd
 * @property {string} cwd
 * @property {boolean} [fix]                automatically fix problems
 * @property {string[]|string} [ignore]
 * @property {string[]|string} [extensions]
 * @property {string[]|string} [globals]    custom global variables to declare
 * @property {string[]|string} [global]
 * @property {string[]|string} [plugins]    custom eslint plugins
 * @property {string[]|string} [plugin]
 * @property {string[]|string} [envs]       custom eslint environment
 * @property {string[]|string} [env]
 * @property {string} [parser]              custom js parser (e.g. babel-eslint)
 * @property {boolean} [useGitIgnore]       use .gitignore? (default: true)
 * @property {boolean} [usePackageJson]     use options from nearest package.json? (default: true)
 * @property {boolean} [noDefaultIgnore]
 * @property {boolean} [noDefaultExtensions]
 */
 
/**
 * @callback CustomEslintConfigResolver
 * @param {Readonly<EslintOptions>} eslintConfig
 * @param {Readonly<ResolveOptions>} opts
 * @param {import('pkg-conf').Config} packageOpts
 * @param {string} rootDir
 * @returns {EslintOptions}
 */
 
/**
 * @param {Readonly<ResolveOptions>} rawOpts
 * @param {Readonly<EslintOptions>} baseEslintConfig
 * @param {CustomEslintConfigResolver} [customEslintConfigResolver]
 * @returns {EslintOptions}
 */
const resolveEslintConfig = function (rawOpts, baseEslintConfig, customEslintConfigResolver) {
  const opts = {
    usePackageJson: true,
    useGitIgnore: true,
    gitIgnoreFile: ['.gitignore', '.git/info/exclude'],
    ...rawOpts
  }
 
  const eslintConfig = {
    ...baseEslintConfig,
    cwd: opts.cwd,
    fix: !!opts.fix
  }
 
  /** @type {import('pkg-conf').Config} */
  let packageOpts = {}
  let rootPath = ''
 
  if (opts.usePackageJson || opts.useGitIgnore) {
    packageOpts = pkgConf.sync(opts.cmd, { cwd: opts.cwd })
    const packageJsonPath = pkgConf.filepath(packageOpts)
    if (packageJsonPath) rootPath = path.dirname(packageJsonPath)
  }
 
  if (!opts.usePackageJson) packageOpts = {}
 
  addIgnore(eslintConfig, ensureStringArrayValue(packageOpts.ignore))
  addIgnore(eslintConfig, opts.ignore)
 
  if (!packageOpts.noDefaultIgnore && !opts.noDefaultIgnore) {
    addIgnore(eslintConfig, DEFAULT_IGNORE)
  }
 
  addExtensions(eslintConfig, ensureStringArrayValue(packageOpts.extensions))
  addExtensions(eslintConfig, opts.extensions)
 
  if (!packageOpts.noDefaultExtensions && !opts.noDefaultExtensions) {
    addExtensions(eslintConfig, DEFAULT_EXTENSIONS)
  }
 
  if (opts.useGitIgnore && rootPath !== '') {
    (Array.isArray(opts.gitIgnoreFile) ? opts.gitIgnoreFile : [opts.gitIgnoreFile])
      .map(gitIgnoreFile => {
        try {
          return fs.readFileSync(path.join(rootPath, gitIgnoreFile), 'utf8')
        } catch (err) {
          return null
        }
      })
      .filter(Boolean)
      .forEach(gitignore => {
        gitignore && addIgnore(eslintConfig, gitignore.split(/\r?\n/))
      })
  }
 
  addGlobals(eslintConfig, ensureStringArrayValue(packageOpts.globals || packageOpts.global))
  addGlobals(eslintConfig, opts.globals || opts.global)
 
  addPlugins(eslintConfig, ensureStringArrayValue(packageOpts.plugins || packageOpts.plugin))
  addPlugins(eslintConfig, opts.plugins || opts.plugin)
 
  addEnvs(eslintConfig, ensureStringArrayValue(packageOpts.envs || packageOpts.env))
  addEnvs(eslintConfig, opts.envs || opts.env)
 
  setParser(eslintConfig, typeof packageOpts.parser === 'string' ? packageOpts.parser : opts.parser)
 
  if (customEslintConfigResolver) {
    let rootDir
    if (opts.usePackageJson) {
      const filePath = pkgConf.filepath(packageOpts)
      rootDir = filePath ? path.dirname(filePath) : opts.cwd
    } else {
      rootDir = opts.cwd
    }
    return customEslintConfigResolver(eslintConfig, opts, packageOpts, rootDir)
  } else {
    return eslintConfig
  }
}
 
module.exports = { resolveEslintConfig }