Skip to content

Commit

Permalink
Change to improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 26, 2023
1 parent 8b1ff41 commit 8aabf74
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 5 additions & 1 deletion lib/ast-to-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ function toReact(state, node, index, parent) {

if (!basic && typeof component !== 'function') {
throw new Error(
`Component for name \`${name}\` not defined or is not renderable`
'Unexpected value `' +
component +
'` for `' +
name +
'`, expected component or tag name'
)
}

Expand Down
10 changes: 6 additions & 4 deletions lib/react-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,19 @@ export function ReactMarkdown(options) {
)
}

const hastNode = processor.runSync(processor.parse(file), file)
const hastTree = processor.runSync(processor.parse(file), file)

if (hastNode.type !== 'root') {
throw new TypeError('Expected a `root` node')
if (hastTree.type !== 'root') {
throw new TypeError(
'Unexpected `' + hastTree.type + '` node, expected `root`'
)
}

/** @type {ReactElement} */
let result = React.createElement(
React.Fragment,
{},
childrenToReact({options, schema: html, listDepth: 0}, hastNode)
childrenToReact({options, schema: html, listDepth: 0}, hastTree)
)

if (options.className) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rehype-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function rehypeFilter(options) {
) {
if (options.allowedElements && options.disallowedElements) {
throw new TypeError(
'Only one of `allowedElements` and `disallowedElements` should be defined'
'Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other'
)
}

Expand Down
8 changes: 4 additions & 4 deletions test/test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ test('react-markdown', async function (t) {
disallowedElements={['a']}
/>
)
}, /only one of/i)
}, /Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other/)
}
)

Expand Down Expand Up @@ -631,7 +631,7 @@ test('react-markdown', async function (t) {
)
})

await t.test('should fail on invalid component', function () {
await t.test('should fail on an invalid component', function () {
assert.throws(function () {
asHtml(
<Markdown
Expand All @@ -642,7 +642,7 @@ test('react-markdown', async function (t) {
}}
/>
)
}, /Component for name `h1`/)
}, /Unexpected value `123` for `h1`, expected component or tag name/)
})

await t.test('should support `null`, `undefined` in components', function () {
Expand Down Expand Up @@ -1204,7 +1204,7 @@ test('react-markdown', async function (t) {
await t.test('should fail on a plugin replacing `root`', function () {
assert.throws(function () {
asHtml(<Markdown children="a" rehypePlugins={[plugin]} />)
}, /Expected a `root` node/)
}, /Unexpected `comment` node, expected `root/)

function plugin() {
/**
Expand Down

0 comments on commit 8aabf74

Please sign in to comment.