Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3334,6 +3334,7 @@ fn idl_ts(idl: &Idl) -> Result<String> {
let idl_name = &idl.metadata.name;
let type_name = idl_name.to_pascal_case();
let mut camel_idl = serde_json::to_value(idl)?;
normalize_string_const_values_for_ts(&mut camel_idl);
camel_case_idl_identifiers(&mut camel_idl);
let camel_idl = serde_json::to_string_pretty(&serde_json::from_value::<Idl>(camel_idl)?)?;

Expand All @@ -3349,6 +3350,37 @@ export type {type_name} = {camel_idl};
))
}

fn normalize_string_const_values_for_ts(idl: &mut JsonValue) {
let Some(constants) = idl.get_mut("constants").and_then(JsonValue::as_array_mut) else {
return;
};

for constant in constants {
let Some(constant) = constant.as_object_mut() else {
continue;
};

if !matches!(
constant.get("type").and_then(JsonValue::as_str),
Some("string")
) {
continue;
}

let Some(value) = constant.get_mut("value") else {
continue;
};

let Some(value_str) = value.as_str() else {
continue;
};

if let Ok(parsed_value) = serde_json::from_str::<String>(value_str) {
*value = JsonValue::String(parsed_value);
}
}
}

fn camel_case_idl_identifiers(value: &mut JsonValue) {
match value {
JsonValue::Array(values) => {
Expand Down Expand Up @@ -6788,12 +6820,20 @@ mod tests {
},
},
}],
constants: vec![anchor_lang_idl::types::IdlConst {
name: "seed_prefix".to_string(),
docs: Vec::new(),
ty: IdlType::String,
value: "SEED_PREFIX".to_string(),
}],
constants: vec![
anchor_lang_idl::types::IdlConst {
name: "seed_prefix".to_string(),
docs: Vec::new(),
ty: IdlType::String,
value: "SEED_PREFIX".to_string(),
},
anchor_lang_idl::types::IdlConst {
name: "literal_seed".to_string(),
docs: Vec::new(),
ty: IdlType::String,
value: "\"favor\"".to_string(),
},
],
};

let ts = idl_ts(&idl).unwrap();
Expand All @@ -6813,6 +6853,8 @@ mod tests {
assert!(ts.contains(r#""generic": "itemType""#));
assert!(ts.contains(r#""name": "seedPrefix""#));
assert!(ts.contains(r#""value": "SEED_PREFIX""#));
assert!(ts.contains(r#""name": "literalSeed""#));
assert!(ts.contains(r#""value": "favor""#));
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions tests/idl/idls/idl.json
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,11 @@
"type": "i128",
"value": "1000000"
},
{
"name": "SEED_PREFIX",
"type": "string",
"value": "\"favor\""
},
{
"name": "TEST_CONVERT_MODULE_PATHS",
"docs": [
Expand Down
3 changes: 3 additions & 0 deletions tests/idl/programs/idl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub const BYTE_STR: u8 = b't';
#[constant]
pub const BYTES_STR: &[u8] = b"test";

#[constant]
pub const SEED_PREFIX: &str = "favor";

pub const NO_IDL: u16 = 55;

#[program]
Expand Down
6 changes: 6 additions & 0 deletions tests/idl/tests/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,12 @@ describe("IDL", () => {
c.type === "bytes" &&
c.value === "[116, 101, 115, 116]"
);
checkDefined(
(c) =>
c.name === "seedPrefix" &&
c.type === "string" &&
c.value === '"favor"'
);
});

it("Does not include constants that are not marked with `#[constant]`", () => {
Expand Down
Loading