'ESlint Parsing error: Unexpected token => when deploying firebase functions

Code:

exports.detectEvilUsers = functions.firestore
    .document('messages/{msgId}')
    .onCreate(async (doc, ctx) => {
// code 
});

When I try "firebase deploy --only functions", the message ESlint Parsing error: Unexpected token => appears

Here's my package.json:

{
  "name": "firebase project",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/eslint-parser": "^7.12.13",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "babel-jest": "^26.6.3",
    "firebase": "^8.2.6",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-firebase-hooks": "^2.2.0",
    "react-scripts": "4.0.2",
    "reactstrap": "^8.9.0",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/preset-env": "^7.12.13",
    "eslint": "^7.19.0",
    "eslint-plugin-react": "^7.22.0"
  }
}

My .eslintrc.js:

module.exports = {
    "env": {
        "browser": true,
        "es2021": true
    },
    "parser": "babel-eslint",
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 12,
        "sourceType": "module"
    },
    "plugins": [
        "react"
    ],
    "rules": {
    }
};

and my package.json inside /functions:

{
  "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": "12"
  },
  "main": "index.js",
  "dependencies": {
    "@babel/eslint-parser": "^7.12.13",
    "bad-words": "^3.0.4",
    "firebase-admin": "^9.2.0",
    "firebase-functions": "^3.11.0"
  },
  "devDependencies": {
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

And the .eslintrc.js inside /functions:

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

I installed babel, tried several configs. Nothing works so far. I do not know what to do.



Solution 1:[1]

// I think you have forgot to add closing } 
// this is the right way to create a trigger if there is still an error then that error would not be a trigger error
exports.myTrigger = functions.firestore
  .document('collection-name/{id}')
  .onCreate((snap, context) => { 
  
       const id = context.params.id;
       const data = snap.data();
  
  })

Solution 2:[2]

I found this here: https://javascript.tutorialink.com/parsing-error-unexpected-token-when-trying-to-deploy-firebase-cloud-function-i-couldnt-find-any-answers-on-here/

It isn't choking on the arrow function per se, but rather the async arrow function.

For some reason eslint as installed by the firebase cli defaults to es6: true in firebase/.eslintrc.js. I'm surprised as async arrow functions are pretty common in 2022. Try changing that to something like:

module.exports = {
  root: true,
  env: {
    es2017: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    "quotes": ["error", "double"],
    "max-len": "off",
    "semi": ["error", "never"],
  },
}

I don't know if es2017 is the best version to use in 2022, so someone please inform me what is currently standard.

Solution 3:[3]

std::vector::erase invalidates iterators and references at or after the point of the erase, but the main issue with the posted code is that std::remove shifts the elements in the range, so that i may already be the next element and ++i will skip it.

The OP could use the erase-remove idiom (once!):

auto is_even = [](auto x){ return x % 2 == 0; };
v.erase( std::remove_if(v.begin(), v.end(), is_even)
       , v.end() );

Since C++20, we can use std::erase_if:

std::erase_if(v, is_even);

Solution 4:[4]

Your code is wrong. Test with this input and you will see it break: {1,2,2,2,2,1}

When you remove, all elements are shifted so that the next element is the current one. Because of that you need to either skip the increment, or decrement i when you remove any element. You also need to resize the vector to remove the tail with the "removed" items. Something like:

        if(v[i] % 2 == 0)
        {
            auto it2 = remove(v.begin(), v.end(), v[i]);
            v.erase(it2);
            v.resize(v.size()-1);
            --i;
        }

Having said that, this approach is not efficient and is very risky. You should use erase-remove idiom that "compacts" the elements matching the query to the beginning of the vector, before removing the remaining elements from the tail. This approach has the huge advantage of updating the whole vector only once.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Shoman
Solution 2 bajaco
Solution 3 Bob__
Solution 4