Skip to content

Commit

Permalink
support Angular @let
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jul 16, 2024
1 parent 363f498 commit 5d41279
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dprint_plugin/tests/integration/angular.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
}

<div>{{ a ?. b ( ) }}</div>

@let user = getUser ( 1 );
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ source: dprint_plugin/tests/integration.rs
}

<div>{{ a?.b() }}</div>

@let user = getUser(1);
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ source: dprint_plugin/tests/integration.rs
}

<div>{{ a?.b() }}</div>

@let user = getUser(1);
6 changes: 6 additions & 0 deletions markup_fmt/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ pub struct AngularInterpolation<'s> {
pub expr: &'s str,
}

pub struct AngularLet<'s> {
pub name: &'s str,
pub expr: &'s str,
}

pub struct AngularSwitch<'s> {
pub expr: &'s str,
pub cases: Vec<AngularCase<'s>>,
Expand Down Expand Up @@ -109,6 +114,7 @@ pub enum Node<'s> {
AngularFor(AngularFor<'s>),
AngularIf(AngularIf<'s>),
AngularInterpolation(AngularInterpolation<'s>),
AngularLet(AngularLet<'s>),
AngularSwitch(AngularSwitch<'s>),
AstroExpr(AstroExpr<'s>),
Comment(Comment<'s>),
Expand Down
2 changes: 2 additions & 0 deletions markup_fmt/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct SyntaxError {
pub enum SyntaxErrorKind {
ExpectAngularFor,
ExpectAngularIf,
ExpectAngularLet,
ExpectAngularSwitch,
ExpectAstroAttr,
ExpectAstroExpr,
Expand Down Expand Up @@ -53,6 +54,7 @@ impl fmt::Display for SyntaxError {
let reason: Cow<_> = match self.kind {
SyntaxErrorKind::ExpectAngularFor => "expect Angular `@for`".into(),
SyntaxErrorKind::ExpectAngularIf => "expect Angular `@if`".into(),
SyntaxErrorKind::ExpectAngularLet => "expect Angular `@let`".into(),
SyntaxErrorKind::ExpectAngularSwitch => "expect Angular `@switch`".into(),
SyntaxErrorKind::ExpectAstroAttr => "expect Astro attribute".into(),
SyntaxErrorKind::ExpectAstroExpr => "expect Astro expression".into(),
Expand Down
33 changes: 33 additions & 0 deletions markup_fmt/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,38 @@ impl<'s> Parser<'s> {
Ok(unsafe { self.source.get_unchecked(start..end) })
}

fn parse_angular_let(&mut self) -> PResult<AngularLet<'s>> {
if self
.chars
.next_if(|(_, c)| *c == '@')
.and_then(|_| self.chars.next_if(|(_, c)| *c == 'l'))
.and_then(|_| self.chars.next_if(|(_, c)| *c == 'e'))
.and_then(|_| self.chars.next_if(|(_, c)| *c == 't'))
.is_none()
{
return Err(self.emit_error(SyntaxErrorKind::ExpectAngularLet));
}
self.skip_ws();

let name = self.parse_identifier()?;
self.skip_ws();
if self.chars.next_if(|(_, c)| *c == '=').is_none() {
return Err(self.emit_error(SyntaxErrorKind::ExpectChar('=')));
}
self.skip_ws();
let start = self
.chars
.peek()
.map(|(i, _)| *i)
.unwrap_or(self.source.len());
let expr = self.parse_angular_inline_script(start)?;
if self.chars.next_if(|(_, c)| *c == ';').is_none() {
return Err(self.emit_error(SyntaxErrorKind::ExpectChar(';')));
}

Ok(AngularLet { name, expr })
}

fn parse_angular_switch(&mut self) -> PResult<AngularSwitch<'s>> {
if self
.chars
Expand Down Expand Up @@ -1290,6 +1322,7 @@ impl<'s> Parser<'s> {
Some((_, 'i')) => self.parse_angular_if().map(Node::AngularIf),
Some((_, 'f')) => self.parse_angular_for().map(Node::AngularFor),
Some((_, 's')) => self.parse_angular_switch().map(Node::AngularSwitch),
Some((_, 'l')) => self.parse_angular_let().map(Node::AngularLet),
_ => self.parse_text_node().map(Node::Text),
}
}
Expand Down
14 changes: 14 additions & 0 deletions markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ impl<'s> DocGen<'s> for AngularInterpolation<'s> {
}
}

impl<'s> DocGen<'s> for AngularLet<'s> {
fn doc<E, F>(&self, ctx: &mut Ctx<'_, E, F>, _: &State<'s>) -> Doc<'s>
where
F: for<'a> FnMut(&Path, &'a str, usize) -> Result<Cow<'a, str>, E>,
{
Doc::text("@let ")
.append(Doc::text(self.name))
.append(Doc::text(" = "))
.append(Doc::text(ctx.format_general_expr(self.expr)))
.append(Doc::text(";"))
}
}

impl<'s> DocGen<'s> for AngularSwitch<'s> {
fn doc<E, F>(&self, ctx: &mut Ctx<'_, E, F>, state: &State<'s>) -> Doc<'s>
where
Expand Down Expand Up @@ -878,6 +891,7 @@ impl<'s> DocGen<'s> for Node<'s> {
Node::AngularInterpolation(angular_interpolation) => {
angular_interpolation.doc(ctx, state)
}
Node::AngularLet(angular_let) => angular_let.doc(ctx, state),
Node::AngularSwitch(angular_switch) => angular_switch.doc(ctx, state),
Node::AstroExpr(astro_expr) => astro_expr.doc(ctx, state),
Node::Comment(comment) => comment.doc(ctx, state),
Expand Down
1 change: 1 addition & 0 deletions markup_fmt/tests/fmt/angular/let/let.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@let user = getUser();
4 changes: 4 additions & 0 deletions markup_fmt/tests/fmt/angular/let/let.component.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: markup_fmt/tests/fmt.rs
---
@let user = getUser();

0 comments on commit 5d41279

Please sign in to comment.