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

Inline if/unless with function call breaks #1049

Closed
Papierkorb opened this issue Aug 8, 2015 · 4 comments
Closed

Inline if/unless with function call breaks #1049

Papierkorb opened this issue Aug 8, 2015 · 4 comments

Comments

@Papierkorb
Copy link

Hello. Running the following snippet on opalrb.org/try throws a Javascript error (See below):

Element.find('.container').each do |element|
  next unless element.prop 'data-bound'
  puts element.inspect
end

But when I move the unless condition into a variable and check just that variable, it works:

Element.find('.container').each do |element|
  foo = element.prop 'data-bound'
  next unless foo
  puts element.inspect
end

The above snippet should output two lines like <Element ..>


JavaScript error when trying the first snippet looks like this:

TypeError: $a is undefined
TMP_2@http://opalrb.org/javascripts/try.js line 20 > eval:22:55
x.yield1@http://opalrb.org/javascripts/try.js:4:27752
Opal.modules["opal/jquery/element"]/</l@http://opalrb.org/javascripts/try.js:20:1567
@http://opalrb.org/javascripts/try.js line 20 > eval:20:1
@http://opalrb.org/javascripts/try.js line 20 > eval:2:1
Opal.modules.try_opal/</def.$eval_code@http://opalrb.org/javascripts/try.js:20:27285
Opal.modules.try_opal/</def.$run_code@http://opalrb.org/javascripts/try.js:20:27177
Opal.modules.try_opal/TMP_3@http://opalrb.org/javascripts/try.js:20:28023
K.Callbacks/f@http://opalrb.org/javascripts/application.js:2:16761
K.Callbacks/p.fireWith@http://opalrb.org/javascripts/application.js:2:17547
.ready@http://opalrb.org/javascripts/application.js:2:12306
pt@http://opalrb.org/javascripts/application.js:2:9734

The interesting pieces from the generated JavaScript code:

Broken snippet:

if ((($a = element.$prop("data-bound")) !== nil && (!$a.$$is_boolean || $a == true))) {
} else {
  return nil;
};

Working snippet:

foo = element.$prop("data-bound");
 (foo !== false && foo !== nil) {
} else {
  return nil;
};

I think that (!$a.$$is_boolean || $a == true) in the broken snippet is the culprit, as $prop() returns undefined. Verification by manually calling it in the JavaScript console of the developer tools:

Opal.Element.$find(".container").$prop("data-bound");
// => undefined
@meh
Copy link
Member

meh commented Aug 19, 2015

Ruby methods should never return non-Ruby values.

@meh meh closed this as completed Aug 19, 2015
@meh
Copy link
Member

meh commented Aug 19, 2015

For the sake of discussion #867 would fix this, but I'm strongly against it because things would break again as soon as you try to call a method on any bubbled up undefined or null instance and it would be even harder to debug.

@Papierkorb
Copy link
Author

Isn't this a bug with opal-jquery then, in that a method it provides may yield weird results?

@meh
Copy link
Member

meh commented Aug 19, 2015

@Papierkorb very likely, yes, open an issue on there 🐼

hmdne pushed a commit to hmdne/opal that referenced this issue Jan 27, 2024
Bumps [portfinder](https://github.com/http-party/node-portfinder) from 1.0.27 to 1.0.28.
- [Release notes](https://github.com/http-party/node-portfinder/releases)
- [Commits](http-party/node-portfinder@v1.0.27...v1.0.28)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants