Skip to content

Commit 2abdc9b

Browse files
committed
Add vfile reporting
1 parent c9d54e2 commit 2abdc9b

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

web/src/remark/auto-js-code.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Output:
3636
*/
3737

3838
import type * as md from 'mdast'
39-
import type { } from 'mdast-util-mdx'; // Type-only empty import to register MDX types into mdast
39+
import type {} from 'mdast-util-mdx' // Type-only empty import to register MDX types into mdast
4040
import assert from 'node:assert/strict'
4141
import * as path from 'node:path'
4242
import * as prettier from 'prettier'
@@ -54,8 +54,13 @@ const autoJSCodePlugin: Plugin<[], md.Root> = () => async (tree, file) => {
5454

5555
visitParents(tree, 'code', (node, ancestors) => {
5656
if (node.meta && META_FLAG_REGEX.test(node.meta)) {
57-
if (!node.lang || !SUPPORTED_LANGS.has(node.lang)) {
58-
throw new Error(`Unsupported language: ${node.lang}`)
57+
if (!node.lang) {
58+
file.fail('No language specified', { place: node.position })
59+
}
60+
if (!SUPPORTED_LANGS.has(node.lang)) {
61+
file.fail(`Unsupported language: ${node.lang}`, {
62+
place: node.position,
63+
})
5964
}
6065

6166
// We put these aside for processing later
@@ -67,7 +72,7 @@ const autoJSCodePlugin: Plugin<[], md.Root> = () => async (tree, file) => {
6772

6873
for (const { node, ancestors } of nodesToProcess) {
6974
const parent = ancestors.at(-1)
70-
assert(parent) // It must have a parent because the root node is a fully formed tree
75+
assert(parent) // The node is never a `Root` node, so it will always have a parent
7176
assert(node.meta && node.lang) // Already checked in the visitor
7277

7378
// Remove our flag from the meta so other plugins don't trip up

web/src/remark/code-with-hole.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ const HOLE_REPLACEMENT = '/* ... */'
3333

3434
const SUPPORTED_LANGS = new Set(['js', 'jsx', 'ts', 'tsx'])
3535

36-
const codeWithHolePlugin: Plugin<[], md.Root> = () => (tree) => {
36+
const codeWithHolePlugin: Plugin<[], md.Root> = () => (tree, file) => {
3737
visitParents(tree, 'code', (node) => {
3838
if (node.meta && META_FLAG_REGEX.test(node.meta)) {
39-
if (!node.lang || !SUPPORTED_LANGS.has(node.lang)) {
40-
throw new Error(`Unsupported language: ${node.lang}`)
39+
if (!node.lang) {
40+
file.fail('No language specified', { place: node.position })
41+
}
42+
if (!SUPPORTED_LANGS.has(node.lang)) {
43+
file.fail(`Unsupported language: ${node.lang}`, {
44+
place: node.position,
45+
})
4146
}
4247

4348
// Remove our flag from the meta so other plugins don't trip up

0 commit comments

Comments
 (0)