Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ambiguousname committed Dec 3, 2024
1 parent edcf1f5 commit 12ddcd0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
18 changes: 15 additions & 3 deletions tool/src/js/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> {
let type_name = self.formatter.fmt_type_name(opaque_id);

// Add to the import list:
self.add_import(type_name.clone().into(), None, super::gen::ImportUsage::Both);
self.add_import(
type_name.clone().into(),
None,
super::gen::ImportUsage::Both,
);

if self.tcx.resolve_type(opaque_id).attrs().disable {
self.errors
Expand All @@ -88,7 +92,11 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> {
let type_name = self.formatter.fmt_type_name(id);

// Add to the import list:
self.add_import(type_name.clone().into(), None, super::gen::ImportUsage::Both);
self.add_import(
type_name.clone().into(),
None,
super::gen::ImportUsage::Both,
);

if self.tcx.resolve_type(id).attrs().disable {
self.errors
Expand All @@ -101,7 +109,11 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> {
let type_name = self.formatter.fmt_type_name(enum_id);

// Add to the import list:
self.add_import(type_name.clone().into(), None, super::gen::ImportUsage::Both);
self.add_import(
type_name.clone().into(),
None,
super::gen::ImportUsage::Both,
);

if self.tcx.resolve_type(enum_id).attrs().disable {
self.errors
Expand Down
6 changes: 3 additions & 3 deletions tool/src/js/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<'tcx> JSFormatter<'tcx> {
type_name: &str,
typescript: bool,
relative_path: String,
file_name : &str
file_name: &str,
) -> String {
format!(
r#"{{ {type_name} }} from "{relative_path}{file_name}{}"#,
Expand All @@ -127,7 +127,7 @@ impl<'tcx> JSFormatter<'tcx> {
type_name: &str,
typescript: bool,
relative_path: String,
file_name : &str
file_name: &str,
) -> String {
format!(
r#"import {}{}""#,
Expand Down Expand Up @@ -159,7 +159,7 @@ impl<'tcx> JSFormatter<'tcx> {
type_name: &str,
typescript: bool,
relative_path: String,
file_name : &str
file_name: &str,
) -> String {
format!(
r#"export {}""#,
Expand Down
66 changes: 48 additions & 18 deletions tool/src/js/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ impl<'ctx, 'tcx> TyGenContext<'ctx, 'tcx> {

let mut new_imports = Vec::new();
for import in self.imports.borrow().iter() {
if import.usage == ImportUsage::Both || (import.usage == ImportUsage::Typescript && typescript) || (import.usage == ImportUsage::Module && !typescript) {
new_imports.push(
self.formatter
.fmt_import_statement(&import.import_type, typescript, "./".into(), &import.import_file),
);
if import.usage == ImportUsage::Both
|| (import.usage == ImportUsage::Typescript && typescript)
|| (import.usage == ImportUsage::Module && !typescript)
{
new_imports.push(self.formatter.fmt_import_statement(
&import.import_type,
typescript,
"./".into(),
&import.import_file,
));
}
}

Expand All @@ -68,21 +73,36 @@ impl<'ctx, 'tcx> TyGenContext<'ctx, 'tcx> {
/// A wrapper for `borrow_mut`ably inserting new imports.
///
/// I do this to avoid borrow checking madness.
pub(super) fn add_import(&self, import_str: Cow<'tcx, str>, import_file : Option<Cow<'tcx, str>>, usage: ImportUsage) {
pub(super) fn add_import(
&self,
import_str: Cow<'tcx, str>,
import_file: Option<Cow<'tcx, str>>,
usage: ImportUsage,
) {
self.imports.borrow_mut().insert(ImportInfo {
import_type: import_str.clone(),
import_file: import_file.unwrap_or(self.formatter.fmt_file_name_extensionless(&import_str).into()),usage
import_file: import_file.unwrap_or(
self.formatter
.fmt_file_name_extensionless(&import_str)
.into(),
),
usage,
});
}

/// Exists for the same reason as [`Self::add_import`].
///
/// Right now, only used for removing any self imports.
pub(super) fn remove_import(&self, import_str: Cow<'tcx, str>, import_file : Option<Cow<'tcx, str>>, usage: ImportUsage) {
self.imports.borrow_mut().remove(&ImportInfo{
pub(super) fn remove_import(
&self,
import_str: Cow<'tcx, str>,
import_file: Option<Cow<'tcx, str>>,
usage: ImportUsage,
) {
self.imports.borrow_mut().remove(&ImportInfo {
import_type: import_str,
import_file: import_file.unwrap_or_default(),
usage
usage,
});
}

Expand Down Expand Up @@ -394,16 +414,26 @@ impl<'ctx, 'tcx> TyGenContext<'ctx, 'tcx> {

for param in method.params.iter() {
let base_type = self.gen_js_type_str(&param.ty);
let param_type_str = format!("{base_type}{}",
let param_type_str = format!(
"{base_type}{}",
// If we're a struct, accept the StructType_Obj type as an input as well.
if let Type::Struct(..) = &param.ty {
let obj_ty : Cow<'tcx, str> = format!("{base_type}_Obj").into();
self.add_import(obj_ty.clone(), Some(self.formatter.fmt_file_name_extensionless(&base_type).into()), ImportUsage::Typescript);
let obj_ty: Cow<'tcx, str> = format!("{base_type}_Obj").into();
self.add_import(
obj_ty.clone(),
Some(
self.formatter
.fmt_file_name_extensionless(&base_type)
.into(),
),
ImportUsage::Typescript,
);
format!(" | {obj_ty}")
} else {
"".into()
}
).into();
)
.into();

let param_info = ParamInfo {
name: self.formatter.fmt_param_name(param.name.as_str()),
Expand Down Expand Up @@ -634,13 +664,13 @@ pub(super) enum ImportUsage {
/// .d.ts files only
Typescript,
/// Both .mjs and .d.ts
Both
Both,
}

pub(super) struct ImportInfo<'info> {
import_type : Cow<'info, str>,
import_file : Cow<'info, str>,
usage : ImportUsage
import_type: Cow<'info, str>,
import_file: Cow<'info, str>,
usage: ImportUsage,
}

/// Imports are only unique if they use a different type. We don't care about anything else.
Expand Down
6 changes: 5 additions & 1 deletion tool/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ pub(crate) fn run<'tcx>(
let file_name = formatter.fmt_file_name(&context.type_name, &file_type);

// Remove our self reference:
context.remove_import(context.type_name.clone().into(), None, gen::ImportUsage::Both);
context.remove_import(
context.type_name.clone().into(),
None,
gen::ImportUsage::Both,
);

files.add_file(file_name, context.generate_base(ts, contents));
}
Expand Down

0 comments on commit 12ddcd0

Please sign in to comment.