Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix ecmaVersion in one example, add checks #18241

Merged
merged 2 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/rules/strict.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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