Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/eslint-config / src / mergeConfig.js
Size: Mime:
// type Key = 'settings' | 'plugins' | 'rules' | 'globals' | 'env' | 'parser'
// type Extension = { [key in keyof Key]: any }

/**
 * @note this also defines the order
 */
function toDefaultObj() {
  const obj = {}
  obj.plugins = []
  obj.parser = ''
  obj.parserOptions = {}
  obj.settings = {}
  obj.rules = {}
  obj.globals = {}
  obj.env = {}
  // obj.extends = undefined
  // Object.seal(obj)
  return obj
}

const keys = Object.freeze(['parser', 'plugins', 'parserOptions', 'settings', 'env', 'rules', 'globals'])

function requireEslintStyle(path) {
  try {
    const required = require(path)
    return required
  } catch (requireException) {
    try {
      const required = require('eslint-config-' + path)
      return required
    } catch (requireAgainException) {
      console.error(requireException)
      console.error(requireAgainException)
      return {}
    }
  }
}

function mergeConfig(config) {
  const obj = toDefaultObj()

  config.extends.map(requireEslintStyle).forEach(extension => {
    // eslint-disable-next-line
    Object.keys(extension).forEach(key => {
      if (keys.includes(key)) {
        const value = extension[key]
        if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
          // console.debug('changing value of: ' + key + ' to: ' + value)
          obj[key] = value
        } else if (Array.isArray(value)) {
          // merge + uniq (for primitives anyway)
          obj[key] = Array.from(new Set([...obj[key], ...value]))
        } else if (typeof value === 'object') {
          // console.debug('merging: ' + key)
          Object.assign(obj[key], value)
        } else {
          const serialized = JSON.stringify({ [key]: value }, undefined, 2)
          throw new TypeError('value is not string, not object: ' + serialized)
        }
      } else {
        if (key === 'extends') {
          // console.log('@todo!!!')
          return
        }
        console.warn('key not in known keys: ' + key)
      }
    })
  })

  // trying to use it with `Linter` but https://github.com/eslint/eslint/issues/4119
  // obj.plugins = obj.plugins.map(plugin => require('eslint-plugin-' + plugin))

  return obj
}

exports.mergeConfig = mergeConfig