ESLint: How to set "new-cap" rule's "capIsNewExceptions" option within a file?

5.2k views Asked by At

Here is my attempt to set ESLint's new-cap rule to accept "S" as an allowed function name:

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

I have tried to apply the ESLint documentation carefully.

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).

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.

But it doesn't work. I still receive the ESLint warning.

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?

1

There are 1 answers

0
sainaen On BEST ANSWER

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:
0 — disables the rule,
1 — makes it a warning,
2 — makes it an error.

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