Adding ESLint with gulp.js

By  on  

I've noticed that I am a loose coder on my personal projects but want some level of decorum on Mozilla and other open source projects.  The more developers you have contributing to a project, the tighter the ship you must keep.  The easiest way to do that is requiring contributions to meet a certain code convention criteria via a tool like ESLint.  Since I like to use gulp.js for my build process, I thought I'd share a very basic use of ESLint for your project.

You start by adding ESLint to your package.json file or installing via NPM manually:

npm install gulp-eslint

With ESLint available somewhere within the node path, you can set up a lint task within your gulpfile.js:

gulp.task('lint', function() {
  return gulp.src('lib/**').pipe(eslint({
    'rules':{
        'quotes': [1, 'single'],
        'semi': [1, 'always']
    }
  }))
  .pipe(eslint.format())
  // Brick on failure to be super strict
  .pipe(eslint.failOnError());
});

You can get a full list of rules and possible values here.  How strict you want to be depends on your general philosophy within JavaScript.  Many people make lint a part of their test task as well so that travis-ci can reject code that isn't up to snuff.

Now that I've written this post, I'll probably take the time to add ESLint to my personal projects so that I can get in the habit of always coding to a certain standard.  Practice makes perfect!

Recent Features

  • By
    Welcome to My New Office

    My first professional web development was at a small print shop where I sat in a windowless cubical all day. I suffered that boxed in environment for almost five years before I was able to find a remote job where I worked from home. The first...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

  • By
    MooTools HTML Police: dwMarkupMarine

    We've all inherited rubbish websites from webmasters that couldn't master valid HTML. You know the horrid markup: paragraph tags with align attributes and body tags with background attributes. It's almost a sin what they do. That's where dwMarkupMarine comes in.

  • By
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

Discussion

  1. Bradley

    One thing you’d likely want to handle differently is putting your linting rules in the root directory of your project in a file named .eslintrc. Then it’s won’t bloat up your gulpfile if you have lots of rules, stays in your git repos, etc.

    There’s also a .eslintignore to specify directories and files to avoid for linting, for node_modules for example.

  2. I can recommend using lints as Editor/Ide plugins.
    It may be annoying (errors and warning pop up while you write) but on the other hand it helps to learn the style.

    Atom has quite collection of various lints: https://atom.io/users/AtomLinter

  3. nathan

    gulp-eslint has not been updated for a while and still uses ESLint 6 instead of ESLint 8, missing a lot of new features. I would recommend using an alternative that is actively maintained like gulp-eslint-new, or simply running ESLint as an npm task.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!