Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to ensure no console.log on precommit? #4684

Closed
ggregoire opened this issue Jun 26, 2018 · 6 comments
Closed

Best way to ensure no console.log on precommit? #4684

ggregoire opened this issue Jun 26, 2018 · 6 comments

Comments

@ggregoire
Copy link

Question

Since we can't update the ESLint config without ejecting, I'm curious how people achieve this?

Run something like detect-log with Husky?

@bugzpodder
Copy link

Can you elaborate a little bit more on your question?

@ggregoire
Copy link
Author

ggregoire commented Jun 26, 2018

With Husky and Lint-staged, you can run commands with a precommit hook, for example Prettier, Flow, ESLint, etc. In doing so, if you have a Flow or ESLint error in your code, Husky will block the commit process. I do it inside projects bootstrapped with CRA and it works pretty well.

Now I'd like to block the commit process if there are remaining console.log in the code.

The easiest way would be to add https://eslint.org/docs/rules/no-console to the ESLint config. But we can't edit this config without ejecting. So I'm wondering how people achieve this.

@bugzpodder
Copy link

Sure, its actually fairly simple:

  1. create .eslintrc.json in your cra app directory:
    Here is mine:
{
  "env": {
    "browser": true,
    "es6": true,
    "jest": true,
    "node": true
  },
  "extends": ["eslint:recommended", "plugin:flowtype/recommended", "plugin:react/recommended"],
  "parser": "babel-eslint",
  "parserOptions": {
    "allowImportExportEverywhere": false,
    "ecmaFeatures": {
      "experimentalObjectRestSpread": true,
      "jsx": true
    },
    "sourceType": "module"
  },
  "plugins": ["flowtype", "react"],
  "rules": {
    "brace-style": ["error", "1tbs", { "allowSingleLine": true }],
  1. Install the same version of eslint as create-react-app:
 "devDependencies": {
    "babel-eslint": "8.2.1",
    "eslint": "4.19.1",
  1. add the appropriate precommit hooks:
"lint-staged": {
    "concurrent": false,
    "linters": {
      "src/**/*.{js,jsx}": [
        "prettier --write",
        "eslint --fix",
        "git add"
      ],

@miraage
Copy link

miraage commented Jun 27, 2018

@bugzpodder, I see no fix option for no-console rule.

https://eslint.org/docs/rules/no-console

compare to

https://eslint.org/docs/rules/semi

@bugzpodder
Copy link

bugzpodder commented Jun 27, 2018

Yes, it will not automatically fix it. So if you left out a console.log in your code, precommit with eslint --fix will fail if you have the no-console enabled and the developer needs to manually addreess it by removing it or add an eslint-ignore comment.

This should address the author's intent of blocking commits with console.log. The --fix option is meant to address the other most common issues.

@ggregoire
Copy link
Author

@bugzpodder It sounds good. Let me try it then I'll close the issue. :)

@Timer Timer closed this as completed Jul 23, 2018
@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants