Skip to content

Commit

Permalink
docs: fix ecmaVersion in one example, add checks (#18241)
Browse files Browse the repository at this point in the history
* docs: fix `ecmaVersion` in one example, add checks

* restore trailing spaces
  • Loading branch information
mdjermanovic committed Mar 30, 2024
1 parent 7747097 commit 26384d3
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/src/rules/strict.md
Expand Up @@ -169,7 +169,7 @@ function foo() {

:::

::: incorrect { "ecmaVersion": 6, "sourceType": "script" }
::: incorrect { "ecmaVersion": 2015, "sourceType": "script" }

```js
/*eslint strict: ["error", "function"]*/
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/bad-examples.md
Expand Up @@ -52,3 +52,19 @@ const foo = "baz";
```

:::

:::correct { "ecmaVersion": "latest" }

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

:::

:::correct { "ecmaVersion": 6 }

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

:::
32 changes: 32 additions & 0 deletions tests/fixtures/good-examples.md
Expand Up @@ -31,3 +31,35 @@ The following code block is not a rule example, so it won't be checked:
```js
!@#$%^&*()
```

:::correct { "ecmaVersion": 3, "sourceType": "script" }

```js
var x;
```

:::

:::correct { "ecmaVersion": 5, "sourceType": "script" }

```js
var x = { import: 5 };
```

:::

:::correct { "ecmaVersion": 2015 }

```js
let x;
```

:::

:::correct { "ecmaVersion": 2024 }

```js
let x = /a/v;
```

:::
5 changes: 4 additions & 1 deletion tests/tools/check-rule-examples.js
Expand Up @@ -7,6 +7,7 @@
const assert = require("assert");
const { execFile } = require("child_process");
const { promisify } = require("util");
const { LATEST_ECMA_VERSION } = require("../../conf/ecma-version");

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -78,8 +79,10 @@ describe("check-rule-examples", () => {
"\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[2m56:1\x1B[22m \x1B[31merror\x1B[39m Remove unnecessary \"ecmaVersion\":\"latest\"\x1B[0m\n" +
`\x1B[0m \x1B[2m64:1\x1B[22m \x1B[31merror\x1B[39m "ecmaVersion" must be one of ${[3, 5, ...Array.from({ length: LATEST_ECMA_VERSION - 2015 + 1 }, (_, index) => index + 2015)].join(", ")}\x1B[0m\n` +
"\x1B[0m\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✖ 9 problems (9 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
30 changes: 30 additions & 0 deletions tools/check-rule-examples.js
Expand Up @@ -13,6 +13,7 @@ const markdownItContainer = require("markdown-it-container");
const markdownItRuleExample = require("../docs/tools/markdown-it-rule-example");
const ConfigCommentParser = require("../lib/linter/config-comment-parser");
const rules = require("../lib/rules");
const { LATEST_ECMA_VERSION } = require("../conf/ecma-version");

//------------------------------------------------------------------------------
// Typedefs
Expand All @@ -28,6 +29,12 @@ const rules = require("../lib/rules");

const STANDARD_LANGUAGE_TAGS = new Set(["javascript", "js", "jsx"]);

const VALID_ECMA_VERSIONS = new Set([
3,
5,
...Array.from({ length: LATEST_ECMA_VERSION - 2015 + 1 }, (_, index) => index + 2015) // 2015, 2016, ..., LATEST_ECMA_VERSION
]);

const commentParser = new ConfigCommentParser();

/**
Expand Down Expand Up @@ -79,6 +86,29 @@ async function findProblems(filename) {
});
}

if (parserOptions && typeof parserOptions.ecmaVersion !== "undefined") {
const { ecmaVersion } = parserOptions;
let ecmaVersionErrorMessage;

if (ecmaVersion === "latest") {
ecmaVersionErrorMessage = 'Remove unnecessary "ecmaVersion":"latest".';
} else if (typeof ecmaVersion !== "number") {
ecmaVersionErrorMessage = '"ecmaVersion" must be a number.';
} else if (!VALID_ECMA_VERSIONS.has(ecmaVersion)) {
ecmaVersionErrorMessage = `"ecmaVersion" must be one of ${[...VALID_ECMA_VERSIONS].join(", ")}.`;
}

if (ecmaVersionErrorMessage) {
problems.push({
fatal: false,
severity: 2,
message: ecmaVersionErrorMessage,
line: codeBlockToken.map[0] - 1,
column: 1
});
}
}

const { ast, error } = tryParseForPlayground(code, parserOptions);

if (ast) {
Expand Down

0 comments on commit 26384d3

Please sign in to comment.