简体   繁体   中英

.eslintrc.js keys with “-” (dash)

eslint allows formats other than json, including .js if it exports as module. Unfortunately, the keys required by eslint include dashes like prefer-const below. Quoting is required. This appears to work.

But: Is there a workaround that allows the .js preferences to not require quotes? Prettier, for example, allows camel case for .prettierrc.js. That does not appear to work for eslint.

module.exports = {
  env: {
    browser: true,
    es6:     true
  },
  extends:  "standard",
  parserOptions: {
    sourceType: "module"
  },
  rules: {
    curly: [ 0 ],
    "prefer-const": [ 2 ]
  }
}

If it matters to you, I would go on and write a function to convert the key names from camel case to dash style.

I was able to come up with a proof-of-concept in a few lines, so that shouldn't be too much work.

function fromCamelCase(rules) {
  return Object.entries(rules).reduce(
    (obj, [key, value]) =>
      (obj[key.replace(/[A-Z]/, ch => `-${ch.toLowerCase()}`)] = value, obj),
    { }
  );
}

module.exports = {
  env: {
    browser: true,
    es6:     true
  },
  extends:  "standard",
  parserOptions: {
    sourceType: "module"
  },
  rules: fromCamelCase({
    curly: [ 0 ],
    preferConst: [ 2 ]
  })
}

If I'm reading the source code correctly, eslint doesn't allow aliasing rule names, so creating a custom plugin is not an option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM