Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix define-trait format w/ test #1691

Merged
merged 2 commits into from
Feb 24, 2025
Merged
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
109 changes: 49 additions & 60 deletions components/clarinet-format/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ impl<'a> Aggregator<'a> {
| DefineFunctions::PrivateFunction => self.function(list),
DefineFunctions::Constant
| DefineFunctions::PersistedVariable
| DefineFunctions::NonFungibleToken => self.constant(list),
| DefineFunctions::FungibleToken
| DefineFunctions::NonFungibleToken => {
self.constant(list, previous_indentation)
}
DefineFunctions::Map => self.format_map(list, previous_indentation),
DefineFunctions::UseTrait | DefineFunctions::ImplTrait => {
// these are the same as the following but need a trailing newline
Expand All @@ -288,9 +291,6 @@ impl<'a> Aggregator<'a> {
self.format_source_exprs(list, previous_indentation)
)
}
DefineFunctions::FungibleToken => {
self.fungible_token(list, previous_indentation)
}
DefineFunctions::Trait => self.define_trait(list, previous_indentation),
}
} else {
Expand Down Expand Up @@ -319,27 +319,38 @@ impl<'a> Aggregator<'a> {
fn define_trait(&self, exprs: &[PreSymbolicExpression], previous_indentation: &str) -> String {
let mut acc = "(define-trait ".to_string();
let indentation = &self.settings.indentation.to_string();
let space = format!("{}{}", indentation, previous_indentation);
acc.push_str(&self.format_source_exprs(&[exprs[1].clone()], previous_indentation));
let mut iter = exprs[2..].iter().peekable();
acc.push('\n');
acc.push_str(&space);
acc.push('(');
let mut iter = exprs[2].match_list().unwrap().iter().peekable();
while let Some(expr) = iter.next() {
let trailing = get_trailing_comment(expr, &mut iter);
acc.push('\n');
acc.push_str(indentation);
acc.push_str(&self.format_source_exprs(&[expr.clone()], indentation));
if let Some(comment) = trailing {
acc.push(' ');
acc.push_str(&self.display_pse(comment, previous_indentation));
if let Some(list) = expr.match_list() {
let trailing = get_trailing_comment(expr, &mut iter);
acc.push('\n');
acc.push_str(&space);
acc.push_str(indentation);
acc.push_str(&self.format_list(list, indentation));

if let Some(comment) = trailing {
acc.push(' ');
acc.push_str(&self.display_pse(comment, previous_indentation));
}
}
}
acc.push('\n');
acc.push_str(&space);
acc.push(')');
acc.push('\n');
acc.push_str(previous_indentation);
acc.push(')');
acc
}

fn fungible_token(
&self,
exprs: &[PreSymbolicExpression],
previous_indentation: &str,
) -> String {
let mut acc = "(define-fungible-token ".to_string();
fn constant(&self, exprs: &[PreSymbolicExpression], previous_indentation: &str) -> String {
let func_type = self.display_pse(exprs.first().unwrap(), "");
let mut acc = format!("({func_type} ");
let mut iter = exprs[1..].iter().peekable();
while let Some(expr) = iter.next() {
let trailing = get_trailing_comment(expr, &mut iter);
Expand All @@ -353,39 +364,9 @@ impl<'a> Aggregator<'a> {
}
}
acc.push(')');
acc.push('\n');
acc
}
fn constant(&self, exprs: &[PreSymbolicExpression]) -> String {
let func_type = self.display_pse(exprs.first().unwrap(), "");
let indentation = &self.settings.indentation.to_string();
let mut acc = format!("({func_type} ");

if let Some((name, args)) = name_and_args(exprs) {
acc.push_str(&self.display_pse(name, ""));

// Access the value from args
if let Some(value) = args.first() {
if let Some(list) = value.match_list() {
acc.push_str(&format!(
"\n{}({})",
indentation,
self.format_source_exprs(list, "")
));
acc.push_str("\n)");
} else {
// Handle non-list values (e.g., literals or simple expressions)
acc.push(' ');
acc.push_str(&self.display_pse(value, ""));
acc.push(')');
}
}

acc.push('\n');
acc
} else {
panic!("Expected a valid constant definition with (name value)")
}
}
fn format_map(&self, exprs: &[PreSymbolicExpression], previous_indentation: &str) -> String {
let mut acc = "(define-map ".to_string();
let indentation = &self.settings.indentation.to_string();
Expand Down Expand Up @@ -707,7 +688,13 @@ impl<'a> Aggregator<'a> {
fn display_pse(&self, pse: &PreSymbolicExpression, previous_indentation: &str) -> String {
match pse.pre_expr {
PreSymbolicExpressionType::Atom(ref value) => t(value.as_str()).to_string(),
PreSymbolicExpressionType::AtomValue(ref value) => value.to_string(),
PreSymbolicExpressionType::AtomValue(ref value) => match value {
clarity::vm::types::Value::Principal(c) => {
format!("'{}", c.to_string())
}
// Fill in these explicitly
_ => value.to_string(),
},
PreSymbolicExpressionType::List(ref items) => {
self.format_list(items, previous_indentation)
}
Expand Down Expand Up @@ -936,11 +923,11 @@ mod tests_formatter {

#[test]
fn test_fungible_token() {
let src = "(define-fungible-token hello)";
let src = "(define-fungible-token hello)\n";
let result = format_with_default(&String::from(src));
assert_eq!(result, src);

let src = "(define-fungible-token hello u100)";
let src = "(define-fungible-token hello u100)\n";
let result = format_with_default(&String::from(src));
assert_eq!(result, src);
}
Expand Down Expand Up @@ -1205,12 +1192,9 @@ mod tests_formatter {
}
#[test]
fn test_constant() {
let src = "(define-constant something 1)\n";
let src = "(define-constant minter 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.minter)\n";
let result = format_with_default(&String::from(src));
assert_eq!(result, "(define-constant something 1)\n");
let src2 = "(define-constant something (1 2))\n";
let result2 = format_with_default(&String::from(src2));
assert_eq!(result2, "(define-constant something\n (1 2)\n)\n");
assert_eq!(result, src);
}

#[test]
Expand Down Expand Up @@ -1317,11 +1301,16 @@ mod tests_formatter {
}

#[test]
#[ignore]
fn define_data_var_test() {
let src = "(define-data-var my-data-var principal tx-sender)\n";
let result = format_with_default(src);
assert_eq!(src, result);
}
#[test]
fn define_trait_test() {
// TODO: Not sure how this should be formatted
let src = r#"(define-trait token-trait
((transfer? (principal principal uint) (response uint uint))
(
(transfer? (principal principal uint) (response uint uint)) ;; comment
(get-balance (principal) (response uint uint))
)
)"#;
Expand Down
136 changes: 35 additions & 101 deletions components/clarinet-format/tests/golden-intended/BNS-V2.clar
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
);; Only authorized caller to flip the switch and update URI
(define-constant DEPLOYER tx-sender)
;; (new) Var to store the token URI, allowing for metadata association with the NFT
(define-data-var token-uri
(string-ascii 256)
)
(define-data-var token-uri (string-ascii 256) "ipfs://QmUQY1aZ799SPRaNBFqeCvvmZ4fTQfZvWHauRvHAukyQDB")
(define-public (update-token-uri
(new-token-uri (string-ascii 256))
)
Expand All @@ -41,9 +39,7 @@
))
)

(define-data-var contract-uri
(string-ascii 256)
)
(define-data-var contract-uri (string-ascii 256) "ipfs://QmWKTZEMQNWngp23i7bgPzkineYC9LDvcxYkwNyVQVoH8y")
(define-public (update-contract-uri
(new-contract-uri (string-ascii 256))
)
Expand All @@ -55,104 +51,42 @@
)

;; errors
(define-constant ERR-UNWRAP
(err u101)
)
(define-constant ERR-NOT-AUTHORIZED
(err u102)
)
(define-constant ERR-NOT-LISTED
(err u103)
)
(define-constant ERR-WRONG-COMMISSION
(err u104)
)
(define-constant ERR-LISTED
(err u105)
)
(define-constant ERR-NO-NAME
(err u106)
)
(define-constant ERR-HASH-MALFORMED
(err u107)
)
(define-constant ERR-STX-BURNT-INSUFFICIENT
(err u108)
)
(define-constant ERR-PREORDER-NOT-FOUND
(err u109)
)
(define-constant ERR-CHARSET-INVALID
(err u110)
)
(define-constant ERR-NAMESPACE-ALREADY-EXISTS
(err u111)
)
(define-constant ERR-PREORDER-CLAIMABILITY-EXPIRED
(err u112)
)
(define-constant ERR-NAMESPACE-NOT-FOUND
(err u113)
)
(define-constant ERR-OPERATION-UNAUTHORIZED
(err u114)
)
(define-constant ERR-NAMESPACE-ALREADY-LAUNCHED
(err u115)
)
(define-constant ERR-NAMESPACE-PREORDER-LAUNCHABILITY-EXPIRED
(err u116)
)
(define-constant ERR-NAMESPACE-NOT-LAUNCHED
(err u117)
)
(define-constant ERR-NAME-NOT-AVAILABLE
(err u118)
)
(define-constant ERR-NAMESPACE-BLANK
(err u119)
)
(define-constant ERR-NAME-BLANK
(err u120)
)
(define-constant ERR-NAME-PREORDERED-BEFORE-NAMESPACE-LAUNCH
(err u121)
)
(define-constant ERR-NAMESPACE-HAS-MANAGER
(err u122)
)
(define-constant ERR-OVERFLOW
(err u123)
)
(define-constant ERR-NO-NAMESPACE-MANAGER
(err u124)
)
(define-constant ERR-FAST-MINTED-BEFORE
(err u125)
)
(define-constant ERR-PREORDERED-BEFORE
(err u126)
)
(define-constant ERR-NAME-NOT-CLAIMABLE-YET
(err u127)
)
(define-constant ERR-IMPORTED-BEFORE
(err u128)
)
(define-constant ERR-LIFETIME-EQUAL-0
(err u129)
)
(define-constant ERR-MIGRATION-IN-PROGRESS
(err u130)
)
(define-constant ERR-NO-PRIMARY-NAME
(err u131)
)
(define-constant ERR-UNWRAP (err u101))
(define-constant ERR-NOT-AUTHORIZED (err u102))
(define-constant ERR-NOT-LISTED (err u103))
(define-constant ERR-WRONG-COMMISSION (err u104))
(define-constant ERR-LISTED (err u105))
(define-constant ERR-NO-NAME (err u106))
(define-constant ERR-HASH-MALFORMED (err u107))
(define-constant ERR-STX-BURNT-INSUFFICIENT (err u108))
(define-constant ERR-PREORDER-NOT-FOUND (err u109))
(define-constant ERR-CHARSET-INVALID (err u110))
(define-constant ERR-NAMESPACE-ALREADY-EXISTS (err u111))
(define-constant ERR-PREORDER-CLAIMABILITY-EXPIRED (err u112))
(define-constant ERR-NAMESPACE-NOT-FOUND (err u113))
(define-constant ERR-OPERATION-UNAUTHORIZED (err u114))
(define-constant ERR-NAMESPACE-ALREADY-LAUNCHED (err u115))
(define-constant ERR-NAMESPACE-PREORDER-LAUNCHABILITY-EXPIRED (err u116))
(define-constant ERR-NAMESPACE-NOT-LAUNCHED (err u117))
(define-constant ERR-NAME-NOT-AVAILABLE (err u118))
(define-constant ERR-NAMESPACE-BLANK (err u119))
(define-constant ERR-NAME-BLANK (err u120))
(define-constant ERR-NAME-PREORDERED-BEFORE-NAMESPACE-LAUNCH (err u121))
(define-constant ERR-NAMESPACE-HAS-MANAGER (err u122))
(define-constant ERR-OVERFLOW (err u123))
(define-constant ERR-NO-NAMESPACE-MANAGER (err u124))
(define-constant ERR-FAST-MINTED-BEFORE (err u125))
(define-constant ERR-PREORDERED-BEFORE (err u126))
(define-constant ERR-NAME-NOT-CLAIMABLE-YET (err u127))
(define-constant ERR-IMPORTED-BEFORE (err u128))
(define-constant ERR-LIFETIME-EQUAL-0 (err u129))
(define-constant ERR-MIGRATION-IN-PROGRESS (err u130))
(define-constant ERR-NO-PRIMARY-NAME (err u131))
;; variables
;; (new) Variable to see if migration is complete
(define-data-var migration-complete bool)
(define-data-var migration-complete bool false)
;; (new) Counter to keep track of the last minted NFT ID, ensuring unique identifiers
(define-data-var bns-index uint)
(define-data-var bns-index uint u0)
;; maps
;; (new) Map to track market listings, associating NFT IDs with price and commission details
(define-map market
Expand Down
Loading