将箭头函数用于 firebase 函数时出现“意外标记”

Wil*_*lly 2 javascript node.js firebase eslint google-cloud-functions

当我尝试将我的函数部署到 firebase 时,我遇到了 ESLint 错误。具体来说,当我认为 firebase 支持箭头函数时,它似乎不喜欢箭头函数。我使用默认提供的 ESLint 来初始化函数文件firebase init functions

/root/functions/helpertools.js
  62:84  error  Parsing error: Unexpected token =>
Run Code Online (Sandbox Code Playgroud)

第 62 行是以下代码块的第一行:

const getCostEstimateBetweenPlaceIDs = async (placeIdOrigin,placeIDdestination)=> {
    let routeInfo = await calculateRoute(placeIdOrigin,placeIDdestination,true)
    let minutes = convertHourMinTextToMinutes(routeInfo.duration)
    let miles = convertKMTextToMiles(routeInfo.distance)

    return {cost:uberCostEstimate(minutes,miles),minutes:minutes,miles:miles}
}
Run Code Online (Sandbox Code Playgroud)

.eslintrc.js的如下:

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    quotes: ["error", "double"],
  },
};
Run Code Online (Sandbox Code Playgroud)

从我的 package.json 中:

    "@eslint/eslintrc": {
      "version": "0.4.3",
      "resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-0.4.3.tgz",
      "integrity": "sha512-J6KFFz5QCYUJq3pf0mjEcCJVERbzv71PUIDczuh9JkwGEzced6CO5ADLHB1rbf/+oPBtoPfMYNOpGDzCANlbXw==",
      "dev": true,
      "requires": {
        "ajv": "^6.12.4",
        "debug": "^4.1.1",
        "espree": "^7.3.0",
        "globals": "^13.9.0",
        "ignore": "^4.0.6",
        "import-fresh": "^3.2.1",
        "js-yaml": "^3.13.1",
        "minimatch": "^3.0.4",
        "strip-json-comments": "^3.1.1"
      }
    },
Run Code Online (Sandbox Code Playgroud)

从我的 package-lock.json 中:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "14"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}
Run Code Online (Sandbox Code Playgroud)

Pri*_*dra 6

我已经对此进行了测试,并且我确认您肯定需要两件事来解决您面临的问题,即将 package.json 中的脚本部分更改为以下内容:

\n

"scripts": { "lint": "eslint", ... }而不是\xe2\x80\x9cscripts\xe2\x80\x9d: {\xe2\x80\x9clint\xe2\x80\x9d:\xe2\x80\x9deslint .\xe2\x80\x9d, \xe2\x80\xa6}默认的。\n因此, .从那里删除它,它是自动生成的,但可能会导致此类问题。

\n

箭头函数是 ES6 功能,但这里有异步箭头函数。\n异步函数一般是 ES8(或 2017)功能。因此,您需要指定将解析器的 ecmaVersion 更改为版本 8,因此将 .eslintrc.js 文件更改为:

\n
parserOptions: { parser: 'babel-eslint', ecmaVersion: 8, },\n
Run Code Online (Sandbox Code Playgroud)\n

这将使解析器知道在使用 async 之后需要 => 标记。

\n