Skip to content

Commit c7d98b9

Browse files
author
Robert Masen
committed
add js doc @param and @returns annotations
1 parent 8abe0f9 commit c7d98b9

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,14 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
368368
Ok(self)
369369
}
370370

371+
pub fn js_doc_comments(&self) -> String {
372+
let mut ret: String = self.js_arguments.iter().map(|a| {
373+
format!("@param {{{}}} {}\n", a.1, a.0)
374+
}).collect();
375+
ret.push_str(&format!("@returns {{{}}}", self.ret_ty));
376+
ret
377+
}
378+
371379
/// Generate the actual function.
372380
///
373381
/// The `prefix` specified is typically the string "function" but may be
@@ -377,7 +385,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
377385
/// Returns two strings, the first of which is the JS expression for the
378386
/// generated function shim and the second is a TypeScript signature of the
379387
/// JS expression.
380-
pub fn finish(&self, prefix: &str, invoc: &str) -> (String, String) {
388+
pub fn finish(&self, prefix: &str, invoc: &str) -> (String, String, String) {
381389
let js_args = self
382390
.js_arguments
383391
.iter()
@@ -417,6 +425,6 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
417425
"{} {}({}): {};\n",
418426
prefix, self.js_name, ts_args, self.ret_ty
419427
);
420-
(js, ts)
428+
(js, ts, self.js_doc_comments())
421429
}
422430
}

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

+20-13
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct ExportedClass {
4343
}
4444

4545
struct ClassField {
46-
comments: String,
46+
comments: Vec<String>,
4747
name: String,
4848
readonly: bool,
4949
}
@@ -55,7 +55,6 @@ pub struct SubContext<'a, 'b: 'a> {
5555

5656
impl<'a> Context<'a> {
5757
fn export(&mut self, name: &str, contents: &str, comments: Option<String>) {
58-
let contents = contents;
5958
let contents = contents.trim();
6059
if let Some(ref c) = comments {
6160
self.globals.push_str(c);
@@ -596,14 +595,14 @@ impl<'a> Context<'a> {
596595
));
597596
cx.finish("", &format!("wasm.{}", wasm_setter)).0
598597
};
599-
let (get, _ts) = Js2Rust::new(&field.name, self)
598+
let (get, _ts, js_doc) = Js2Rust::new(&field.name, self)
600599
.method(true, false)
601600
.ret(&Some(descriptor))?
602601
.finish("", &format!("wasm.{}", wasm_getter));
603602
if !dst.ends_with("\n") {
604603
dst.push_str("\n");
605604
}
606-
dst.push_str(&field.comments);
605+
dst.push_str(&format_doc_comments(&field.comments, Some(js_doc)));
607606
dst.push_str("get ");
608607
dst.push_str(&field.name);
609608
dst.push_str(&get);
@@ -1653,11 +1652,11 @@ impl<'a, 'b> SubContext<'a, 'b> {
16531652
.exported_classes
16541653
.entry(s.name.clone())
16551654
.or_insert_with(Default::default);
1656-
class.comments = format_doc_comments(&s.comments);
1655+
class.comments = format_doc_comments(&s.comments, None);
16571656
class.fields.extend(s.fields.iter().map(|f| ClassField {
16581657
name: f.name.clone(),
16591658
readonly: f.readonly,
1660-
comments: format_doc_comments(&f.comments),
1659+
comments: f.comments.clone(),
16611660
}));
16621661
}
16631662

@@ -1674,13 +1673,13 @@ impl<'a, 'b> SubContext<'a, 'b> {
16741673
Some(d) => d,
16751674
};
16761675

1677-
let (js, ts) = Js2Rust::new(&export.function.name, self.cx)
1676+
let (js, ts, js_doc) = Js2Rust::new(&export.function.name, self.cx)
16781677
.process(descriptor.unwrap_function())?
16791678
.finish("function", &format!("wasm.{}", export.function.name));
16801679
self.cx.export(
16811680
&export.function.name,
16821681
&js,
1683-
Some(format_doc_comments(&export.comments)),
1682+
Some(format_doc_comments(&export.comments, Some(js_doc))),
16841683
);
16851684
self.cx.globals.push_str("\n");
16861685
self.cx.typescript.push_str("export ");
@@ -1701,18 +1700,19 @@ impl<'a, 'b> SubContext<'a, 'b> {
17011700
Some(d) => d,
17021701
};
17031702

1704-
let (js, ts) = Js2Rust::new(&export.function.name, self.cx)
1703+
let (js, ts, js_doc) = Js2Rust::new(&export.function.name, self.cx)
17051704
.method(export.method, export.consumed)
17061705
.process(descriptor.unwrap_function())?
17071706
.finish("", &format!("wasm.{}", wasm_name));
1707+
17081708
let class = self
17091709
.cx
17101710
.exported_classes
17111711
.entry(class_name.to_string())
17121712
.or_insert(ExportedClass::default());
17131713
class
17141714
.contents
1715-
.push_str(&format_doc_comments(&export.comments));
1715+
.push_str(&format_doc_comments(&export.comments, Some(js_doc)));
17161716
if !export.method {
17171717
class.contents.push_str("static ");
17181718
class.typescript.push_str("static ");
@@ -1960,7 +1960,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
19601960
self.cx.export(
19611961
&enum_.name,
19621962
&format!("Object.freeze({{ {} }})", variants),
1963-
Some(format_doc_comments(&enum_.comments)),
1963+
Some(format_doc_comments(&enum_.comments, None)),
19641964
);
19651965
self.cx
19661966
.typescript
@@ -2011,10 +2011,17 @@ impl<'a, 'b> SubContext<'a, 'b> {
20112011
}
20122012
}
20132013

2014-
fn format_doc_comments(comments: &Vec<String>) -> String {
2014+
fn format_doc_comments(comments: &Vec<String>, js_doc_comments: Option<String>) -> String {
20152015
let body: String = comments
20162016
.iter()
20172017
.map(|c| format!("*{}\n", c.trim_matches('"')))
20182018
.collect();
2019-
format!("/**\n{}*/\n", body)
2019+
let doc = if let Some(docs) = js_doc_comments {
2020+
docs.lines()
2021+
.map(|l| format!("* {} \n", l))
2022+
.collect()
2023+
} else {
2024+
String::new()
2025+
};
2026+
format!("/**\n{}{}*/\n", body, doc)
20202027
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
142142
}
143143

144144
if let Some((f, mutable)) = arg.stack_closure() {
145-
let (js, _ts) = {
145+
let (js, _ts, _js_doc) = {
146146
let mut builder = Js2Rust::new("", self.cx);
147147
if mutable {
148148
builder
@@ -179,7 +179,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
179179
}
180180

181181
if let Some(closure) = arg.ref_closure() {
182-
let (js, _ts) = {
182+
let (js, _ts, _js_doc) = {
183183
let mut builder = Js2Rust::new("", self.cx);
184184
if closure.mutable {
185185
builder

tests/all/comments.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ fn works() {
4141
p.gen_bindings();
4242
let js = p.read_js();
4343
let comments = extract_doc_comments(&js);
44-
assert!(comments.iter().all(|c| c == "This comment should exist"));
44+
assert!(comments.iter().all(|c| c == "This comment should exist" ||
45+
c.starts_with("@")));
4546
}
4647

4748
/// Pull out all lines in a js string that start with

0 commit comments

Comments
 (0)