Use Context::export in write_classes

No need to doubly check for `nodejs`
This commit is contained in:
Alex Crichton 2018-04-03 13:25:10 -07:00
parent 23f6049619
commit 62e9f580d0

View File

@ -50,6 +50,8 @@ impl<'a> Context<'a> {
} else { } else {
if contents.starts_with("function") { if contents.starts_with("function") {
format!("export function {} {}\n", name, &contents[8..]) format!("export function {} {}\n", name, &contents[8..])
} else if contents.starts_with("class") {
format!("export {}\n", contents)
} else { } else {
format!("export const {} = {};\n", name, contents) format!("export const {} = {};\n", name, contents)
} }
@ -251,14 +253,8 @@ impl<'a> Context<'a> {
fn write_classes(&mut self) { fn write_classes(&mut self) {
let classes = mem::replace(&mut self.exported_classes, Default::default()); let classes = mem::replace(&mut self.exported_classes, Default::default());
for (class, exports) in classes { for (class, exports) in classes {
let mut dst = String::new(); let mut dst = format!("class {} {{\n", class);
let global_export = if self.config.nodejs { let mut ts_dst = format!("export {}", dst);
format!("module.exports.{} = class {} {{\n", class, class)
} else {
format!("export class {} {{", class)
};
dst.push_str(&global_export);
let mut ts_dst = dst.clone();
ts_dst.push_str(" ts_dst.push_str("
public ptr: number; public ptr: number;
"); ");
@ -312,7 +308,7 @@ impl<'a> Context<'a> {
dst.push_str("}\n"); dst.push_str("}\n");
ts_dst.push_str("}\n"); ts_dst.push_str("}\n");
self.globals.push_str(&dst); self.export(&class, &dst);
self.typescript.push_str(&ts_dst); self.typescript.push_str(&ts_dst);
} }
} }