Skip to content

Commit eec01f0

Browse files
authored
docs: switch rule examples config format to languageOptions (#19277)
* docs: switch rule examples config format to `languageOptions` * revert trailing spaces * mark languageOptions as possibly undefined in callbacks
1 parent b36ca0a commit eec01f0

12 files changed

+50
-44
lines changed

docs/.eleventy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,20 @@ module.exports = function(eleventyConfig) {
197197

198198
// markdown-it plugin options for playground-linked code blocks in rule examples.
199199
const ruleExampleOptions = markdownItRuleExample({
200-
open({ type, code, parserOptions, env, codeBlockToken }) {
200+
open({ type, code, languageOptions, env, codeBlockToken }) {
201201

202-
prismESLintHook.addContentMustBeMarked(codeBlockToken.content, parserOptions);
202+
prismESLintHook.addContentMustBeMarked(codeBlockToken.content, languageOptions);
203203

204204
const isRuleRemoved = !Object.hasOwn(env.rules_meta, env.title);
205205

206206
if (isRuleRemoved) {
207207
return `<div class="${type}">`;
208208
}
209209

210-
// See https://github.com/eslint/eslint.org/blob/ac38ab41f99b89a8798d374f74e2cce01171be8b/src/playground/App.js#L44
210+
// See https://github.com/eslint/eslint.org/blob/29e1d8a000592245e4a30c1996e794643e9b263a/src/playground/App.js#L91-L105
211211
const state = encodeToBase64(
212212
JSON.stringify({
213-
options: { parserOptions },
213+
options: languageOptions ? { languageOptions } : void 0,
214214
text: code
215215
})
216216
);

docs/src/rules/jsx-quotes.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ This rule has a string option:
3737

3838
Examples of **incorrect** code for this rule with the default `"prefer-double"` option:
3939

40-
:::incorrect { "ecmaFeatures": { "jsx": true } }
40+
:::incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
4141

4242
```jsx
4343
/*eslint jsx-quotes: ["error", "prefer-double"]*/
@@ -49,7 +49,7 @@ Examples of **incorrect** code for this rule with the default `"prefer-double"`
4949

5050
Examples of **correct** code for this rule with the default `"prefer-double"` option:
5151

52-
:::correct { "ecmaFeatures": { "jsx": true } }
52+
:::correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
5353

5454
```jsx
5555
/*eslint jsx-quotes: ["error", "prefer-double"]*/
@@ -64,7 +64,7 @@ Examples of **correct** code for this rule with the default `"prefer-double"` op
6464

6565
Examples of **incorrect** code for this rule with the `"prefer-single"` option:
6666

67-
:::incorrect { "ecmaFeatures": { "jsx": true } }
67+
:::incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
6868

6969
```jsx
7070
/*eslint jsx-quotes: ["error", "prefer-single"]*/
@@ -76,7 +76,7 @@ Examples of **incorrect** code for this rule with the `"prefer-single"` option:
7676

7777
Examples of **correct** code for this rule with the `"prefer-single"` option:
7878

79-
:::correct { "ecmaFeatures": { "jsx": true } }
79+
:::correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
8080

8181
```jsx
8282
/*eslint jsx-quotes: ["error", "prefer-single"]*/

docs/src/rules/keyword-spacing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ if (foo) {
5858

5959
Examples of **correct** code for this rule with the default `{ "before": true }` option:
6060

61-
::: correct { "ecmaFeatures": { "jsx": true } }
61+
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
6262

6363
```jsx
6464
/*eslint keyword-spacing: ["error", { "before": true }]*/
@@ -172,7 +172,7 @@ if(foo) {
172172

173173
Examples of **correct** code for this rule with the default `{ "after": true }` option:
174174

175-
::: correct { "ecmaFeatures": { "jsx": true } }
175+
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
176176

177177
```jsx
178178
/*eslint keyword-spacing: ["error", { "after": true }]*/

docs/src/rules/no-extra-parens.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ foo ? bar : (baz || qux);
212212

213213
Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "all" }` options:
214214

215-
::: correct { "ecmaFeatures": { "jsx": true } }
215+
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
216216

217217
```jsx
218218
/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "all" }] */
@@ -228,7 +228,7 @@ const ThatComponent = (
228228

229229
Examples of **incorrect** code for this rule with the `all` and `{ "ignoreJSX": "multi-line" }` options:
230230

231-
::: incorrect { "ecmaFeatures": { "jsx": true } }
231+
::: incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
232232

233233
```jsx
234234
/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "multi-line" }] */
@@ -240,7 +240,7 @@ const ThatComponent = (<div><p /></div>)
240240

241241
Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "multi-line" }` options:
242242

243-
::: correct { "ecmaFeatures": { "jsx": true } }
243+
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
244244

245245
```jsx
246246
/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "multi-line" }] */
@@ -260,7 +260,7 @@ const ThatComponent = (
260260

261261
Examples of **incorrect** code for this rule with the `all` and `{ "ignoreJSX": "single-line" }` options:
262262

263-
::: incorrect { "ecmaFeatures": { "jsx": true } }
263+
::: incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
264264

265265
```jsx
266266
/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "single-line" }] */
@@ -280,7 +280,7 @@ const ThatComponent = (
280280

281281
Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "single-line" }` options:
282282

283-
::: correct { "ecmaFeatures": { "jsx": true } }
283+
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
284284

285285
```jsx
286286
/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "single-line" }] */

docs/src/rules/no-inline-comments.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Comments inside the curly braces in JSX are allowed to be on the same line as th
5454

5555
Examples of **incorrect** code for this rule:
5656

57-
::: incorrect { "ecmaFeatures": { "jsx": true } }
57+
::: incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
5858

5959
```jsx
6060
/*eslint no-inline-comments: "error"*/
@@ -74,7 +74,7 @@ var bar = (
7474

7575
Examples of **correct** code for this rule:
7676

77-
::: correct { "ecmaFeatures": { "jsx": true } }
77+
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
7878

7979
```jsx
8080
/*eslint no-inline-comments: "error"*/

docs/src/rules/no-irregular-whitespace.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function thing() {
202202

203203
Examples of additional **correct** code for this rule with the `{ "skipJSXText": true }` option:
204204

205-
::: correct { "ecmaFeatures": { "jsx": true } }
205+
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
206206

207207
```jsx
208208
/*eslint no-irregular-whitespace: ["error", { "skipJSXText": true }]*/

docs/src/rules/no-unused-expressions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ JSX is most-commonly used in the React ecosystem, where it is compiled to `React
251251

252252
Examples of **incorrect** code for the `{ "enforceForJSX": true }` option:
253253

254-
::: incorrect { "ecmaFeatures": { "jsx": true } }
254+
::: incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
255255

256256
```jsx
257257
/*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/
@@ -265,7 +265,7 @@ Examples of **incorrect** code for the `{ "enforceForJSX": true }` option:
265265

266266
Examples of **correct** code for the `{ "enforceForJSX": true }` option:
267267

268-
::: correct { "ecmaFeatures": { "jsx": true } }
268+
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
269269

270270
```jsx
271271
/*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/

docs/src/rules/space-before-keywords.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function bar() {
6767

6868
Examples of **correct** code for this rule with the default `"always"` option:
6969

70-
::: correct { "ecmaFeatures": { "jsx": true } }
70+
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
7171

7272
```js
7373
/*eslint space-before-keywords: ["error", "always"]*/

docs/tools/markdown-it-rule-example.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
const { docsExampleCodeToParsableCode } = require("./code-block-utils");
44

5-
/** @typedef {import("../../lib/shared/types").ParserOptions} ParserOptions */
5+
/** @typedef {import("../../lib/shared/types").LanguageOptions} LanguageOptions */
66

77
/**
88
* A callback function to handle the opening of container blocks.
99
* @callback OpenHandler
1010
* @param {Object} data Callback data.
1111
* @param {"correct" | "incorrect"} data.type The type of the example.
1212
* @param {string} data.code The example code.
13-
* @param {ParserOptions} data.parserOptions The parser options to be passed to the Playground.
13+
* @param {LanguageOptions | undefined} data.languageOptions The language options to be passed to the Playground.
1414
* @param {Object} data.codeBlockToken The `markdown-it` token for the code block inside the container.
1515
* @param {Object} data.env Additional Eleventy metadata, if available.
1616
* @returns {string | undefined} If a text is returned, it will be appended to the rendered output
@@ -31,7 +31,7 @@ const { docsExampleCodeToParsableCode } = require("./code-block-utils");
3131
*
3232
* - Ensure that the plugin instance only matches container blocks tagged with 'correct' or
3333
* 'incorrect'.
34-
* - Parse the optional `parserOptions` after the correct/incorrect tag.
34+
* - Parse the optional `languageOptions` after the correct/incorrect tag.
3535
* - Apply common transformations to the code inside the code block, like stripping '⏎' at the end
3636
* of a line or the last newline character.
3737
*
@@ -47,7 +47,7 @@ const { docsExampleCodeToParsableCode } = require("./code-block-utils");
4747
*
4848
* markdownIt()
4949
* .use(markdownItContainer, "rule-example", markdownItRuleExample({
50-
* open({ type, code, parserOptions, codeBlockToken, env }) {
50+
* open({ type, code, languageOptions, codeBlockToken, env }) {
5151
* // do something
5252
* }
5353
* close() {
@@ -72,14 +72,14 @@ function markdownItRuleExample({ open, close }) {
7272
return typeof text === "string" ? text : "";
7373
}
7474

75-
const { type, parserOptionsJSON } = /^\s*(?<type>\S+)(\s+(?<parserOptionsJSON>\S.*?))?\s*$/u.exec(tagToken.info).groups;
76-
const parserOptions = { sourceType: "module", ...(parserOptionsJSON && JSON.parse(parserOptionsJSON)) };
75+
const { type, languageOptionsJSON } = /^\s*(?<type>\S+)(\s+(?<languageOptionsJSON>\S.*?))?\s*$/u.exec(tagToken.info).groups;
76+
const languageOptions = languageOptionsJSON ? JSON.parse(languageOptionsJSON) : void 0;
7777
const codeBlockToken = tokens[index + 1];
7878

7979
// Remove trailing newline and presentational `⏎` characters (https://github.com/eslint/eslint/issues/17627):
8080
const code = docsExampleCodeToParsableCode(codeBlockToken.content);
8181

82-
const text = open({ type, code, parserOptions, codeBlockToken, env });
82+
const text = open({ type, code, languageOptions, codeBlockToken, env });
8383

8484
// Return an empty string to avoid appending unexpected text to the output.
8585
return typeof text === "string" ? text : "";

docs/tools/prism-eslint-hook.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ try {
2727
// ignore
2828
}
2929

30-
/** @typedef {import("../../lib/shared/types").ParserOptions} ParserOptions */
30+
/** @typedef {import("../../lib/shared/types").LanguageOptions} LanguageOptions */
3131

3232
/**
3333
* Content that needs to be marked with ESLint
@@ -37,19 +37,19 @@ let contentMustBeMarked;
3737

3838
/**
3939
* Parser options received from the `::: incorrect` or `::: correct` container.
40-
* @type {ParserOptions|undefined}
40+
* @type {LanguageOptions|undefined}
4141
*/
42-
let contentParserOptions;
42+
let contentLanguageOptions;
4343

4444
/**
4545
* Set content that needs to be marked.
4646
* @param {string} content Source code content that marks ESLint errors.
47-
* @param {ParserOptions} options The options used for validation.
47+
* @param {LanguageOptions} options The options used for validation.
4848
* @returns {void}
4949
*/
5050
function addContentMustBeMarked(content, options) {
5151
contentMustBeMarked = content;
52-
contentParserOptions = options;
52+
contentLanguageOptions = options;
5353
}
5454

5555
/**
@@ -113,7 +113,7 @@ function installPrismESLintMarkerHook() {
113113
return;
114114
}
115115
contentMustBeMarked = void 0;
116-
const parserOptions = contentParserOptions;
116+
const config = contentLanguageOptions ? { languageOptions: contentLanguageOptions } : {};
117117

118118
const code = env.code;
119119

@@ -148,7 +148,7 @@ function installPrismESLintMarkerHook() {
148148

149149
// Remove trailing newline and presentational `⏎` characters
150150
docsExampleCodeToParsableCode(code),
151-
{ languageOptions: { sourceType: parserOptions.sourceType, parserOptions } },
151+
config,
152152
{ filename: "code.js" }
153153
);
154154

tests/fixtures/good-examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default⏎
99

1010
:::
1111

12-
::: correct { "ecmaFeatures": { "jsx": true } }
12+
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }
1313

1414
```jsx
1515
const foo = <bar></bar>;

tools/check-rule-examples.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const { LATEST_ECMA_VERSION } = require("../conf/ecma-version");
2121

2222
/** @typedef {import("../lib/shared/types").LintMessage} LintMessage */
2323
/** @typedef {import("../lib/shared/types").LintResult} LintResult */
24-
/** @typedef {import("../lib/shared/types").ParserOptions} ParserOptions */
24+
/** @typedef {import("../lib/shared/types").LanguageOptions} LanguageOptions */
2525

2626
//------------------------------------------------------------------------------
2727
// Helpers
@@ -40,12 +40,18 @@ const commentParser = new ConfigCommentParser();
4040
/**
4141
* Tries to parse a specified JavaScript code with Playground presets.
4242
* @param {string} code The JavaScript code to parse.
43-
* @param {ParserOptions} parserOptions Explicitly specified parser options.
43+
* @param {LanguageOptions} [languageOptions] Explicitly specified language options.
4444
* @returns {{ ast: ASTNode } | { error: SyntaxError }} An AST with comments, or a `SyntaxError` object if the code cannot be parsed.
4545
*/
46-
function tryParseForPlayground(code, parserOptions) {
46+
function tryParseForPlayground(code, languageOptions) {
4747
try {
48-
const ast = parse(code, { ecmaVersion: "latest", ...parserOptions, comment: true, loc: true });
48+
const ast = parse(code, {
49+
ecmaVersion: languageOptions?.ecmaVersion ?? "latest",
50+
sourceType: languageOptions?.sourceType ?? "module",
51+
...languageOptions?.parserOptions,
52+
comment: true,
53+
loc: true
54+
});
4955

5056
return { ast };
5157
} catch (error) {
@@ -64,7 +70,7 @@ async function findProblems(filename) {
6470
const isRuleRemoved = !rules.has(title);
6571
const problems = [];
6672
const ruleExampleOptions = markdownItRuleExample({
67-
open({ code, parserOptions, codeBlockToken }) {
73+
open({ code, languageOptions, codeBlockToken }) {
6874
const languageTag = codeBlockToken.info;
6975

7076
if (!STANDARD_LANGUAGE_TAGS.has(languageTag)) {
@@ -86,8 +92,8 @@ async function findProblems(filename) {
8692
});
8793
}
8894

89-
if (parserOptions && typeof parserOptions.ecmaVersion !== "undefined") {
90-
const { ecmaVersion } = parserOptions;
95+
if (typeof languageOptions?.ecmaVersion !== "undefined") {
96+
const { ecmaVersion } = languageOptions;
9197
let ecmaVersionErrorMessage;
9298

9399
if (ecmaVersion === "latest") {
@@ -109,7 +115,7 @@ async function findProblems(filename) {
109115
}
110116
}
111117

112-
const { ast, error } = tryParseForPlayground(code, parserOptions);
118+
const { ast, error } = tryParseForPlayground(code, languageOptions);
113119

114120
if (ast) {
115121
let hasRuleConfigComment = false;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy