Skip to content

Commit

Permalink
Get type name without index math
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-shigueo committed Jul 24, 2024
1 parent 8a43254 commit bc10807
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
20 changes: 14 additions & 6 deletions ts-rs/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,25 @@ fn merge(original_contents: String, new_contents: String) -> String {

let new_decl = new_decl.trim_matches('\n');

let new_decl_start = new_decl.find(DECLARATION_START).unwrap() + DECLARATION_START.len();
let new_decl_end = new_decl_start + new_decl[new_decl_start..].find(' ').unwrap();
let new_decl_name = &new_decl[new_decl_start..new_decl_end];
let new_decl_name = new_decl
.split(DECLARATION_START)
.nth(1)
.unwrap()
.split_whitespace()
.next()
.unwrap();

let original_decls = original_decls.split("\n\n").map(|x| x.trim_matches('\n'));

let mut inserted = false;
for decl in original_decls {
let decl_start = decl.find(DECLARATION_START).unwrap() + DECLARATION_START.len();
let decl_end = decl_start + decl[decl_start..].find(' ').unwrap();
let decl_name = &decl[decl_start..decl_end];
let decl_name = decl
.split(DECLARATION_START)
.nth(1)
.unwrap()
.split_whitespace()
.next()
.unwrap();

if inserted || decl_name < new_decl_name {
buffer.push('\n');
Expand Down
25 changes: 3 additions & 22 deletions ts-rs/tests/integration/same_file_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,21 @@ struct DepB {
}

#[derive(TS)]
#[ts(export_to = "same_file_export/types.ts")]
#[ts(export, export_to = "same_file_export/types.ts")]
struct A {
foo: DepA,
}

#[derive(TS)]
#[ts(export_to = "same_file_export/types.ts")]
#[ts(export, export_to = "same_file_export/types.ts")]
struct B {
foo: DepB,
}

#[derive(TS)]
#[ts(export_to = "same_file_export/types.ts")]
#[ts(export, export_to = "same_file_export/types.ts")]
struct C {
foo: DepA,
bar: DepB,
biz: B,
}

#[test]
fn all_types_exported() {
A::export_all().unwrap();
B::export_all().unwrap();
C::export_all().unwrap();

let contents = std::fs::read_to_string(&A::default_output_path().unwrap()).unwrap();

if cfg!(feature = "format") {
assert!(contents.contains("export type A = { foo: DepA }"));
assert!(contents.contains("export type B = { foo: DepB }"));
assert!(contents.contains("export type C = { foo: DepA; bar: DepB; biz: B }"));
} else {
assert!(contents.contains(&A::decl()));
assert!(contents.contains(&B::decl()));
assert!(contents.contains(&C::decl()));
}
}

0 comments on commit bc10807

Please sign in to comment.