Skip to content

Commit 660f308

Browse files
authored
chore(macro)!: change rename defaults to match psr
BREAKING CHANGE: Methods and Properties are renamed to camelCase by default. Classes to PascalCase, constants to UPPER_CASE and functions to snake_case Refs: #189, #436
1 parent aebb43a commit 660f308

File tree

9 files changed

+187
-78
lines changed

9 files changed

+187
-78
lines changed

crates/macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ proc-macro = true
1414
[dependencies]
1515
syn = { version = "2.0.100", features = ["full", "extra-traits", "printing"] }
1616
darling = "0.20"
17-
ident_case = "1.0.1"
1817
quote = "1.0.9"
1918
proc-macro2 = "1.0.26"
2019
lazy_static = "1.4.0"
2120
anyhow = "1.0"
21+
convert_case = "0.8.0"
2222

2323
[lints.rust]
2424
missing_docs = "warn"

crates/macros/src/class.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use quote::quote;
55
use syn::{Attribute, Expr, Fields, ItemStruct};
66

77
use crate::helpers::get_docs;
8-
use crate::parsing::PhpRename;
8+
use crate::parsing::{PhpRename, RenameRule};
99
use crate::prelude::*;
1010

1111
#[derive(FromAttributes, Debug, Default)]
@@ -35,7 +35,7 @@ pub struct ClassEntryAttribute {
3535
pub fn parser(mut input: ItemStruct) -> Result<TokenStream> {
3636
let attr = StructAttributes::from_attributes(&input.attrs)?;
3737
let ident = &input.ident;
38-
let name = attr.rename.rename(ident.to_string());
38+
let name = attr.rename.rename(ident.to_string(), RenameRule::Pascal);
3939
let docs = get_docs(&attr.attrs)?;
4040
input.attrs.retain(|attr| !attr.path().is_ident("php"));
4141

@@ -107,7 +107,9 @@ struct Property<'a> {
107107

108108
impl Property<'_> {
109109
pub fn name(&self) -> String {
110-
self.attr.rename.rename(self.ident.to_string())
110+
self.attr
111+
.rename
112+
.rename(self.ident.to_string(), RenameRule::Camel)
111113
}
112114
}
113115

crates/macros/src/constant.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use quote::{format_ident, quote};
44
use syn::ItemConst;
55

66
use crate::helpers::get_docs;
7-
use crate::parsing::PhpRename;
7+
use crate::parsing::{PhpRename, RenameRule};
88
use crate::prelude::*;
99

1010
const INTERNAL_CONST_DOC_PREFIX: &str = "_internal_const_docs_";
@@ -23,7 +23,9 @@ pub(crate) struct PhpConstAttribute {
2323
pub fn parser(mut item: ItemConst) -> Result<TokenStream> {
2424
let attr = PhpConstAttribute::from_attributes(&item.attrs)?;
2525

26-
let name = attr.rename.rename(item.ident.to_string());
26+
let name = attr
27+
.rename
28+
.rename(item.ident.to_string(), RenameRule::ScreamingSnake);
2729
let name_ident = format_ident!("{INTERNAL_CONST_NAME_PREFIX}{}", item.ident);
2830

2931
let docs = get_docs(&attr.attrs)?;

crates/macros/src/function.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use syn::spanned::Spanned as _;
77
use syn::{Expr, FnArg, GenericArgument, ItemFn, PatType, PathArguments, Type, TypePath};
88

99
use crate::helpers::get_docs;
10-
use crate::parsing::{PhpRename, Visibility};
10+
use crate::parsing::{PhpRename, RenameRule, Visibility};
1111
use crate::prelude::*;
1212
use crate::syn_ext::DropLifetimes;
1313

@@ -46,7 +46,9 @@ pub fn parser(mut input: ItemFn) -> Result<TokenStream> {
4646

4747
let func = Function::new(
4848
&input.sig,
49-
php_attr.rename.rename(input.sig.ident.to_string()),
49+
php_attr
50+
.rename
51+
.rename(input.sig.ident.to_string(), RenameRule::Snake),
5052
args,
5153
php_attr.optional,
5254
docs,

crates/macros/src/impl_.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use syn::{Expr, Ident, ItemImpl};
88
use crate::constant::PhpConstAttribute;
99
use crate::function::{Args, CallType, Function, MethodReceiver};
1010
use crate::helpers::get_docs;
11-
use crate::parsing::{MethodRename, PhpRename, Rename, RenameRule, Visibility};
11+
use crate::parsing::{PhpRename, RenameRule, Visibility};
1212
use crate::prelude::*;
1313

1414
/// Method types.
@@ -48,8 +48,7 @@ pub fn parser(mut input: ItemImpl) -> Result<TokenStream> {
4848
let mut parsed = ParsedImpl::new(
4949
path,
5050
args.rename_methods.unwrap_or(RenameRule::Camel),
51-
args.rename_constants
52-
.unwrap_or(RenameRule::ScreamingSnakeCase),
51+
args.rename_constants.unwrap_or(RenameRule::ScreamingSnake),
5352
);
5453
parsed.parse(input.items.iter_mut())?;
5554

@@ -184,8 +183,9 @@ impl<'a> ParsedImpl<'a> {
184183
match items {
185184
syn::ImplItem::Const(c) => {
186185
let attr = PhpConstAttribute::from_attributes(&c.attrs)?;
187-
let name = c.ident.rename(self.rename_constants);
188-
let name = attr.rename.rename(name);
186+
let name = attr
187+
.rename
188+
.rename(c.ident.to_string(), self.rename_constants);
189189
let docs = get_docs(&attr.attrs)?;
190190
c.attrs.retain(|attr| !attr.path().is_ident("php"));
191191

@@ -197,8 +197,9 @@ impl<'a> ParsedImpl<'a> {
197197
}
198198
syn::ImplItem::Fn(method) => {
199199
let attr = PhpFunctionImplAttribute::from_attributes(&method.attrs)?;
200-
let name = method.sig.ident.rename_method(self.rename_methods);
201-
let name = attr.rename.rename(name);
200+
let name = attr
201+
.rename
202+
.rename_method(method.sig.ident.to_string(), self.rename_methods);
202203
let docs = get_docs(&attr.attrs)?;
203204
method.attrs.retain(|attr| !attr.path().is_ident("php"));
204205

0 commit comments

Comments
 (0)