Skip to content
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

try to fix js_name error when both getter and setter used #2074

Merged
merged 2 commits into from
Apr 10, 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
6 changes: 0 additions & 6 deletions crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,6 @@ impl Function {
pub fn infer_setter_property(&self) -> Result<String, Diagnostic> {
let name = self.name.to_string();

// if `#[wasm_bindgen(js_name = "...")]` is used then that explicitly
// because it was hand-written anyway.
if self.renamed_via_js_name {
return Ok(name);
}

// Otherwise we infer names based on the Rust function name.
if !name.starts_with("set_") {
bail_span!(
Expand Down
12 changes: 11 additions & 1 deletion crates/macro-support/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cell::Cell;

use ast::OperationKind;
use backend::ast;
use backend::util::{ident_ty, ShortHash};
use backend::Diagnostic;
Expand Down Expand Up @@ -710,7 +711,16 @@ fn function_from_decl(

let (name, name_span, renamed_via_js_name) =
if let Some((js_name, js_name_span)) = opts.js_name() {
(js_name.to_string(), js_name_span, true)
let kind = operation_kind(&opts);
let prefix = match kind {
OperationKind::Setter(_) => "set_",
_ => "",
};
(
format!("{}{}", prefix, js_name.to_string()),
js_name_span,
true,
)
} else {
(decl_name.to_string(), decl_name.span(), false)
};
Expand Down
42 changes: 42 additions & 0 deletions tests/wasm/getters_and_setters.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,48 @@ exports._7_js = (rules) => {
return rules;
}

exports._8_js = (rules) => {
let value = rules.new_js_name__no_getter_setter_with_name__getter_setter_without_name__same_getter_setter_name;
assert.equal(value, 8);
rules.new_js_name__no_getter_setter_with_name__getter_setter_without_name__same_getter_setter_name = value * 2;
return rules;
}

exports._9_js = (rules) => {
let value = rules.new_js_name__no_getter_setter_with_name__getter_setter_without_name__same_getter_setter_name__same_getter_setter_origin_name;
assert.equal(value, 9);
rules.new_js_name__no_getter_setter_with_name__getter_setter_without_name__same_getter_setter_name__same_getter_setter_origin_name = value * 2;
return rules;
}

exports._10_js = (rules) => {
let value = rules.new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_field__same_getter_setter_name;
assert.equal(value, 10);
rules.new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_field__same_getter_setter_name = value * 2;
return rules;
}

exports._11_js = (rules) => {
let value = rules.new_js_name__getter_with_name__no_getter_without_name_for_field__same_getter_setter_name;
assert.equal(value, 11);
rules.new_js_name__setter_with_name__no_setter_without_name_for_field__same_getter_setter_name = value * 2;
return rules;
}

exports._12_js = (rules) => {
let value = rules.new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_field__same_getter_setter_name__same_getter_setter_origin_name;
assert.equal(value, 12);
rules.new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_field__same_getter_setter_name__same_getter_setter_origin_name = value * 2;
return rules;
}

exports._13_js = (rules) => {
let value = rules.new_js_name__getter_with_name__no_getter_without_name_for_field__same_getter_setter_name__same_getter_setter_origin_name;
assert.equal(value, 13);
rules.new_js_name__setter_with_name__no_setter_without_name_for_field__same_getter_setter_name__same_getter_setter_origin_name = value * 2;
return rules;
}

exports.test_getter_compute = x => {
assert.equal(x.foo, 3)
};
Expand Down
142 changes: 142 additions & 0 deletions tests/wasm/getters_and_setters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ extern "C" {
fn _5_js(rules: Rules) -> Rules;
fn _6_js(rules: Rules) -> Rules;
fn _7_js(rules: Rules) -> Rules;
fn _8_js(rules: Rules) -> Rules;
fn _9_js(rules: Rules) -> Rules;
fn _10_js(rules: Rules) -> Rules;
fn _11_js(rules: Rules) -> Rules;
fn _12_js(rules: Rules) -> Rules;
fn _13_js(rules: Rules) -> Rules;

fn test_getter_compute(x: GetterCompute);
fn test_setter_compute(x: SetterCompute);
Expand Down Expand Up @@ -86,6 +92,106 @@ impl Rules {
pub fn set_js_name__setter_with_name__no_setter_without_name_for_field(&mut self, field: i32) {
self.field = field;
}

#[wasm_bindgen(getter, js_name = new_js_name__no_getter_setter_with_name__getter_setter_without_name__same_getter_setter_name)]
pub fn js_name__no_getter_with_name__getter_without_name__same_getter_setter_name(
&self,
) -> i32 {
self.field
}
#[wasm_bindgen(js_name = new_js_name__no_getter_setter_with_name__getter_setter_without_name__same_getter_setter_name, setter)]
pub fn set_js_name__no_setter_with_name__setter_without_name__same_getter_setter_name(
&mut self,
field: i32,
) {
self.field = field;
}

#[wasm_bindgen(getter, js_name = new_js_name__no_getter_setter_with_name__getter_setter_without_name__same_getter_setter_name__same_getter_setter_origin_name)]
pub fn js_name__no_getter_setter_with_name__getter_setter_without_name__same_getter_setter_name__same_getter_setter_origin_name(
&self,
) -> i32 {
self.field
}
#[wasm_bindgen(js_name = new_js_name__no_getter_setter_with_name__getter_setter_without_name__same_getter_setter_name__same_getter_setter_origin_name, setter)]
pub fn set_js_name__no_getter_setter_with_name__getter_setter_without_name__same_getter_setter_name__same_getter_setter_origin_name(
&mut self,
field: i32,
) {
self.field = field;
}

#[wasm_bindgen(
getter = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_field__same_getter_setter_name,
js_name = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_method__same_getter_setter_name)]
pub fn js_name__getter_with_name__no_getter_without_name__same_getter_setter_name(
&self,
) -> i32 {
self.field
}
#[wasm_bindgen(
js_name = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_method__same_getter_setter_name,
setter = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_field__same_getter_setter_name)]
pub fn set_js_name__setter_with_name__no_setter_without_name__same_getter_setter_name(
&mut self,
field: i32,
) {
self.field = field;
}

#[wasm_bindgen(
getter = new_js_name__getter_with_name__no_getter_without_name_for_field__same_getter_setter_name,
js_name = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_method__same_getter_setter_name__no_same_field_name)]
pub fn js_name__getter_with_name__no_getter_without_name__same_getter_setter_name__no_same_field_name(
&self,
) -> i32 {
self.field
}
#[wasm_bindgen(
js_name = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_method__same_getter_setter_name__no_same_field_name,
setter = new_js_name__setter_with_name__no_setter_without_name_for_field__same_getter_setter_name)]
pub fn set_js_name__setter_with_name__no_setter_without_name__same_getter_setter_name__no_same_field_name(
&mut self,
field: i32,
) {
self.field = field;
}

