From 5727cc882df02ba35d9e2b3d634a091eafc5d4bb Mon Sep 17 00:00:00 2001 From: PrashantSIngh2618 <38156926+PrashantSingh2618@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:02:19 +0530 Subject: [PATCH] Validate line highlight ranges in markdown code blocks Code fence metadata like `{3,7}` can reference lines past the end of the code block. The site silently ignores those lines, so the line the author meant to highlight is never highlighted. Extend lint-markdown-code-blocks to report highlighted lines outside the code block, using the same pattern as getHighlightLines() in CodeBlock.tsx, and fix the three existing cases in the cacheSignal reference and the "Components and Hooks must be pure" page. Co-Authored-By: Claude Opus 5.5 --- .../content/out-of-bounds-line-highlight.md | 9 +++++ .../fixtures/src/content/valid-highlight.md | 10 +++++ .../lint-markdown-code-blocks.test.js | 38 ++++++++++--------- eslint-local-rules/rules/inline-highlights.js | 32 +++++++++++++++- .../rules/lint-markdown-code-blocks.js | 13 ++++--- src/content/reference/react/cacheSignal.md | 2 +- .../components-and-hooks-must-be-pure.md | 4 +- 7 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 eslint-local-rules/__tests__/fixtures/src/content/out-of-bounds-line-highlight.md diff --git a/eslint-local-rules/__tests__/fixtures/src/content/out-of-bounds-line-highlight.md b/eslint-local-rules/__tests__/fixtures/src/content/out-of-bounds-line-highlight.md new file mode 100644 index 00000000000..415aebf8f5d --- /dev/null +++ b/eslint-local-rules/__tests__/fixtures/src/content/out-of-bounds-line-highlight.md @@ -0,0 +1,9 @@ +```js {1,3} +function submitAction() {} +submitAction(); +``` + +```js {1-3} +function submitAction() {} +submitAction(); +``` diff --git a/eslint-local-rules/__tests__/fixtures/src/content/valid-highlight.md b/eslint-local-rules/__tests__/fixtures/src/content/valid-highlight.md index dd6074029d1..4b796121074 100644 --- a/eslint-local-rules/__tests__/fixtures/src/content/valid-highlight.md +++ b/eslint-local-rules/__tests__/fixtures/src/content/valid-highlight.md @@ -5,3 +5,13 @@ function submitAction() {} ```js [[1, 1, "\\"hidden\\""]] const mode = "hidden"; ``` + +```js {2} +function submitAction() {} +submitAction(); +``` + +```js {1-2} [[1, 2, "submitAction"]] +function submitAction() {} +submitAction(); +``` diff --git a/eslint-local-rules/__tests__/lint-markdown-code-blocks.test.js b/eslint-local-rules/__tests__/lint-markdown-code-blocks.test.js index f0b0e991ec1..8b692dc9853 100644 --- a/eslint-local-rules/__tests__/lint-markdown-code-blocks.test.js +++ b/eslint-local-rules/__tests__/lint-markdown-code-blocks.test.js @@ -11,12 +11,7 @@ const path = require('path'); const {ESLint} = require('eslint'); const plugin = require('..'); -const FIXTURES_DIR = path.join( - __dirname, - 'fixtures', - 'src', - 'content' -); +const FIXTURES_DIR = path.join(__dirname, 'fixtures', 'src', 'content'); const PARSER_PATH = path.join(__dirname, '..', 'parser.js'); function createESLint({fix = false} = {}) { @@ -53,11 +48,7 @@ async function lintFixture(name, {fix = false} = {}) { async function run() { const basicResult = await lintFixture('basic-error.md'); - assert.strictEqual( - basicResult.messages.length, - 1, - 'expected one diagnostic' - ); + assert.strictEqual(basicResult.messages.length, 1, 'expected one diagnostic'); assert( basicResult.messages[0].message.includes('Calling setState during render'), 'expected message to mention setState during render' @@ -91,9 +82,7 @@ async function run() { fix: true, }); assert( - duplicateFixed.output.includes( - "{expectedErrors: {'react-compiler': [4]}}" - ), + duplicateFixed.output.includes("{expectedErrors: {'react-compiler': [4]}}"), 'expected duplicates to be rewritten to a single canonical block' ); assert( @@ -118,9 +107,7 @@ async function run() { fix: true, }); assert( - malformedFixed.output.includes( - "{expectedErrors: {'react-compiler': [4]}}" - ), + malformedFixed.output.includes("{expectedErrors: {'react-compiler': [4]}}"), 'expected malformed metadata to be replaced with canonical form' ); @@ -154,9 +141,24 @@ async function run() { outOfBoundsHighlightResult.messages[0].message, 'Code highlight line 3 is outside this code block' ); + + const outOfBoundsLineHighlightResult = await lintFixture( + 'out-of-bounds-line-highlight.md' + ); + assert.strictEqual( + outOfBoundsLineHighlightResult.messages.length, + 2, + 'expected a highlighted line and range end past the code block to fail' + ); + for (const message of outOfBoundsLineHighlightResult.messages) { + assert.strictEqual( + message.message, + 'Highlighted line 3 is outside this code block, which has 2 lines' + ); + } } -run().catch(error => { +run().catch((error) => { console.error(error); process.exitCode = 1; }); diff --git a/eslint-local-rules/rules/inline-highlights.js b/eslint-local-rules/rules/inline-highlights.js index 1e25fa6b9e9..96f6c8a34f8 100644 --- a/eslint-local-rules/rules/inline-highlights.js +++ b/eslint-local-rules/rules/inline-highlights.js @@ -63,4 +63,34 @@ function validateInlineHighlights(meta, code) { return errors; } -module.exports = {validateInlineHighlights}; +// Matches the line ranges read by getHighlightLines() in +// src/components/MDX/CodeBlock/CodeBlock.tsx, e.g. `{1-3,7}`. +const LINE_HIGHLIGHT_REGEX = /{([\d,-]+)}/; + +function validateLineHighlights(meta, code) { + const match = LINE_HIGHLIGHT_REGEX.exec(meta); + if (!match) { + return []; + } + + const lineCount = code.split('\n').length; + const errors = []; + + for (const range of match[1].split(',')) { + for (const bound of range.split('-')) { + if (bound === '') { + continue; + } + const lineNo = Number(bound); + if (lineNo < 1 || lineNo > lineCount) { + errors.push( + `Highlighted line ${lineNo} is outside this code block, which has ${lineCount} lines` + ); + } + } + } + + return errors; +} + +module.exports = {validateInlineHighlights, validateLineHighlights}; diff --git a/eslint-local-rules/rules/lint-markdown-code-blocks.js b/eslint-local-rules/rules/lint-markdown-code-blocks.js index 14bf10a1269..c946a58296e 100644 --- a/eslint-local-rules/rules/lint-markdown-code-blocks.js +++ b/eslint-local-rules/rules/lint-markdown-code-blocks.js @@ -16,7 +16,10 @@ const { setCompilerExpectedLines, } = require('./metadata'); const {normalizeDiagnostics} = require('./diagnostics'); -const {validateInlineHighlights} = require('./inline-highlights'); +const { + validateInlineHighlights, + validateLineHighlights, +} = require('./inline-highlights'); const {parseMarkdownFile} = require('./markdown'); const {runReactCompiler} = require('./react-compiler'); @@ -44,10 +47,10 @@ module.exports = { const {blocks} = parseMarkdownFile(sourceCode.text, filename); // For each supported code block, run the compiler and reconcile metadata. for (const block of blocks) { - for (const message of validateInlineHighlights( - block.meta, - block.code - )) { + for (const message of [ + ...validateLineHighlights(block.meta, block.code), + ...validateInlineHighlights(block.meta, block.code), + ]) { context.report({ node, loc: block.position, diff --git a/src/content/reference/react/cacheSignal.md b/src/content/reference/react/cacheSignal.md index c898ca53b1d..b224bfd2ebb 100644 --- a/src/content/reference/react/cacheSignal.md +++ b/src/content/reference/react/cacheSignal.md @@ -28,7 +28,7 @@ const signal = cacheSignal(); Call `cacheSignal` to get an `AbortSignal`. -```js {3,7} +```js {3} import {cacheSignal} from 'react'; async function Component() { await fetch(url, { signal: cacheSignal() }); diff --git a/src/content/reference/rules/components-and-hooks-must-be-pure.md b/src/content/reference/rules/components-and-hooks-must-be-pure.md index 973561c2251..06dd520faad 100644 --- a/src/content/reference/rules/components-and-hooks-must-be-pure.md +++ b/src/content/reference/rules/components-and-hooks-must-be-pure.md @@ -309,13 +309,13 @@ function useIconStyle(icon) { If you were to mutate the Hook's arguments, the custom hook's memoization will become incorrect, so it's important to avoid doing that. -```js {4} +```js {2} style = useIconStyle(icon); // `style` is memoized based on `icon` icon.enabled = false; // Bad: 🔴 never mutate hook arguments directly style = useIconStyle(icon); // previously memoized result is returned ``` -```js {4} +```js {2} style = useIconStyle(icon); // `style` is memoized based on `icon` icon = { ...icon, enabled: false }; // Good: ✅ make a copy instead style = useIconStyle(icon); // new value of `style` is calculated