Skip to content

Commit

Permalink
docs: Disallow multiple rule configuration comments in the same examp…
Browse files Browse the repository at this point in the history
…le (#18116)
  • Loading branch information
mdjermanovic committed Feb 14, 2024
1 parent 9aa4df3 commit f95cd27
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
9 changes: 4 additions & 5 deletions docs/src/rules/func-name-matching.md
Expand Up @@ -54,8 +54,7 @@ Examples of **correct** code for this rule:

```js
/*eslint func-name-matching: "error"*/
/*eslint func-name-matching: ["error", "always"]*/ // these are equivalent
/*eslint-env es6*/
// equivalent to /*eslint func-name-matching: ["error", "always"]*/

var foo = function foo() {};
var foo = function() {};
Expand Down Expand Up @@ -157,7 +156,7 @@ Examples of **correct** code for the `{ considerPropertyDescriptor: true }` opti

```js
/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
/*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/ // these are equivalent
// equivalent to /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/
var obj = {};
Object.create(obj, {foo:{value: function foo() {}}});
Object.defineProperty(obj, 'bar', {value: function bar() {}});
Expand All @@ -173,7 +172,7 @@ Examples of **incorrect** code for the `{ considerPropertyDescriptor: true }` op

```js
/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
/*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/ // these are equivalent
// equivalent to /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/
var obj = {};
Object.create(obj, {foo:{value: function bar() {}}});
Object.defineProperty(obj, 'bar', {value: function baz() {}});
Expand All @@ -193,7 +192,7 @@ Examples of **incorrect** code for the `{ includeCommonJSModuleExports: true }`

```js
/*eslint func-name-matching: ["error", { "includeCommonJSModuleExports": true }]*/
/*eslint func-name-matching: ["error", "always", { "includeCommonJSModuleExports": true }]*/ // these are equivalent
// equivalent to /*eslint func-name-matching: ["error", "always", { "includeCommonJSModuleExports": true }]*/

module.exports = function foo(name) {};
module['exports'] = function foo(name) {};
Expand Down
18 changes: 16 additions & 2 deletions docs/src/rules/lines-around-comment.md
Expand Up @@ -653,9 +653,9 @@ const [

### ignorePattern

By default this rule ignores comments starting with the following words: `eslint`, `jshint`, `jslint`, `istanbul`, `global`, `exported`, `jscs`. To ignore more comments in addition to the defaults, set the `ignorePattern` option to a string pattern that will be passed to the [`RegExp` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp).
By default this rule ignores comments starting with the following words: `eslint`, `jshint`, `jslint`, `istanbul`, `global`, `exported`, `jscs`.

Examples of **correct** code for the `ignorePattern` option:
Examples of **correct** code for this rule:

::: correct

Expand All @@ -665,9 +665,23 @@ Examples of **correct** code for the `ignorePattern` option:
foo();
/* jshint mentioned in this comment */
bar();
```

:::

To ignore more comments in addition to the defaults, set the `ignorePattern` option to a string pattern that will be passed to the [`RegExp` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp).

Examples of **correct** code for the `ignorePattern` option:

::: correct

```js
/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */

foo();
/* jshint mentioned in this comment */
bar();

foo();
/* a valid comment using pragma in it */
```
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/bad-examples.md
Expand Up @@ -42,3 +42,13 @@ const foo = "baz";
```

:::

:::correct

```js
/* eslint no-restricted-syntax: "error" */

/* eslint no-restricted-syntax: ["error", "ArrayPattern"] */
```

:::
3 changes: 2 additions & 1 deletion tests/tools/check-rule-examples.js
Expand Up @@ -77,8 +77,9 @@ describe("check-rule-examples", () => {
"\x1B[0m \x1B[2m23:7\x1B[22m \x1B[31merror\x1B[39m Syntax error: Identifier 'foo' has already been declared\x1B[0m\n" +
"\x1B[0m \x1B[2m31:1\x1B[22m \x1B[31merror\x1B[39m Example code should contain a configuration comment like /* eslint no-restricted-syntax: \"error\" */\x1B[0m\n" +
"\x1B[0m \x1B[2m41:1\x1B[22m \x1B[31merror\x1B[39m Failed to parse JSON from ' doesn't allow this comment'\x1B[0m\n" +
"\x1B[0m \x1B[2m51:1\x1B[22m \x1B[31merror\x1B[39m Duplicate /* eslint no-restricted-syntax */ configuration comment. Each example should contain only one. Split this example into multiple examples\x1B[0m\n" +
"\x1B[0m\x1B[0m\n" +
"\x1B[0m\x1B[31m\x1B[1m✖ 6 problems (6 errors, 0 warnings)\x1B[22m\x1B[39m\x1B[0m\n" +
"\x1B[0m\x1B[31m\x1B[1m✖ 7 problems (7 errors, 0 warnings)\x1B[22m\x1B[39m\x1B[0m\n" +
"\x1B[0m\x1B[31m\x1B[1m\x1B[22m\x1B[39m\x1B[0m\n";

assert.strictEqual(normalizedStderr, expectedStderr);
Expand Down
9 changes: 9 additions & 0 deletions tools/check-rule-examples.js
Expand Up @@ -96,6 +96,15 @@ async function findProblems(filename) {
parseError.line += codeBlockToken.map[0] + 1;
problems.push(parseError);
} else if (Object.hasOwn(parseResult.config, title)) {
if (hasRuleConfigComment) {
problems.push({
fatal: false,
severity: 2,
message: `Duplicate /* eslint ${title} */ configuration comment. Each example should contain only one. Split this example into multiple examples.`,
line: codeBlockToken.map[0] + 1 + comment.loc.start.line,
column: comment.loc.start.column + 1
});
}
hasRuleConfigComment = true;
}
}
Expand Down

0 comments on commit f95cd27

Please sign in to comment.