#[wasm_bindgen(
getter = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_field__same_getter_setter_name__same_getter_setter_origin_name,
js_name = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_method__same_getter_setter_name__same_getter_setter_origin_name)]
pub fn js_name__getter_setter_with_name__no_getter_setter_without_name__same_getter_setter_name__same_getter_setter_origin_name(
&self,
) -> i32 {
self.field
}
#[wasm_bindgen(
js_name = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_method__same_getter_setter_name__same_getter_setter_origin_name,
setter = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_field__same_getter_setter_name__same_getter_setter_origin_name)]
pub fn set_js_name__getter_setter_with_name__no_getter_setter_without_name__same_getter_setter_name__same_getter_setter_origin_name(
&mut self,
field: i32,
) {
self.field = field;
}

#[wasm_bindgen(
getter = new_js_name__getter_with_name__no_getter_without_name_for_field__same_getter_setter_name__same_getter_setter_origin_name,
js_name = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_method__same_getter_setter_name__same_getter_setter_origin_name__no_same_field_name)]
pub fn js_name__getter_setter_with_name__no_getter_setter_without_name__same_getter_setter_name__same_getter_setter_origin_name__no_same_field_name(
&self,
) -> i32 {
self.field
}
#[wasm_bindgen(
js_name = new_js_name__getter_setter_with_name__no_getter_setter_without_name_for_method__same_getter_setter_name__same_getter_setter_origin_name__no_same_field_name,
setter = new_js_name__setter_with_name__no_setter_without_name_for_field__same_getter_setter_name__same_getter_setter_origin_name)]
pub fn set_js_name__getter_setter_with_name__no_getter_setter_without_name__same_getter_setter_name__same_getter_setter_origin_name__no_same_field_name(
&mut self,
field: i32,
) {
self.field = field;
}
}

#[wasm_bindgen_test]
Expand Down Expand Up @@ -130,6 +236,42 @@ fn _7_rust() {
assert_eq!(rules.field, 14);
}

#[wasm_bindgen_test]
fn _8_rust() {
let rules = _8_js(Rules { field: 8 });
assert_eq!(rules.field, 16);
}

#[wasm_bindgen_test]
fn _9_rust() {
let rules = _9_js(Rules { field: 9 });
assert_eq!(rules.field, 18);
}

#[wasm_bindgen_test]
fn _10_rust() {
let rules = _10_js(Rules { field: 10 });
assert_eq!(rules.field, 20);
}

#[wasm_bindgen_test]
fn _11_rust() {
let rules = _11_js(Rules { field: 11 });
assert_eq!(rules.field, 22);
}

#[wasm_bindgen_test]
fn _12_rust() {
let rules = _12_js(Rules { field: 12 });
assert_eq!(rules.field, 24);
}

#[wasm_bindgen_test]
fn _13_rust() {
let rules = _13_js(Rules { field: 13 });
assert_eq!(rules.field, 26);
}

#[wasm_bindgen]
struct GetterCompute;

Expand Down