Skip to content

Instantly share code, notes, and snippets.

@twilson63
Last active October 20, 2017 11:32
Show Gist options
  • Save twilson63/a2266100ae247d76c5f14b36462f53ae to your computer and use it in GitHub Desktop.
Save twilson63/a2266100ae247d76c5f14b36462f53ae to your computer and use it in GitHub Desktop.
Setting up eslint for Fun Functional JavaScript

Fun Functional JavaScript Linting

Linters are great ways to keep your team on the same page.

This document will walk through how to setup eslint to check your project using the following linting libraries:

  • standard
  • promise
  • fp
  • funfp

Standard supplies basic JavaScript standard rules, promise focuses on promise linting rules, fp focuses on core functional programming techniques, and funfp is more opinionated to focus on restricting patterns that can cause errors if null or undefined exist.

Getting Started

Npm install eslint and its plugins

npm install eslint eslint-plugin-standard eslint-config-standard eslint-plugin-promise eslint-plugin-fp eslint-plugin-funfp -D

Create a .eslintrc.js file in your project directory

module.exports = {
    "extends": [
      "standard",
      "plugin:fp/recommended",
      "plugin:funfp/recommended"
    ],
    "rules": {
      "fp/no-nil": "off",
      "fp/no-mutation": ["error", {
        "commonjs": true,
        "allowThis": true,
        "exceptions": [
          {"object": "foo", "property": "bar"}
        ]
      }],
      "promise/always-return": "error",
      "promise/no-return-wrap": "error",
      "promise/param-names": "error",
      "promise/catch-or-return": "error",
      "promise/no-native": "off",
      "promise/no-nesting": "warn",
      "promise/no-promise-in-callback": "warn",
      "promise/no-callback-in-promise": "warn",
      "promise/avoid-new": "warn"
    },
    "plugins": [
        "standard",
        "promise",
        "fp",
        "funfp"
    ]
}

These are the rules I have enabled for my project, but you can turn on/off any rules you like.

create a script in npm scripts for linting your

"lint": "eslint [files]"

You can use file paths like src/**/*.js

Incorporate the linting script in your test script

"test": "npm run lint & faucet src/**/**/*test.js"

Setup a git-hook to disallow push without lint and tests passing

try https://www.npmjs.com/package/pre-push

Conclusion

Linting can be a great constraint to guide your team to a common path, don't be afraid to adjust the rules as they best work for your team.

Appendix

How to disable a eslint line

// eslint-disable-next-line [rule name optional]

or

dosomething() // eslint-disable-line [rule name optional]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment