Skip to content

Commit f785112

Browse files
idl: Add docs field for constants (otter-sec#2887)
1 parent d81b323 commit f785112

11 files changed

Lines changed: 44 additions & 24 deletions

File tree

CHANGELOG.md

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

4445
### Fixes
4546

examples/tutorial/basic-0/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"node": ">=11"
1515
},
1616
"scripts": {
17-
"test": "anchor test --skip-lint"
17+
"test": "anchor test --skip-lint && anchor clean"
1818
}
1919
}

examples/tutorial/basic-1/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"node": ">=11"
1515
},
1616
"scripts": {
17-
"test": "anchor test --skip-lint"
17+
"test": "anchor test --skip-lint && anchor clean"
1818
}
1919
}

examples/tutorial/basic-2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"node": ">=11"
1515
},
1616
"scripts": {
17-
"test": "anchor test --skip-lint"
17+
"test": "anchor test --skip-lint && anchor clean"
1818
}
1919
}

examples/tutorial/basic-3/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"node": ">=11"
1515
},
1616
"scripts": {
17-
"test": "anchor test --skip-lint"
17+
"test": "anchor test --skip-lint && anchor clean"
1818
}
1919
}

examples/tutorial/basic-4/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"node": ">=11"
1515
},
1616
"scripts": {
17-
"test": "anchor test --skip-lint"
17+
"test": "anchor test --skip-lint && anchor clean"
1818
}
1919
}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
2-
"name": "basic-5",
3-
"version": "0.29.0",
4-
"license": "(MIT OR Apache-2.0)",
5-
"homepage": "https://github.com/coral-xyz/anchor#readme",
6-
"bugs": {
7-
"url": "https://github.com/coral-xyz/anchor/issues"
8-
},
9-
"repository": {
10-
"type": "git",
11-
"url": "https://github.com/coral-xyz/anchor.git"
12-
},
13-
"engines": {
14-
"node": ">=11"
15-
},
16-
"scripts": {
17-
"test": "anchor test --skip-lint"
18-
}
19-
}
2+
"name": "basic-5",
3+
"version": "0.29.0",
4+
"license": "(MIT OR Apache-2.0)",
5+
"homepage": "https://github.com/coral-xyz/anchor#readme",
6+
"bugs": {
7+
"url": "https://github.com/coral-xyz/anchor/issues"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/coral-xyz/anchor.git"
12+
},
13+
"engines": {
14+
"node": ">=11"
15+
},
16+
"scripts": {
17+
"test": "anchor test --skip-lint && anchor clean"
18+
}
19+
}

idl/src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ pub struct IdlEvent {
137137
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
138138
pub struct IdlConst {
139139
pub name: String,
140+
#[serde(default, skip_serializing_if = "is_default")]
141+
pub docs: Vec<String>,
140142
#[serde(rename = "type")]
141143
pub ty: IdlType,
142144
pub value: String,

lang/syn/src/idl/constant.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,31 @@ use proc_macro2::TokenStream;
33
use quote::{format_ident, quote};
44

55
use super::{
6-
common::{gen_print_section, get_idl_module_path},
6+
common::{gen_print_section, get_idl_module_path, get_no_docs},
77
defined::gen_idl_type,
88
};
9+
use crate::parser::docs;
910

1011
pub fn gen_idl_print_fn_constant(item: &syn::ItemConst) -> TokenStream {
1112
let idl = get_idl_module_path();
13+
let no_docs = get_no_docs();
1214

1315
let name = item.ident.to_string();
1416
let expr = &item.expr;
1517
let fn_name = format_ident!("__anchor_private_print_idl_const_{}", name.to_snake_case());
1618

19+
let docs = match docs::parse(&item.attrs) {
20+
Some(docs) if !no_docs => quote! { vec![#(#docs.into()),*] },
21+
_ => quote! { vec![] },
22+
};
23+
1724
let fn_body = match gen_idl_type(&item.ty, &[]) {
1825
Ok((ty, _)) => gen_print_section(
1926
"const",
2027
quote! {
2128
#idl::IdlConst {
2229
name: #name.into(),
30+
docs: #docs,
2331
ty: #ty,
2432
value: format!("{:?}", #expr),
2533
}

tests/idl/programs/docs/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use anchor_lang::prelude::*;
44

55
declare_id!("Docs111111111111111111111111111111111111111");
66

7+
/// Documentation comment for constant
8+
#[constant]
9+
pub const MY_CONST: u8 = 42;
10+
711
/// This is a doc comment for the program
812
#[program]
913
pub mod docs {

0 commit comments

Comments
 (0)