Eslint

编程入门 行业动态 更新时间:2024-10-25 15:20:14
Eslint - 脚本和模块的SourceType混合(Eslint - SourceType mixture of script and module)

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

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

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

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

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

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.

最满意答案

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

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

使用eslint .正常的脚本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 }

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

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.

更多推荐

本文发布于:2023-07-20 12:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Eslint

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!