Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eslint - per file parserOptions.sourceType?

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?

like image 601
Boyang Avatar asked Sep 19 '16 02:09

Boyang


2 Answers

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).
like image 195
Aymeric Bouzy aybbyk Avatar answered Nov 12 '22 19:11

Aymeric Bouzy aybbyk


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.

like image 12
cartant Avatar answered Nov 12 '22 19:11

cartant