Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```js {1,3}
function submitAction() {}
submitAction();
```

```js {1-3}
function submitAction() {}
submitAction();
```
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```
38 changes: 20 additions & 18 deletions eslint-local-rules/__tests__/lint-markdown-code-blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} = {}) {
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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(
Expand All @@ -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'
);

Expand Down Expand Up @@ -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;
});
32 changes: 31 additions & 1 deletion eslint-local-rules/rules/inline-highlights.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
13 changes: 8 additions & 5 deletions eslint-local-rules/rules/lint-markdown-code-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/content/reference/react/cacheSignal.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading