'How to prevent console.log when pushing commits with Husky

So... I'm not even entirely sure if my hooks are working but this is my current setup. I'm using "husky": "7.0.4" and "lint-staged": "12.1.2".

My pre-commit on .husky looks like this

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged

My package.json has this

{
  "lint-staged": {
    "src/**/*.{js,jsx,ts,tsx}": [
      "yarn lint",
      "git add"
    ]
  },
  "scripts": {
    "lint": "prettier --write 'src/**/*.{tsx, ts, css}' & eslint --max-warnings 0 --ignore-path .gitignore . --ext ts --ext tsx",
  }
}

I'm not entirely sure where to start to be able to get a hook that allows me to detect console.log() on my staged / pushed files.



Solution 1:[1]

If you have a .eslintrc file, you can add a rule to throw error or warning when running eslint like this:

rules: {
  "no-console": "error", // OR "warn" if you just want to throw a warning
}

Note that this will throw an error for any console method (console.log, console.error, console.warn). So if you want to specifically block only console.log, you can set it like this:

"rules": {
  "no-console": ["error", { "allow": ["warn", "error"] }]
}

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 Freudk1k2k3