Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

Add doc comments to constants

+41 -8
+2 -2
CHANGELOG.md
··· 4 4 5 5 ### Compiler 6 6 7 - - The generated JavaScript functions now include any doc comment as a JSDoc 8 - comment, making it easier to use the generated code and browse its 7 + - Generated JavaScript functions and constants now include any doc comment as a 8 + JSDoc comment, making it easier to use the generated code and browse its 9 9 documentation from JavaScript. 10 10 ([Giacomo Cavalieri](https://github.com/giacomocavalieri)) 11 11
+14 -5
compiler-core/src/javascript.rs
··· 323 323 publicity, 324 324 name, 325 325 value, 326 + documentation, 326 327 .. 327 - }) => Some(self.module_constant(*publicity, name, value)), 328 + }) => Some(self.module_constant(*publicity, name, value, documentation)), 328 329 329 330 Definition::Function(function) => { 330 331 // If there's an external JavaScript implementation then it will be imported, ··· 574 575 publicity: Publicity, 575 576 name: &'a EcoString, 576 577 value: &'a TypedConstant, 578 + documentation: &'a Option<(u32, EcoString)>, 577 579 ) -> Output<'a> { 578 580 let head = if publicity.is_private() { 579 581 "const " ··· 593 595 594 596 let document = generator.constant_expression(Context::Constant, value)?; 595 597 598 + let jsdoc = if let Some((_, documentation)) = documentation { 599 + jsdoc_comment(documentation, publicity).append(line()) 600 + } else { 601 + nil() 602 + }; 603 + 596 604 Ok(docvec![ 605 + jsdoc, 597 606 head, 598 607 maybe_escape_identifier(name), 599 608 " = ", ··· 629 638 let function_doc = match &function.documentation { 630 639 None => nil(), 631 640 Some((_, documentation)) => { 632 - function_doc(documentation, function.publicity).append(line()) 641 + jsdoc_comment(documentation, function.publicity).append(line()) 633 642 } 634 643 }; 635 644 ··· 702 711 } 703 712 } 704 713 705 - fn function_doc<'a>(documentation: &'a EcoString, publicity: Publicity) -> Document<'a> { 714 + fn jsdoc_comment<'a>(documentation: &'a EcoString, publicity: Publicity) -> Document<'a> { 706 715 let doc_lines = documentation 707 716 .trim_end() 708 717 .split('\n') ··· 715 724 if !publicity.is_public() { 716 725 // If the function is not public we hide the documentation using 717 726 // the `@ignore` tag: https://jsdoc.app/tags-ignore 718 - doc = docvec![doc, " * ", line(), " * @ignore"]; 727 + doc = docvec![doc, " * ", line(), " * @ignore", line()]; 719 728 } 720 729 // And finally we close the doc comment 721 - docvec![doc, line(), " */"] 730 + docvec![doc, " */"] 722 731 } 723 732 724 733 #[derive(Debug)]
+10
compiler-core/src/javascript/tests/consts.rs
··· 126 126 fn constructor_function_in_constant() { 127 127 assert_js!("pub const a = Ok"); 128 128 } 129 + 130 + #[test] 131 + fn constants_get_their_own_jsdoc_comment() { 132 + assert_js!( 133 + " 134 + /// 11 is clearly the best number! 135 + pub const jaks_favourite_number = 11 136 + " 137 + ); 138 + }
+15
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__consts__constants_get_their_own_jsdoc_comment.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/consts.rs 3 + expression: "\n/// 11 is clearly the best number!\npub const jaks_favourite_number = 11\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + /// 11 is clearly the best number! 8 + pub const jaks_favourite_number = 11 9 + 10 + 11 + ----- COMPILED JAVASCRIPT 12 + /** 13 + * 11 is clearly the best number! 14 + */ 15 + export const jaks_favourite_number = 11;
-1
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__functions__public_function_gets_jsdoc.snap
··· 14 14 /** 15 15 * Hello! This is the documentation of the `main` 16 16 * function. 17 - 18 17 */ 19 18 export function main() { 20 19 return 1;