idl: Add `docs` field for constants (#2887)

This commit is contained in:
acheron 2024-04-03 21:59:43 +02:00 committed by GitHub
parent c138a55b72
commit 4de70aabee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 44 additions and 24 deletions

View File

@ -40,6 +40,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- ts: Add `prepend` option to MethodBuilder `preInstructions` method ([#2863](https://github.com/coral-xyz/anchor/pull/2863)).
- lang: Add `declare_program!` macro ([#2857](https://github.com/coral-xyz/anchor/pull/2857)).
- cli: Add `deactivate_feature` flag to `solana-test-validator` config in Anchor.toml ([#2872](https://github.com/coral-xyz/anchor/pull/2872)).
- idl: Add `docs` field for constants ([#2887](https://github.com/coral-xyz/anchor/pull/2887)).
### Fixes

View File

@ -14,6 +14,6 @@
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
"test": "anchor test --skip-lint && anchor clean"
}
}

View File

@ -14,6 +14,6 @@
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
"test": "anchor test --skip-lint && anchor clean"
}
}

View File

@ -14,6 +14,6 @@
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
"test": "anchor test --skip-lint && anchor clean"
}
}

View File

@ -14,6 +14,6 @@
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
"test": "anchor test --skip-lint && anchor clean"
}
}

View File

@ -14,6 +14,6 @@
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
"test": "anchor test --skip-lint && anchor clean"
}
}

View File

@ -1,19 +1,19 @@
{
"name": "basic-5",
"version": "0.29.0",
"license": "(MIT OR Apache-2.0)",
"homepage": "https://github.com/coral-xyz/anchor#readme",
"bugs": {
"url": "https://github.com/coral-xyz/anchor/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/coral-xyz/anchor.git"
},
"engines": {
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
}
}
"name": "basic-5",
"version": "0.29.0",
"license": "(MIT OR Apache-2.0)",
"homepage": "https://github.com/coral-xyz/anchor#readme",
"bugs": {
"url": "https://github.com/coral-xyz/anchor/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/coral-xyz/anchor.git"
},
"engines": {
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint && anchor clean"
}
}

View File

@ -137,6 +137,8 @@ pub struct IdlEvent {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlConst {
pub name: String,
#[serde(default, skip_serializing_if = "is_default")]
pub docs: Vec<String>,
#[serde(rename = "type")]
pub ty: IdlType,
pub value: String,

View File

@ -3,23 +3,31 @@ use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use super::{
common::{gen_print_section, get_idl_module_path},
common::{gen_print_section, get_idl_module_path, get_no_docs},
defined::gen_idl_type,
};
use crate::parser::docs;
pub fn gen_idl_print_fn_constant(item: &syn::ItemConst) -> TokenStream {
let idl = get_idl_module_path();
let no_docs = get_no_docs();
let name = item.ident.to_string();
let expr = &item.expr;
let fn_name = format_ident!("__anchor_private_print_idl_const_{}", name.to_snake_case());
let docs = match docs::parse(&item.attrs) {
Some(docs) if !no_docs => quote! { vec![#(#docs.into()),*] },
_ => quote! { vec![] },
};
let fn_body = match gen_idl_type(&item.ty, &[]) {
Ok((ty, _)) => gen_print_section(
"const",
quote! {
#idl::IdlConst {
name: #name.into(),
docs: #docs,
ty: #ty,
value: format!("{:?}", #expr),
}

View File

@ -4,6 +4,10 @@ use anchor_lang::prelude::*;
declare_id!("Docs111111111111111111111111111111111111111");
/// Documentation comment for constant
#[constant]
pub const MY_CONST: u8 = 42;
/// This is a doc comment for the program
#[program]
pub mod docs {

View File

@ -42,4 +42,9 @@ describe("Docs", () => {
"Account attribute doc comment should appear in the IDL",
]);
});
it("includes constant doc comment", () => {
const myConst = program.idl.constants.find((c) => c.name === "myConst")!;
assert.deepEqual(myConst.docs, ["Documentation comment for constant"]);
});
});