From 4711b1f0720eb54e458ca5a16cb655013693b628 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 9 Sep 2022 15:42:07 +0200 Subject: Add support for mdx attribute (value) expressions --- src/construct/partial_mdx_expression.rs | 30 +++- src/construct/partial_mdx_jsx.rs | 50 +++++- src/event.rs | 2 + src/state.rs | 9 + tests/mdx_jsx_flow.rs | 35 ++-- tests/mdx_jsx_text.rs | 302 +++++++++++++++----------------- 6 files changed, 248 insertions(+), 180 deletions(-) diff --git a/src/construct/partial_mdx_expression.rs b/src/construct/partial_mdx_expression.rs index a949347..aeea52e 100644 --- a/src/construct/partial_mdx_expression.rs +++ b/src/construct/partial_mdx_expression.rs @@ -36,13 +36,22 @@ pub fn before(tokenizer: &mut Tokenizer) -> State { State::Next(StateName::MdxExpressionEolAfter) }, Some(b'}') if tokenizer.tokenize_state.size == 0 => { - tokenizer.enter(Name::MdxExpressionMarker); - tokenizer.consume(); - tokenizer.exit(Name::MdxExpressionMarker); - tokenizer.exit(tokenizer.tokenize_state.token_1.clone()); - State::Ok + if tokenizer.tokenize_state.token_1 == Name::MdxJsxTagAttributeValueExpression && !tokenizer.tokenize_state.seen { + State::Error(format!( + "{}:{}: Unexpected empty in expression, expected a value between braces", + tokenizer.point.line, tokenizer.point.column + )) + } else { + tokenizer.tokenize_state.seen = false; + tokenizer.enter(Name::MdxExpressionMarker); + tokenizer.consume(); + tokenizer.exit(Name::MdxExpressionMarker); + tokenizer.exit(tokenizer.tokenize_state.token_1.clone()); + State::Ok + } }, Some(_) => { + tokenizer.tokenize_state.seen = true; tokenizer.enter(Name::MdxExpressionData); State::Retry(StateName::MdxExpressionInside) } @@ -56,9 +65,11 @@ pub fn inside(tokenizer: &mut Tokenizer) -> State { tokenizer.exit(Name::MdxExpressionData); State::Retry(StateName::MdxExpressionBefore) } else { - // To do: only count if agnostic. + // To do: don’t count if gnostic. if tokenizer.current == Some(b'{') { tokenizer.tokenize_state.size += 1; + } else if tokenizer.current == Some(b'}') { + tokenizer.tokenize_state.size -= 1; } tokenizer.consume(); @@ -67,8 +78,11 @@ pub fn inside(tokenizer: &mut Tokenizer) -> State { } pub fn eol_after(tokenizer: &mut Tokenizer) -> State { - // Lazy continuation in a flow expression is a syntax error. - if tokenizer.tokenize_state.token_1 == Name::MdxFlowExpression && tokenizer.lazy { + // Lazy continuation in a flow expression (or in a flow tag) is a syntax error. + if (tokenizer.tokenize_state.token_1 == Name::MdxFlowExpression + || tokenizer.tokenize_state.token_2 == Name::MdxJsxFlowTag) + && tokenizer.lazy + { State::Error(format!( "{}:{}: Unexpected lazy line in expression in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc", tokenizer.point.line, tokenizer.point.column diff --git a/src/construct/partial_mdx_jsx.rs b/src/construct/partial_mdx_jsx.rs index 05a2c0c..9177b5b 100644 --- a/src/construct/partial_mdx_jsx.rs +++ b/src/construct/partial_mdx_jsx.rs @@ -610,7 +610,17 @@ pub fn attribute_before(tokenizer: &mut Tokenizer) -> State { // End of tag. Some(b'>') => State::Retry(StateName::MdxJsxTagEnd), // Attribute expression. - Some(b'{') => unreachable!("to do: attribute expression"), + Some(b'{') => { + // To do: force `spread: true` if gnostic. + // To do: pass `start_point` if gnostic. + tokenizer.tokenize_state.token_2 = tokenizer.tokenize_state.token_1.clone(); + tokenizer.tokenize_state.token_1 = Name::MdxJsxTagAttributeExpression; + tokenizer.attempt( + State::Next(StateName::MdxJsxAttributeExpressionAfter), + State::Nok, + ); + State::Retry(StateName::MdxExpressionStart) + } _ => { // Start of an attribute name. if id_start(char_after_index( @@ -633,6 +643,19 @@ pub fn attribute_before(tokenizer: &mut Tokenizer) -> State { } } +/// After attribute expression. +/// +/// ```markdown +/// > | a e +/// ^ +/// ``` +pub fn attribute_expression_after(tokenizer: &mut Tokenizer) -> State { + tokenizer.tokenize_state.token_1 = tokenizer.tokenize_state.token_2.clone(); + tokenizer.tokenize_state.token_2 = Name::Data; + tokenizer.attempt(State::Next(StateName::MdxJsxAttributeBefore), State::Nok); + State::Retry(StateName::MdxJsxEsWhitespaceStart) +} + /// In primary attribute name. /// /// ```markdown @@ -862,7 +885,16 @@ pub fn attribute_value_before(tokenizer: &mut Tokenizer) -> State { State::Next(StateName::MdxJsxAttributeValueQuotedStart) } // Attribute value expression. - Some(b'{') => unreachable!("to do: attribute value expression"), + Some(b'{') => { + // To do: pass `start_point` if gnostic. + tokenizer.tokenize_state.token_2 = tokenizer.tokenize_state.token_1.clone(); + tokenizer.tokenize_state.token_1 = Name::MdxJsxTagAttributeValueExpression; + tokenizer.attempt( + State::Next(StateName::MdxJsxAttributeValueExpressionAfter), + State::Nok, + ); + State::Retry(StateName::MdxExpressionStart) + } _ => crash( tokenizer, "before attribute value", @@ -878,6 +910,20 @@ pub fn attribute_value_before(tokenizer: &mut Tokenizer) -> State { } } +/// After attribute value expression. +/// +/// ```markdown +/// > | a f +/// ^ +/// ``` +pub fn attribute_value_expression_after(tokenizer: &mut Tokenizer) -> State { + tokenizer.tokenize_state.token_1 = tokenizer.tokenize_state.token_2.clone(); + tokenizer.tokenize_state.token_2 = Name::Data; + tokenizer.exit(Name::MdxJsxTagAttribute); + tokenizer.attempt(State::Next(StateName::MdxJsxAttributeBefore), State::Nok); + State::Retry(StateName::MdxJsxEsWhitespaceStart) +} + /// Before quoted literal attribute value. /// /// ```markdown diff --git a/src/event.rs b/src/event.rs index b18835e..2375369 100644 --- a/src/event.rs +++ b/src/event.rs @@ -3171,6 +3171,8 @@ pub enum Name { MdxTextExpression, MdxExpressionMarker, // void MdxExpressionData, // void + MdxJsxTagAttributeValueExpression, + MdxJsxTagAttributeExpression, } /// List of void events, used to make sure everything is working well. diff --git a/src/state.rs b/src/state.rs index 2158966..dcabbd7 100644 --- a/src/state.rs +++ b/src/state.rs @@ -448,6 +448,8 @@ pub enum Name { MdxExpressionBefore, MdxExpressionInside, MdxExpressionEolAfter, + MdxJsxAttributeValueExpressionAfter, + MdxJsxAttributeExpressionAfter, } #[allow(clippy::too_many_lines)] @@ -467,6 +469,13 @@ pub fn call(tokenizer: &mut Tokenizer, name: Name) -> State { Name::MdxExpressionInside => construct::partial_mdx_expression::inside, Name::MdxExpressionEolAfter => construct::partial_mdx_expression::eol_after, + Name::MdxJsxAttributeValueExpressionAfter => { + construct::partial_mdx_jsx::attribute_value_expression_after + } + Name::MdxJsxAttributeExpressionAfter => { + construct::partial_mdx_jsx::attribute_expression_after + } + Name::AttentionStart => construct::attention::start, Name::AttentionInside => construct::attention::inside, diff --git a/tests/mdx_jsx_flow.rs b/tests/mdx_jsx_flow.rs index ff53dcb..9b0453f 100644 --- a/tests/mdx_jsx_flow.rs +++ b/tests/mdx_jsx_flow.rs @@ -33,12 +33,11 @@ fn mdx_jsx_flow_agnostic() -> Result<(), String> { "should support an element w/ containers as content" ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("", &mdx)?, - // "", - // "should support attributes" - // ); + assert_eq!( + micromark_with_options("", &mdx)?, + "", + "should support attributes" + ); Ok(()) } @@ -97,7 +96,7 @@ fn mdx_jsx_flow_essence() -> Result<(), String> { ); assert_eq!( - micromark_with_options("> d", &mdx) + micromark_with_options("> ", &mdx) .err() .unwrap(), "2:1: Unexpected lazy line in jsx in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc", @@ -105,7 +104,7 @@ fn mdx_jsx_flow_essence() -> Result<(), String> { ); assert_eq!( - micromark_with_options("> d", &mdx) + micromark_with_options("> ", &mdx) .err() .unwrap(), "2:1: Unexpected lazy line in jsx in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc", @@ -113,17 +112,33 @@ fn mdx_jsx_flow_essence() -> Result<(), String> { ); assert_eq!( - micromark_with_options("> e", &mdx) + micromark_with_options("> ", &mdx) .err() .unwrap(), "2:1: Unexpected lazy line in jsx in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc", "should not support lazy flow (4)" ); + assert_eq!( + micromark_with_options("> ", &mdx) + .err() + .unwrap(), + "2:1: Unexpected lazy line in expression in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc", + "should not support lazy flow (5)" + ); + + assert_eq!( + micromark_with_options("> ", &mdx) + .err() + .unwrap(), + "2:1: Unexpected lazy line in expression in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc", + "should not support lazy flow (6)" + ); + assert_eq!( micromark_with_options("> a\n", &mdx)?, "
\n

a

\n
\n", - "should not support lazy flow (5)" + "should not support lazy flow (7)" ); Ok(()) diff --git a/tests/mdx_jsx_text.rs b/tests/mdx_jsx_text.rs index 782fb6a..1872b05 100644 --- a/tests/mdx_jsx_text.rs +++ b/tests/mdx_jsx_text.rs @@ -67,19 +67,17 @@ fn mdx_jsx_text_agnosic() -> Result<(), String> { "should support an unclosed element" ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a c", &mdx)?, - // "

a c

", - // "should support an attribute expression" - // ); + assert_eq!( + micromark_with_options("a c", &mdx)?, + "

a c

", + "should support an attribute expression" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a d", &mdx)?, - // "

a d

", - // "should support an attribute value expression" - // ); + assert_eq!( + micromark_with_options("a d", &mdx)?, + "

a d

", + "should support an attribute value expression" + ); Ok(()) } @@ -109,85 +107,79 @@ fn mdx_jsx_text_gnostic() -> Result<(), String> { "should support an unclosed element" ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a d", &mdx)?, - // "

a d

", - // "should support an attribute expression" - // ); + assert_eq!( + micromark_with_options("a d", &mdx)?, + "

a d

", + "should support an attribute expression" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a f", &mdx)?, - // "

a f

", - // "should support more complex attribute expression (1)" - // ); + assert_eq!( + micromark_with_options("a f", &mdx)?, + "

a f

", + "should support more complex attribute expression (1)" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a d", &mdx)?, - // "

a d

", - // "should support more complex attribute expression (2)" - // ); + assert_eq!( + micromark_with_options("a d", &mdx)?, + "

a d

", + "should support more complex attribute expression (2)" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a d", &mdx)?, - // "

a d

", - // "should support an attribute value expression" - // ); + assert_eq!( + micromark_with_options("a d", &mdx)?, + "

