Skip to content

docs: switch rule examples config format to languageOptions #19277

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

Merged
merged 3 commits into from
Dec 31, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/.eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,20 @@ module.exports = function(eleventyConfig) {

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

prismESLintHook.addContentMustBeMarked(codeBlockToken.content, parserOptions);
prismESLintHook.addContentMustBeMarked(codeBlockToken.content, languageOptions);

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

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

// See https://github.com/eslint/eslint.org/blob/ac38ab41f99b89a8798d374f74e2cce01171be8b/src/playground/App.js#L44
// See https://github.com/eslint/eslint.org/blob/29e1d8a000592245e4a30c1996e794643e9b263a/src/playground/App.js#L91-L105
const state = encodeToBase64(
JSON.stringify({
options: { parserOptions },
options: languageOptions ? { languageOptions } : void 0,
text: code
})
);
Expand Down
8 changes: 4 additions & 4 deletions docs/src/rules/jsx-quotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ This rule has a string option:

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

:::incorrect { "ecmaFeatures": { "jsx": true } }
:::incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

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

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

:::correct { "ecmaFeatures": { "jsx": true } }
:::correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

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

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

:::incorrect { "ecmaFeatures": { "jsx": true } }
:::incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

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

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

:::correct { "ecmaFeatures": { "jsx": true } }
:::correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

```jsx
/*eslint jsx-quotes: ["error", "prefer-single"]*/
Expand Down
4 changes: 2 additions & 2 deletions docs/src/rules/keyword-spacing.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ if (foo) {

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

::: correct { "ecmaFeatures": { "jsx": true } }
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

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

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

::: correct { "ecmaFeatures": { "jsx": true } }
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

```jsx
/*eslint keyword-spacing: ["error", { "after": true }]*/
Expand Down
10 changes: 5 additions & 5 deletions docs/src/rules/no-extra-parens.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ foo ? bar : (baz || qux);

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

::: correct { "ecmaFeatures": { "jsx": true } }
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

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

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

::: incorrect { "ecmaFeatures": { "jsx": true } }
::: incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

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

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

::: correct { "ecmaFeatures": { "jsx": true } }
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

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

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

::: incorrect { "ecmaFeatures": { "jsx": true } }
::: incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

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

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

::: correct { "ecmaFeatures": { "jsx": true } }
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

```jsx
/* eslint no-extra-parens: ["error", "all", { ignoreJSX: "single-line" }] */
Expand Down
4 changes: 2 additions & 2 deletions docs/src/rules/no-inline-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Comments inside the curly braces in JSX are allowed to be on the same line as th

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

::: incorrect { "ecmaFeatures": { "jsx": true } }
::: incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

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

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

::: correct { "ecmaFeatures": { "jsx": true } }
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

```jsx
/*eslint no-inline-comments: "error"*/
Expand Down
2 changes: 1 addition & 1 deletion docs/src/rules/no-irregular-whitespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function thing() {

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

::: correct { "ecmaFeatures": { "jsx": true } }
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

```jsx
/*eslint no-irregular-whitespace: ["error", { "skipJSXText": true }]*/
Expand Down
4 changes: 2 additions & 2 deletions docs/src/rules/no-unused-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ JSX is most-commonly used in the React ecosystem, where it is compiled to `React

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

::: incorrect { "ecmaFeatures": { "jsx": true } }
::: incorrect { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

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

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

::: correct { "ecmaFeatures": { "jsx": true } }
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

```jsx
/*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/
Expand Down
2 changes: 1 addition & 1 deletion docs/src/rules/space-before-keywords.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function bar() {

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

::: correct { "ecmaFeatures": { "jsx": true } }
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

```js
/*eslint space-before-keywords: ["error", "always"]*/
Expand Down
14 changes: 7 additions & 7 deletions docs/tools/markdown-it-rule-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

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

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

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

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

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

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

// Return an empty string to avoid appending unexpected text to the output.
return typeof text === "string" ? text : "";
Expand Down
14 changes: 7 additions & 7 deletions docs/tools/prism-eslint-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ try {
// ignore
}

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

/**
* Content that needs to be marked with ESLint
Expand All @@ -37,19 +37,19 @@ let contentMustBeMarked;

/**
* Parser options received from the `::: incorrect` or `::: correct` container.
* @type {ParserOptions|undefined}
* @type {LanguageOptions|undefined}
*/
let contentParserOptions;
let contentLanguageOptions;

/**
* Set content that needs to be marked.
* @param {string} content Source code content that marks ESLint errors.
* @param {ParserOptions} options The options used for validation.
* @param {LanguageOptions} options The options used for validation.
* @returns {void}
*/
function addContentMustBeMarked(content, options) {
contentMustBeMarked = content;
contentParserOptions = options;
contentLanguageOptions = options;
}

/**
Expand Down Expand Up @@ -113,7 +113,7 @@ function installPrismESLintMarkerHook() {
return;
}
contentMustBeMarked = void 0;
const parserOptions = contentParserOptions;
const config = contentLanguageOptions ? { languageOptions: contentLanguageOptions } : {};

const code = env.code;

Expand Down Expand Up @@ -148,7 +148,7 @@ function installPrismESLintMarkerHook() {

// Remove trailing newline and presentational `⏎` characters
docsExampleCodeToParsableCode(code),
{ languageOptions: { sourceType: parserOptions.sourceType, parserOptions } },
config,
{ filename: "code.js" }
);

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/good-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default⏎

:::

::: correct { "ecmaFeatures": { "jsx": true } }
::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } }

```jsx
const foo = <bar></bar>;
Expand Down
22 changes: 14 additions & 8 deletions tools/check-rule-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const { LATEST_ECMA_VERSION } = require("../conf/ecma-version");

/** @typedef {import("../lib/shared/types").LintMessage} LintMessage */
/** @typedef {import("../lib/shared/types").LintResult} LintResult */
/** @typedef {import("../lib/shared/types").ParserOptions} ParserOptions */
/** @typedef {import("../lib/shared/types").LanguageOptions} LanguageOptions */

//------------------------------------------------------------------------------
// Helpers
Expand All @@ -40,12 +40,18 @@ const commentParser = new ConfigCommentParser();
/**
* Tries to parse a specified JavaScript code with Playground presets.
* @param {string} code The JavaScript code to parse.
* @param {ParserOptions} parserOptions Explicitly specified parser options.
* @param {LanguageOptions} [languageOptions] Explicitly specified language options.
* @returns {{ ast: ASTNode } | { error: SyntaxError }} An AST with comments, or a `SyntaxError` object if the code cannot be parsed.
*/
function tryParseForPlayground(code, parserOptions) {
function tryParseForPlayground(code, languageOptions) {
try {
const ast = parse(code, { ecmaVersion: "latest", ...parserOptions, comment: true, loc: true });
const ast = parse(code, {
ecmaVersion: languageOptions?.ecmaVersion ?? "latest",
sourceType: languageOptions?.sourceType ?? "module",
...languageOptions?.parserOptions,
comment: true,
loc: true
});

return { ast };
} catch (error) {
Expand All @@ -64,7 +70,7 @@ async function findProblems(filename) {
const isRuleRemoved = !rules.has(title);
const problems = [];
const ruleExampleOptions = markdownItRuleExample({
open({ code, parserOptions, codeBlockToken }) {
open({ code, languageOptions, codeBlockToken }) {
const languageTag = codeBlockToken.info;

if (!STANDARD_LANGUAGE_TAGS.has(languageTag)) {
Expand All @@ -86,8 +92,8 @@ async function findProblems(filename) {
});
}

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

if (ecmaVersion === "latest") {
Expand All @@ -109,7 +115,7 @@ async function findProblems(filename) {
}
}

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

if (ast) {
let hasRuleConfigComment = false;
Expand Down
Loading
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