-
-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05c1a26
commit 87b0524
Showing
4 changed files
with
106 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'opal/nodes/base' | ||
|
||
module Opal | ||
class Parser | ||
class IfNode < Node | ||
children :test, :true_body, :false_body | ||
|
||
def compile | ||
truthy, falsy = self.truthy, self.falsy | ||
|
||
push "if (" | ||
|
||
# optimize unless (we don't want a else() unless we need to) | ||
if falsy and !truthy | ||
truthy = falsy | ||
falsy = nil | ||
js_falsy test | ||
else | ||
js_truthy test | ||
end | ||
|
||
push ") {" | ||
|
||
# skip if-body if no truthy sexp | ||
indent { line stmt(truthy) } if truthy | ||
|
||
if falsy | ||
if falsy.type == :if | ||
line "} else ", stmt(falsy) | ||
else | ||
indent do | ||
line "} else {" | ||
line stmt(falsy) | ||
end | ||
|
||
line "}" | ||
end | ||
else | ||
push "}" | ||
end | ||
|
||
wrap "(function() {", "; return nil; })()" if needs_wrapper? | ||
end | ||
|
||
def truthy | ||
needs_wrapper? ? @parser.returns(true_body || s(:nil)) : true_body | ||
end | ||
|
||
def falsy | ||
needs_wrapper? ? @parser.returns(false_body || s(:nil)) : false_body | ||
end | ||
|
||
def needs_wrapper? | ||
expr? or recv? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters