Skip to content

Handle Expr::Group in the macro parser #2395

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 1 commit into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
84 changes: 45 additions & 39 deletions crates/macro-support/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,14 @@ fn get_ty(mut ty: &syn::Type) -> &syn::Type {
ty
}

fn get_expr(mut expr: &syn::Expr) -> &syn::Expr {
while let syn::Expr::Group(g) = expr {
expr = &g.expr;
}

expr
}

impl<'a> ConvertToAst<(BindgenAttrs, &'a ast::ImportModule)> for syn::ForeignItemFn {
type Target = ast::ImportKind;

Expand Down Expand Up @@ -1074,24 +1082,24 @@ fn import_enum(enum_: syn::ItemEnum, program: &mut ast::Program) -> Result<(), D
_ => bail_span!(v.fields, "only C-Style enums allowed with #[wasm_bindgen]"),
}

match &v.discriminant {
Some((
_,
syn::Expr::Lit(syn::ExprLit {
attrs: _,
lit: syn::Lit::Str(str_lit),
}),
)) => {
let (_, expr) = match &v.discriminant {
Some(pair) => pair,
None => {
bail_span!(v, "all variants must have a value");
}
};
match get_expr(expr) {
syn::Expr::Lit(syn::ExprLit {
attrs: _,
lit: syn::Lit::Str(str_lit),
}) => {
variants.push(v.ident.clone());
variant_values.push(str_lit.value());
}
Some((_, expr)) => bail_span!(
expr => bail_span!(
expr,
"enums with #[wasm_bidngen] cannot mix string and non-string values",
),
None => {
bail_span!(v, "all variants must have a value");
}
}
}

Expand Down Expand Up @@ -1122,18 +1130,17 @@ impl<'a> MacroParse<(&'a mut TokenStream, BindgenAttrs)> for syn::ItemEnum {
let generate_typescript = opts.skip_typescript().is_none();

// Check if the first value is a string literal
match self.variants[0].discriminant {
Some((
_,
if let Some((_, expr)) = &self.variants[0].discriminant {
match get_expr(expr) {
syn::Expr::Lit(syn::ExprLit {
attrs: _,
lit: syn::Lit::Str(_),
}),
)) => {
opts.check_used()?;
return import_enum(self, program);
}) => {
opts.check_used()?;
return import_enum(self, program);
}
_ => {}
}
_ => {}
}
let js_name = opts
.js_name()
Expand Down Expand Up @@ -1169,28 +1176,27 @@ impl<'a> MacroParse<(&'a mut TokenStream, BindgenAttrs)> for syn::ItemEnum {
}

let value = match &v.discriminant {
Some((
_,
Some((_, expr)) => match get_expr(expr) {
syn::Expr::Lit(syn::ExprLit {
attrs: _,
lit: syn::Lit::Int(int_lit),
}),
)) => match int_lit.base10_digits().parse::<u32>() {
Ok(v) => v,
Err(_) => {
bail_span!(
int_lit,
"enums with #[wasm_bindgen] can only support \
}) => match int_lit.base10_digits().parse::<u32>() {
Ok(v) => v,
Err(_) => {
bail_span!(
int_lit,
"enums with #[wasm_bindgen] can only support \
numbers that can be represented as u32"
);
}
);
}
},
expr => bail_span!(
expr,
"enums with #[wasm_bidngen] may only have \
number literal values",
),
},
None => i as u32,
Some((_, expr)) => bail_span!(
expr,
"enums with #[wasm_bidngen] may only have \
number literal values",
),
};

let comments = extract_doc_comments(&v.attrs);
Expand Down Expand Up @@ -1242,15 +1248,15 @@ impl MacroParse<BindgenAttrs> for syn::ItemConst {
bail_span!(self, "#[wasm_bindgen] will not work on constants unless you are defining a #[wasm_bindgen(typescript_custom_section)].");
}

match *self.expr {
match get_expr(&self.expr) {
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(litstr),
..
}) => {
program.typescript_custom_sections.push(litstr.value());
}
_ => {
bail_span!(self, "Expected a string literal to be used with #[wasm_bindgen(typescript_custom_section)].");
expr => {
bail_span!(expr, "Expected a string literal to be used with #[wasm_bindgen(typescript_custom_section)].");
}
}

Expand Down
9 changes: 9 additions & 0 deletions crates/typescript-tests/src/omit_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ pub enum MyEnum {
Two,
Three,
}

macro_rules! generate_ts {
($lit:literal) => {
#[wasm_bindgen(typescript_custom_section)]
const _: &str = $lit;
};
}

generate_ts!("");