diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-09-19 11:17:26 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-09-19 11:17:26 +0200 |
commit | fe618ff6e38ec0ed4da72a3935fd9ea64ee1cef5 (patch) | |
tree | e1db142b47091ab2a3cb3ed09f52e4218d1355fa /tests/mdx_jsx_text.rs | |
parent | d4cc03c65a9657d22c59d50f9b3d38b483362560 (diff) | |
download | markdown-rs-fe618ff6e38ec0ed4da72a3935fd9ea64ee1cef5.tar.gz markdown-rs-fe618ff6e38ec0ed4da72a3935fd9ea64ee1cef5.tar.bz2 markdown-rs-fe618ff6e38ec0ed4da72a3935fd9ea64ee1cef5.zip |
Add support for parsing MDX ESM, expressions
This commit adds support for hooks that lets a user integrate another
parser with `micromark-rs`, to parse ESM and expressions according to
a particular grammar (such as a programming language, typically
JavaScript).
For an example integrating with SWC, see `tests/test_utils/mod.rs`.
The integration occurs with two functions passed in `options`:
`mdx_expression_parse` and `mdx_esm_parse`.
The can signal back to micromark when they are successful,
whether there is an error at the end (in which case micromark will
try to parse more), or whether there is a syntax error (in which case
micromark will crash).
Diffstat (limited to 'tests/mdx_jsx_text.rs')
-rw-r--r-- | tests/mdx_jsx_text.rs | 96 |
1 files changed, 48 insertions, 48 deletions
diff --git a/tests/mdx_jsx_text.rs b/tests/mdx_jsx_text.rs index cf507ee..be76d6f 100644 --- a/tests/mdx_jsx_text.rs +++ b/tests/mdx_jsx_text.rs @@ -1,6 +1,8 @@ extern crate micromark; +mod test_utils; use micromark::{micromark_with_options, Constructs, Options}; use pretty_assertions::assert_eq; +use test_utils::{parse_esm, parse_expression}; #[test] fn mdx_jsx_text_core() -> Result<(), String> { @@ -84,99 +86,97 @@ fn mdx_jsx_text_agnosic() -> Result<(), String> { #[test] fn mdx_jsx_text_gnostic() -> Result<(), String> { - let mdx = Options { + let swc = Options { constructs: Constructs::mdx(), + mdx_esm_parse: Some(Box::new(parse_esm)), + mdx_expression_parse: Some(Box::new(parse_expression)), ..Options::default() }; assert_eq!( - micromark_with_options("a <b /> c", &mdx)?, + micromark_with_options("a <b /> c", &swc)?, "<p>a c</p>", "should support a self-closing element" ); assert_eq!( - micromark_with_options("a <b> c </b> d", &mdx)?, + micromark_with_options("a <b> c </b> d", &swc)?, "<p>a c d</p>", "should support a closed element" ); assert_eq!( - micromark_with_options("a <b> c", &mdx)?, + micromark_with_options("a <b> c", &swc)?, "<p>a c</p>", "should support an unclosed element" ); assert_eq!( - micromark_with_options("a <b {...c} /> d", &mdx)?, + micromark_with_options("a <b {...c} /> d", &swc)?, "<p>a d</p>", "should support an attribute expression" ); assert_eq!( - micromark_with_options("a <b {...{c: 1, d: Infinity, e: false}} /> f", &mdx)?, + micromark_with_options("a <b {...{c: 1, d: Infinity, e: false}} /> f", &swc)?, "<p>a f</p>", "should support more complex attribute expression (1)" ); assert_eq!( - micromark_with_options("a <b {...[1, Infinity, false]} /> d", &mdx)?, + micromark_with_options("a <b {...[1, Infinity, false]} /> d", &swc)?, "<p>a d</p>", "should support more complex attribute expression (2)" ); assert_eq!( - micromark_with_options("a <b c={1 + 1} /> d", &mdx)?, + micromark_with_options("a <b c={1 + 1} /> d", &swc)?, "<p>a d</p>", "should support an attribute value expression" ); assert_eq!( - micromark_with_options("a <b c={} /> d", &mdx) + micromark_with_options("a <b c={} /> d", &swc) .err() .unwrap(), - "1:9: Unexpected empty expression, expected a value between braces", + "1:15: Could not parse expression with swc: Unexpected eof", "should crash on an empty attribute value expression" ); - // To do: swc. - // assert_eq!( - // micromark_with_options("a <b {1 + 1} /> c", &swc) - // .err() - // .unwrap(), - // "Could not parse expression with acorn: Unexpected token", - // "should crash on a non-spread attribute expression" - // ); - - // To do: swc. - // assert_eq!( - // micromark_with_options("a <b c={?} /> d", &swc) - // .err() - // .unwrap(), - // "Could not parse expression with acorn: Unexpected token", - // "should crash on invalid JS in an attribute value expression" - // ); - - // To do: swc. - // assert_eq!( - // micromark_with_options("a <b {?} /> c", &swc) - // .err() - // .unwrap(), - // "Could not parse expression with acorn: Unexpected token", - // "should crash on invalid JS in an attribute expression" - // ); - - // To do: swc. - // assert_eq!( - // micromark_with_options("a <b{c=d}={}/> f", &swc) - // .err() - // .unwrap(), - // "Unexpected `ExpressionStatement` in code: expected an object spread", - // "should crash on invalid JS in an attribute expression (2)" - // ); - - assert_eq!( - micromark_with_options("a <b c={(2)} d={<e />} /> f", &mdx)?, + assert_eq!( + micromark_with_options("a <b {1 + 1} /> c", &swc) + .err() + .unwrap(), + "1:18: Could not parse expression with swc: Expected ',', got '}'", + "should crash on a non-spread attribute expression" + ); + + assert_eq!( + micromark_with_options("a <b c={?} /> d", &swc) + .err() + .unwrap(), + "1:16: Could not parse expression with swc: Unexpected token `?`. Expected this, import, async, function, [ for array literal, { for object literal, @ for decorator, function, class, null, true, false, number, bigint, string, regexp, ` for template literal, (, or an identifier", + "should crash on invalid JS in an attribute value expression" + ); + + assert_eq!( + micromark_with_options("a <b {?} /> c", &swc) + .err() + .unwrap(), + "1:14: Could not parse expression with swc: Unexpected token `?`. Expected identifier, string literal, numeric literal or [ for the computed key", + "should crash on invalid JS in an attribute expression" + ); + + assert_eq!( + micromark_with_options("a <b{c=d}={}/> f", &swc) + .err() + .unwrap(), + "1:6: Expected a single spread value, such as `...x`", + "should crash on invalid JS in an attribute expression (2)" + ); + + assert_eq!( + micromark_with_options("a <b c={(2)} d={<e />} /> f", &swc)?, "<p>a f</p>", "should support parenthesized expressions" ); |