sink.rs 12 KB

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