Firebase云函数扩展运算符未通过ESLint检查并导致上传失败

9

请问有谁能帮忙解决代码检查错误?这个代码已经测试过并且完美运行。唯一的问题是我无法在云函数基础设施上部署它。

我确定.eslintrc文件出了问题。请问有人可以帮助我如何使用扩展运算符吗?

这是代码检查器报错的地方:

const rightChoices = _.map(snapshot.val(), (val, questionId) => {
  return { ...val, questionId};
})

这是我的.eslintrc文件:
{
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
  "jsx": true,
  "experimentalObjectRestSpread": true
}
}
"plugins":[
"promise"
],
"extends": "eslint:recommended",
"rules": {
// Removed rule "disallow the use of console" from recommended eslint rules
"no-console": "off",

// Removed rule "disallow multiple spaces in regular expressions" from recommended eslint rules
"no-regex-spaces": "off",

// Removed rule "disallow the use of debugger" from recommended eslint rules
"no-debugger": "off",

// Removed rule "disallow unused variables" from recommended eslint rules
"no-unused-vars": "off",

// Removed rule "disallow mixed spaces and tabs for indentation" from recommended eslint rules
"no-mixed-spaces-and-tabs": "off",

// Removed rule "disallow the use of undeclared variables unless mentioned in /*global */ comments" from recommended eslint rules
"no-undef": "off",

// Warn against template literal placeholder syntax in regular strings
"no-template-curly-in-string": 1,

// Warn if return statements do not either always or never specify values
"consistent-return": 1,

// Warn if no return statements in callbacks of array methods
"array-callback-return": 1,

// Require the use of === and !==
"eqeqeq": 2,

// Disallow the use of alert, confirm, and prompt
"no-alert": 2,

// Disallow the use of arguments.caller or arguments.callee
"no-caller": 2,

// Disallow null comparisons without type-checking operators
"no-eq-null": 2,

// Disallow the use of eval()
"no-eval": 2,

// Warn against extending native types
"no-extend-native": 1,

// Warn against unnecessary calls to .bind()
"no-extra-bind": 1,

// Warn against unnecessary labels
"no-extra-label": 1,

// Disallow leading or trailing decimal points in numeric literals
"no-floating-decimal": 2,

// Warn against shorthand type conversions
"no-implicit-coercion": 1,

// Warn against function declarations and expressions inside loop statements
"no-loop-func": 1,

// Disallow new operators with the Function object
"no-new-func": 2,

// Warn against new operators with the String, Number, and Boolean objects
"no-new-wrappers": 1,

// Disallow throwing literals as exceptions
"no-throw-literal": 2,

// Require using Error objects as Promise rejection reasons
"prefer-promise-reject-errors": 2,

// Enforce “for” loop update clause moving the counter in the right direction
"for-direction": 2,

// Enforce return statements in getters
"getter-return": 2,

// Disallow await inside of loops
"no-await-in-loop": 2,

// Disallow comparing against -0
"no-compare-neg-zero": 2,

// Warn against catch clause parameters from shadowing variables in the outer scope
"no-catch-shadow": 1,

// Disallow identifiers from shadowing restricted names
"no-shadow-restricted-names": 2,

// Enforce return statements in callbacks of array methods
"callback-return": 2,

// Require error handling in callbacks
"handle-callback-err": 2,

// Warn against string concatenation with __dirname and __filename
"no-path-concat": 1,

// Prefer using arrow functions for callbacks
"prefer-arrow-callback": 1,

// Return inside each then() to create readable and reusable Promise chains.
// Forces developers to return console logs and http calls in promises.
"promise/always-return": 2,

//Enforces the use of catch() on un-returned promises
"promise/catch-or-return": 2,

// Warn against nested then() or catch() statements
"promise/no-nesting": 1
}}

如果您在问题中添加实际的错误消息将会很有帮助。 - Doug Stevenson
3个回答

26

为了后人,因为这个问题已经有500次的查阅并且没有解决方案:对我来说这个方法有效 -

在.eslintrc.json中更改

"parserOptions": {
   // Required for certain syntax usages
   "ecmaVersion": 2017,
   "sourceType": "module"
 },

"parserOptions": {
   // Required for certain syntax usages
   "ecmaVersion": 2019,
   "sourceType": "module"
 },

那么尝试重新部署

#只能在我的机器上运行


3
"ecmaVersion": 2019也对我有用,谢谢。 - Ivan Chernykh
1
只有 "ecmaVersion": 2019 对我来说就足够了 :D - 0xh8h
运行得非常好! - Minoru

2
ESLint确实支持对象的rest/spread,但您需要通过配置选择加入。您可以将ecmaVersion设置为2018(在parserOptions中),或将ecmaFeatures.experimentalObjectRestSpread设置为true(也在parserOptions中,请注意需要创建一个ecmaFeatures对象)。
您需要像这样添加env.eslint:
{
  "env": {
    "es6": true
  },
  "extends": [
    "eslint:recommended",
    "google"
  ],
  "parserOptions": {
    "ecmaVersion": 9,
    "sourceType": "module"
  }
}

想了解更多信息,请阅读这篇文章:https://codeburst.io/es6-in-cloud-functions-for-firebase-959b35e31cb0。希望这能够帮到您。


我已经将 ecmaVersion 设置为 2018 并将 experimentalObjectRestSpread 设置为 true。请检查文件。但仍然无法正常工作。我正在使用 Firebase Cloud Functions 项目。当我添加这个时,错误会消失,但在部署期间会显示错误。 - Gaurav Harchwani
@GauravHarchwani,我已经添加了一个博客链接,请查看 :) - ParthS007

0

我曾经遇到过同样的问题。将Node更新到8+版本应该可以解决它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接