Eslint - 脚本和模块的SourceType混合

Jef*_*rdt 5 javascript eslint

我们开始混合一些es6模块和eslint只是抱怨当你不使用sourceType:import时导入/导出

Parsing error: 'import' and 'export' may appear only with 'sourceType: module'at line 1 col 1
Run Code Online (Sandbox Code Playgroud)

但是,如果我将sourceType更改为module,那么每个文件都有'use strict'; 在顶部得到标记说模块中不需要使用严格.

模块是我的jsx文件,我的js文件是POJ所以我需要两个sourceTypes来运行.

关于如何强制eslint与模块和脚本一起行为的任何想法?我想避免运行两个单独的eslintrc文件和规则集只是为了让一个是模块而另一个是脚本.

btm*_*lls 9

只要您的脚本有一个扩展名并且您的模块具有不同的扩展名,那么您可以拥有两个不同的配置.

// .eslintrc.json
{
    // ...
    // Your normal config goes here
    // ...
}
Run Code Online (Sandbox Code Playgroud)

eslint .像你一直在做的那样,使用正常的脚本.它默认为默认sourceType: "script".eslintrc.json,默认情况下只打开*.js文件.

// .eslintrc.modules.json
{
    "extends": "./.eslintrc.json",
    "parserOptions": {
        "sourceType": "module"
    },
    // You can have additional module-specific config here if you want
}
Run Code Online (Sandbox Code Playgroud)

现在你可以只使用模块lint eslint --config .eslintrc.modules.json --ext .jsx .,这将拉动模块配置,这只是普通的扩展.eslintrc.json,它只会lint *.jsx文件.