Skip to content

Commit

Permalink
idl: Add docs field for constants (#2887)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Apr 3, 2024
1 parent c138a55 commit 4de70aa
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/basic-0/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
"test": "anchor test --skip-lint && anchor clean"
}
}
2 changes: 1 addition & 1 deletion examples/tutorial/basic-1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
"test": "anchor test --skip-lint && anchor clean"
}
}
2 changes: 1 addition & 1 deletion examples/tutorial/basic-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
"test": "anchor test --skip-lint && anchor clean"
}
}
2 changes: 1 addition & 1 deletion examples/tutorial/basic-3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
"test": "anchor test --skip-lint && anchor clean"
}
}
2 changes: 1 addition & 1 deletion examples/tutorial/basic-4/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
"test": "anchor test --skip-lint && anchor clean"
}
}
36 changes: 18 additions & 18 deletions examples/tutorial/basic-5/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
2 changes: 2 additions & 0 deletions idl/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion lang/syn/src/idl/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
4 changes: 4 additions & 0 deletions tests/idl/programs/docs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions tests/idl/tests/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
});
});

0 comments on commit 4de70aa

Please sign in to comment.