Eslint-脚本和模块的SourceType混合 [英] Eslint - SourceType mixture of script and module

查看:103
本文介绍了Eslint-脚本和模块的SourceType混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们开始混入一些es6模块,当您使用import/export而不使用sourceType:script时,eslint会抱怨

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

但是,如果我将sourceType更改为module,则每个具有"use strict"的文件;顶部被标记为不需要在模块中使用严格.

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.

这些模块是我的jsx文件,而我的js文件是POJ,所以我需要两个sourceType都可以运行.

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

关于如何强制eslint在模块和脚本中运行的任何想法?我想避免运行两个单独的eslintrc文件和规则集,以使一个成为模块而另一个成为脚本.

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.

推荐答案

只要脚本具有一个扩展名,而模块具有不同的扩展名,则可以具有两个不同的配置.

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
    // ...
}

使用 eslint.来填充您的常规脚本,就像您一直在做的那样.默认为 sourceType:"script" ,默认情况下提取 .eslintrc.json ,默认情况下仅提取 *.js 文件.

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
}

现在您可以使用 eslint --config .eslintrc.modules.json --ext .jsx.来仅填充模块,这将拉出模块config,这只是普通配置的扩展 .eslintrc.json ,并且只会删除 *.jsx 文件.

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.

这篇关于Eslint-脚本和模块的SourceType混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