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_esm() -> 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("import a from 'b'\n\nc", &swc)?, "
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)?, "