Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eslint - SourceType mixture of script and module

We are starting to mix in some es6 modules and eslint justly complains when you use import/export when not using the sourceType: script

Parsing error: 'import' and 'export' may appear only with 'sourceType: module'at line 1 col 1

However if I change sourceType to module then every file that has 'use strict'; at the top gets flagged saying use strict isn't needed in modules.

The modules are my jsx files and my js files are POJ so I need both sourceTypes to be operating.

Any ideas on how to coerce eslint to behave with both modules and scripts? I would like to avoid running two seperate eslintrc files and rule sets just to have one be modules and the other be scripts.

like image 825
JeffBaumgardt Avatar asked Apr 02 '16 00:04

JeffBaumgardt


3 Answers

As long as as your scripts have one extension and your modules have a different extension, then you can have two different configs.

// .eslintrc.json
{
    // ...
    // Your normal config goes here
    // ...
}

Lint your normal scripts with eslint ., just like you've been doing. It defaults to sourceType: "script", pulls .eslintrc.json by default, and only lints *.js files by default.

// .eslintrc.modules.json
{
    "extends": "./.eslintrc.json",
    "parserOptions": {
        "sourceType": "module"
    },
    // You can have additional module-specific config here if you want
}

Now you can lint just the modules using eslint --config .eslintrc.modules.json --ext .jsx ., which will pull the modules config, which is just an extension of the normal .eslintrc.json, and it will only lint the *.jsx files.

like image 71
btmills Avatar answered Oct 05 '22 18:10

btmills


There's a nicer approach now in eslint 4.1.0+:

module.exports = {
  overrides: [
    {
      files: [ "rollup.config.js" ],
      parserOptions: { sourceType: "module" },
    }
  ]
};

You can also change rules or whatever you want, per-glob, in the overrides section.

like image 30
Joe Hildebrand Avatar answered Oct 05 '22 17:10

Joe Hildebrand


I've created the main file in the root folder with option sourceType: script. When in a folder ./front file .eslintrc.json:

{
    "extends": "../.eslintrc.json",
    "parserOptions": {
        "sourceType": "module"
    }
}
like image 44
egmen Avatar answered Oct 05 '22 18:10

egmen