Skip to content

Commit a508589

Browse files
author
robert masen
committed
gen comments
1 parent a5f1984 commit a508589

File tree

1 file changed

+32
-19
lines changed
  • crates/cli-support/src/js

1 file changed

+32
-19
lines changed

crates/cli-support/src/js/mod.rs

+32-19
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ pub struct Context<'a> {
3535

3636
#[derive(Default)]
3737
pub struct ExportedClass {
38+
comments: String,
3839
contents: String,
3940
typescript: String,
4041
constructor: Option<String>,
4142
fields: Vec<ClassField>,
4243
}
4344

4445
struct ClassField {
46+
comments: String,
4547
name: String,
4648
readonly: bool,
4749
}
@@ -52,9 +54,12 @@ pub struct SubContext<'a, 'b: 'a> {
5254
}
5355

5456
impl<'a> Context<'a> {
55-
fn export(&mut self, name: &str, contents: &str) {
57+
fn export(&mut self, name: &str, contents: &str, comments: Option<String>) {
5658
let contents = deindent(contents);
5759
let contents = contents.trim();
60+
if let Some(ref c) = comments {
61+
self.globals.push_str(c);
62+
}
5863
let global = if self.config.nodejs {
5964
if contents.starts_with("class") {
6065
format!("{1}\nmodule.exports.{0} = {0};\n", name, contents)
@@ -396,7 +401,7 @@ impl<'a> Context<'a> {
396401
.with_context(|_| {
397402
format!("failed to generate internal JS function `{}`", name)
398403
})?;
399-
self.export(name, &contents);
404+
self.export(name, &contents, None);
400405
Ok(())
401406
}
402407

@@ -461,7 +466,7 @@ impl<'a> Context<'a> {
461466
function(ptr) {{
462467
return addHeapObject({}.__construct(ptr));
463468
}}
464-
", name));
469+
", name), None);
465470
}
466471

467472
for field in class.fields.iter() {
@@ -484,7 +489,7 @@ impl<'a> Context<'a> {
484489
.method(true)
485490
.ret(&Some(descriptor))?
486491
.finish("", &format!("wasm.{}", wasm_getter));
487-
492+
dst.push_str(&field.comments);
488493
dst.push_str("get ");
489494
dst.push_str(&field.name);
490495
dst.push_str(&get);
@@ -510,7 +515,7 @@ impl<'a> Context<'a> {
510515
dst.push_str("}\n");
511516
ts_dst.push_str("}\n");
512517

513-
self.export(&name, &dst);
518+
self.export(&name, &dst, Some(class.comments.clone()));
514519
self.typescript.push_str(&ts_dst);
515520

516521
Ok(())
@@ -534,7 +539,7 @@ impl<'a> Context<'a> {
534539

535540
fn rewrite_imports(&mut self, module_name: &str) {
536541
for (name, contents) in self._rewrite_imports(module_name) {
537-
self.export(&name, &contents);
542+
self.export(&name, &contents, None);
538543
}
539544
}
540545

@@ -691,7 +696,7 @@ impl<'a> Context<'a> {
691696
return;
692697
throw new Error('stack is not currently empty');
693698
}
694-
");
699+
", None);
695700
}
696701
}
697702

@@ -715,7 +720,7 @@ impl<'a> Context<'a> {
715720
throw new Error('slab is not currently empty');
716721
}}
717722
}}
718-
", initial_values.len()));
723+
", initial_values.len()), None);
719724
}
720725
}
721726

@@ -1406,7 +1411,7 @@ impl<'a> Context<'a> {
14061411

14071412
// Ensure a blank line between adjacent items, and ensure everything is
14081413
// terminated with a newline.
1409-
while !self.globals.ends_with("\n\n\n") {
1414+
while !self.globals.ends_with("\n\n\n") && !self.globals.ends_with("*/\n") {
14101415
self.globals.push_str("\n");
14111416
}
14121417
self.globals.push_str(s);
@@ -1452,14 +1457,16 @@ impl<'a, 'b> SubContext<'a, 'b> {
14521457
self.generate_enum(e);
14531458
}
14541459
for s in self.program.structs.iter() {
1455-
self.cx.exported_classes
1460+
let mut class = self.cx.exported_classes
14561461
.entry(s.name.clone())
1457-
.or_insert_with(Default::default)
1458-
.fields
1459-
.extend(s.fields.iter().map(|s| {
1462+
.or_insert_with(Default::default);
1463+
class.comments = format_doc_comments(&s.comments);
1464+
class.fields
1465+
.extend(s.fields.iter().map(|f| {
14601466
ClassField {
1461-
name: s.name.clone(),
1462-
readonly: s.readonly,
1467+
name: f.name.clone(),
1468+
readonly: f.readonly,
1469+
comments: format_doc_comments(&f.comments),
14631470
}
14641471
}));
14651472
}
@@ -1477,7 +1484,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
14771484
let (js, ts) = Js2Rust::new(&export.function.name, self.cx)
14781485
.process(descriptor.unwrap_function())?
14791486
.finish("function", &format!("wasm.{}", export.function.name));
1480-
self.cx.export(&export.function.name, &js);
1487+
self.cx.export(&export.function.name, &js, Some(format_doc_comments(&export.comments)));
14811488
self.cx.globals.push_str("\n");
14821489
self.cx.typescript.push_str("export ");
14831490
self.cx.typescript.push_str(&ts);
@@ -1593,7 +1600,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
15931600
function() {{
15941601
return addHeapObject({});
15951602
}}
1596-
", obj));
1603+
", obj), None);
15971604
Ok(())
15981605
}
15991606

@@ -1688,7 +1695,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
16881695
.catch(import.catch)
16891696
.process(descriptor.unwrap_function())?
16901697
.finish(&target);
1691-
self.cx.export(&import.shim, &js);
1698+
self.cx.export(&import.shim, &js, None);
16921699
Ok(())
16931700
}
16941701

@@ -1698,7 +1705,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
16981705
for variant in enum_.variants.iter() {
16991706
variants.push_str(&format!("{}:{},", variant.name, variant.value));
17001707
}
1701-
self.cx.export(&enum_.name, &format!("Object.freeze({{ {} }})", variants));
1708+
self.cx.export(&enum_.name, &format!("Object.freeze({{ {} }})", variants), Some(format_doc_comments(&enum_.comments)));
17021709
self.cx.typescript.push_str(&format!("export enum {} {{", enum_.name));
17031710

17041711
variants.clear();
@@ -1764,3 +1771,9 @@ fn deindent(s: &str) -> String {
17641771
}
17651772
ret
17661773
}
1774+
1775+
1776+
fn format_doc_comments(comments: &Vec<String>) -> String {
1777+
let body: String = comments.iter().map(|c| format!("*{}\n", c.trim_matches('"'))).collect();
1778+
format!("/**\n{}*/\n", body)
1779+
}

0 commit comments

Comments
 (0)