Skip to content

Function Attributes #4394

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

Merged
merged 50 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
26c1dab
Update README.md
rouzwelt Dec 27, 2024
55a74d6
init
rouzwelt Jan 6, 2025
fac43b5
rm
rouzwelt Jan 6, 2025
de4e449
fix test
rouzwelt Jan 6, 2025
11c2571
fix schema hash
rouzwelt Jan 6, 2025
c43561e
update shared schema version
rouzwelt Jan 6, 2025
003312f
Update schema_hash_approval.rs
rouzwelt Jan 6, 2025
e111551
update
rouzwelt Jan 6, 2025
59faf69
docs
rouzwelt Jan 6, 2025
c115222
docs
rouzwelt Jan 6, 2025
13acbe4
docs
rouzwelt Jan 6, 2025
73d39b6
fix debug impl
rouzwelt Jan 6, 2025
6734413
Update run.sh
rouzwelt Jan 6, 2025
e0b04f9
update
rouzwelt Jan 7, 2025
6e7e1d2
fix ts doc generator fn
rouzwelt Jan 7, 2025
f24b605
update
rouzwelt Jan 7, 2025
f3a6ba0
fold fn attrs inside original props
rouzwelt Jan 8, 2025
c25c5dd
Update parser.rs
rouzwelt Jan 8, 2025
e46deda
update test
rouzwelt Jan 8, 2025
9507ed8
update test
rouzwelt Jan 8, 2025
d861076
Update parser.rs
rouzwelt Jan 8, 2025
935286e
Update function-attributes.md
rouzwelt Jan 8, 2025
19bfb47
Update ast.rs
rouzwelt Jan 8, 2025
5707447
fix docs
rouzwelt Jan 8, 2025
375570d
impl requested changes
rouzwelt Jan 9, 2025
bd11aab
fix
rouzwelt Jan 9, 2025
9d167f0
fmt
rouzwelt Jan 9, 2025
0a598c4
typo
rouzwelt Jan 9, 2025
2ea857e
fmt
rouzwelt Jan 9, 2025
227a60b
fix test
rouzwelt Jan 9, 2025
185f21b
typo
rouzwelt Jan 9, 2025
e7f2618
Add ability to specific attributes that error when unused
daxpedda Jan 9, 2025
0992426
Respect MSRV
daxpedda Jan 9, 2025
2e8e6c1
apply minor requested changes
rouzwelt Jan 9, 2025
76baf36
update
rouzwelt Jan 9, 2025
14dc57a
typo
rouzwelt Jan 9, 2025
225acf5
minor fix
rouzwelt Jan 9, 2025
44f8a55
add test for no fn attr on 'self' argument
rouzwelt Jan 9, 2025
1aec830
use fold()
rouzwelt Jan 9, 2025
0cbe5a8
better fold()
rouzwelt Jan 10, 2025
c3468e9
even better fold()
rouzwelt Jan 11, 2025
f5f779c
Reduce new allocations to a minimum
daxpedda Jan 11, 2025
e7cb0be
Adjust some wording
daxpedda Jan 11, 2025
9f06652
minor fix and changelog
rouzwelt Jan 12, 2025
bae360a
fix error msg
rouzwelt Jan 12, 2025
89e500e
fix ui test
rouzwelt Jan 12, 2025
129c471
Update parser.rs
rouzwelt Jan 12, 2025
83d7d7f
Update CHANGELOG.md
rouzwelt Jan 12, 2025
6c5e576
Adjust changelog and documentation
daxpedda Jan 12, 2025
ff4064e
Replace macros with functions
daxpedda Jan 12, 2025
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
26 changes: 26 additions & 0 deletions crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,32 @@ pub struct Function {
pub generate_jsdoc: bool,
/// Whether this is a function with a variadict parameter
pub variadic: bool,
/// Function attributes used to provide extra information about function's components
pub fn_attrs: Option<FunctionAttributes>,
}

/// Extra information about a function's components
#[cfg_attr(feature = "extra-traits", derive(Debug))]
#[derive(Clone, Default)]
pub struct FunctionAttributes {
/// Function's return attributes
pub ret: FunctionComponentAttributes,
/// Function's arguments attributes
pub args: Vec<FunctionComponentAttributes>,
}

/// Information about a function's component
#[cfg_attr(feature = "extra-traits", derive(Debug))]
#[derive(Clone, Default)]
pub struct FunctionComponentAttributes {
/// Specifies the type for a function component
pub ty: Option<String>,
/// Description of the function component
pub desc: Option<String>,
/// Specifies a name of the function argument
pub name: Option<String>,
/// Specifies if the component is optional (used for function arguments)
pub optional: bool,
}

/// Information about a Struct being exported
Expand Down
25 changes: 25 additions & 0 deletions crates/backend/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,44 @@ fn shared_function<'a>(func: &'a ast::Function, _intern: &'a Interner) -> Functi
.iter()
.enumerate()
.map(|(idx, arg)| {
// use argument's "js_name" if it was provided in attributes
if let Some(arg_js_name) = func
.fn_attrs
.as_ref()
.and_then(|attrs| attrs.args.get(idx).and_then(|v| v.name.clone()))
{
return arg_js_name;
}
if let syn::Pat::Ident(x) = &*arg.pat {
return x.ident.unraw().to_string();
}
format!("arg{}", idx)
})
.collect::<Vec<_>>();
let fn_attrs = func.fn_attrs.as_ref().map(|attrs| FunctionAttributes {
ret: FunctionComponentAttributes {
ty: attrs.ret.ty.clone(),
desc: attrs.ret.desc.clone(),
optional: false,
},
args: attrs
.args
.iter()
.map(|arg_attr| FunctionComponentAttributes {
ty: arg_attr.ty.clone(),
desc: arg_attr.desc.clone(),
optional: arg_attr.optional,
})
.collect::<Vec<_>>(),
});
Function {
arg_names,
asyncness: func.r#async,
name: &func.name,
generate_typescript: func.generate_typescript,
generate_jsdoc: func.generate_jsdoc,
variadic: func.variadic,
fn_attrs,
}
}

Expand Down
20 changes: 19 additions & 1 deletion crates/cli-support/src/decode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ops::Deref, str};
use std::{fmt::Debug, ops::Deref, str};

pub trait Decode<'src>: Sized {
fn decode(data: &mut &'src [u8]) -> Self;
Expand Down Expand Up @@ -94,6 +94,24 @@ impl<'src, T: Decode<'src>> Decode<'src> for Option<T> {
}
}

impl Debug for FunctionAttributes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"FunctionAttributes {{ ret: {:?}, args: {:?} }}",
self.ret, self.args
))
}
}

impl Debug for FunctionComponentAttributes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"FunctionComponentAttributes {{ ty: {:?}, desc: {:?}, optional: {} }}",
self.ty, self.desc, self.optional
))
}
}

macro_rules! decode_struct {
($name:ident ($($lt:tt)*) $($field:ident: $ty:ty,)*) => {
pub struct $name <$($lt)*> {
Expand Down
Loading
Loading