eslint - per file parserOptions.sourceType?
Asked Answered
R

2

14

My project uses Webpack and es6 modules for most of the files. These files run in browser, and bundled by Webpack.

There are just a small number of files, that run in node. They are not touched by Webpack and I don't see any benefit including them in webpack. They don't support import as it's not implemented yet in node (or V8).

Now in .eslintrc, if I set parserOptions.sourceType to script, it errs out in the browser files ("import and export only allowed in module!"). If parserOptions.sourceType set to module, it errs out in node files.

So how to do per-file parserOptions? /* eslint-env xxx */ doesn't work in this case

Edit

I can probably use directory-specific .eslintrc, but that would mean duplicate all other configs for the sake of changing just one option. Any better option?

Rentier answered 19/9, 2016 at 2:46 Comment(2)
Are you able to take advantage of the the hierarchical nature of .eslintrc files? I was under the impression that they inherited the config from any .eslintrc in the parent directory. I have a vague memory of using that feature.Purity
@Purity Ah, thanks! It is hierarchical. It's not mentioned in doc AFAIK. Would you make it a answer so that I can accept itRentier
P
12

You should be able to take advantage of the hierarchical nature of ESLint configuration (.eslintrc) files:

ESLint will automatically look for them in the directory of the file to be linted, and in successive parent directories all the way up to the root directory of the filesystem. This option is useful when you want different configurations for different parts of a project or when you want others to be able to use ESLint directly without needing to remember to pass in the configuration file.

Note that .eslintrc files in child directories inherit the configurations from .eslintrc files in parent directories, so it's easy to override particular settings.

Purity answered 19/9, 2016 at 3:16 Comment(0)
Y
21

Another option is using overrides as such :

{
  "extends": "eslint:recommended",
  "overrides": [{
    "files": ["path/to/some/file.js", "path/to/some/folder/**.js"],
    "parserOptions": {
      "sourceType": "module"
    }
  }]
}

See https://eslint.org/docs/user-guide/configuring#example-configuration

The benefits of such a solution :

  • if you need only a single file in a folder to have specific overrides, you can
  • if you need to share the same overrides in multiple places, you prevent yourself from having duplicate files (which is good DRY practice).
Yeld answered 30/8, 2017 at 15:59 Comment(0)
P
12

You should be able to take advantage of the hierarchical nature of ESLint configuration (.eslintrc) files:

ESLint will automatically look for them in the directory of the file to be linted, and in successive parent directories all the way up to the root directory of the filesystem. This option is useful when you want different configurations for different parts of a project or when you want others to be able to use ESLint directly without needing to remember to pass in the configuration file.

Note that .eslintrc files in child directories inherit the configurations from .eslintrc files in parent directories, so it's easy to override particular settings.

Purity answered 19/9, 2016 at 3:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.