Skip to content

Commit

Permalink
Merge pull request #1221 from rhysd/format-dts-file
Browse files Browse the repository at this point in the history
Format .d.ts file
  • Loading branch information
alexcrichton authored Feb 1, 2019
2 parents 66be3d3 + 6e2f104 commit 2e80313
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
8 changes: 6 additions & 2 deletions crates/cli-support/src/js/js2rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,12 +750,16 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
.map(|s| format!("{}: {}", s.0, s.1))
.collect::<Vec<_>>()
.join(", ");
let mut ts = format!("{} {}({})", prefix, self.js_name, ts_args);
let mut ts = if prefix.is_empty() {
format!("{}({})", self.js_name, ts_args)
} else {
format!("{} {}({})", prefix, self.js_name, ts_args)
};
if self.constructor.is_none() {
ts.push_str(": ");
ts.push_str(&self.ret_ty);
}
ts.push_str(";\n");
ts.push(';');
(js, ts, self.js_doc_comments())
}
}
14 changes: 8 additions & 6 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ impl<'a> Context<'a> {
",
name,
));
ts_dst.push_str("free(): void;\n");
ts_dst.push_str(" free(): void;");
dst.push_str(&class.contents);
ts_dst.push_str(&class.typescript);
dst.push_str("}\n");
Expand Down Expand Up @@ -2399,6 +2399,8 @@ impl<'a, 'b> SubContext<'a, 'b> {
.contents
.push_str(&format_doc_comments(&export.comments, Some(js_doc)));

class.typescript.push_str(" "); // Indentation

if export.is_constructor {
if class.has_constructor {
bail!("found duplicate constructor `{}`", export.function.name);
Expand Down Expand Up @@ -2571,12 +2573,12 @@ impl<'a, 'b> SubContext<'a, 'b> {
.typescript
.push_str(&format!("export enum {} {{", enum_.name));

variants.clear();
for variant in enum_.variants.iter() {
variants.push_str(&format!("{},", variant.name));
self.cx
.typescript
.push_str(&format!("\n {},", variant.name));
}
self.cx.typescript.push_str(&variants);
self.cx.typescript.push_str("}\n");
self.cx.typescript.push_str("\n}\n");
}

fn generate_struct(&mut self, struct_: &decode::Struct) -> Result<(), Error> {
Expand All @@ -2596,7 +2598,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
.argument(&descriptor)?
.ret(&Descriptor::Unit)?;
ts_dst.push_str(&format!(
"{}{}: {};\n",
"\n {}{}: {};",
if field.readonly { "readonly " } else { "" },
field.name,
&cx.js_arguments[0].1
Expand Down

0 comments on commit 2e80313

Please sign in to comment.