a \n?>
", diff --git a/tests/gfm_table.rs b/tests/gfm_table.rs index b7f884a7..8c46a302 100644 --- a/tests/gfm_table.rs +++ b/tests/gfm_table.rs @@ -1037,7 +1037,8 @@ bar "###, &Options { allow_dangerous_html: true, - ..gfm.clone() + constructs: Constructs::gfm(), + ..Options::default() } )?, r###"$foo$ bar
c
", + "should support an import" + ); + + assert_eq!( + micromark_with_options("export default a\n\nb", &swc)?, + "b
", + "should support an export" + ); + + assert_eq!( + micromark_with_options("impossible", &swc)?, + "impossible
", + "should not support other keywords (`impossible`)" + ); + + assert_eq!( + micromark_with_options("exporting", &swc)?, + "exporting
", + "should not support other keywords (`exporting`)" + ); + + assert_eq!( + micromark_with_options("import.", &swc)?, + "import.
", + "should not support a non-whitespace after the keyword" + ); + + assert_eq!( + micromark_with_options("import('a')", &swc)?, + "import('a')
", + "should not support a non-whitespace after the keyword (import-as-a-function)" + ); + + assert_eq!( + micromark_with_options(" import a from 'b'\n export default c", &swc)?, + "import a from 'b'\nexport default c
", + "should not support an indent" + ); + + assert_eq!( + micromark_with_options("- import a from 'b'\n> export default c", &swc)?, + "\n", + "should not support keywords in containers" + ); + + assert_eq!( + micromark_with_options("import a from 'b'\nexport default c", &swc)?, + "", + "should support imports and exports in the same “block”" + ); + + assert_eq!( + micromark_with_options("import a from 'b'\n\nexport default c", &swc)?, + "", + "should support imports and exports in separate “blocks”" + ); + + assert_eq!( + micromark_with_options("a\n\nimport a from 'b'\n\nb\n\nexport default c", &swc)?, + "export default c
\n
a
\nb
\n", + "should support imports and exports in between other constructs" + ); + + assert_eq!( + micromark_with_options("a\nimport a from 'b'\n\nb\nexport default c", &swc)?, + "a\nimport a from 'b'
\nb\nexport default c
", + "should not support import/exports when interrupting paragraphs" + ); + + assert_eq!( + micromark_with_options("import a", &swc).err().unwrap(), + "1:9: Could not parse esm with swc: Expected ',', got 'c
", + "should support line endings in import/exports" + ); + + assert_eq!( + micromark_with_options("export {\n\n a\n\n} from 'b'\n\nc", &swc)?, + "c
", + "should support blank lines in import/exports" + ); + + assert_eq!( + micromark_with_options("import a from 'b'\n*md*?", &swc) + .err() + .unwrap(), + "2:6: Could not parse esm 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 markdown after import/export w/o blank line" + ); + + assert_eq!( + micromark_with_options("export var a = 1\n// b\n/* c */\n\nd", &swc)?, + "d
", + "should support comments in “blocks”" + ); + + assert_eq!( + micromark_with_options("export var a = 1\nvar b\n\nc", &swc) + .err() + .unwrap(), + "2:1: Unexpected statement in code: only import/exports are supported", + "should crash on other statements in “blocks”" + ); + + assert_eq!( + micromark_with_options("import ('a')\n\nb", &swc) + .err() + .unwrap(), + "1:1: Unexpected statement in code: only import/exports are supported", + "should crash on import-as-a-function with a space `import (x)`" + ); + + assert_eq!( + micromark_with_options("import a from 'b'\nexport {a}\n\nc", &swc)?, + "c
", + "should support a reexport from another import" + ); + + assert_eq!( + micromark_with_options("import a from 'b';\nexport {a};\n\nc", &swc)?, + "c
", + "should support a reexport from another import w/ semicolons" + ); + + assert_eq!( + micromark_with_options("import a from 'b'\nexport {a as default}\n\nc", &swc)?, + "c
", + "should support a reexport default from another import" + ); + + assert_eq!( + micromark_with_options("export var a = () => ", &swc)?, + "", + "should support JSX by default" + ); + + assert_eq!( + micromark_with_options("export {a}\n", &swc)?, + "", + "should support EOF after EOL" + ); + + assert_eq!( + micromark_with_options("import a from 'b'\n\nexport {a}\n\nc", &swc)?, + "c
", + "should support a reexport from another esm block (1)" + ); + + assert_eq!( + micromark_with_options("import a from 'b'\n\nexport {a}\n\n# c", &swc)?, + "a
", -// "should support a line ending in an expression" -// ); - -// assert_eq!( -// micromark_with_options("{ a } \t\nb", &swc)?, -// "b
", -// "should support expressions followed by spaces" -// ); - -// assert_eq!( -// micromark_with_options(" { a }\nb", &swc)?, -// "b
", -// "should support expressions preceded by spaces" -// ); - -// assert_eq!( -// micromark_with_options(" {`\n a\n `}", &swc)?, -// "", -// "should support indented expressions" -// ); - -// assert_eq!( -// micromark_with_options("a{(b)}c", &swc)?, -// "ac
", -// "should support expressions padded w/ parens" -// ); - -// assert_eq!( -// micromark_with_options("a{/* b */ ( (c) /* d */ + (e) )}f", &swc)?, -// "af
", -// "should support expressions padded w/ parens and comments" -// ); - -// Ok(()) -// } - -// To do: move to JSX, actually test spread in expressions? -// To do: swc. -// #[test] -// fn mdx_expression_spread() -> Result<(), String> { -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("a {b} c", &swc); -// // }, -// // /Unexpected `Property` in code: only spread elements are supported/, -// // "should crash if not a spread" -// // ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("a {...?} c", &swc); -// // }, -// // /Could not parse expression with swc: Unexpected token/, -// // "should crash on an incorrect spread" -// // ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("a {...b,c} d", &swc); -// // }, -// // /Unexpected extra content in spread: only a single spread is supported/, -// // "should crash if a spread and other things" -// // ); - -// assert_eq!( -// micromark_with_options("a {} b", &swc)?, -// "a b
", -// "should support an empty spread" -// ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("a {} b", &swc); -// // }, -// // /Unexpected empty expression/, -// // "should crash on an empty spread w/ `allowEmpty: false`" -// // ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("{a=b}", &swc); -// // }, -// // /Could not parse expression with swc: Shorthand property assignments are valid only in destructuring patterns/, -// // "should crash if not a spread w/ `allowEmpty`" -// // ); - -// assert_eq!( -// micromark_with_options("a {/* b */} c", &swc)?, -// "a c
", -// "should support a comment spread" -// ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("a {/* b */} c", &swc); -// // }, -// // /Unexpected empty expression/, -// // "should crash on a comment spread w/ `allowEmpty: false`" -// // ); - -// assert_eq!( -// micromark_with_options("a {...b} c", &swc)?, -// "a c
", -// "should support a spread" -// ); - -// Ok(()) -// } +#[test] +fn mdx_expression_flow_gnostic() -> Result<(), String> { + 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}", &swc)?, + "", + "should support an expression" + ); + + assert_eq!( + micromark_with_options("{}", &swc)?, + "", + "should support an empty expression" + ); + + assert_eq!( + micromark_with_options("{a", &swc).err().unwrap(), + "1:3: Unexpected end of file in expression, expected a corresponding closing brace for `{`", + "should crash if no closing brace is found (1)" + ); + + assert_eq!( + micromark_with_options("{b { c }", &swc).err().unwrap(), + "1:4: Could not parse expression with swc: Unexpected content after expression", + "should crash if no closing brace is found (2)" + ); + + assert_eq!( + micromark_with_options("{\n}\na", &swc)?, + "a
", + "should support a line ending in an expression" + ); + + assert_eq!( + micromark_with_options("{ a } \t\nb", &swc)?, + "b
", + "should support expressions followed by spaces" + ); + + assert_eq!( + micromark_with_options(" { a }\nb", &swc)?, + "b
", + "should support expressions preceded by spaces" + ); + + assert_eq!( + micromark_with_options(" {`\n a\n `}", &swc)?, + "", + "should support indented expressions" + ); + + assert_eq!( + micromark_with_options("a{(b)}c", &swc)?, + "ac
", + "should support expressions padded w/ parens" + ); + + assert_eq!( + micromark_with_options("a{/* b */ ( (c) /* d */ + (e) )}f", &swc)?, + "af
", + "should support expressions padded w/ parens and comments" + ); + + Ok(()) +} + +#[test] +fn mdx_expression_spread() -> Result<(), String> { + 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("", &swc)?, + "", + "should support spreads for attribute expression" + ); + + assert_eq!( + micromark_with_options("", &swc).err().unwrap(), + "1:5: Expected a single spread value, such as `...x`", + "should crash if not a spread" + ); + + assert_eq!( + micromark_with_options("", &swc).err().unwrap(), + "1:13: 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 an incorrect spread" + ); + + assert_eq!( + micromark_with_options("", &swc) + .err() + .unwrap(), + "1:5: Expected a single spread value, such as `...x`", + "should crash if a spread and other things" + ); + + assert_eq!( + micromark_with_options("", &swc).err().unwrap(), + "1:5: Expected a single spread value, such as `...x`", + "should crash on an empty spread" + ); + + assert_eq!( + micromark_with_options("", &swc).err().unwrap(), + "1:12: Could not parse expression with swc: assignment property is invalid syntax", + "should crash if not an identifier" + ); + + assert_eq!( + micromark_with_options("", &swc) + .err() + .unwrap(), + "1:5: Expected a single spread value, such as `...x`", + "should crash on a comment spread" + ); + + Ok(()) +} diff --git a/tests/mdx_expression_text.rs b/tests/mdx_expression_text.rs index b42faf2c..3a489651 100644 --- a/tests/mdx_expression_text.rs +++ b/tests/mdx_expression_text.rs @@ -1,147 +1,144 @@ 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}; -// To do: swc. -// #[test] -// fn mdx_expression_text_gnostic_core() -> Result<(), String> { -// assert_eq!( -// micromark_with_options("a {} b", &swc)?, -// "a b
", -// "should support an empty expression (1)" -// ); - -// assert_eq!( -// micromark_with_options("a { \t\r\n} b", &swc)?, -// "a b
", -// "should support an empty expression (2)" -// ); - -// assert_eq!( -// micromark_with_options("a {/**/} b", &swc)?, -// "a b
", -// "should support a multiline comment (1)" -// ); - -// assert_eq!( -// micromark_with_options("a { /*\n*/\t} b", &swc)?, -// "a b
", -// "should support a multiline comment (2)" -// ); - -// assert_eq!( -// micromark_with_options("a {/*b*//*c*/} d", &swc)?, -// "a d
", -// "should support a multiline comment (3)" -// ); - -// assert_eq!( -// micromark_with_options("a {b/*c*/} d", &swc)?, -// "a d
", -// "should support a multiline comment (4)" -// ); - -// assert_eq!( -// micromark_with_options("a {/*b*/c} d", &swc)?, -// "a d
", -// "should support a multiline comment (4)" -// ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("a {//} b", &swc); -// // }, -// // /Could not parse expression with swc: Unexpected token/, -// // "should crash on an incorrect line comment (1)" -// // ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("a { // b } c", &swc); -// // }, -// // /Could not parse expression with swc: Unexpected token/, -// // "should crash on an incorrect line comment (2)" -// // ); - -// assert_eq!( -// micromark_with_options("a {//\n} b", &swc)?, -// "a b
", -// "should support a line comment followed by a line ending" -// ); - -// assert_eq!( -// micromark_with_options("a {// b\nd} d", &swc)?, -// "a d
", -// "should support a line comment followed by a line ending and an expression" -// ); - -// assert_eq!( -// micromark_with_options("a {b// c\n} d", &swc)?, -// "a d
", -// "should support an expression followed by a line comment and a line ending" -// ); - -// assert_eq!( -// micromark_with_options("a {/*b*/ // c\n} d", &swc)?, -// "a d
", -// "should support comments (1)" -// ); - -// assert_eq!( -// micromark_with_options("a {b.c} d", &swc)?, -// "a d
", -// "should support expression statements (1)" -// ); - -// assert_eq!( -// micromark_with_options("a {1 + 1} b", &swc)?, -// "a b
", -// "should support expression statements (2)" -// ); - -// assert_eq!( -// micromark_with_options("a {function () {}} b", &swc)?, -// "a b
", -// "should support expression statements (3)" -// ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("a {var b = \"c\"} d", &swc); -// // }, -// // /Could not parse expression with swc: Unexpected token/, -// // "should crash on non-expressions" -// // ); - -// assert_eq!( -// micromark_with_options("> a {\n> b} c", &swc)?, -// "\n", -// "should support expressions in containers" -// ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("> a {\n> b<} c", &swc); -// // }, -// // /Could not parse expression with swc: Unexpected token/, -// // "should crash on incorrect expressions in containers (1)" -// // ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("> a {\n> b\n> c} d", &swc); -// // }, -// // /Could not parse expression with swc: Unexpected content after expression/, -// // "should crash on incorrect expressions in containers (2)" -// // ); - -// Ok(()) -// } +#[test] +fn mdx_expression_text_gnostic_core() -> Result<(), String> { + 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", &swc)?, + "a c
\n
a b
", + "should support an empty expression (1)" + ); + + assert_eq!( + micromark_with_options("a { \t\r\n} b", &swc)?, + "a b
", + "should support an empty expression (2)" + ); + + assert_eq!( + micromark_with_options("a {/**/} b", &swc)?, + "a b
", + "should support a multiline comment (1)" + ); + + assert_eq!( + micromark_with_options("a { /*\n*/\t} b", &swc)?, + "a b
", + "should support a multiline comment (2)" + ); + + assert_eq!( + micromark_with_options("a {/*b*//*c*/} d", &swc)?, + "a d
", + "should support a multiline comment (3)" + ); + + assert_eq!( + micromark_with_options("a {b/*c*/} d", &swc)?, + "a d
", + "should support a multiline comment (4)" + ); + + assert_eq!( + micromark_with_options("a {/*b*/c} d", &swc)?, + "a d
", + "should support a multiline comment (4)" + ); + + assert_eq!( + micromark_with_options("a {//} b", &swc).err().unwrap(), + "1:4: Could not parse expression with swc: Unexpected eof", + "should crash on an incorrect line comment (1)" + ); + + assert_eq!( + micromark_with_options("a { // b } c", &swc).err().unwrap(), + "1:4: Could not parse expression with swc: Unexpected eof", + "should crash on an incorrect line comment (2)" + ); + + assert_eq!( + micromark_with_options("a {//\n} b", &swc)?, + "a b
", + "should support a line comment followed by a line ending" + ); + + assert_eq!( + micromark_with_options("a {// b\nd} d", &swc)?, + "a d
", + "should support a line comment followed by a line ending and an expression" + ); + + assert_eq!( + micromark_with_options("a {b// c\n} d", &swc)?, + "a d
", + "should support an expression followed by a line comment and a line ending" + ); + + assert_eq!( + micromark_with_options("a {/*b*/ // c\n} d", &swc)?, + "a d
", + "should support comments (1)" + ); + + assert_eq!( + micromark_with_options("a {b.c} d", &swc)?, + "a d
", + "should support expression statements (1)" + ); + + assert_eq!( + micromark_with_options("a {1 + 1} b", &swc)?, + "a b
", + "should support expression statements (2)" + ); + + assert_eq!( + micromark_with_options("a {function () {}} b", &swc)?, + "a b
", + "should support expression statements (3)" + ); + + assert_eq!( + micromark_with_options("a {var b = \"c\"} d", &swc).err().unwrap(), + "1:7: Could not parse expression with swc: Unexpected token `var`. 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 non-expressions" + ); + + assert_eq!( + micromark_with_options("> a {\n> b} c", &swc)?, + "\n", + "should support expressions in containers" + ); + + assert_eq!( + micromark_with_options("> a {\n> b<} c", &swc) + .err() + .unwrap(), + "2:8: Could not parse expression with swc: Unexpected eof", + "should crash on incorrect expressions in containers (1)" + ); + + assert_eq!( + micromark_with_options("> a {\n> b\n> c} d", &swc) + .err() + .unwrap(), + "3:3: Could not parse expression with swc: Unexpected content after expression", + "should crash on incorrect expressions in containers (2)" + ); + + Ok(()) +} #[test] fn mdx_expression_text_agnostic() -> Result<(), String> { @@ -197,77 +194,74 @@ fn mdx_expression_text_agnostic() -> Result<(), String> { Ok(()) } -// // To do: swc. -// #[test] -// fn mdx_expression_text_gnostic() -> Result<(), String> { -// assert_eq!( -// micromark_with_options("a {b} c", &swc)?, -// "a c
\n
a c
", -// "should support an expression" -// ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("a {??} b", &swc); -// // }, -// // /Could not parse expression with swc: Unexpected token/, -// // "should crash on an incorrect expression" -// // ); - -// assert_eq!( -// micromark_with_options("a {} b", &swc)?, -// "a b
", -// "should support an empty expression" -// ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("a {b c", &swc); -// // }, -// // /Unexpected end of file in expression, expected a corresponding closing brace for `{`/, -// // "should crash if no closing brace is found (1)" -// // ); - -// // To do: errors. -// // t.throws( -// // () => { -// // micromark_with_options("a {b { c } d", &swc); -// // }, -// // /Could not parse expression with swc: Unexpected content after expression/, -// // "should crash if no closing brace is found (2)" -// // ); - -// assert_eq!( -// micromark_with_options("a {\n} b", &swc)?, -// "a b
", -// "should support a line ending in an expression" -// ); - -// assert_eq!( -// micromark_with_options("a } b", &swc)?, -// "a } b
", -// "should support just a closing brace" -// ); - -// assert_eq!( -// micromark_with_options("{ a } b", &swc)?, -// "b
", -// "should support expressions as the first thing when following by other things" -// ); - -// assert_eq!( -// micromark_with_options("a { /* { */ } b", &swc)?, -// "a b
", -// "should support an unbalanced opening brace (if JS permits)" -// ); - -// assert_eq!( -// micromark_with_options("a { /* } */ } b", &swc)?, -// "a b
", -// "should support an unbalanced closing brace (if JS permits)" -// ); - -// Ok(()) -// } +#[test] +fn mdx_expression_text_gnostic() -> Result<(), String> { + 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", &swc)?, + "a c
", + "should support an expression" + ); + + assert_eq!( + micromark_with_options("a {??} b", &swc).err().unwrap(), + "1:9: Could not parse expression with swc: Unexpected eof", + "should crash on an incorrect expression" + ); + + assert_eq!( + micromark_with_options("a {} b", &swc)?, + "a b
", + "should support an empty expression" + ); + + assert_eq!( + micromark_with_options("a {b c", &swc).err().unwrap(), + "1:7: Unexpected end of file in expression, expected a corresponding closing brace for `{`", + "should crash if no closing brace is found (1)" + ); + + assert_eq!( + micromark_with_options("a {b { c } d", &swc).err().unwrap(), + "1:6: Could not parse expression with swc: Unexpected content after expression", + "should crash if no closing brace is found (2)" + ); + + assert_eq!( + micromark_with_options("a {\n} b", &swc)?, + "a b
", + "should support a line ending in an expression" + ); + + assert_eq!( + micromark_with_options("a } b", &swc)?, + "a } b
", + "should support just a closing brace" + ); + + assert_eq!( + micromark_with_options("{ a } b", &swc)?, + "b
", + "should support expressions as the first thing when following by other things" + ); + + assert_eq!( + micromark_with_options("a { /* { */ } b", &swc)?, + "a b
", + "should support an unbalanced opening brace (if JS permits)" + ); + + assert_eq!( + micromark_with_options("a { /* } */ } b", &swc)?, + "a b
", + "should support an unbalanced closing brace (if JS permits)" + ); + + Ok(()) +} diff --git a/tests/mdx_jsx_text.rs b/tests/mdx_jsx_text.rs index cf507eec..be76d6f9 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 c", &mdx)?, + micromark_with_options("a c", &swc)?, "a c
", "should support a self-closing element" ); assert_eq!( - micromark_with_options("a c d", &mdx)?, + micromark_with_options("a c d", &swc)?, "a c d
", "should support a closed element" ); assert_eq!( - micromark_with_options("a c", &mdx)?, + micromark_with_options("a c", &swc)?, "a c
", "should support an unclosed element" ); assert_eq!( - micromark_with_options("a d", &mdx)?, + micromark_with_options("a d", &swc)?, "a d
", "should support an attribute expression" ); assert_eq!( - micromark_with_options("a f", &mdx)?, + micromark_with_options("a f", &swc)?, "a f
", "should support more complex attribute expression (1)" ); assert_eq!( - micromark_with_options("a d", &mdx)?, + micromark_with_options("a d", &swc)?, "a d
", "should support more complex attribute expression (2)" ); assert_eq!( - micromark_with_options("a d", &mdx)?, + micromark_with_options("a d", &swc)?, "a d
", "should support an attribute value expression" ); assert_eq!( - micromark_with_options("a d", &mdx) + micromark_with_options("a 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 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 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 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 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 } /> f", &mdx)?, + assert_eq!( + micromark_with_options("a 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 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 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 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 } /> f", &swc)?, "a f
", "should support parenthesized expressions" ); diff --git a/tests/mdx_swc.rs b/tests/mdx_swc.rs new file mode 100644 index 00000000..c9a2a61d --- /dev/null +++ b/tests/mdx_swc.rs @@ -0,0 +1,47 @@ +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_swc() -> Result<(), String> { + let mdx = 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("{'}'}", &mdx)?, + "", + "should support JavaScript-aware flow expressions w/ `mdx_expression_parse`" + ); + + assert_eq!( + micromark_with_options("a {'}'} b", &mdx)?, + "a b
", + "should support JavaScript-aware text expressions w/ `mdx_expression_parse`" + ); + + assert_eq!( + micromark_with_options("", &mdx)?, + "", + "should support JavaScript-aware attribute expressions w/ `mdx_expression_parse`" + ); + + assert_eq!( + micromark_with_options("", &mdx)?, + "", + "should support JavaScript-aware attribute value expressions w/ `mdx_expression_parse`" + ); + + assert_eq!( + micromark_with_options("import a from 'b'\n\nexport {a}\n\n# c", &mdx)?, + "