"Unexpected token" when using arrow function for firebase functions

Viewed 419

I am encountering an error with ESLint when attempting to deploy my functions to firebase. Specifically, it seems it is not liking arrow functions when I thought firebase supported arrow functions. I am using the ESLint provided by default initializing the functions files using firebase init functions.

/root/functions/helpertools.js
  62:84  error  Parsing error: Unexpected token =>

Line 62 is the first of the following code block:

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}
}

My .eslintrc.js is the following:

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    quotes: ["error", "double"],
  },
};

From my 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"
      }
    },

From my 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
}
1 Answers

I have tested this and I confirm that there are two things you definitely need, to fix the issue you are facing, would be to change in your package.json the scripts section to the following:

"scripts": { "lint": "eslint", ... } instead of “scripts”: {“lint”:”eslint .”, …} which is default. So, removing the . from there, which is auto-generated but might cause this kind of issues.

Arrow functions are an ES6 feature, but here you have an async arrow function. Async functions in general are an ES8 (or 2017) feature. Therefore you need to specify the change the ecmaVersion of the parser to version 8, so changing in your .eslintrc.js file to this:

parserOptions: { parser: 'babel-eslint', ecmaVersion: 8, },

This will let the parser know to expect the => token after async is used.

Related