From 33fbccb31a7e2fcaa6315aeabbde35cb21e1b774 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Thu, 26 Dec 2024 10:38:23 +0100 Subject: [PATCH 1/3] docs: switch rule examples config format to `languageOptions` --- docs/.eleventy.js | 8 ++++---- docs/src/rules/jsx-quotes.md | 8 ++++---- docs/src/rules/keyword-spacing.md | 4 ++-- docs/src/rules/no-extra-parens.md | 10 +++++----- docs/src/rules/no-inline-comments.md | 4 ++-- docs/src/rules/no-irregular-whitespace.md | 2 +- docs/src/rules/no-unused-expressions.md | 4 ++-- docs/src/rules/space-before-keywords.md | 2 +- docs/tools/markdown-it-rule-example.js | 14 +++++++------- docs/tools/prism-eslint-hook.js | 14 +++++++------- tests/fixtures/good-examples.md | 4 ++-- tools/check-rule-examples.js | 22 ++++++++++++++-------- 12 files changed, 51 insertions(+), 45 deletions(-) diff --git a/docs/.eleventy.js b/docs/.eleventy.js index c526abae509c..caebabcab639 100644 --- a/docs/.eleventy.js +++ b/docs/.eleventy.js @@ -197,9 +197,9 @@ 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); @@ -207,10 +207,10 @@ module.exports = function(eleventyConfig) { return `
`; } - // 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 }) ); diff --git a/docs/src/rules/jsx-quotes.md b/docs/src/rules/jsx-quotes.md index 25ea1295dc64..41e145302e57 100644 --- a/docs/src/rules/jsx-quotes.md +++ b/docs/src/rules/jsx-quotes.md @@ -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"]*/ @@ -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"]*/ @@ -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"]*/ @@ -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"]*/ diff --git a/docs/src/rules/keyword-spacing.md b/docs/src/rules/keyword-spacing.md index e6c485a75af7..ba278295da8a 100644 --- a/docs/src/rules/keyword-spacing.md +++ b/docs/src/rules/keyword-spacing.md @@ -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 }]*/ @@ -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 }]*/ diff --git a/docs/src/rules/no-extra-parens.md b/docs/src/rules/no-extra-parens.md index 3685d88b7932..851107ceb1fb 100644 --- a/docs/src/rules/no-extra-parens.md +++ b/docs/src/rules/no-extra-parens.md @@ -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" }] */ @@ -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" }] */ @@ -240,7 +240,7 @@ const ThatComponent = (

) 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" }] */ @@ -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" }] */ @@ -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" }] */ diff --git a/docs/src/rules/no-inline-comments.md b/docs/src/rules/no-inline-comments.md index d6a32db4e47b..548ac746eb94 100644 --- a/docs/src/rules/no-inline-comments.md +++ b/docs/src/rules/no-inline-comments.md @@ -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"*/ @@ -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"*/ diff --git a/docs/src/rules/no-irregular-whitespace.md b/docs/src/rules/no-irregular-whitespace.md index eb0086f57ab4..8fb3f3fac49c 100644 --- a/docs/src/rules/no-irregular-whitespace.md +++ b/docs/src/rules/no-irregular-whitespace.md @@ -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 }]*/ diff --git a/docs/src/rules/no-unused-expressions.md b/docs/src/rules/no-unused-expressions.md index 52379726dcea..939f5f66f8a2 100644 --- a/docs/src/rules/no-unused-expressions.md +++ b/docs/src/rules/no-unused-expressions.md @@ -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 }]*/ @@ -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 }]*/ diff --git a/docs/src/rules/space-before-keywords.md b/docs/src/rules/space-before-keywords.md index 7d969d2c83d8..01c1ac85a68b 100644 --- a/docs/src/rules/space-before-keywords.md +++ b/docs/src/rules/space-before-keywords.md @@ -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"]*/ diff --git a/docs/tools/markdown-it-rule-example.js b/docs/tools/markdown-it-rule-example.js index a769b120db52..dc3ab4dc4f5e 100644 --- a/docs/tools/markdown-it-rule-example.js +++ b/docs/tools/markdown-it-rule-example.js @@ -2,7 +2,7 @@ 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. @@ -10,7 +10,7 @@ const { docsExampleCodeToParsableCode } = require("./code-block-utils"); * @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} 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 @@ -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. * @@ -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() { @@ -72,14 +72,14 @@ function markdownItRuleExample({ open, close }) { return typeof text === "string" ? text : ""; } - const { type, parserOptionsJSON } = /^\s*(?\S+)(\s+(?\S.*?))?\s*$/u.exec(tagToken.info).groups; - const parserOptions = { sourceType: "module", ...(parserOptionsJSON && JSON.parse(parserOptionsJSON)) }; + const { type, languageOptionsJSON } = /^\s*(?\S+)(\s+(?\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 : ""; diff --git a/docs/tools/prism-eslint-hook.js b/docs/tools/prism-eslint-hook.js index 74dd5197ceae..bbbe764c9d6a 100644 --- a/docs/tools/prism-eslint-hook.js +++ b/docs/tools/prism-eslint-hook.js @@ -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 @@ -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; } /** @@ -113,7 +113,7 @@ function installPrismESLintMarkerHook() { return; } contentMustBeMarked = void 0; - const parserOptions = contentParserOptions; + const config = contentLanguageOptions ? { languageOptions: contentLanguageOptions } : {}; const code = env.code; @@ -148,7 +148,7 @@ function installPrismESLintMarkerHook() { // Remove trailing newline and presentational `⏎` characters docsExampleCodeToParsableCode(code), - { languageOptions: { sourceType: parserOptions.sourceType, parserOptions } }, + config, { filename: "code.js" } ); diff --git a/tests/fixtures/good-examples.md b/tests/fixtures/good-examples.md index eaed65d3d046..104be623863e 100644 --- a/tests/fixtures/good-examples.md +++ b/tests/fixtures/good-examples.md @@ -9,7 +9,7 @@ export default⏎ ::: -::: correct { "ecmaFeatures": { "jsx": true } } +::: correct { "parserOptions": { "ecmaFeatures": { "jsx": true } } } ```jsx const foo = ; @@ -19,7 +19,7 @@ const foo = ; A test with multiple spaces after 'correct': -:::correct +:::correct ```js ``` diff --git a/tools/check-rule-examples.js b/tools/check-rule-examples.js index b2c9cd0a9900..e04447802921 100644 --- a/tools/check-rule-examples.js +++ b/tools/check-rule-examples.js @@ -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 @@ -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) { @@ -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)) { @@ -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") { @@ -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; From ad872780823fb4a488cf72f956a0ee59b2e620cd Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sat, 28 Dec 2024 17:07:40 +0100 Subject: [PATCH 2/3] revert trailing spaces --- tests/fixtures/good-examples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/good-examples.md b/tests/fixtures/good-examples.md index 104be623863e..fcb7d95cd4e8 100644 --- a/tests/fixtures/good-examples.md +++ b/tests/fixtures/good-examples.md @@ -19,7 +19,7 @@ const foo = ; A test with multiple spaces after 'correct': -:::correct +:::correct ```js ``` From 98010d436b619e864cff9ea8ccca10428620a5e4 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sat, 28 Dec 2024 19:11:04 +0100 Subject: [PATCH 3/3] mark languageOptions as possibly undefined in callbacks --- docs/tools/markdown-it-rule-example.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tools/markdown-it-rule-example.js b/docs/tools/markdown-it-rule-example.js index dc3ab4dc4f5e..7b95e3c4c644 100644 --- a/docs/tools/markdown-it-rule-example.js +++ b/docs/tools/markdown-it-rule-example.js @@ -10,7 +10,7 @@ const { docsExampleCodeToParsableCode } = require("./code-block-utils"); * @param {Object} data Callback data. * @param {"correct" | "incorrect"} data.type The type of the example. * @param {string} data.code The example code. - * @param {LanguageOptions} data.languageOptions The language 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 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