mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 03:21:20 -05:00
adding the notion of existence -- postfixing an expression with a question mark will check if to see if it's not null or undefined
This commit is contained in:
@@ -19,7 +19,7 @@ token INDENT OUTDENT
|
|||||||
|
|
||||||
# Declare order of operations.
|
# Declare order of operations.
|
||||||
prechigh
|
prechigh
|
||||||
nonassoc UMINUS SPLAT NOT '!' '!!' '~' '++' '--'
|
nonassoc UMINUS SPLAT NOT '!' '!!' '~' '++' '--' '?'
|
||||||
left '*' '/' '%'
|
left '*' '/' '%'
|
||||||
left '+' '-'
|
left '+' '-'
|
||||||
left '<<' '>>' '>>>'
|
left '<<' '>>' '>>>'
|
||||||
@@ -72,6 +72,7 @@ rule
|
|||||||
| Switch
|
| Switch
|
||||||
| Extends
|
| Extends
|
||||||
| Splat
|
| Splat
|
||||||
|
| Existence
|
||||||
| Comment
|
| Comment
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -181,6 +182,10 @@ rule
|
|||||||
| Expression INSTANCEOF Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
| Expression INSTANCEOF Expression { result = OpNode.new(val[1], val[0], val[2]) }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
Existence:
|
||||||
|
Expression '?' { result = ExistenceNode.new(val[0]) }
|
||||||
|
;
|
||||||
|
|
||||||
# Function definition.
|
# Function definition.
|
||||||
Code:
|
Code:
|
||||||
ParamList "=>" Block { result = CodeNode.new(val[0], val[2]) }
|
ParamList "=>" Block { result = CodeNode.new(val[0], val[2]) }
|
||||||
|
|||||||
@@ -715,6 +715,19 @@ module CoffeeScript
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Check an expression for existence (meaning not null or undefined).
|
||||||
|
class ExistenceNode < Node
|
||||||
|
attr_reader :expression
|
||||||
|
|
||||||
|
def initialize(expression)
|
||||||
|
@expression = expression
|
||||||
|
end
|
||||||
|
|
||||||
|
def compile(o={})
|
||||||
|
write("(#{@expression.compile(super(o))} != undefined)")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# An extra set of parentheses, supplied by the script source.
|
# An extra set of parentheses, supplied by the script source.
|
||||||
# You can't wrap parentheses around bits that get compiled into JS statements,
|
# You can't wrap parentheses around bits that get compiled into JS statements,
|
||||||
# unfortunately.
|
# unfortunately.
|
||||||
|
|||||||
Reference in New Issue
Block a user