gpt4 book ai didi

javascript - Eslint - 脚本和模块的 SourceType 混合体

转载 作者:数据小太阳 更新时间:2023-10-29 05:51:47 28 4
gpt4 key购买 nike

我们开始混入一些 es6 模块,当您在不使用 sourceType: 脚本的情况下使用 import/export 时,eslint 会公正地提示

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

但是,如果我将 sourceType 更改为模块,那么每个具有“use strict”的文件;在顶部被标记为模块中不需要 use strict。

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

关于如何强制 eslint 与模块和脚本一起工作有什么想法吗?我想避免运行两个单独的 eslintrc 文件和规则集,只是让一个是模块,另一个是脚本。

最佳答案

只要您的脚本有一个扩展名,而您的模块有一个不同的扩展名,那么您就可以有两种不同的配置。

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

使用 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 . 只检查模块,这将拉取模块配置,这只是正常的扩展.eslintrc.json,它只会检查 *.jsx 文件。

关于javascript - Eslint - 脚本和模块的 SourceType 混合体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36367656/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com