简体   繁体   English

.eslintrc.js键,带“-”(破折号)

[英].eslintrc.js keys with “-” (dash)

eslint allows formats other than json, including .js if it exports as module. eslint允许使用除json以外的其他格式,如果以模块形式导出,则包括.js。 Unfortunately, the keys required by eslint include dashes like prefer-const below. 不幸的是,eslint所需的键包括以下破折号,如preferred-const。 Quoting is required. 需要报价。 This appears to work. 这似乎起作用。

But: Is there a workaround that allows the .js preferences to not require quotes? 但是:是否有一种解决方法,允许.js首选项不需要引号? Prettier, for example, allows camel case for .prettierrc.js. 例如,Prettier允许使用.prettierrc.js的驼峰式大小写。 That does not appear to work for eslint. 这似乎不适用于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. 如果我正确地阅读了源代码 ,则eslint不允许使用别名规则名称,因此创建自定义插件不是一种选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 allow: 带有 eslintrc.js 的选项 - How to use allow: options with eslintrc.js React Native 中的“.eslintrc.js”是什么? - What is “.eslintrc.js” in React Native? 如何获取.eslintrc.js忽略所有无js文件 - How to get .eslintrc.js ignore all none js files 必须使用import加载ES Module.eslintrc.js - Must use import to load ES Module .eslintrc.js 在 .eslintrc.js 文件中使用导出默认值而不是 module.exports - Use export default in .eslintrc.js file instead of module.exports 错误:.eslintrc.js 中的 ESLint 配置无效:-意外的顶级属性“文件” - Error: ESLint configuration in .eslintrc.js is invalid: - Unexpected top-level property "files" env.es6=true 和 parserOptions.ecmaVersion=6 at.eslintrc.js 有什么区别? - What the difference between env.es6=true and parserOptions.ecmaVersion=6 at .eslintrc.js? npm init nuxt-app 结果出现问题“无法加载在‘.eslintrc.js » @nuxtjs/eslint-config’中声明的插件‘unicorn’” - npm init nuxt-app results a problem "Failed to load plugin 'unicorn' declared in '.eslintrc.js » @nuxtjs/eslint-config'" ESLint 8.31.0“错误:无法加载在‘.eslintrc.js » eslint-config-standard’中声明的插件‘import’:找不到模块‘array.prototype.flatmap’” - ESLint 8.31.0 "Error: Failed to load plugin 'import' declared in '.eslintrc.js » eslint-config-standard': Cannot find module 'array.prototype.flatmap" TypeScript的哈希表和带破折号的键 - TypeScript's hashtables and keys with a dash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM