From a3caf126b03533ea9b75b64056152500d38ce148 Mon Sep 17 00:00:00 2001 From: theduke Date: Sun, 18 Aug 2019 18:12:25 +0200 Subject: [PATCH] derive: Upgrade syn/quote/proc_macro dependencies to 1.0 (#413) --- juniper_codegen/Cargo.toml | 6 +- juniper_codegen/src/derive_enum.rs | 8 +-- juniper_codegen/src/derive_input_object.rs | 10 +-- juniper_codegen/src/derive_scalar_value.rs | 27 ++++---- juniper_codegen/src/impl_object.rs | 30 ++++----- juniper_codegen/src/util.rs | 71 ++++++++-------------- 6 files changed, 63 insertions(+), 89 deletions(-) diff --git a/juniper_codegen/Cargo.toml b/juniper_codegen/Cargo.toml index d4921fdc..dcadf4ef 100644 --- a/juniper_codegen/Cargo.toml +++ b/juniper_codegen/Cargo.toml @@ -15,9 +15,9 @@ edition = "2018" proc-macro = true [dependencies] -proc-macro2 = "0.4" -syn = { version = "0.15.28", features = ["full", "extra-traits", "parsing"] } -quote = "0.6" +proc-macro2 = "1.0.1" +syn = { version = "1.0.3", features = ["full", "extra-traits", "parsing"] } +quote = "1.0.2" regex = "1" lazy_static = "1.0.0" diff --git a/juniper_codegen/src/derive_enum.rs b/juniper_codegen/src/derive_enum.rs index b28baf58..6246d0ff 100644 --- a/juniper_codegen/src/derive_enum.rs +++ b/juniper_codegen/src/derive_enum.rs @@ -223,7 +223,7 @@ pub fn impl_enum(ast: &syn::DeriveInput, is_internal: bool) -> TokenStream { where __S: 'r, { let meta = registry.build_enum_type::<#ident>(&(), &[ - #(#values)* + #values ]); #meta_description meta.into_meta() @@ -236,7 +236,7 @@ pub fn impl_enum(ast: &syn::DeriveInput, is_internal: bool) -> TokenStream { _: &#juniper_path::Executor ) -> #juniper_path::Value<__S> { match self { - #(#resolves)* + #resolves } } } @@ -248,7 +248,7 @@ pub fn impl_enum(ast: &syn::DeriveInput, is_internal: bool) -> TokenStream { match v.as_enum_value().or_else(|| { v.as_scalar_value::().map(|s| s as &str) }) { - #(#from_inputs)* + #from_inputs _ => None, } } @@ -257,7 +257,7 @@ pub fn impl_enum(ast: &syn::DeriveInput, is_internal: bool) -> TokenStream { impl<__S: #juniper_path::ScalarValue> #juniper_path::ToInputValue<__S> for #ident { fn to_input_value(&self) -> #juniper_path::InputValue<__S> { match self { - #(#to_inputs)* + #to_inputs } } } diff --git a/juniper_codegen/src/derive_input_object.rs b/juniper_codegen/src/derive_input_object.rs index 1d9416e9..514a4474 100644 --- a/juniper_codegen/src/derive_input_object.rs +++ b/juniper_codegen/src/derive_input_object.rs @@ -103,8 +103,8 @@ impl ObjFieldAttrs { } match item { - NestedMeta::Meta(Meta::Word(ref ident)) => { - if ident == "default" { + NestedMeta::Meta(Meta::Path(ref path)) => { + if path.is_ident("default") { res.default = true; continue; } @@ -294,7 +294,7 @@ pub fn impl_input_object(ast: &syn::DeriveInput, is_internal: bool) -> TokenStre where #scalar: 'r { let fields = &[ - #(#meta_fields)* + #meta_fields ]; let meta = registry.build_input_object_type::<#ident>(&(), fields); #meta_description @@ -311,7 +311,7 @@ pub fn impl_input_object(ast: &syn::DeriveInput, is_internal: bool) -> TokenStre { if let Some(obj) = value.to_object_value() { let item = #ident { - #(#from_inputs)* + #from_inputs }; Some(item) } @@ -326,7 +326,7 @@ pub fn impl_input_object(ast: &syn::DeriveInput, is_internal: bool) -> TokenStre { fn to_input_value(&self) -> #juniper_path::InputValue<#scalar> { #juniper_path::InputValue::object(vec![ - #(#to_inputs)* + #to_inputs ].into_iter().collect()) } } diff --git a/juniper_codegen/src/derive_scalar_value.rs b/juniper_codegen/src/derive_scalar_value.rs index 9f8165a4..dddb18f9 100644 --- a/juniper_codegen/src/derive_scalar_value.rs +++ b/juniper_codegen/src/derive_scalar_value.rs @@ -20,31 +20,28 @@ impl syn::parse::Parse for TransparentAttributes { description: None, }; - let content; - syn::parenthesized!(content in input); - - while !content.is_empty() { - let ident: syn::Ident = content.parse()?; + while !input.is_empty() { + let ident: syn::Ident = input.parse()?; match ident.to_string().as_str() { "name" => { - content.parse::()?; - let val = content.parse::()?; + input.parse::()?; + let val = input.parse::()?; output.name = Some(val.value()); } "description" => { - content.parse::()?; - let val = content.parse::()?; + input.parse::()?; + let val = input.parse::()?; output.description = Some(val.value()); } "transparent" => { output.transparent = Some(true); } other => { - return Err(content.error(format!("Unknown attribute: {}", other))); + return Err(input.error(format!("Unknown attribute: {}", other))); } } - if content.lookahead1().peek(syn::Token![,]) { - content.parse::()?; + if input.lookahead1().peek(syn::Token![,]) { + input.parse::()?; } } @@ -56,7 +53,7 @@ impl TransparentAttributes { fn from_attrs(attrs: &Vec) -> syn::parse::Result { match util::find_graphql_attr(attrs) { Some(attr) => { - let mut parsed: TransparentAttributes = syn::parse(attr.tts.clone().into())?; + let mut parsed: TransparentAttributes = attr.parse_args()?; if parsed.description.is_none() { parsed.description = util::get_doc_comment(attrs); } @@ -86,7 +83,7 @@ fn impl_scalar_struct( ) -> TokenStream { let field = match data.fields { syn::Fields::Unnamed(ref fields) if fields.unnamed.len() == 1 => { - fields.unnamed.first().unwrap().into_value() + fields.unnamed.first().unwrap() } _ => { panic!("#[derive(GraphQLScalarValue)] may only be applied to enums or tuple structs with a single field"); @@ -254,7 +251,7 @@ where fn derive_from_variant(variant: &Variant, ident: &Ident) -> Result { let ty = match variant.fields { - Fields::Unnamed(ref u) if u.unnamed.len() == 1 => &u.unnamed.first().unwrap().value().ty, + Fields::Unnamed(ref u) if u.unnamed.len() == 1 => &u.unnamed.first().unwrap().ty, _ => { return Err(String::from( diff --git a/juniper_codegen/src/impl_object.rs b/juniper_codegen/src/impl_object.rs index 01fb7450..12af7f41 100644 --- a/juniper_codegen/src/impl_object.rs +++ b/juniper_codegen/src/impl_object.rs @@ -91,7 +91,7 @@ pub fn build_object(args: TokenStream, body: TokenStream, is_internal: bool) -> for item in _impl.items { match item { syn::ImplItem::Method(method) => { - let _type = match &method.sig.decl.output { + let _type = match &method.sig.output { syn::ReturnType::Type(_, ref t) => (**t).clone(), syn::ReturnType::Default => { panic!( @@ -115,21 +115,18 @@ pub fn build_object(args: TokenStream, body: TokenStream, is_internal: bool) -> let mut args = Vec::new(); let mut resolve_parts = Vec::new(); - for arg in method.sig.decl.inputs { + for arg in method.sig.inputs { match arg { - _self @ syn::FnArg::SelfRef(_) => { - // Can be ignored. - // "self" will already be in scope. - // resolve_args.push(quote!(self)); + syn::FnArg::Receiver(rec) => { + if rec.reference.is_none() || rec.mutability.is_some() { + panic!( + "Invalid method receiver {}(self, ...): did you mean '&self'?", + method.sig.ident + ); + } } - syn::FnArg::SelfValue(_) => { - panic!( - "Invalid method receiver {}(self, ...): did you mean '&self'?", - method.sig.ident - ); - } - syn::FnArg::Captured(ref captured) => { - let (arg_ident, is_mut) = match &captured.pat { + syn::FnArg::Typed(ref captured) => { + let (arg_ident, is_mut) = match &*captured.pat { syn::Pat::Ident(ref pat_ident) => { (&pat_ident.ident, pat_ident.mutability.is_some()) } @@ -161,7 +158,7 @@ pub fn build_object(args: TokenStream, body: TokenStream, is_internal: bool) -> // without a reference. (&Context) else if context_type .clone() - .map(|ctx| ctx == &captured.ty) + .map(|ctx| ctx == &*captured.ty) .unwrap_or(false) { panic!( @@ -193,12 +190,11 @@ pub fn build_object(args: TokenStream, body: TokenStream, is_internal: bool) -> }) } } - _ => panic!("Invalid argument type in method {}", method.sig.ident), } } let body = &method.block; - let return_ty = &method.sig.decl.output; + let return_ty = &method.sig.output; let resolver_code = quote!( (|| #return_ty { #( #resolve_parts )* diff --git a/juniper_codegen/src/util.rs b/juniper_codegen/src/util.rs index e5119309..b3ca4845 100644 --- a/juniper_codegen/src/util.rs +++ b/juniper_codegen/src/util.rs @@ -60,11 +60,11 @@ pub fn find_graphql_attr(attrs: &Vec) -> Option<&Attribute> { pub fn get_deprecated(attrs: &Vec) -> Option { for attr in attrs { - match attr.interpret_meta() { - Some(Meta::List(ref list)) if list.ident == "deprecated" => { + match attr.parse_meta() { + Ok(Meta::List(ref list)) if list.path.is_ident("deprecated") => { return Some(get_deprecated_meta_list(list)); } - Some(Meta::Word(ref ident)) if ident == "deprecated" => { + Ok(Meta::Path(ref path)) if path.is_ident("deprecated") => { return Some(DeprecationAttr { reason: None }); } _ => {} @@ -77,7 +77,7 @@ fn get_deprecated_meta_list(list: &MetaList) -> DeprecationAttr { for meta in &list.nested { match meta { &NestedMeta::Meta(Meta::NameValue(ref nv)) => { - if nv.ident == "note" { + if nv.path.is_ident("note") { match &nv.lit { &Lit::Str(ref strlit) => { return DeprecationAttr { @@ -88,8 +88,8 @@ fn get_deprecated_meta_list(list: &MetaList) -> DeprecationAttr { } } else { panic!( - "Unrecognized setting on #[deprecated(..)] attribute: {}", - nv.ident + "Unrecognized setting on #[deprecated(..)] attribute: {:?}", + nv.path, ); } } @@ -147,7 +147,7 @@ fn get_doc_strings(items: &Vec) -> Option> { let comments = items .iter() .filter_map(|item| { - if item.ident == "doc" { + if item.path.is_ident("doc") { match item.lit { Lit::Str(ref strlit) => Some(strlit.value().to_string()), _ => panic!("doc attributes only have string literal"), @@ -168,8 +168,8 @@ fn get_doc_strings(items: &Vec) -> Option> { fn get_doc_attr(attrs: &Vec) -> Option> { let mut docs = Vec::new(); for attr in attrs { - match attr.interpret_meta() { - Some(Meta::NameValue(ref nv)) if nv.ident == "doc" => docs.push(nv.clone()), + match attr.parse_meta() { + Ok(Meta::NameValue(ref nv)) if nv.path.is_ident("doc") => docs.push(nv.clone()), _ => {} } } @@ -182,8 +182,8 @@ fn get_doc_attr(attrs: &Vec) -> Option> { // Get the nested items of a a #[graphql(...)] attribute. pub fn get_graphql_attr(attrs: &Vec) -> Option> { for attr in attrs { - match attr.interpret_meta() { - Some(Meta::List(ref list)) if list.ident == "graphql" => { + match attr.parse_meta() { + Ok(Meta::List(ref list)) if list.path.is_ident("graphql") => { return Some(list.nested.iter().map(|x| x.clone()).collect()); } _ => {} @@ -199,7 +199,7 @@ pub fn keyed_item_value( ) -> Option { match item { // Attributes in the form of `#[graphql(name = "value")]`. - &NestedMeta::Meta(Meta::NameValue(ref nameval)) if nameval.ident == name => { + &NestedMeta::Meta(Meta::NameValue(ref nameval)) if nameval.path.is_ident(name) => { match &nameval.lit { // We have a string attribute value. &Lit::Str(ref strlit) => match validation { @@ -215,7 +215,7 @@ pub fn keyed_item_value( } } // Attributes in the form of `#[graphql(name)]`. - &NestedMeta::Meta(Meta::Word(ref ident)) if ident.to_string() == name => match validation { + &NestedMeta::Meta(Meta::Path(ref path)) if path.is_ident(name) => match validation { AttributeValidation::String => { panic!(format!( "Invalid format for attribute \"{:?}\": expected a string value", @@ -304,18 +304,6 @@ impl syn::parse::Parse for ObjectAttributes { interfaces: Vec::new(), }; - // Skip potential parantheses which are present for regular attributes but not for proc macro - // attributes. - let inner = (|| { - let content; - syn::parenthesized!(content in input); - Ok(content) - })(); - let input = match inner.as_ref() { - Ok(content) => content, - Err(_) => input, - }; - while !input.is_empty() { let ident: syn::Ident = input.parse()?; match ident.to_string().as_str() { @@ -377,7 +365,7 @@ impl ObjectAttributes { // Need to unwrap outer (), which are not present for proc macro attributes, // but are present for regular ones. - let mut a = syn::parse::(attr.tts.clone().into())?; + let mut a: Self = attr.parse_args()?; if a.description.is_none() { a.description = get_doc_comment(attrs); } @@ -409,7 +397,6 @@ impl parse::Parse for FieldAttributeArgument { let content; syn::parenthesized!(content in input); - while !content.is_empty() { let name = content.parse::()?; content.parse::()?; @@ -514,11 +501,7 @@ pub struct FieldAttributes { impl parse::Parse for FieldAttributes { fn parse(input: syn::parse::ParseStream) -> syn::parse::Result { - // Remove wrapping parantheses. - let content; - syn::parenthesized!(content in input); - - let items = Punctuated::::parse_terminated(&content)?; + let items = Punctuated::::parse_terminated(&input)?; let mut output = Self { name: None, @@ -548,8 +531,8 @@ impl parse::Parse for FieldAttributes { } } - if !content.is_empty() { - Err(content.error("Unexpected input")) + if !input.is_empty() { + Err(input.error("Unexpected input")) } else { Ok(output) } @@ -564,12 +547,10 @@ impl FieldAttributes { let doc_comment = get_doc_comment(&attrs); let deprecation = get_deprecated(&attrs); - let attr_opt = attrs - .into_iter() - .find(|attr| path_eq_single(&attr.path, "graphql")); + let attr_opt = attrs.into_iter().find(|attr| attr.path.is_ident("graphql")); let mut output = match attr_opt { - Some(attr) => syn::parse(attr.tts.into())?, + Some(attr) => attr.parse_args()?, None => Self::default(), }; @@ -594,7 +575,7 @@ pub struct GraphQLTypeDefinitionFieldArg { pub name: String, pub description: Option, pub default: Option, - pub _type: syn::Type, + pub _type: Box, } #[derive(Debug)] @@ -864,7 +845,7 @@ mod test { } fn ident(s: &str) -> Ident { - Ident::new(s, Span::call_site()) + quote::format_ident!("{}", s) } mod test_get_doc_strings { @@ -873,7 +854,7 @@ mod test { #[test] fn test_single() { let result = get_doc_strings(&vec![MetaNameValue { - ident: ident("doc").into(), + path: ident("doc").into(), eq_token: Default::default(), lit: litstr("foo"), }]); @@ -887,17 +868,17 @@ mod test { fn test_many() { let result = get_doc_strings(&vec![ MetaNameValue { - ident: ident("doc").into(), + path: ident("doc").into(), eq_token: Default::default(), lit: litstr("foo"), }, MetaNameValue { - ident: ident("doc").into(), + path: ident("doc").into(), eq_token: Default::default(), lit: litstr("\n"), }, MetaNameValue { - ident: ident("doc").into(), + path: ident("doc").into(), eq_token: Default::default(), lit: litstr("bar"), }, @@ -911,7 +892,7 @@ mod test { #[test] fn test_not_doc() { let result = get_doc_strings(&vec![MetaNameValue { - ident: ident("blah").into(), + path: ident("blah").into(), eq_token: Default::default(), lit: litstr("foo"), }]);