9

One of the main features of StandardJS is that it doesn't require configuration.

The problem is that I want to configure it. I don't want to put:

/* eslint-env mocha */

...in every test file. I want to configure StandardJS to treat everything in the test directory as mocha tests.

I've found in the README that some configuration is possible, e.g.:

{
  "standard": {
    "globals": [ "myVar1", "myVar2" ]
  }
}

...but I'm struggling to find more comprehensive documentation about the configuration options. Is it possible to configure StandardJS to treat files in different directories differently?

user7637745
  • 965
  • 2
  • 14
  • 27
Erik B
  • 40,889
  • 25
  • 119
  • 135
  • https://github.com/standard/standard/blob/master/options.js hmmmmmmmmmmmm – Cody G Jun 13 '18 at 12:20
  • This vscode plugin allows the setting to be modified, maybe look for how they do it https://marketplace.visualstudio.com/items?itemName=chenxsan.vscode-standardjs – Cody G Jun 13 '18 at 12:23

1 Answers1

8

You have a couple of options to try out and see what works for your specific project depending on the recent implementation of StandardJS.


Define your own globals

in package.json:

"standard": {
  "globals": [
    "describe",
    "before",
    "after",
    "beforeEach",
    "afterEach",
    "it",
    "assert"
  ]
}

or in .eslintrc:

{
  "globals": {
    "describe": false,
    "before": false,
    "after": false,
    "beforeEach": false,
    "afterEach": false,
    "it": false,
    "assert": false
  }
}

More on ESLint's configuration.


Define an environment

in package.json:

"standard": {
  "env": {
    "mocha": true
  }
}

or in .eslintrc:

{
  "env": {
    "mocha": true
  }
}

Check out currently available environments


Run StandardJS as an NPM script with the environment specified

in package.json:

{
  "scripts": {
    "lint": "standard --env mocha"
  }
}

Use a plugin

after installing the plugin (e.g. eslint-plugin-mocha)

in package.json:

"standard": {
  "plugins": [
    "mocha"
  ]
}

or in .eslintrc:

{
  "plugins": [
    "mocha"
  ]
}

Create your own, customized rules based on StandardJS

Check out this repository. The quick rundown:

Install with:

npm install --save-dev eslint-config-standard eslint-plugin-standard eslint-plugin-promise eslint-plugin-import eslint-plugin-node

Then create a .eslintrc file by extending StandardJS and start to fill with your own rules:

{
  "extends": "standard"
}

Since StandardJS uses ESLint under the hood, you can pretty much configure it however you want it using ESLint's documentation.

user7637745
  • 965
  • 2
  • 14
  • 27
  • 3
    I tried to do that, but it doesn't pick up my `.eslintrc.json`. Are you saying that anything I can configure in a `.eslintrc` file would work in the standard configuration in `package.json`? – Erik B Jun 16 '18 at 20:26
  • StandardJS definitely recommends you to put your **per-project configurations into the project's package.json**, [so try that first](https://standardjs.com/#i-use-a-library-that-pollutes-the-global-namespace-how-do-i-prevent-variable-is-not-defined-errors). Other, than that, try creating a **.eslintrc** *(without any extension)* **OR** **eslintrc.json** *(without leading dot)* in your project's root directory and check, whether StandardJS picks it up. If not, looks like their current implementation prefers the **package.json** option. – user7637745 Jun 17 '18 at 03:12
  • If none of the above works, check out [**this repository**](https://github.com/standard/eslint-config-standard) and try its solution to configure StandardJS. Please, try these options out and get back to me about what solution worked for you. – user7637745 Jun 17 '18 at 03:21
  • That's what I did. I simply configured eslint to use the standard configuration, so that I can have different linting rules for my tests. However, that doesn't answer my question. – Erik B Jun 20 '18 at 14:16