简体   繁体   English

ESLint:如何在文件内设置“ new-cap”规则的“ capIsNewExceptions”选项?

[英]ESLint: How to set “new-cap” rule's “capIsNewExceptions” option within a file?

Here is my attempt to set ESLint's new-cap rule to accept "S" as an allowed function name: 这是我尝试将ESLint的 new-cap规则设置为接受"S"作为允许的函数名称:

/*eslint new-cap : [capIsNewExceptions : ["S"]] */
var S = require("string");
var lines = S(text).lines(); // <-- ESLint still gives a warning for cap 'S'!

My ESLint parser (within IntelliJ) continues to give me the new-cap warning, as noted. 如前所述,我的ESLint解析器(在IntelliJ中)继续向我发出new-cap警告。

I have tried to apply the ESLint documentation carefully. 我试图仔细地应用ESLint文档

From here , I see an example rule, which looks like this: /*eslint quotes: [2, "double"], curly: 2* /, in which I gather that the quotes and curly rules are being set, and that the quotes rule contains two options, which are therefore contained in brackets because the documentation says If a rule has additional options, you can specify them using array literal syntax (it says this right above the example). 这里 ,我看到一个示例规则,如下所示: /*eslint quotes: [2, "double"], curly: 2* /,在其中我收集到quotescurly规则已设置,并且quotes rule包含两个选项,因此包含在方括号中,因为文档中说: If a rule has additional options, you can specify them using array literal syntax (该示例在示例上方)。

Then, from the actual documentation for new-cap , I find capIsNewExceptions is provided as an option, and that the value of this option should be an array of desired function names - just as I've tried to do in my code, above. 然后,从new-cap的实际文档中 ,我发现capIsNewExceptions是作为一个选项提供的,并且该选项的值应该是所需函数名的数组-就像我在上面的代码中所做的那样。

But it doesn't work. 但这是行不通的。 I still receive the ESLint warning. 我仍然收到ESLint警告。

What is the correct syntax to support the use of customizing the capIsNewExceptions option for the new-cap rule inside a Javascript file for use with ESLint? 什么是正确的语法才能支持使用自定义capIsNewExceptions选项的Javascript文件中的new-cap规则与ESLint一起使用?

Try 尝试

/*eslint new-cap: [2, {capIsNewExceptions: ["S"]}]*/
var S = require("string");
var lines = S(text).lines();

Now, why does it work this way? 现在,为什么它会这样工作?

You're right about passing options to the rule using array, but in the docs they mention that the first element of this array is always a “rule ID”: number from 0 to 2, that defines how that rule is applied: 您使用数组将选项传递给规则是正确的,但是在文档中他们提到该数组的第一个元素始终是“规则ID”:从0到2的数字,它定义了如何应用规则:
0 — disables the rule, 0禁用规则,
1 — makes it a warning, 1发出警告,
2 — makes it an error. 2使其出错。

I'm too lazy to check, but I assume that the rest of the array is passed to the rule itself as an options property of the context . 我懒得检查,但是我假设数组的其余部分作为contextoptions属性传递给规则本身。 From the source code of the new-cap rule, it looks like it expects only one option that is an object with possible configuration properties like capIsNewExceptions , newIsCap , etc. new-cap规则的源代码看,它似乎只希望一个选项是一个对象,该对象具有诸如capIsNewExceptionsnewIsCap等可能的配置属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM