From 06664b34ce1011be58949fef0e1ed2186d84d35b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 9 May 2018 08:01:57 -0700 Subject: [PATCH] Fix parsing some Rust keywords in attributes Closes #193 --- crates/backend/src/ast.rs | 21 +++++++++++++++++---- tests/all/imports.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/crates/backend/src/ast.rs b/crates/backend/src/ast.rs index c2249a71..2e442c0b 100644 --- a/crates/backend/src/ast.rs +++ b/crates/backend/src/ast.rs @@ -908,7 +908,7 @@ impl syn::synom::Synom for BindgenAttr { call!(term, "getter") >> val: option!(do_parse!( punct!(=) >> - s: syn!(syn::Ident) >> + s: call!(term2ident) >> (s) )) >> (val) @@ -918,7 +918,7 @@ impl syn::synom::Synom for BindgenAttr { call!(term, "setter") >> val: option!(do_parse!( punct!(=) >> - s: syn!(syn::Ident) >> + s: call!(term2ident) >> (s) )) >> (val) @@ -931,7 +931,7 @@ impl syn::synom::Synom for BindgenAttr { do_parse!( call!(term, "js_namespace") >> punct!(=) >> - ns: syn!(syn::Ident) >> + ns: call!(term2ident) >> (ns) )=> { BindgenAttr::JsNamespace } | @@ -952,7 +952,7 @@ impl syn::synom::Synom for BindgenAttr { do_parse!( call!(term, "js_name") >> punct!(=) >> - ns: syn!(syn::Ident) >> + ns: call!(term2ident) >> (ns) )=> { BindgenAttr::JsName } )); @@ -995,6 +995,19 @@ fn term<'a>(cursor: syn::buffer::Cursor<'a>, name: &str) -> syn::synom::PResult< syn::parse_error() } +fn term2ident<'a>(cursor: syn::buffer::Cursor<'a>) + -> syn::synom::PResult<'a, syn::Ident> +{ + if let Some((term, next)) = cursor.term() { + let n = term.to_string(); + if !n.starts_with("'") { + let i = syn::Ident::new(&n, term.span()); + return Ok((i, next)); + } + } + syn::parse_error() +} + fn assert_no_lifetimes(decl: &mut syn::FnDecl) { struct Walk; diff --git a/tests/all/imports.rs b/tests/all/imports.rs index 36c6c3d5..f76e4d6a 100644 --- a/tests/all/imports.rs +++ b/tests/all/imports.rs @@ -429,3 +429,38 @@ fn versions() { "#) .test(); } + +#[test] +fn rust_keyword() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section, wasm_import_module)] + + extern crate wasm_bindgen; + + use wasm_bindgen::prelude::*; + + #[wasm_bindgen(module = "./test")] + extern { + #[wasm_bindgen(js_name = self)] + fn foo() -> u32; + } + + #[wasm_bindgen] + pub fn run() { + assert_eq!(foo(), 2); + } + "#) + .file("test.ts", r#" + import { run } from "./out"; + + export function self() { + return 2; + } + + export function test() { + run(); + } + "#) + .test(); +}