| @@ -348,7 +348,6 @@ struct BodyLowerer<'c, 'a, 'd> { |
| 348 | 348 | /// Names bound by the pattern being lowered (to detect duplicates and |
| 349 | 349 | /// make every alternative of an or-pattern bind the same names). |
| 350 | 350 | pattern_bindings: Option<PatternBindings>, |
| 351 | | − reported_html: bool, |
| 352 | 351 | } |
| 353 | 352 | |
| 354 | 353 | #[derive(Default)] |
| @@ -368,7 +367,6 @@ impl<'c, 'a, 'd> BodyLowerer<'c, 'a, 'd> { |
| 368 | 367 | body: Body::default(), |
| 369 | 368 | scopes: vec![HashMap::new()], |
| 370 | 369 | pattern_bindings: None, |
| 371 | | − reported_html: false, |
| 372 | 370 | } |
| 373 | 371 | } |
| 374 | 372 | |
| @@ -527,41 +525,162 @@ impl<'c, 'a, 'd> BodyLowerer<'c, 'a, 'd> { |
| 527 | 525 | this.alloc_expr(Expr::Lambda { params, ret, body }, range) |
| 528 | 526 | }), |
| 529 | 527 | ast::Expr::HandleExpr(h) => self.lower_handle(h, range), |
| 530 | | − ast::Expr::HtmlElement(el) => { |
| 531 | | − if !self.reported_html { |
| 532 | | − self.reported_html = true; |
| 533 | | − let span = self.cx.span(range); |
| 534 | | − self.cx.diags.push(Diagnostic::warning( |
| 535 | | − codes::HTML_NOT_CHECKED, |
| 536 | | − "inline HTML is parsed but not type-checked yet (milestone M8)", |
| 537 | | − span, |
| 538 | | − )); |
| 539 | | − } |
| 540 | | − let mut holes = Vec::new(); |
| 541 | | − self.lower_html_holes(el, &mut holes); |
| 542 | | − self.alloc_expr(Expr::Html { holes }, range) |
| 543 | | − } |
| 528 | + ast::Expr::HtmlElement(el) => self.lower_html(el), |
| 529 | + } |
| 530 | + } |
| 531 | + |
| 532 | + /// An element or fragment literal, or a component call. |
| 533 | + fn lower_html(&mut self, el: &ast::HtmlElement) -> ExprId { |
| 534 | + let range = el.syntax().text_range(); |
| 535 | + let open = el.open_tag(); |
| 536 | + let tag = open |
| 537 | + .as_ref() |
| 538 | + .and_then(|o| o.name()) |
| 539 | + .map(|t| t.text().to_owned()); |
| 540 | + let attrs: Vec<HtmlAttr> = open |
| 541 | + .iter() |
| 542 | + .flat_map(|o| o.attrs()) |
| 543 | + .filter_map(|a| self.lower_html_attr(&a)) |
| 544 | + .collect(); |
| 545 | + let children = self.lower_html_children(el); |
| 546 | + match tag { |
| 547 | + Some(tag) if is_component(&tag) => self.lower_component(&tag, attrs, children, range), |
| 548 | + tag => self.alloc_expr( |
| 549 | + Expr::Html(HtmlNode { |
| 550 | + tag, |
| 551 | + attrs, |
| 552 | + children, |
| 553 | + }), |
| 554 | + range, |
| 555 | + ), |
| 544 | 556 | } |
| 545 | 557 | } |
| 546 | 558 | |
| 547 | | − fn lower_html_holes(&mut self, el: &ast::HtmlElement, holes: &mut Vec<ExprId>) { |
| 548 | | − if let Some(open) = el.open_tag() { |
| 549 | | − for attr in open.attrs() { |
| 550 | | − if let Some(e) = attr.expr_value().and_then(|v| v.expr()) { |
| 551 | | − holes.push(self.lower_expr(&e)); |
| 559 | + fn lower_html_attr(&mut self, attr: &ast::HtmlAttr) -> Option<HtmlAttr> { |
| 560 | + let name = attr.name()?.text().to_owned(); |
| 561 | + let span = attr.syntax().text_range(); |
| 562 | + if name.starts_with("on") && name.len() > 2 { |
| 563 | + self.cx.error( |
| 564 | + codes::HTML_EVENT_RESERVED, |
| 565 | + format!("event attribute `{name}` is reserved for the client target (after v0); server-rendered HTML has no event handlers"), |
| 566 | + span, |
| 567 | + ); |
| 568 | + return None; |
| 569 | + } |
| 570 | + let value = if let Some(s) = attr.string_value() { |
| 571 | + let text = s.text(); |
| 572 | + HtmlAttrValue::Str(text[1..text.len().saturating_sub(1).max(1)].to_owned()) |
| 573 | + } else if let Some(e) = attr.expr_value().and_then(|v| v.expr()) { |
| 574 | + HtmlAttrValue::Expr(self.lower_expr(&e)) |
| 575 | + } else { |
| 576 | + HtmlAttrValue::Flag |
| 577 | + }; |
| 578 | + Some(HtmlAttr { name, value, span }) |
| 579 | + } |
| 580 | + |
| 581 | + fn lower_html_children(&mut self, el: &ast::HtmlElement) -> Vec<HtmlChild> { |
| 582 | + let mut children = Vec::new(); |
| 583 | + for content in el.content() { |
| 584 | + match content { |
| 585 | + ast::HtmlContent::Text(t) => { |
| 586 | + let text = jsx_text(t.text()); |
| 587 | + if !text.is_empty() { |
| 588 | + children.push(HtmlChild::Text(text)); |
| 589 | + } |
| 590 | + } |
| 591 | + ast::HtmlContent::Element(inner) => { |
| 592 | + children.push(HtmlChild::Node(self.lower_html(&inner))) |
| 593 | + } |
| 594 | + ast::HtmlContent::Expr(hole) => { |
| 595 | + if let Some(e) = hole.expr() { |
| 596 | + children.push(HtmlChild::Expr(self.lower_expr(&e))); |
| 597 | + } |
| 552 | 598 | } |
| 553 | 599 | } |
| 554 | 600 | } |
| 555 | | − for child in el.children() { |
| 556 | | − match child { |
| 557 | | − ast::HtmlChild::HtmlElement(inner) => self.lower_html_holes(&inner, holes), |
| 558 | | − ast::HtmlChild::HtmlExpr(hole) => { |
| 559 | | − if let Some(e) = hole.expr() { |
| 560 | | − holes.push(self.lower_expr(&e)); |
| 601 | + children |
| 602 | + } |
| 603 | + |
| 604 | + /// `<ui.PostList posts={ps}>...</ui.PostList>` calls |
| 605 | + /// `ui.post_list(posts: ps, children: [...])`. |
| 606 | + fn lower_component( |
| 607 | + &mut self, |
| 608 | + tag: &str, |
| 609 | + attrs: Vec<HtmlAttr>, |
| 610 | + children: Vec<HtmlChild>, |
| 611 | + range: TextRange, |
| 612 | + ) -> ExprId { |
| 613 | + let segments: Vec<&str> = tag.split('.').collect(); |
| 614 | + let function = snake_case(segments.last().copied().unwrap_or_default()); |
| 615 | + let callee = match segments.as_slice() { |
| 616 | + [_] => self.resolve_value(&function, range), |
| 617 | + [module, _] => match self.cx.hir.lookup_module(self.cx.module, module) { |
| 618 | + Some(m) => match self.cx.hir.exports[m.index()].values.get(&function) { |
| 619 | + Some(&def) => self.alloc_expr(Expr::Def(def), range), |
| 620 | + None => { |
| 621 | + self.cx.error( |
| 622 | + codes::UNRESOLVED_NAME, |
| 623 | + format!("component `<{tag}>` needs a public function `{function}` in module `{module}`"), |
| 624 | + range, |
| 625 | + ); |
| 626 | + self.missing(range) |
| 561 | 627 | } |
| 628 | + }, |
| 629 | + None => { |
| 630 | + self.cx.error( |
| 631 | + codes::UNRESOLVED_MODULE, |
| 632 | + format!("no module `{module}` is imported"), |
| 633 | + range, |
| 634 | + ); |
| 635 | + self.missing(range) |
| 562 | 636 | } |
| 637 | + }, |
| 638 | + _ => { |
| 639 | + self.cx.error( |
| 640 | + codes::UNRESOLVED_NAME, |
| 641 | + "component tags have at most one module qualifier", |
| 642 | + range, |
| 643 | + ); |
| 644 | + self.missing(range) |
| 563 | 645 | } |
| 646 | + }; |
| 647 | + let mut args = Vec::new(); |
| 648 | + for attr in attrs { |
| 649 | + let expr = match attr.value { |
| 650 | + HtmlAttrValue::Str(s) => self.alloc_expr(Expr::Lit(Lit::String(s)), attr.span), |
| 651 | + HtmlAttrValue::Expr(e) => e, |
| 652 | + HtmlAttrValue::Flag => { |
| 653 | + self.cx.error( |
| 654 | + codes::COMPONENT_FLAG, |
| 655 | + format!( |
| 656 | + "component argument `{}` needs a value: `{}={{...}}`", |
| 657 | + attr.name, attr.name |
| 658 | + ), |
| 659 | + attr.span, |
| 660 | + ); |
| 661 | + self.missing(attr.span) |
| 662 | + } |
| 663 | + }; |
| 664 | + args.push(Arg { |
| 665 | + label: Some(attr.name), |
| 666 | + expr, |
| 667 | + }); |
| 668 | + } |
| 669 | + if !children.is_empty() { |
| 670 | + let expr = self.alloc_expr(Expr::HtmlChildren(children), range); |
| 671 | + args.push(Arg { |
| 672 | + label: Some("children".into()), |
| 673 | + expr, |
| 674 | + }); |
| 564 | 675 | } |
| 676 | + self.alloc_expr( |
| 677 | + Expr::Call { |
| 678 | + callee, |
| 679 | + args, |
| 680 | + spread: None, |
| 681 | + }, |
| 682 | + range, |
| 683 | + ) |
| 565 | 684 | } |
| 566 | 685 | |
| 567 | 686 | /// A name in expression position: local, then item (own, imported, |
| @@ -1210,3 +1329,56 @@ impl<'c, 'a, 'd> BodyLowerer<'c, 'a, 'd> { |
| 1210 | 1329 | } |
| 1211 | 1330 | } |
| 1212 | 1331 | } |
| 1332 | + |
| 1333 | +/// A component tag starts with an uppercase letter or is qualified. |
| 1334 | +fn is_component(tag: &str) -> bool { |
| 1335 | + tag.starts_with(|c: char| c.is_uppercase()) || tag.contains('.') |
| 1336 | +} |
| 1337 | + |
| 1338 | +/// `PostList` → `post_list`. |
| 1339 | +fn snake_case(name: &str) -> String { |
| 1340 | + let mut out = String::new(); |
| 1341 | + for (i, c) in name.chars().enumerate() { |
| 1342 | + if c.is_uppercase() { |
| 1343 | + if i > 0 { |
| 1344 | + out.push('_'); |
| 1345 | + } |
| 1346 | + out.extend(c.to_lowercase()); |
| 1347 | + } else { |
| 1348 | + out.push(c); |
| 1349 | + } |
| 1350 | + } |
| 1351 | + out |
| 1352 | +} |
| 1353 | + |
| 1354 | +/// JSX whitespace rules: lines are trimmed (except the outer edges of the |
| 1355 | +/// first and last line), blank lines dropped, and the rest joined by a |
| 1356 | +/// space. Text on one line is kept as written. |
| 1357 | +fn jsx_text(raw: &str) -> String { |
| 1358 | + let lines: Vec<&str> = raw.split('\n').collect(); |
| 1359 | + let last = lines.len() - 1; |
| 1360 | + lines |
| 1361 | + .iter() |
| 1362 | + .enumerate() |
| 1363 | + .map(|(i, line)| { |
| 1364 | + let line = if i > 0 { line.trim_start() } else { line }; |
| 1365 | + if i < last { line.trim_end() } else { line } |
| 1366 | + }) |
| 1367 | + .filter(|line| !line.trim().is_empty()) |
| 1368 | + .collect::<Vec<_>>() |
| 1369 | + .join(" ") |
| 1370 | +} |
| 1371 | + |
| 1372 | +#[cfg(test)] |
| 1373 | +mod tests { |
| 1374 | + use super::*; |
| 1375 | + |
| 1376 | + #[test] |
| 1377 | + fn jsx_whitespace() { |
| 1378 | + assert_eq!(jsx_text("Hello, "), "Hello, "); |
| 1379 | + assert_eq!(jsx_text("\n Hello\n world\n "), "Hello world"); |
| 1380 | + assert_eq!(jsx_text("\n \n"), ""); |
| 1381 | + assert_eq!(snake_case("PostList"), "post_list"); |
| 1382 | + assert!(is_component("Card") && is_component("ui.card") && !is_component("div")); |
| 1383 | + } |
| 1384 | +} |