Skip to content

Commit a9ff3cc

Browse files
committed
eslint-local-rules: harden inline highlight parsing + declare missing deps
- Replace greedy INLINE_HIGHLIGHT_REGEX with a non-greedy global regex and validate each [[...]] group independently (per-group JSON parsing). - Declare the runtime dependencies actually required by the local rules (eslint, remark@12, @babel/core, @babel/parser, babel-plugin-react-compiler) so tests run on a clean install. - Add nested-highlight fixture and test asserting valid nested arrays pass. - Drop accidentally committed node_modules and restore .gitignore/yarn.lock.
1 parent 0821e7d commit a9ff3cc

4 files changed

Lines changed: 47 additions & 14 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```js [[1, 1, "submitAction"]]
2+
function submitAction() {}
3+
```

‎eslint-local-rules/__tests__/lint-markdown-code-blocks.test.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ async function run() {
154154
outOfBoundsHighlightResult.messages[0].message,
155155
'Code highlight line 3 is outside this code block'
156156
);
157+
158+
// A nested array like [[1, 1, "text"]] parses as a single three-element
159+
// highlight ([1, 1, "text"]), so it should validate against the code.
160+
const nestedHighlightResult = await lintFixture('nested-highlight.md');
161+
assert.strictEqual(
162+
nestedHighlightResult.messages.length,
163+
0,
164+
'expected a nested inline highlight array to pass'
165+
);
157166
}
158167

159168
run().catch(error => {

‎eslint-local-rules/package.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
"test": "node __tests__/lint-markdown-code-blocks.test.js"
88
},
99
"devDependencies": {
10-
"eslint-mdx": "^2"
10+
"@babel/core": "^7.12.9",
11+
"@babel/parser": "^7.12.9",
12+
"babel-plugin-react-compiler": "^1.0.0",
13+
"eslint": "7.x",
14+
"eslint-mdx": "^2",
15+
"remark": "^12.0.1"
1116
}
1217
}

‎eslint-local-rules/rules/inline-highlights.js‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,43 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
const INLINE_HIGHLIGHT_REGEX = /(\[\[.*\]\])/;
8+
const INLINE_HIGHLIGHT_REGEX = /\[\[(.*?)\]\]/g;
99

1010
function validateInlineHighlights(meta, code) {
11-
const match = INLINE_HIGHLIGHT_REGEX.exec(meta);
12-
if (!match) {
11+
const matches = [...meta.matchAll(INLINE_HIGHLIGHT_REGEX)];
12+
if (matches.length === 0) {
1313
return [];
1414
}
1515

16-
let highlights;
17-
try {
18-
highlights = JSON.parse(match[1]);
19-
} catch (error) {
20-
return ['Code highlight metadata must be valid JSON'];
21-
}
22-
23-
if (!Array.isArray(highlights)) {
24-
return ['Code highlight metadata must be an array'];
16+
const errors = [];
17+
let highlights = [];
18+
for (const match of matches) {
19+
let parsed;
20+
try {
21+
// The fence marker [[...]] is not itself valid JSON, so wrap the
22+
// captured content in brackets to parse it as an array. This makes
23+
// both [1, 1, "text"] and [ [1, 1, "text"] ] (nested form) parse
24+
// into a three-element highlight tuple.
25+
parsed = JSON.parse(`[${match[1]}]`);
26+
} catch (error) {
27+
errors.push('Code highlight metadata must be valid JSON');
28+
continue;
29+
}
30+
if (!Array.isArray(parsed)) {
31+
errors.push('Code highlight metadata must be an array');
32+
continue;
33+
}
34+
// A single group may either wrap the list of highlights
35+
// ([[ [1, 1, "a"], [2, 3, "b"] ]] style) or be one bare
36+
// three-element tuple ([1, 1, "a"] style).
37+
if (parsed.length > 0 && parsed.every(entry => Array.isArray(entry))) {
38+
highlights = highlights.concat(parsed);
39+
} else {
40+
highlights.push(parsed);
41+
}
2542
}
2643

2744
const lines = code.split('\n');
28-
const errors = [];
2945

3046
for (const highlight of highlights) {
3147
if (!Array.isArray(highlight) || highlight.length < 3) {

0 commit comments

Comments
 (0)