a d

", + "should support an attribute value expression" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a d", &mdx) - // .err() - // .unwrap(), - // "Unexpected empty expression", - // "should crash on an empty attribute value expression" - // ); + assert_eq!( + micromark_with_options("a d", &mdx) + .err() + .unwrap(), + "1:9: Unexpected empty in expression, expected a value between braces", + "should crash on an empty attribute value expression" + ); - // To do: expressions. + // To do: swc. // assert_eq!( - // micromark_with_options("a c", &mdx) + // 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: expressions. + // To do: swc. // assert_eq!( - // micromark_with_options("a d", &mdx) + // 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: expressions. + // To do: swc. // assert_eq!( - // micromark_with_options("a c", &mdx) + // 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: expressions. + // To do: swc. // assert_eq!( - // micromark_with_options("a f", &mdx) + // 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)" // ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a } /> f", &mdx)?, - // "

a f

", - // "should support parenthesized expressions" - // ); + assert_eq!( + micromark_with_options("a } /> f", &mdx)?, + "

a f

", + "should support parenthesized expressions" + ); Ok(()) } @@ -421,61 +413,53 @@ fn mdx_jsx_text_complete() -> Result<(), String> { "should crash on a nonconforming character after name" ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a c.", &mdx)?, - // "

a c.

", - // "should support attribute expressions" - // ); + assert_eq!( + micromark_with_options("a c.", &mdx)?, + "

a c.

", + "should support attribute expressions" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a c.", &mdx)?, - // "

a c.

", - // "should support nested balanced braces in attribute expressions" - // ); + assert_eq!( + micromark_with_options("a c.", &mdx)?, + "

a c.

", + "should support nested balanced braces in attribute expressions" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options(".", &mdx)?, - // "

.

", - // "should support attribute expressions directly after a name" - // ); + assert_eq!( + micromark_with_options(".", &mdx)?, + "

.

", + "should support attribute expressions directly after a name" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options(".", &mdx)?, - // "

.

", - // "should support attribute expressions directly after a member name" - // ); + assert_eq!( + micromark_with_options(".", &mdx)?, + "

.

", + "should support attribute expressions directly after a member name" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options(".", &mdx)?, - // "

.

", - // "should support attribute expressions directly after a local name" - // ); + assert_eq!( + micromark_with_options(".", &mdx)?, + "

.

", + "should support attribute expressions directly after a local name" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a .", &mdx)?, - // "

a .

", - // "should support attribute expressions directly after boolean attributes" - // ); + assert_eq!( + micromark_with_options("a .", &mdx)?, + "

a .

", + "should support attribute expressions directly after boolean attributes" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a .", &mdx)?, - // "

a .

", - // "should support attribute expressions directly after boolean qualified attributes" - // ); + assert_eq!( + micromark_with_options("a .", &mdx)?, + "

a .

", + "should support attribute expressions directly after boolean qualified attributes" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a c.", &mdx)?, - // "

a c.

", - // "should support attribute expressions and normal attributes" - // ); + assert_eq!( + micromark_with_options("a c.", &mdx)?, + "

a c.

", + "should support attribute expressions and normal attributes" + ); assert_eq!( micromark_with_options("a c.", &mdx)?, @@ -483,23 +467,21 @@ fn mdx_jsx_text_complete() -> Result<(), String> { "should support attributes" ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a c.", &mdx) - // .err() - // .unwrap(), - // "Unexpected character `~` (U+007E) before attribute name, expected a character that can start an attribute name, such as a letter, `$`, or `_`; whitespace before attributes; or the end of the tag", - // "should crash on a nonconforming character before an attribute name" - // ); + assert_eq!( + micromark_with_options("a c.", &mdx) + .err() + .unwrap(), + "1:12: Unexpected character `~` (U+007E) before attribute name, expected a character that can start an attribute name, such as a letter, `$`, or `_`; whitespace before attributes; or the end of the tag", + "should crash on a nonconforming character before an attribute name" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a c.", &mdx) @@ -553,19 +535,17 @@ fn mdx_jsx_text_complete() -> Result<(), String> { "should crash on a nonconforming character after a local attribute name" ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a c.", &mdx)?, - // "

a c.

", - // "should support attribute value expressions" - // ); + assert_eq!( + micromark_with_options("a c.", &mdx)?, + "

a c.

", + "should support attribute value expressions" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a c.", &mdx)?, - // "

a c.

", - // "should support nested balanced braces in attribute value expressions" - // ); + assert_eq!( + micromark_with_options("a c.", &mdx)?, + "

a c.

", + "should support nested balanced braces in attribute value expressions" + ); assert_eq!( micromark_with_options("a
c.", &mdx) @@ -599,14 +579,13 @@ fn mdx_jsx_text_complete() -> Result<(), String> { "should crash on a missing closing quote in single quoted attribute value" ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("a c.", &mdx) - // .err() - // .unwrap(), - // "Unexpected end of file in expression, expected a corresponding closing brace for `{`", - // "should crash on a missing closing brace in an attribute value expression" - // ); + assert_eq!( + micromark_with_options("a c.", &mdx) + .err() + .unwrap(), + "1:13: Unexpected end of file in expression, expected a corresponding closing brace for `{`", + "should crash on a missing closing brace in an attribute value expression" + ); assert_eq!( micromark_with_options("a c.", &mdx) @@ -622,12 +601,11 @@ fn mdx_jsx_text_complete() -> Result<(), String> { "should support an attribute directly after a value" ); - // To do: expressions. - // assert_eq!( - // micromark_with_options(".", &mdx)?, - // "

.

", - // "should support an attribute directly after an attribute expression" - // ); + assert_eq!( + micromark_with_options(".", &mdx)?, + "

.

", + "should support an attribute directly after an attribute expression" + ); assert_eq!( micromark_with_options("a
c.", &mdx) @@ -797,19 +775,17 @@ fn mdx_jsx_text_complete() -> Result<(), String> { "should support line endings in attribute values" ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("> a f", &mdx)?, - // "
\n

a f

\n
", - // "should support line endings in attribute value expressions" - // ); + assert_eq!( + micromark_with_options("> a f", &mdx)?, + "
\n

a f

\n
", + "should support line endings in attribute value expressions" + ); - // To do: expressions. - // assert_eq!( - // micromark_with_options("> a e", &mdx)?, - // "
\n

a e

\n
", - // "should support line endings in attribute expressions" - // ); + assert_eq!( + micromark_with_options("> a e", &mdx)?, + "
\n

a e

\n
", + "should support line endings in attribute expressions" + ); assert_eq!( micromark_with_options("> a c", &mdx)?, @@ -835,6 +811,12 @@ fn mdx_jsx_text_complete() -> Result<(), String> { "should support lazy text (4)" ); + assert_eq!( + micromark_with_options("> a f", &mdx)?, + "
\n

a f

\n
", + "should support lazy text (5)" + ); + assert_eq!( micromark_with_options("1 < 3", &mdx)?, "

1 < 3

", -- cgit