mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
automatic conversion of arguments into arrays
This commit is contained in:
parent
1ebc4d5f21
commit
5fe419b1ce
4 changed files with 16 additions and 1 deletions
|
@ -12,6 +12,7 @@ token FOR IN BY WHEN WHILE
|
||||||
token SWITCH LEADING_WHEN
|
token SWITCH LEADING_WHEN
|
||||||
token DELETE INSTANCEOF TYPEOF
|
token DELETE INSTANCEOF TYPEOF
|
||||||
token SUPER EXTENDS
|
token SUPER EXTENDS
|
||||||
|
token ARGUMENTS
|
||||||
token NEWLINE
|
token NEWLINE
|
||||||
token COMMENT
|
token COMMENT
|
||||||
token JS
|
token JS
|
||||||
|
@ -97,6 +98,7 @@ rule
|
||||||
| REGEX { result = LiteralNode.new(val[0]) }
|
| REGEX { result = LiteralNode.new(val[0]) }
|
||||||
| BREAK { result = LiteralNode.new(val[0]) }
|
| BREAK { result = LiteralNode.new(val[0]) }
|
||||||
| CONTINUE { result = LiteralNode.new(val[0]) }
|
| CONTINUE { result = LiteralNode.new(val[0]) }
|
||||||
|
| ARGUMENTS { result = LiteralNode.new(val[0]) }
|
||||||
| TRUE { result = LiteralNode.new(true) }
|
| TRUE { result = LiteralNode.new(true) }
|
||||||
| FALSE { result = LiteralNode.new(false) }
|
| FALSE { result = LiteralNode.new(false) }
|
||||||
| YES { result = LiteralNode.new(true) }
|
| YES { result = LiteralNode.new(true) }
|
||||||
|
|
|
@ -15,6 +15,7 @@ module CoffeeScript
|
||||||
"for", "in", "by", "where", "while",
|
"for", "in", "by", "where", "while",
|
||||||
"switch", "when",
|
"switch", "when",
|
||||||
"super", "extends",
|
"super", "extends",
|
||||||
|
"arguments",
|
||||||
"delete", "instanceof", "typeof"]
|
"delete", "instanceof", "typeof"]
|
||||||
|
|
||||||
# Token matching regexes.
|
# Token matching regexes.
|
||||||
|
|
|
@ -134,6 +134,10 @@ module CoffeeScript
|
||||||
class LiteralNode < Node
|
class LiteralNode < Node
|
||||||
STATEMENTS = ['break', 'continue']
|
STATEMENTS = ['break', 'continue']
|
||||||
|
|
||||||
|
CONVERSIONS = {
|
||||||
|
'arguments' => 'Array.prototype.slice.call(arguments, 0)'
|
||||||
|
}
|
||||||
|
|
||||||
attr_reader :value
|
attr_reader :value
|
||||||
|
|
||||||
def initialize(value)
|
def initialize(value)
|
||||||
|
@ -146,9 +150,10 @@ module CoffeeScript
|
||||||
alias_method :statement_only?, :statement?
|
alias_method :statement_only?, :statement?
|
||||||
|
|
||||||
def compile_node(o)
|
def compile_node(o)
|
||||||
|
val = CONVERSIONS[@value.to_s] || @value.to_s
|
||||||
indent = statement? ? o[:indent] : ''
|
indent = statement? ? o[:indent] : ''
|
||||||
ending = statement? ? ';' : ''
|
ending = statement? ? ';' : ''
|
||||||
write(indent + @value.to_s + ending)
|
write("#{indent}#{val}#{ending}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -15,3 +15,10 @@ print(area(
|
||||||
x1
|
x1
|
||||||
y1
|
y1
|
||||||
) is 100)
|
) is 100)
|
||||||
|
|
||||||
|
|
||||||
|
# Arguments are turned into arrays.
|
||||||
|
curried: =>
|
||||||
|
print(area.apply(this, arguments.concat(20, 20)) is 100)
|
||||||
|
|
||||||
|
curried(10, 10)
|
||||||
|
|
Loading…
Reference in a new issue