Overriding Drupal 8's .eslintrc.json File in Your Theme With Extends

 

I had to do a little RTFMing today, and so I thought I'd post about it.

First of all, this is how you set up PhpStorm to use ES6 eslint settings. You may find it useful

Is the linter getting in your way? The first way to override an eslint setting is inline, disabling it on a one-off basis.

If you truly need a new .eslintrc.json file, then you should probably extend the one from Drupal core.

Here's how the linter looks for eslint files. TL;DR: like .gitignore files.

Finally, here's how the extends property of eslintrc.json. Basically, you point it to a file. Properties you specify in your file will not clobber the branch object that was previously specified, only leaves. Or at least that's what seems to happen and seems to be documented. I.e., in the below, defining rules: {import/no-extraneous-dependencies: error} doesn't overwrite the rules object entirely.

Here's what I ended up with at themes/custom/mytheme/.eslintrc.json:

{
  "extends": "../../../core/.eslintrc.json",
  "rules": {
    // We aren't using a package.json file for imports, so turn this off.
    "import/no-extraneous-dependencies": {
      "error": {
        "devDependencies": false,
        "optionalDependencies": false,
        "peerDependencies": false
      }
    },
    // We want to specify .js and .jsx file extensions.
    "import/extensions": ["error", "always", {
      "js": "always",
      "jsx": "always"
    }],
    // Warn, don't error, for unresolved import paths.
    "import/no-unresolved": "warn"
  }
}
}

And here's a list of all the rules you can override. You may want to check out the actual eslint-config-airbnb to see the values they're using. I have just been searching the codebase at /core/node_modules for the specific eslint rule after running npm install from the /core directory to get all the nodejs dependencies (including, of course, eslint-config-airbnb).

About the Author

Hi. My name is Jeremiah John. I'm a sf/f writer and activist.

I just completed a dystopian science fiction novel. I run a website which I created that connects farms with churches, mosques, and synagogues to buy fresh vegetables directly and distribute them on a sliding scale to those in need.

In 2003, I spent six months in prison for civil disobedience while working to close the School of the Americas, converting to Christianity, as one does, while I was in the clink.