Custom Neutrino Linting

By  on  
Neutrino

Last week my friend Eli Perelman shared Modern JavaScript Apps with Neutrino, an awesome new Node.js tool for creating amazing apps with minimal fuss.  No need to learn webpack, scour babel plugins, or search for what exactly is required to get a React.js app up and running -- just install Neutrino and go!  I've been super impressed with Eli's work and the ease of development for customization.

One important customization for me was the ability to modify default linting rules and running the lint routine from command line when I wanted to.  Neutrino does provide a default ESLint rule set, and does lint while you modify your code, but testing if linting passes within CI is also important.  Let's look at how we can create custom linting rules with our own custom preset!

Step 1:  Creating a Preset

Presets allow you to customize elements of your Neutrino app, like ESLint rules, Babel plugins, pathing, and other app-wide global configuration.  Let me first show you the code for custom linting rules and then I'll explain what's going on:

const lint = require('neutrino-lint-base');
const merge = require('deepmerge');

module.exports = neutrino => {
  // Implement custom linting
  lint(neutrino);
  neutrino.config.module
    .rule('lint')
    .loader('eslint', props => merge(props, {
      options: {
        globals: ['describe', 'expect', 'jest', 'test', 'document', 'window', 'fetch'],
        rules: {
          // Don't require () for single argument arrow functions
          'arrow-parens': 'off',
          // Don't require trailing commas
          'comma-dangle': 'off',
          // Don't require file extensions on imports
          'import/extensions': 'off',
          // Don't mark as unresolved without extensions
          'import/no-unresolved': 'off',
          // Don't let ESLint tell us how to use whitespace for imports
          'padded-blocks': 'off',
          // Hold off on propTypes for now
          'react/prop-types': 'off'
        },
        baseConfig: {
          extends: ['airbnb-base', 'plugin:react/recommended']
        }
      }
    }))
};

Sending neutrino into lint preps the Neutrino app for linting.  Next we use merge to deep merge the custom linting config with our own rules:

  1. Extend airbnb-base linting rules with are a very popular set of ES6 guidelines
  2. Extend recommended React.js linting guidelines
  3. Specify which globals we'll allow when linting
  4. Set values for very specific ESLint rules we do or don't want to enforce

Of course the rules I've customized above are completely my preference; you don't need to extend any existing ESLint libraries (like I did with airbnb and React) and you can enforce whichever rules you'd like.

Step 2:  .eslintrc.js

If you want to run linting from the command line at any time (in the case of CI or a post-commit hook, for example), you will need to create a .eslintrc.js file to kick off the linting:

const Neutrino = require('neutrino');
const pkg = require('./package.json');
const api = new Neutrino(pkg.config.presets);

module.exports = api.custom.eslintrc();

.eslintrc.js creates a Neutrino instance with presets defined in package.json (we'll get to that in the next section) and exposes a eslintrc() function that runs the lint routine.

Step 3:  Modify package.json

With the preset created with your custom linting rules in mind, a few changes to package.json must be made.  The first is adding this custom preset file to the config.presets array:

"config": {
  "presets": [
    "neutrino-preset-react",
    "conduit-preset.js"
  ]
},

Next we'll need to add Neutrino's airbnb preset to our dependency list:

yarn add neutrino-preset-airbnb-base -dev

Lastly we'll add a lint key to scripts so that we can run linting from command line:

"scripts": {
  "lint": "./node_modules/eslint/bin/eslint.js --ext .js,.jsx src/ test/",
}

Now we can run the following from command line:

yarn lint

Also note that the custom linting rule are applied to both the manual lint command as well as during webpack's live reload and linting routine!

I love Neutrino because it requires minimal configuration to get up and running but custom configuration is easy when you need to.  Keep an eye on Neutrino moving forward because development is shipping quickly and the community is rallying behind this amazing project!

Recent Features

  • By
    Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    prefers-color-scheme: CSS Media Query

    One device and app feature I've come to appreciate is the ability to change between light and dark modes. If you've ever done late night coding or reading, you know how amazing a dark theme can be for preventing eye strain and the headaches that result.

  • By
    MooTools TextOverlap Plugin

    Developers everywhere seem to be looking for different ways to make use of JavaScript libraries. Some creations are extremely practical, others aren't. This one may be more on the "aren't" side but used correctly, my TextOverlap plugin could add another interesting design element...

Discussion

  1. Peter Galiba

    Why do you need deepmerge in Step 1?

    • deepmerge merges the existing config with your new config.

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