Skip to content

Commit e6661a6

Browse files
committed
Add improved error messages for unclosed JSX
1 parent f5dfca5 commit e6661a6

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

index.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
88
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
99
* @typedef {import('mdast-util-to-markdown').Map} ToMarkdownMap
10+
* @typedef {import('mdast-util-from-markdown').OnError} OnError
1011
* @typedef {import('estree-jsx').Program} Program
1112
* @typedef {import('./complex-types').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression
1213
* @typedef {import('./complex-types').MdxJsxAttribute} MdxJsxAttribute
@@ -310,7 +311,8 @@ function exitMdxJsxTag(token) {
310311
attributes: tag.attributes,
311312
children: []
312313
},
313-
token
314+
token,
315+
onError
314316
)
315317
}
316318

@@ -321,6 +323,26 @@ function exitMdxJsxTag(token) {
321323
}
322324
}
323325

326+
/** @type {OnError} */
327+
function onError(closing, open) {
328+
const tag = /** @type {Tag} */ (this.getData('mdxJsxTag'))
329+
const place = closing ? ' before the end of `' + closing.type + '`' : ''
330+
const position = closing
331+
? {start: closing.start, end: closing.end}
332+
: undefined
333+
334+
throw new VFileMessage(
335+
'Expected a closing tag for `' +
336+
serializeAbbreviatedTag(tag) +
337+
'` (' +
338+
stringifyPosition({start: open.start, end: open.end}) +
339+
')' +
340+
place,
341+
position,
342+
'mdast-util-mdx-jsx:end-tag-mismatch'
343+
)
344+
}
345+
324346
/**
325347
* Serialize a tag, excluding attributes.
326348
* `self-closing` is not supported, because we don’t need it yet.
@@ -344,7 +366,7 @@ function mdxElement(node, _, context) {
344366
const exit = context.enter(node.type)
345367
let attributeValue = ''
346368
let index = -1
347-
/** @type {Array.<string>} */
369+
/** @type {Array<string>} */
348370
const attributes = []
349371
/** @type {string} */
350372
let result

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"@types/tape": "^4.0.0",
5050
"acorn": "^8.0.0",
5151
"c8": "^7.0.0",
52-
"mdast-util-from-markdown": "^1.0.0",
52+
"mdast-util-from-markdown": "^1.1.0",
5353
"micromark-extension-mdx-jsx": "^1.0.0",
5454
"prettier": "^2.0.0",
5555
"remark-cli": "^10.0.0",

test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ test('markdown -> mdast', (t) => {
205205
mdastExtensions: [mdxJsxFromMarkdown]
206206
})
207207
},
208-
/Cannot close `paragraph` \(1:1-1:8\): a different token \(`mdxJsxTextTag`, 1:3-1:6\) is open/,
208+
/Expected a closing tag for `<b>` \(1:3-1:6\) before the end of `paragraph`/,
209209
'should crash on an unclosed text jsx (agnostic)'
210210
)
211211

@@ -216,7 +216,7 @@ test('markdown -> mdast', (t) => {
216216
mdastExtensions: [mdxJsxFromMarkdown]
217217
})
218218
},
219-
/Cannot close document, a token \(`mdxJsxFlowTag`, 1:1-1:4\) is still open/,
219+
/Expected a closing tag for `<a>` \(1:1-1:4\)/,
220220
'should crash on an unclosed flow jsx (agnostic)'
221221
)
222222

@@ -1076,7 +1076,7 @@ test('markdown -> mdast', (t) => {
10761076
mdastExtensions: [mdxJsxFromMarkdown]
10771077
})
10781078
},
1079-
/Cannot close `emphasis` \(1:3-1:19\): a different token \(`mdxJsxTextTag`, 1:9-1:12\) is open/,
1079+
/Expected a closing tag for `<b>` \(1:9-1:12\) before the end of `emphasis`/,
10801080
'should crash when misnesting w/ attention (emphasis)'
10811081
)
10821082

@@ -1087,7 +1087,7 @@ test('markdown -> mdast', (t) => {
10871087
mdastExtensions: [mdxJsxFromMarkdown]
10881088
})
10891089
},
1090-
/Cannot close `strong` \(1:3-1:21\): a different token \(`mdxJsxTextTag`, 1:10-1:13\) is open/,
1090+
/Expected a closing tag for `<b>` \(1:10-1:13\) before the end of `strong`/,
10911091
'should crash when misnesting w/ attention (strong)'
10921092
)
10931093

@@ -1098,7 +1098,7 @@ test('markdown -> mdast', (t) => {
10981098
mdastExtensions: [mdxJsxFromMarkdown]
10991099
})
11001100
},
1101-
/Cannot close `link` \(1:3-1:22\): a different token \(`mdxJsxTextTag`, 1:9-1:12\) is open/,
1101+
/Expected a closing tag for `<b>` \(1:9-1:12\) before the end of `link`/,
11021102
'should crash when misnesting w/ label (link)'
11031103
)
11041104

@@ -1109,7 +1109,7 @@ test('markdown -> mdast', (t) => {
11091109
mdastExtensions: [mdxJsxFromMarkdown]
11101110
})
11111111
},
1112-
/Cannot close `image` \(1:3-1:23\): a different token \(`mdxJsxTextTag`, 1:10-1:13\) is open/,
1112+
/Expected a closing tag for `<b>` \(1:10-1:13\) before the end of `image`/,
11131113
'should crash when misnesting w/ label (image)'
11141114
)
11151115

0 commit comments

Comments
 (0)