Generate r#async method names in web-sys

This is a new keyword in the 2018 edition!
This commit is contained in:
Alex Crichton 2019-03-26 08:16:53 -07:00
parent a6fe0cefa8
commit 778e497186
2 changed files with 22 additions and 2 deletions

View File

@ -30,6 +30,26 @@ pub fn rust_ident(name: &str) -> Ident {
panic!("tried to create empty Ident (from \"\")");
} else if is_rust_keyword(name) {
Ident::new(&format!("{}_", name), proc_macro2::Span::call_site())
// we didn't historically have `async` in the `is_rust_keyword` list above,
// so for backwards compatibility reasons we need to generate an `async`
// identifier as well, but we'll be sure to use a raw identifier to ease
// compatibility with the 2018 edition.
//
// Note, though, that `proc-macro` doesn't support a normal way to create a
// raw identifier. To get around that we do some wonky parsing to
// roundaboutly create one.
} else if name == "async" {
let ident = "r#async"
.parse::<proc_macro2::TokenStream>()
.unwrap()
.into_iter()
.next()
.unwrap();
match ident {
proc_macro2::TokenTree::Ident(i) => i,
_ => unreachable!(),
}
} else if name.chars().next().unwrap().is_ascii_digit() {
Ident::new(&format!("N{}", name), proc_macro2::Span::call_site())
} else {

View File

@ -34,9 +34,9 @@ fn test_script_element() {
element.set_charset("UTF-8");
assert_eq!(element.charset(), "UTF-8", "Should have a charset");
assert!(element.async(), "Should be async");
assert!(element.r#async(), "Should be async");
element.set_async(false);
assert!(!element.async(), "Shouldn't be a async");
assert!(!element.r#async(), "Shouldn't be a async");
assert!(!element.defer(), "Shouldn't be a defer");
element.set_defer(true);