@@ -4,7 +4,7 @@ use std::vec;
44
55use clarity:: vm:: {
66 representations:: { PreSymbolicExpression , PreSymbolicExpressionType , Span } ,
7- types:: { SequenceSubtype , StringSubtype , TypeSignature as ClarityTypeSignature } ,
7+ types:: { PrincipalData , SequenceSubtype , StringSubtype , TypeSignature as ClarityTypeSignature } ,
88 ClarityName , Value as ClarityValue ,
99} ;
1010use oxc_allocator:: Allocator ;
@@ -23,6 +23,9 @@ fn type_signature_to_pse(
2323 ClarityTypeSignature :: UIntType => PreSymbolicExpression :: atom ( ClarityName :: from ( "uint" ) ) ,
2424 ClarityTypeSignature :: IntType => PreSymbolicExpression :: atom ( ClarityName :: from ( "int" ) ) ,
2525 ClarityTypeSignature :: BoolType => PreSymbolicExpression :: atom ( ClarityName :: from ( "bool" ) ) ,
26+ ClarityTypeSignature :: PrincipalType => {
27+ PreSymbolicExpression :: atom ( ClarityName :: from ( "principal" ) )
28+ }
2629 ClarityTypeSignature :: SequenceType ( seq_subtype) => match seq_subtype {
2730 SequenceSubtype :: StringType ( string_subtype) => match string_subtype {
2831 StringSubtype :: ASCII ( len) => PreSymbolicExpression :: list ( vec ! [
@@ -80,6 +83,22 @@ fn convert_expression_with_type(
8083 }
8184 _ => return Err ( anyhow:: anyhow!( "Invalid expression for Bool" ) ) ,
8285 } ,
86+ ClarityTypeSignature :: PrincipalType => match & expr {
87+ OxcExpression :: StringLiteral ( str) => PreSymbolicExpression :: atom_value (
88+ ClarityValue :: Principal ( PrincipalData :: parse ( str. value . as_str ( ) ) ?) ,
89+ ) ,
90+ OxcExpression :: Identifier ( ident) => {
91+ if ident. name == "txSender" {
92+ PreSymbolicExpression :: atom ( ClarityName :: from ( "tx-sender" ) )
93+ } else {
94+ return Err ( anyhow:: anyhow!(
95+ "Invalid identifier for Principal: {}" ,
96+ ident. name
97+ ) ) ;
98+ }
99+ }
100+ _ => return Err ( anyhow:: anyhow!( "Invalid expression for Principal" ) ) ,
101+ } ,
83102 ClarityTypeSignature :: SequenceType ( SequenceSubtype :: StringType ( StringSubtype :: ASCII (
84103 _len,
85104 ) ) ) => match & expr {
@@ -124,7 +143,7 @@ fn convert_expression_with_type(
124143
125144 _ => return Err ( anyhow:: anyhow!( "Unsupported expression for Tuple" ) ) ,
126145 } ,
127- _ => return Err ( anyhow:: anyhow!( "Unsupported type for constant " ) ) ,
146+ _ => return Err ( anyhow:: anyhow!( "Unsupported type for variable " ) ) ,
128147 } )
129148}
130149
@@ -167,7 +186,7 @@ fn convert_function(
167186 . parameters
168187 . iter ( )
169188 . map ( |( name, r#type) | {
170- let name = PreSymbolicExpression :: atom ( ClarityName :: from ( name. as_str ( ) ) ) ;
189+ let name = PreSymbolicExpression :: atom ( ClarityName :: from ( to_kebab_case ( name) . as_str ( ) ) ) ;
171190 let r#type = type_signature_to_pse ( r#type) ?;
172191 Ok ( vec ! [ name, r#type] )
173192 } )
@@ -331,6 +350,19 @@ mod test {
331350 // The following tests use the assert_pses_eq function to dynamically
332351 // build the expected PSEs from the Clarity source code.
333352
353+ #[ test]
354+ fn test_principal_data_var ( ) {
355+ let ts_src =
356+ "const owner = new DataVar<Principal>(\" ST3PF13W7Z0RRM42A8VZRVFQ75SV1K26RXEP8YGKJ\" );" ;
357+ assert_pses_eq (
358+ ts_src,
359+ r#"(define-data-var owner principal 'ST3PF13W7Z0RRM42A8VZRVFQ75SV1K26RXEP8YGKJ)"# ,
360+ ) ;
361+
362+ let ts_src = "const owner = new DataVar<Principal>(txSender);" ;
363+ assert_pses_eq ( ts_src, r#"(define-data-var owner principal tx-sender)"# ) ;
364+ }
365+
334366 #[ test]
335367 fn test_convert_tuple_type ( ) {
336368 let ts_src = "const state = new DataVar<{ ok: Int }>({ ok: 1 });" ;
@@ -416,4 +448,19 @@ mod test {
416448 } ;
417449 assert_pses_eq ( ts_src, r#"(define-public (myfunc) (ok true))"# ) ;
418450 }
451+
452+ #[ test]
453+ fn test_function_arg_casing ( ) {
454+ let ts_src = indoc ! {
455+ r#"const addr = new DataVar<Principal>(txSender);
456+ function updateAddr(newAddr: Principal) { return ok(addr.set(newAddr)); }"#
457+ } ;
458+ assert_pses_eq (
459+ ts_src,
460+ indoc ! {
461+ r#"(define-data-var addr principal tx-sender)
462+ (define-private (update-addr (new-addr principal)) (ok (var-set addr new-addr)))"#
463+ } ,
464+ ) ;
465+ }
419466}
0 commit comments