sink.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. use dioxus_autofmt::*;
  2. use proc_macro2::TokenStream as TokenStream2;
  3. fn print_block(wrong: &str) {
  4. let formatted = fmt_block(wrong).unwrap();
  5. println!("{}", formatted);
  6. }
  7. #[test]
  8. fn formats_block() {
  9. let block = r#"
  10. div {
  11. div {
  12. class: "asd",
  13. class: "asd",class: "asd",class: "asd",class: "asd",class: "asd",
  14. key: "ddd",
  15. onclick: move |_| {
  16. let blah = 120;
  17. true
  18. },
  19. blah: 123,
  20. onclick: move |_| {
  21. let blah = 120;
  22. true
  23. },
  24. onclick: move |_| {
  25. let blah = 120;
  26. true
  27. },
  28. onclick: move |_| {
  29. let blah = 120;
  30. true
  31. },
  32. div {
  33. div {
  34. "hi"
  35. }
  36. h2 {
  37. class: "asd",
  38. }
  39. }
  40. Component {}
  41. Component<Generic> {}
  42. }
  43. }
  44. "#;
  45. let formatted = fmt_block(block).unwrap();
  46. println!("{formatted}");
  47. }
  48. #[test]
  49. fn parse_comment() {
  50. let block = r#"
  51. div {
  52. adsasd: "asd", // this is a comment
  53. }
  54. "#;
  55. let parsed: TokenStream2 = syn::parse_str(block).unwrap();
  56. dbg!(parsed);
  57. }
  58. #[test]
  59. fn print_cases() {
  60. print_block(
  61. r#"
  62. div {
  63. adsasd: "asd",
  64. h1 {"asd"}
  65. div {
  66. div {
  67. "hello"
  68. }
  69. div {
  70. "goodbye"
  71. }
  72. div { class: "broccoli",
  73. div {
  74. "hi"
  75. }
  76. }
  77. div { class: "broccolibroccolibroccolibroccolibroccolibroccolibroccolibroccolibroccolibroccoli",
  78. div {
  79. "hi"
  80. }
  81. }
  82. div { class: "alksdjasd", onclick: move |_| {
  83. // hi!
  84. liberty!();
  85. },
  86. div {
  87. "hi"
  88. }
  89. }
  90. commented {
  91. // is unparalled
  92. class: "asdasd",
  93. // My genius
  94. div {
  95. "hi"
  96. }
  97. div {
  98. }
  99. }
  100. }
  101. }
  102. "#,
  103. );
  104. }
  105. #[test]
  106. fn format_comments() {
  107. let block = r#"
  108. div {
  109. adsasd: "asd", block: "asd",
  110. // this is a comment
  111. "hello"
  112. // this is a comment 1
  113. // this is a comment 2
  114. "hello"
  115. div {
  116. // this is a comment
  117. "hello"
  118. }
  119. div {
  120. // empty space
  121. }
  122. div {
  123. key: "asd", class: "asdsda",
  124. div {}
  125. h3 {}
  126. }
  127. }
  128. "#;
  129. let formatted = fmt_block(block).unwrap();
  130. println!("{formatted}");
  131. }
  132. #[test]
  133. fn formats_component() {
  134. let block = r#"
  135. Component {
  136. adsasd: "asd",
  137. // this is a comment
  138. onclick: move |_| {
  139. let blah = 120;
  140. let blah = 122;
  141. },
  142. }
  143. "#;
  144. let formatted = fmt_block(block).unwrap();
  145. println!("{formatted}");
  146. }
  147. #[test]
  148. fn formats_component_complex() {
  149. let block = r#"
  150. div {
  151. Component {
  152. adsasd: "asd",
  153. onclick: move |_| {
  154. let a = a;
  155. }
  156. div {
  157. "thing"
  158. }
  159. }
  160. Component {
  161. asdasd: "asdasd",
  162. asdasd: "asdasdasdasdasdasdasdasdasdasd",
  163. ..Props {
  164. a: 10,
  165. b: 20
  166. }
  167. }
  168. Component {
  169. //
  170. asdasd: "asdasd",
  171. ..Props {
  172. a: 10,
  173. b: 20,
  174. c: {
  175. fn main() {
  176. //
  177. }
  178. }
  179. }
  180. "content"
  181. }
  182. }
  183. "#;
  184. let formatted = fmt_block(block).unwrap();
  185. println!("{formatted}");
  186. }
  187. #[test]
  188. fn formats_element() {
  189. let block = r#"
  190. div {
  191. a: "1234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910",
  192. a: "123",
  193. a: "123",
  194. a: "123",
  195. a: "123",
  196. a: "123",
  197. a: "123",
  198. a: "123",
  199. a: "123",
  200. }
  201. "#;
  202. let formatted = fmt_block(block).unwrap();
  203. println!("{formatted}");
  204. }
  205. #[test]
  206. fn formats_element_short() {
  207. let block = r#"
  208. div {
  209. a: "123",
  210. a: "123",
  211. a: "123",
  212. a: "123",
  213. a: "123",
  214. a: "123",
  215. a: "123",
  216. a: "123",
  217. a: "123",
  218. }
  219. "#;
  220. let formatted = fmt_block(block).unwrap();
  221. println!("{formatted}");
  222. }
  223. #[test]
  224. fn formats_element_nested() {
  225. let block = r#"
  226. h3 {
  227. class: "mb-2 text-xl font-bold",
  228. "Invite Member"
  229. }
  230. "#;
  231. let formatted = fmt_block(block).unwrap();
  232. assert_eq!(
  233. formatted,
  234. r#"h3 { class: "mb-2 text-xl font-bold", "Invite Member" }"#
  235. );
  236. }
  237. #[test]
  238. fn formats_element_props_on_top() {
  239. // Gets compressed because there's no real comments
  240. let block = r#"
  241. h3 {
  242. class: "mb-2 text-xl font-bold mb-2 text-xl font-bold mb-2 text-xl font-bold mb-2 text-xl font-bold mb-2 text-xl font-bold",
  243. "Invite Member"
  244. }
  245. "#;
  246. let formatted = fmt_block(block).unwrap();
  247. println!("{formatted}");
  248. }
  249. #[test]
  250. fn formats_element_nested_no_trailing_tabs() {
  251. let block = r#"
  252. img { class: "mb-6 mx-auto h-24", src: "artemis-assets/images/friends.png", alt: "", }
  253. "#;
  254. let formatted = fmt_block(block).unwrap();
  255. assert_eq!(
  256. formatted,
  257. r#"img { class: "mb-6 mx-auto h-24", src: "artemis-assets/images/friends.png", alt: "" }"#
  258. );
  259. }
  260. #[test]
  261. fn formats_element_with_correct_indent() {
  262. let block = r###"
  263. div {
  264. a { class: "py-2 px-3 bg-indigo-500 hover:bg-indigo-600 rounded text-xs text-white", href: "#",
  265. "Send invitation"
  266. }
  267. }
  268. "###;
  269. let formatted = fmt_block(block).unwrap();
  270. println!("{formatted}");
  271. }
  272. #[test]
  273. fn small_elements_and_text_are_small() {
  274. let block = r###"
  275. a { class: "text-white",
  276. "Send invitation"
  277. }
  278. "###;
  279. let formatted = fmt_block(block).unwrap();
  280. assert_eq!(formatted, r#"a { class: "text-white", "Send invitation" }"#);
  281. }
  282. #[test]
  283. fn formats_component_man_props() {
  284. let block = r#"
  285. Component {
  286. ..MyProps {
  287. val: 123
  288. },
  289. adsasd: "asd", // this is a comment
  290. onclick: move |_| {
  291. let blah = 120;
  292. },
  293. }
  294. "#;
  295. let formatted = fmt_block(block).unwrap();
  296. println!("{formatted}");
  297. }
  298. #[test]
  299. fn formats_component_tiny() {
  300. let block = r#"
  301. Component { a: 123
  302. }
  303. "#;
  304. let formatted = fmt_block(block).unwrap();
  305. println!("{formatted}");
  306. }
  307. #[test]
  308. fn formats_exprs() {
  309. let block = r#"
  310. ul {
  311. div {}
  312. (0..10).map(|f| rsx! {
  313. li { "hi" }
  314. })
  315. div {}
  316. }
  317. "#;
  318. let formatted = fmt_block(block).unwrap();
  319. println!("{formatted}");
  320. }
  321. #[test]
  322. fn formats_exprs_neg_indent() {
  323. let block = r#"
  324. ul {
  325. (0..10).map(|f| rsx!{
  326. li {
  327. "hi"
  328. }
  329. })
  330. }
  331. "#;
  332. let formatted = fmt_block(block).unwrap();
  333. println!("{formatted}");
  334. }
  335. #[test]
  336. fn formats_exprs_handlers() {
  337. let block = r#"
  338. button {
  339. class: "flex items-center pl-3 py-3 pr-2 text-gray-500 hover:bg-indigo-50 rounded",
  340. onclick: move |evt| {
  341. show_user_menu.set(!show_user_menu.get()); evt.cancel_bubble(); },
  342. onclick: move |evt|
  343. show_user_menu.set(!show_user_menu.get()),
  344. span { class: "inline-block mr-4",
  345. icons::icon_14 {}
  346. }
  347. span { "Settings" }
  348. }
  349. "#;
  350. let formatted = fmt_block(block).unwrap();
  351. println!("{formatted}");
  352. }
  353. #[test]
  354. fn formats_complex() {
  355. let block = r#"
  356. li {
  357. Link {
  358. class: "flex items-center pl-3 py-3 pr-4 {active_class} rounded",
  359. to: "{to}",
  360. span { class: "inline-block mr-3",
  361. icons::icon_0 {}
  362. }
  363. span { "{name}" }
  364. children.is_some().then(|| rsx! {
  365. span {
  366. class: "inline-block ml-auto hover:bg-gray-500",
  367. onclick: move |evt| {
  368. // open.set(!open.get());
  369. evt.cancel_bubble();
  370. },
  371. icons::icon_8 {}
  372. }
  373. })
  374. }
  375. div {
  376. class: "px-4",
  377. is_current.then(|| rsx!{ children })
  378. // open.then(|| rsx!{ children })
  379. }
  380. }
  381. "#;
  382. let formatted = fmt_block(block).unwrap();
  383. println!("{formatted}");
  384. }
  385. #[test]
  386. fn formats_document() {
  387. let block = r#"
  388. rsx!{
  389. Component {
  390. adsasd: "asd", // this is a comment
  391. onclick: move |_| {
  392. let blah = 120;
  393. },
  394. }
  395. }
  396. "#;
  397. let formatted = fmt_file(block);
  398. println!("{formatted:?}");
  399. }
  400. #[test]
  401. fn component_path_mod_style() {
  402. let block = r#"
  403. rsx!{
  404. my::thing::Component {
  405. adsasd: "asd", // this is a comment
  406. onclick: move |_| {
  407. let blah = 120;
  408. },
  409. }
  410. }
  411. "#;
  412. let formatted = fmt_file(block);
  413. println!("{formatted:?}");
  414. }
  415. #[test]
  416. fn formats_valid_rust_src() {
  417. let src = r#"
  418. //
  419. rsx! {
  420. div {}
  421. div {
  422. h3 {"asd"
  423. }
  424. }
  425. }
  426. "#;
  427. let formatted = fmt_file(src);
  428. println!("{formatted:?}");
  429. }
  430. #[test]
  431. fn formats_valid_rust_src_with_indents() {
  432. let mut src = r#"
  433. #[inline_props]
  434. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  435. const ICON_SIZE: u32 = 36;
  436. rsx! {
  437. div {
  438. h1 {"thing"}
  439. }
  440. }
  441. }
  442. "#
  443. .to_string();
  444. let formatted = fmt_file(&src);
  445. let block = formatted.into_iter().next().unwrap();
  446. src.replace_range(
  447. block.start - 1..block.end + 1,
  448. &format!("{{ {} }}", &block.formatted),
  449. );
  450. }
  451. #[test]
  452. fn formats_multiple_blocks() {
  453. let mut src = r#"
  454. #[inline_props]
  455. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  456. const ICON_SIZE: u32 = 36;
  457. rsx! {
  458. div {
  459. h1 {"thing"}
  460. }
  461. }
  462. rsx! {
  463. div {
  464. Ball {
  465. a: rsx!{
  466. "asdasd"
  467. }
  468. }
  469. }
  470. }
  471. }
  472. #[inline_props]
  473. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  474. const ICON_SIZE: u32 = 36;
  475. rsx! {
  476. div {
  477. h1 {"thing"}
  478. }
  479. }
  480. rsx! {
  481. div {
  482. Ball {
  483. a: rsx!{
  484. "asdasd"
  485. }
  486. }
  487. }
  488. }
  489. }
  490. "#
  491. .to_string();
  492. let formatted = fmt_file(&src);
  493. dbg!(&formatted);
  494. let block = formatted.into_iter().next().unwrap();
  495. src.replace_range(
  496. block.start - 1..block.end + 1,
  497. &format!("{{ {} }}", &block.formatted),
  498. );
  499. }
  500. #[test]
  501. fn empty_blocks() {
  502. let src = r###"
  503. pub fn Alert(cx: Scope) -> Element {
  504. cx.render(rsx! {
  505. div { }
  506. })
  507. }
  508. "###
  509. .to_string();
  510. let formatted = fmt_file(&src);
  511. dbg!(&formatted);
  512. }