2010-12-29 14:06:57 -05:00
|
|
|
|
# Function Literals
|
|
|
|
|
# -----------------
|
|
|
|
|
|
2011-01-03 04:17:00 -05:00
|
|
|
|
# TODO: add indexing and method invocation tests: (->)[0], (->).call()
|
|
|
|
|
|
2010-12-29 14:06:57 -05:00
|
|
|
|
# * Function Definition
|
|
|
|
|
# * Bound Function Definition
|
|
|
|
|
# * Parameter List Features
|
|
|
|
|
# * Splat Parameters
|
|
|
|
|
# * Context (@) Parameters
|
|
|
|
|
# * Parameter Destructuring
|
|
|
|
|
# * Default Parameters
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
|
# Function Definition
|
2010-12-29 14:06:57 -05:00
|
|
|
|
|
|
|
|
|
x = 1
|
|
|
|
|
y = {}
|
|
|
|
|
y.x = -> 3
|
|
|
|
|
ok x is 1
|
|
|
|
|
ok typeof(y.x) is 'function'
|
|
|
|
|
ok y.x instanceof Function
|
|
|
|
|
ok y.x() is 3
|
|
|
|
|
|
|
|
|
|
# The empty function should not cause a syntax error.
|
|
|
|
|
->
|
|
|
|
|
() ->
|
|
|
|
|
|
|
|
|
|
# Multiple nested function declarations mixed with implicit calls should not
|
|
|
|
|
# cause a syntax error.
|
|
|
|
|
(one) -> (two) -> three four, (five) -> six seven, eight, (nine) ->
|
|
|
|
|
|
|
|
|
|
# with multiple single-line functions on the same line.
|
|
|
|
|
func = (x) -> (x) -> (x) -> x
|
|
|
|
|
ok func(1)(2)(3) is 3
|
|
|
|
|
|
|
|
|
|
# Make incorrect indentation safe.
|
|
|
|
|
func = ->
|
|
|
|
|
obj = {
|
|
|
|
|
key: 10
|
|
|
|
|
}
|
|
|
|
|
obj.key - 5
|
|
|
|
|
eq func(), 5
|
|
|
|
|
|
2010-12-30 22:48:31 -05:00
|
|
|
|
# Ensure that functions with the same name don't clash with helper functions.
|
|
|
|
|
del = -> 5
|
|
|
|
|
ok del() is 5
|
|
|
|
|
|
2010-12-29 14:06:57 -05:00
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
|
# Bound Function Definition
|
2010-12-29 14:06:57 -05:00
|
|
|
|
|
|
|
|
|
obj =
|
|
|
|
|
bound: ->
|
|
|
|
|
(=> this)()
|
|
|
|
|
unbound: ->
|
|
|
|
|
(-> this)()
|
|
|
|
|
nested: ->
|
|
|
|
|
(=>
|
|
|
|
|
(=>
|
|
|
|
|
(=> this)()
|
|
|
|
|
)()
|
|
|
|
|
)()
|
|
|
|
|
eq obj, obj.bound()
|
|
|
|
|
ok obj isnt obj.unbound()
|
|
|
|
|
eq obj, obj.nested()
|
|
|
|
|
|
|
|
|
|
|
2011-09-25 21:44:23 -04:00
|
|
|
|
test "even more fancy bound functions", ->
|
2011-12-24 07:04:34 -05:00
|
|
|
|
obj =
|
2011-09-25 21:44:23 -04:00
|
|
|
|
one: ->
|
|
|
|
|
do =>
|
|
|
|
|
return this.two()
|
|
|
|
|
two: ->
|
|
|
|
|
do =>
|
|
|
|
|
do =>
|
|
|
|
|
do =>
|
|
|
|
|
return this.three
|
|
|
|
|
three: 3
|
|
|
|
|
|
|
|
|
|
eq obj.one(), 3
|
|
|
|
|
|
|
|
|
|
|
[CS2] Output ES2015 arrow functions, default parameters, rest parameters (#4311)
* Eliminate wrapper around “bound” (arrow) functions; output `=>` for such functions
* Remove irrelevant (and breaking) tests
* Minor cleanup
* When a function parameter is a splat (i.e., it uses the ES2015 rest parameter syntax) output that parameter as ES2015
* Rearrange function parameters when one of the parameters is a splat and isn’t the last parameter (very WIP)
* Handle params like `@param`, adding assignment expressions for them when they appear; ensure splat parameter is last
* Add parameter names (not a text like `'\nValue IdentifierLiteral: a'`) to the scope, so that parameters can’t be deleted; move body-related lines together; more explanation of what’s going on
* For parameters with a default value, correctly add the parameter name to the function scope
* Handle expansions in function parameters: when an expansion is found, set the parameters to only be the original parameters left of the expansion, then an `...args` parameter; and in the function body define variables for the parameters to the right of the expansion, including setting default values
* Handle splat parameters the same way we handle expansions: if a splat parameter is found, it becomes the last parameter in the function definition, and all following parameters get declared in the function body. Fix the splat/rest parameter values after the post-splat parameters have been extracted from it. Clean up `Code.compileNode` so that we loop through the parameters only once, and we create all expressions using calls like `new IdentifierLiteral` rather than `@makeCode`.
* Fix parameter name when a parameter is a splat attached to `this` (e.g. `@param...`)
* Rather than assigning post-splat parameters based on index, use slice; passes test “Functions with splats being called with too few arguments”
* Dial back our w00t indentation
* Better parsing of parameter names (WIP)
* Refactor processing of splat/expansion parameters
* Fix assignment of default parameters for parameters that come after a splat
* Better check for whether a param is attached to `this`
* More understandable variable names
* For parameters after a splat or expansion, assign them similar to the 1.x destructuring method of using `arguments`, except only concern ourselves with the post-splat parameters instead of all parameters; and use the splat/expansion parameter name, since `arguments` in ES fat arrow functions refers to the parent function’s `arguments` rather than the fat arrow function’s arguments/parameters
* Don’t add unnamed parameters (like `[]` as a parameter) to the function scope
* Disallow multiple splat/expansion parameters in function definitions; disallow lone expansion parameters
* Fix `this` params not getting assigned if the parameter is after a splat parameter
* Allow names of function parameters attached to `this` to be reserved words
* Always add a statement to the function body defining a variable with its default value, if it has one, if the variable `== null`; this covers the case when ES doesn’t apply the default value when `null` is passed in as a value, but CoffeeScript expects `null` and `undefined` to act interchangeably
* Aftermath of having both `undefined` and `null` trigger the use of default values for parameters with default values
* More careful parsing of destructured parameters
* Fall back to processing destructured parameters in the function body, to account for `this` or default values within destructured objects
* Clean up comments
* Restore new bare function test, minus the arrow function part of it
* Test that bound/arrow functions aren’t overwriting the `arguments` object, which should refer to the parent scope’s `arguments` (like `this`)
* Follow ES2015 spec for parameter default values: `null` gets assigned as as `null`, not the default value
* Mimic ES default parameters behavior for parameters after a splat or expansion parameter
* Bound functions cannot be generators: remove no-longer-relevant test, add check to throw error if `yield` appears inside a bound (arrow) function
* Error for bound generator functions should underline the `yield`
2016-10-26 01:26:13 -04:00
|
|
|
|
test "arguments in bound functions inherit from parent function", ->
|
|
|
|
|
# The `arguments` object in an ES arrow function refers to the `arguments`
|
|
|
|
|
# of the parent scope, just like `this`. In the CoffeeScript 1.x
|
|
|
|
|
# implementation of `=>`, the `arguments` object referred to the arguments
|
|
|
|
|
# of the arrow function; but per the ES2015 spec, `arguments` should refer
|
|
|
|
|
# to the parent.
|
|
|
|
|
arrayEq ((a...) -> a)([1, 2, 3]), ((a...) => a)([1, 2, 3])
|
|
|
|
|
|
|
|
|
|
parent = (a, b, c) ->
|
|
|
|
|
(bound = =>
|
|
|
|
|
[arguments[0], arguments[1], arguments[2]]
|
|
|
|
|
)()
|
|
|
|
|
arrayEq [1, 2, 3], parent(1, 2, 3)
|
|
|
|
|
|
|
|
|
|
|
2011-04-20 22:16:56 -04:00
|
|
|
|
test "self-referencing functions", ->
|
|
|
|
|
changeMe = ->
|
|
|
|
|
changeMe = 2
|
|
|
|
|
|
|
|
|
|
changeMe()
|
|
|
|
|
eq changeMe, 2
|
|
|
|
|
|
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
|
# Parameter List Features
|
2010-12-29 14:06:57 -05:00
|
|
|
|
|
|
|
|
|
test "splats", ->
|
|
|
|
|
arrayEq [0, 1, 2], (((splat...) -> splat) 0, 1, 2)
|
2012-01-09 12:56:18 -05:00
|
|
|
|
arrayEq [2, 3], (((_, _1, splat...) -> splat) 0, 1, 2, 3)
|
|
|
|
|
arrayEq [0, 1], (((splat..., _, _1) -> splat) 0, 1, 2, 3)
|
|
|
|
|
arrayEq [2], (((_, _1, splat..., _2) -> splat) 0, 1, 2, 3)
|
2010-12-29 14:06:57 -05:00
|
|
|
|
|
2017-08-17 16:13:52 -04:00
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
|
arrayEq [0, 1, 2], (((splat ...) -> splat) 0, 1, 2)
|
|
|
|
|
arrayEq [2, 3], (((_, _1, splat ...) -> splat) 0, 1, 2, 3)
|
|
|
|
|
arrayEq [0, 1], (((splat ..., _, _1) -> splat) 0, 1, 2, 3)
|
|
|
|
|
arrayEq [2], (((_, _1, splat ..., _2) -> splat) 0, 1, 2, 3)
|
|
|
|
|
|
2012-04-23 20:41:56 -04:00
|
|
|
|
test "destructured splatted parameters", ->
|
|
|
|
|
arr = [0,1,2]
|
|
|
|
|
splatArray = ([a...]) -> a
|
|
|
|
|
splatArrayRest = ([a...],b...) -> arrayEq(a,b); b
|
|
|
|
|
arrayEq splatArray(arr), arr
|
|
|
|
|
arrayEq splatArrayRest(arr,0,1,2), arr
|
|
|
|
|
|
2017-08-17 16:13:52 -04:00
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
|
splatArray = ([a ...]) -> a
|
|
|
|
|
splatArrayRest = ([a ...],b ...) -> arrayEq(a,b); b
|
|
|
|
|
|
2018-04-23 12:50:42 -04:00
|
|
|
|
test "#4884: object-destructured splatted parameters", ->
|
|
|
|
|
f = ({length}...) -> length
|
|
|
|
|
eq f(4, 5, 6), 3
|
|
|
|
|
f = ({length: len}...) -> len
|
|
|
|
|
eq f(4, 5, 6), 3
|
|
|
|
|
f = ({length}..., last) -> [length, last]
|
|
|
|
|
arrayEq f(4, 5, 6), [2, 6]
|
|
|
|
|
f = ({length: len}..., last) -> [len, last]
|
|
|
|
|
arrayEq f(4, 5, 6), [2, 6]
|
|
|
|
|
|
2010-12-29 14:06:57 -05:00
|
|
|
|
test "@-parameters: automatically assign an argument's value to a property of the context", ->
|
|
|
|
|
nonce = {}
|
|
|
|
|
|
|
|
|
|
((@prop) ->).call context = {}, nonce
|
|
|
|
|
eq nonce, context.prop
|
|
|
|
|
|
[CS2] Comments (#4572)
* Make `addLocationDataFn` more DRY
* Style fixes
* Provide access to full parser inside our custom function running in parser.js; rename the function to lay the groundwork for adding data aside from location data
* Fix style.
* Fix style.
* Label test comments
* Update grammar to remove comment tokens; update DSL to call new helper function that preserves comments through parsing
* New implementation of compiling block comments: the lexer pulls them out of the token stream, attaching them as a property to a token; the rewriter moves the attachment around so it lives on a token that is destined to make it through to compilation (and in a good placement); and the nodes render the block comment. All tests but one pass (commented out).
* If a comment follows a class declaration, move the comment inside the class body
* Style
* Improve indentation of multiline comments
* Fix indentation for block comments, at least in the cases covered by the one failing test
* Don’t reverse the order of unshifted comments
* Simplify rewriter’s handling of comments, generalizing the special case
* Expand the list of tokens we need to avoid for passing comments through the parser; get some literal tokens to have nodes created for them so that the comments pass through
* Improve comments; fix multiline flag
* Prepare HereComments for processing line comments
* Line comments, first draft: the tests pass, but the line comments aren’t indented and sometimes trail previous lines when they shouldn’t; updated compiler output in following commit
* Updated compiler, now with line comments
* `process` doesn’t exist in the browser, so we should check for its existence first
* Update parser output
* Test that proves #4290 is fixed
* Indent line comments, first pass
* Compiled output with indented line comments
* Comments that start a new line shouldn’t trail; don’t skip comments attached to generated tokens; stop looking for indentation once we hit a newline
* Revised output
* Cleanup
* Split “multiline” line comment tokens, shifting them forward or back as appropriate
* Fix comments in module specifiers
* Abstract attaching comments to a node
* Line comments in interpolated strings
* Line comments can’t be multiline anymore
* Improve handling of blank lines and indentation of following comments that start a new line (i.e. don’t trail)
* Make comments compilation more object-oriented
* Remove lots of dead code that we don’t need anymore because a comment is never a node, only a fragment
* Improve eqJS helper
* Fix #4290 definitively, with improved output for arrays with interspersed block comments
* Add support for line comments output interspersed within arrays
* Fix mistake, don’t lose the variable we’re working on
* Remove redundant replacements
* Check for indentation only from the start of the string
* Indentations in generated JS are always multiples of two spaces (never tabs) so just look for 2+ spaces
* Update package versions; run Babel twice, once for each preset, temporarily until a Babili bug is fixed that prevents it from running with the env preset
* Don’t rely on `fragment.type`, which can break when the compiler is minified
* Updated generated docs and browser compiler
* Output block comments after function arguments
* Comments appear above scope `var` declarations; better tracking of generated `JS` tokens created only to shepherd comments through to the output
* Create new FuncGlyph node, to hold comments we want to output near the function parameters
* Block comments between `)` and `->`/`=>` get output between `)` and `{`.
* Fix indentation of comments that are the first line inside a bare mode block
* Updated output
* Full Flow example
* Updated browser compiler
* Abstract and organize comment fragment generation code; store more properties on the comment fragment objects; make `throw` behave like `return`
* Abstract token insertion code
* Add missing locationData to STRING_START token, giving it the locationData of the overall StringWithInterpolations token so that comments attached to STRING_START end up on the StringWithInterpolations node
* Allow `SUPER` tokens to carry comments
* Rescue comments from `Existence` nodes and `If` nodes’ conditions
* Rescue comments after `\` line continuation tokens
* Updated compiled output
* Updated browser compiler
* Output block comments in the same `compileFragments` method as line comments, except for inline block comments
* Comments before splice
* Updated browser compiler
* Track compiledComments as a property of Base, to ensure that it’s not a global variable
* Docs: split up the Usage section
* Docs for type annotations via Flow; updated docs output
* Update regular comments documentation
* Updated browser compiler
* Comments before soak
* Comments before static methods, and probably before `@variable =` (this) assignments generally
* Comments before ‘if exists?’, refactor comment before ‘if this.var’ to be more precise, improve helper methods
* Comments before a method that contains ‘super()’ should output above the method property, not above the ‘super.method()’ call
* Fix missing comments before `if not` (i.e. before a UNARY token)
* Fix comments before ‘for’; add test for comment before assignment if (fixed in earlier commit)
* Comments within heregexes
* Updated browser compiler
* Update description to reflect what’s now happening in compileCommentFragments
* Preserve blank lines between line comments; output “whitespace-only” line comments as blank lines, rather than `//` following by whitespace
* Better future-proof comments tests
* Comments before object destructuring; abstract method for setting comments aside before compilation
* Handle more cases of comments before or after `for` loop declaration lines
* Fix indentation of comments preceding `for` loops
* Fix comment before splat function parameter
* Catch another RegexWithInterpolations comment edge case
* Updated browser compiler
* Change heregex example to one that’s more readable; update output
* Remove a few last references to the defunct HERECOMMENT token
* Abstract location hash creation into a function
* Improved clarity per code review notes
* Updated browser compiler
2017-08-02 22:34:34 -04:00
|
|
|
|
# Allow splats alongside the special argument
|
2010-12-29 14:06:57 -05:00
|
|
|
|
((splat..., @prop) ->).apply context = {}, [0, 0, nonce]
|
|
|
|
|
eq nonce, context.prop
|
|
|
|
|
|
2017-08-17 16:13:52 -04:00
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
|
((splat ..., @prop) ->).apply context = {}, [0, 0, nonce]
|
|
|
|
|
eq nonce, context.prop
|
|
|
|
|
|
[CS2] Comments (#4572)
* Make `addLocationDataFn` more DRY
* Style fixes
* Provide access to full parser inside our custom function running in parser.js; rename the function to lay the groundwork for adding data aside from location data
* Fix style.
* Fix style.
* Label test comments
* Update grammar to remove comment tokens; update DSL to call new helper function that preserves comments through parsing
* New implementation of compiling block comments: the lexer pulls them out of the token stream, attaching them as a property to a token; the rewriter moves the attachment around so it lives on a token that is destined to make it through to compilation (and in a good placement); and the nodes render the block comment. All tests but one pass (commented out).
* If a comment follows a class declaration, move the comment inside the class body
* Style
* Improve indentation of multiline comments
* Fix indentation for block comments, at least in the cases covered by the one failing test
* Don’t reverse the order of unshifted comments
* Simplify rewriter’s handling of comments, generalizing the special case
* Expand the list of tokens we need to avoid for passing comments through the parser; get some literal tokens to have nodes created for them so that the comments pass through
* Improve comments; fix multiline flag
* Prepare HereComments for processing line comments
* Line comments, first draft: the tests pass, but the line comments aren’t indented and sometimes trail previous lines when they shouldn’t; updated compiler output in following commit
* Updated compiler, now with line comments
* `process` doesn’t exist in the browser, so we should check for its existence first
* Update parser output
* Test that proves #4290 is fixed
* Indent line comments, first pass
* Compiled output with indented line comments
* Comments that start a new line shouldn’t trail; don’t skip comments attached to generated tokens; stop looking for indentation once we hit a newline
* Revised output
* Cleanup
* Split “multiline” line comment tokens, shifting them forward or back as appropriate
* Fix comments in module specifiers
* Abstract attaching comments to a node
* Line comments in interpolated strings
* Line comments can’t be multiline anymore
* Improve handling of blank lines and indentation of following comments that start a new line (i.e. don’t trail)
* Make comments compilation more object-oriented
* Remove lots of dead code that we don’t need anymore because a comment is never a node, only a fragment
* Improve eqJS helper
* Fix #4290 definitively, with improved output for arrays with interspersed block comments
* Add support for line comments output interspersed within arrays
* Fix mistake, don’t lose the variable we’re working on
* Remove redundant replacements
* Check for indentation only from the start of the string
* Indentations in generated JS are always multiples of two spaces (never tabs) so just look for 2+ spaces
* Update package versions; run Babel twice, once for each preset, temporarily until a Babili bug is fixed that prevents it from running with the env preset
* Don’t rely on `fragment.type`, which can break when the compiler is minified
* Updated generated docs and browser compiler
* Output block comments after function arguments
* Comments appear above scope `var` declarations; better tracking of generated `JS` tokens created only to shepherd comments through to the output
* Create new FuncGlyph node, to hold comments we want to output near the function parameters
* Block comments between `)` and `->`/`=>` get output between `)` and `{`.
* Fix indentation of comments that are the first line inside a bare mode block
* Updated output
* Full Flow example
* Updated browser compiler
* Abstract and organize comment fragment generation code; store more properties on the comment fragment objects; make `throw` behave like `return`
* Abstract token insertion code
* Add missing locationData to STRING_START token, giving it the locationData of the overall StringWithInterpolations token so that comments attached to STRING_START end up on the StringWithInterpolations node
* Allow `SUPER` tokens to carry comments
* Rescue comments from `Existence` nodes and `If` nodes’ conditions
* Rescue comments after `\` line continuation tokens
* Updated compiled output
* Updated browser compiler
* Output block comments in the same `compileFragments` method as line comments, except for inline block comments
* Comments before splice
* Updated browser compiler
* Track compiledComments as a property of Base, to ensure that it’s not a global variable
* Docs: split up the Usage section
* Docs for type annotations via Flow; updated docs output
* Update regular comments documentation
* Updated browser compiler
* Comments before soak
* Comments before static methods, and probably before `@variable =` (this) assignments generally
* Comments before ‘if exists?’, refactor comment before ‘if this.var’ to be more precise, improve helper methods
* Comments before a method that contains ‘super()’ should output above the method property, not above the ‘super.method()’ call
* Fix missing comments before `if not` (i.e. before a UNARY token)
* Fix comments before ‘for’; add test for comment before assignment if (fixed in earlier commit)
* Comments within heregexes
* Updated browser compiler
* Update description to reflect what’s now happening in compileCommentFragments
* Preserve blank lines between line comments; output “whitespace-only” line comments as blank lines, rather than `//` following by whitespace
* Better future-proof comments tests
* Comments before object destructuring; abstract method for setting comments aside before compilation
* Handle more cases of comments before or after `for` loop declaration lines
* Fix indentation of comments preceding `for` loops
* Fix comment before splat function parameter
* Catch another RegexWithInterpolations comment edge case
* Updated browser compiler
* Change heregex example to one that’s more readable; update output
* Remove a few last references to the defunct HERECOMMENT token
* Abstract location hash creation into a function
* Improved clarity per code review notes
* Updated browser compiler
2017-08-02 22:34:34 -04:00
|
|
|
|
# Allow the argument itself to be a splat
|
2010-12-29 14:06:57 -05:00
|
|
|
|
((@prop...) ->).call context = {}, 0, nonce, 0
|
|
|
|
|
eq nonce, context.prop[1]
|
|
|
|
|
|
2017-08-17 16:13:52 -04:00
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
|
((@prop ...) ->).call context = {}, 0, nonce, 0
|
|
|
|
|
eq nonce, context.prop[1]
|
|
|
|
|
|
[CS2] Comments (#4572)
* Make `addLocationDataFn` more DRY
* Style fixes
* Provide access to full parser inside our custom function running in parser.js; rename the function to lay the groundwork for adding data aside from location data
* Fix style.
* Fix style.
* Label test comments
* Update grammar to remove comment tokens; update DSL to call new helper function that preserves comments through parsing
* New implementation of compiling block comments: the lexer pulls them out of the token stream, attaching them as a property to a token; the rewriter moves the attachment around so it lives on a token that is destined to make it through to compilation (and in a good placement); and the nodes render the block comment. All tests but one pass (commented out).
* If a comment follows a class declaration, move the comment inside the class body
* Style
* Improve indentation of multiline comments
* Fix indentation for block comments, at least in the cases covered by the one failing test
* Don’t reverse the order of unshifted comments
* Simplify rewriter’s handling of comments, generalizing the special case
* Expand the list of tokens we need to avoid for passing comments through the parser; get some literal tokens to have nodes created for them so that the comments pass through
* Improve comments; fix multiline flag
* Prepare HereComments for processing line comments
* Line comments, first draft: the tests pass, but the line comments aren’t indented and sometimes trail previous lines when they shouldn’t; updated compiler output in following commit
* Updated compiler, now with line comments
* `process` doesn’t exist in the browser, so we should check for its existence first
* Update parser output
* Test that proves #4290 is fixed
* Indent line comments, first pass
* Compiled output with indented line comments
* Comments that start a new line shouldn’t trail; don’t skip comments attached to generated tokens; stop looking for indentation once we hit a newline
* Revised output
* Cleanup
* Split “multiline” line comment tokens, shifting them forward or back as appropriate
* Fix comments in module specifiers
* Abstract attaching comments to a node
* Line comments in interpolated strings
* Line comments can’t be multiline anymore
* Improve handling of blank lines and indentation of following comments that start a new line (i.e. don’t trail)
* Make comments compilation more object-oriented
* Remove lots of dead code that we don’t need anymore because a comment is never a node, only a fragment
* Improve eqJS helper
* Fix #4290 definitively, with improved output for arrays with interspersed block comments
* Add support for line comments output interspersed within arrays
* Fix mistake, don’t lose the variable we’re working on
* Remove redundant replacements
* Check for indentation only from the start of the string
* Indentations in generated JS are always multiples of two spaces (never tabs) so just look for 2+ spaces
* Update package versions; run Babel twice, once for each preset, temporarily until a Babili bug is fixed that prevents it from running with the env preset
* Don’t rely on `fragment.type`, which can break when the compiler is minified
* Updated generated docs and browser compiler
* Output block comments after function arguments
* Comments appear above scope `var` declarations; better tracking of generated `JS` tokens created only to shepherd comments through to the output
* Create new FuncGlyph node, to hold comments we want to output near the function parameters
* Block comments between `)` and `->`/`=>` get output between `)` and `{`.
* Fix indentation of comments that are the first line inside a bare mode block
* Updated output
* Full Flow example
* Updated browser compiler
* Abstract and organize comment fragment generation code; store more properties on the comment fragment objects; make `throw` behave like `return`
* Abstract token insertion code
* Add missing locationData to STRING_START token, giving it the locationData of the overall StringWithInterpolations token so that comments attached to STRING_START end up on the StringWithInterpolations node
* Allow `SUPER` tokens to carry comments
* Rescue comments from `Existence` nodes and `If` nodes’ conditions
* Rescue comments after `\` line continuation tokens
* Updated compiled output
* Updated browser compiler
* Output block comments in the same `compileFragments` method as line comments, except for inline block comments
* Comments before splice
* Updated browser compiler
* Track compiledComments as a property of Base, to ensure that it’s not a global variable
* Docs: split up the Usage section
* Docs for type annotations via Flow; updated docs output
* Update regular comments documentation
* Updated browser compiler
* Comments before soak
* Comments before static methods, and probably before `@variable =` (this) assignments generally
* Comments before ‘if exists?’, refactor comment before ‘if this.var’ to be more precise, improve helper methods
* Comments before a method that contains ‘super()’ should output above the method property, not above the ‘super.method()’ call
* Fix missing comments before `if not` (i.e. before a UNARY token)
* Fix comments before ‘for’; add test for comment before assignment if (fixed in earlier commit)
* Comments within heregexes
* Updated browser compiler
* Update description to reflect what’s now happening in compileCommentFragments
* Preserve blank lines between line comments; output “whitespace-only” line comments as blank lines, rather than `//` following by whitespace
* Better future-proof comments tests
* Comments before object destructuring; abstract method for setting comments aside before compilation
* Handle more cases of comments before or after `for` loop declaration lines
* Fix indentation of comments preceding `for` loops
* Fix comment before splat function parameter
* Catch another RegexWithInterpolations comment edge case
* Updated browser compiler
* Change heregex example to one that’s more readable; update output
* Remove a few last references to the defunct HERECOMMENT token
* Abstract location hash creation into a function
* Improved clarity per code review notes
* Updated browser compiler
2017-08-02 22:34:34 -04:00
|
|
|
|
# The argument should not be able to be referenced normally
|
Fix #1500, #1574, #3318: Name generated vars uniquely
Any variables generated by CoffeeScript are now made sure to be named to
something not present in the source code being compiled. This way you can no
longer interfere with them, either on purpose or by mistake. (#1500, #1574)
For example, `({a}, _arg) ->` now compiles correctly. (#1574)
As opposed to the somewhat complex implementations discussed in #1500, this
commit takes a very simple approach by saving all used variables names using a
single pass over the token stream. Any generated variables are then made sure
not to exist in that list.
`(@a) -> a` used to be equivalent to `(@a) -> @a`, but now throws a runtime
`ReferenceError` instead (unless `a` exists in an upper scope of course). (#3318)
`(@a) ->` used to compile to `(function(a) { this.a = a; })`. Now it compiles to
`(function(_at_a) { this.a = _at_a; })`. (But you cannot access `_at_a` either,
of course.)
Because of the above, `(@a, a) ->` is now valid; `@a` and `a` are not duplicate
parameters.
Duplicate this-parameters with a reserved word, such as `(@case, @case) ->`,
used to compile but now throws, just like regular duplicate parameters.
2015-01-10 17:04:30 -05:00
|
|
|
|
code = '((@prop) -> prop).call {}'
|
|
|
|
|
doesNotThrow -> CoffeeScript.compile code
|
|
|
|
|
throws (-> CoffeeScript.run code), ReferenceError
|
|
|
|
|
code = '((@prop) -> _at_prop).call {}'
|
|
|
|
|
doesNotThrow -> CoffeeScript.compile code
|
|
|
|
|
throws (-> CoffeeScript.run code), ReferenceError
|
2010-12-29 14:06:57 -05:00
|
|
|
|
|
|
|
|
|
test "@-parameters and splats with constructors", ->
|
|
|
|
|
a = {}
|
|
|
|
|
b = {}
|
|
|
|
|
class Klass
|
|
|
|
|
constructor: (@first, splat..., @last) ->
|
|
|
|
|
|
|
|
|
|
obj = new Klass a, 0, 0, b
|
|
|
|
|
eq a, obj.first
|
|
|
|
|
eq b, obj.last
|
|
|
|
|
|
2017-08-17 16:13:52 -04:00
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
|
class Klass
|
|
|
|
|
constructor: (@first, splat ..., @last) ->
|
|
|
|
|
|
|
|
|
|
obj = new Klass a, 0, 0, b
|
|
|
|
|
eq a, obj.first
|
|
|
|
|
eq b, obj.last
|
|
|
|
|
|
2010-12-29 14:06:57 -05:00
|
|
|
|
test "destructuring in function definition", ->
|
|
|
|
|
(([{a: [b], c}]...) ->
|
|
|
|
|
eq 1, b
|
|
|
|
|
eq 2, c
|
|
|
|
|
) {a: [1], c: 2}
|
|
|
|
|
|
2017-08-17 16:13:52 -04:00
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
|
(([{a: [b], c}] ...) ->
|
|
|
|
|
eq 1, b
|
|
|
|
|
eq 2, c
|
|
|
|
|
) {a: [1], c: 2}
|
|
|
|
|
|
2015-08-22 15:39:26 -04:00
|
|
|
|
context = {}
|
|
|
|
|
(([{a: [b, c = 2], @d, e = 4}]...) ->
|
|
|
|
|
eq 1, b
|
|
|
|
|
eq 2, c
|
|
|
|
|
eq @d, 3
|
|
|
|
|
eq context.d, 3
|
|
|
|
|
eq e, 4
|
|
|
|
|
).call context, {a: [1], d: 3}
|
|
|
|
|
|
2015-09-27 08:59:37 -04:00
|
|
|
|
(({a: aa = 1, b: bb = 2}) ->
|
|
|
|
|
eq 5, aa
|
|
|
|
|
eq 2, bb
|
|
|
|
|
) {a: 5}
|
|
|
|
|
|
2015-08-22 15:39:26 -04:00
|
|
|
|
ajax = (url, {
|
|
|
|
|
async = true,
|
|
|
|
|
beforeSend = (->),
|
|
|
|
|
cache = true,
|
|
|
|
|
method = 'get',
|
|
|
|
|
data = {}
|
|
|
|
|
}) ->
|
|
|
|
|
{url, async, beforeSend, cache, method, data}
|
|
|
|
|
|
|
|
|
|
fn = ->
|
2017-04-06 13:06:45 -04:00
|
|
|
|
deepEqual ajax('/home', beforeSend: fn, method: 'post'), {
|
2015-08-22 15:39:26 -04:00
|
|
|
|
url: '/home', async: true, beforeSend: fn, cache: true, method: 'post', data: {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test "#4005: `([a = {}]..., b) ->` weirdness", ->
|
|
|
|
|
fn = ([a = {}]..., b) -> [a, b]
|
|
|
|
|
deepEqual fn(5), [{}, 5]
|
|
|
|
|
|
2017-08-17 16:13:52 -04:00
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
|
fn = ([a = {}] ..., b) -> [a, b]
|
|
|
|
|
deepEqual fn(5), [{}, 5]
|
|
|
|
|
|
2010-12-29 14:06:57 -05:00
|
|
|
|
test "default values", ->
|
|
|
|
|
nonceA = {}
|
|
|
|
|
nonceB = {}
|
2012-01-09 12:56:18 -05:00
|
|
|
|
a = (_,_1,arg=nonceA) -> arg
|
2010-12-29 14:06:57 -05:00
|
|
|
|
eq nonceA, a()
|
|
|
|
|
eq nonceA, a(0)
|
|
|
|
|
eq nonceB, a(0,0,nonceB)
|
|
|
|
|
eq nonceA, a(0,0,undefined)
|
[CS2] Output ES2015 arrow functions, default parameters, rest parameters (#4311)
* Eliminate wrapper around “bound” (arrow) functions; output `=>` for such functions
* Remove irrelevant (and breaking) tests
* Minor cleanup
* When a function parameter is a splat (i.e., it uses the ES2015 rest parameter syntax) output that parameter as ES2015
* Rearrange function parameters when one of the parameters is a splat and isn’t the last parameter (very WIP)
* Handle params like `@param`, adding assignment expressions for them when they appear; ensure splat parameter is last
* Add parameter names (not a text like `'\nValue IdentifierLiteral: a'`) to the scope, so that parameters can’t be deleted; move body-related lines together; more explanation of what’s going on
* For parameters with a default value, correctly add the parameter name to the function scope
* Handle expansions in function parameters: when an expansion is found, set the parameters to only be the original parameters left of the expansion, then an `...args` parameter; and in the function body define variables for the parameters to the right of the expansion, including setting default values
* Handle splat parameters the same way we handle expansions: if a splat parameter is found, it becomes the last parameter in the function definition, and all following parameters get declared in the function body. Fix the splat/rest parameter values after the post-splat parameters have been extracted from it. Clean up `Code.compileNode` so that we loop through the parameters only once, and we create all expressions using calls like `new IdentifierLiteral` rather than `@makeCode`.
* Fix parameter name when a parameter is a splat attached to `this` (e.g. `@param...`)
* Rather than assigning post-splat parameters based on index, use slice; passes test “Functions with splats being called with too few arguments”
* Dial back our w00t indentation
* Better parsing of parameter names (WIP)
* Refactor processing of splat/expansion parameters
* Fix assignment of default parameters for parameters that come after a splat
* Better check for whether a param is attached to `this`
* More understandable variable names
* For parameters after a splat or expansion, assign them similar to the 1.x destructuring method of using `arguments`, except only concern ourselves with the post-splat parameters instead of all parameters; and use the splat/expansion parameter name, since `arguments` in ES fat arrow functions refers to the parent function’s `arguments` rather than the fat arrow function’s arguments/parameters
* Don’t add unnamed parameters (like `[]` as a parameter) to the function scope
* Disallow multiple splat/expansion parameters in function definitions; disallow lone expansion parameters
* Fix `this` params not getting assigned if the parameter is after a splat parameter
* Allow names of function parameters attached to `this` to be reserved words
* Always add a statement to the function body defining a variable with its default value, if it has one, if the variable `== null`; this covers the case when ES doesn’t apply the default value when `null` is passed in as a value, but CoffeeScript expects `null` and `undefined` to act interchangeably
* Aftermath of having both `undefined` and `null` trigger the use of default values for parameters with default values
* More careful parsing of destructured parameters
* Fall back to processing destructured parameters in the function body, to account for `this` or default values within destructured objects
* Clean up comments
* Restore new bare function test, minus the arrow function part of it
* Test that bound/arrow functions aren’t overwriting the `arguments` object, which should refer to the parent scope’s `arguments` (like `this`)
* Follow ES2015 spec for parameter default values: `null` gets assigned as as `null`, not the default value
* Mimic ES default parameters behavior for parameters after a splat or expansion parameter
* Bound functions cannot be generators: remove no-longer-relevant test, add check to throw error if `yield` appears inside a bound (arrow) function
* Error for bound generator functions should underline the `yield`
2016-10-26 01:26:13 -04:00
|
|
|
|
eq null, a(0,0,null) # Per ES2015, `null` doesn’t trigger a parameter default value
|
2010-12-29 14:06:57 -05:00
|
|
|
|
eq false , a(0,0,false)
|
|
|
|
|
eq nonceB, a(undefined,undefined,nonceB,undefined)
|
2012-01-09 12:56:18 -05:00
|
|
|
|
b = (_,arg=nonceA,_1,_2) -> arg
|
2010-12-29 14:06:57 -05:00
|
|
|
|
eq nonceA, b()
|
|
|
|
|
eq nonceA, b(0)
|
|
|
|
|
eq nonceB, b(0,nonceB)
|
|
|
|
|
eq nonceA, b(0,undefined)
|
[CS2] Output ES2015 arrow functions, default parameters, rest parameters (#4311)
* Eliminate wrapper around “bound” (arrow) functions; output `=>` for such functions
* Remove irrelevant (and breaking) tests
* Minor cleanup
* When a function parameter is a splat (i.e., it uses the ES2015 rest parameter syntax) output that parameter as ES2015
* Rearrange function parameters when one of the parameters is a splat and isn’t the last parameter (very WIP)
* Handle params like `@param`, adding assignment expressions for them when they appear; ensure splat parameter is last
* Add parameter names (not a text like `'\nValue IdentifierLiteral: a'`) to the scope, so that parameters can’t be deleted; move body-related lines together; more explanation of what’s going on
* For parameters with a default value, correctly add the parameter name to the function scope
* Handle expansions in function parameters: when an expansion is found, set the parameters to only be the original parameters left of the expansion, then an `...args` parameter; and in the function body define variables for the parameters to the right of the expansion, including setting default values
* Handle splat parameters the same way we handle expansions: if a splat parameter is found, it becomes the last parameter in the function definition, and all following parameters get declared in the function body. Fix the splat/rest parameter values after the post-splat parameters have been extracted from it. Clean up `Code.compileNode` so that we loop through the parameters only once, and we create all expressions using calls like `new IdentifierLiteral` rather than `@makeCode`.
* Fix parameter name when a parameter is a splat attached to `this` (e.g. `@param...`)
* Rather than assigning post-splat parameters based on index, use slice; passes test “Functions with splats being called with too few arguments”
* Dial back our w00t indentation
* Better parsing of parameter names (WIP)
* Refactor processing of splat/expansion parameters
* Fix assignment of default parameters for parameters that come after a splat
* Better check for whether a param is attached to `this`
* More understandable variable names
* For parameters after a splat or expansion, assign them similar to the 1.x destructuring method of using `arguments`, except only concern ourselves with the post-splat parameters instead of all parameters; and use the splat/expansion parameter name, since `arguments` in ES fat arrow functions refers to the parent function’s `arguments` rather than the fat arrow function’s arguments/parameters
* Don’t add unnamed parameters (like `[]` as a parameter) to the function scope
* Disallow multiple splat/expansion parameters in function definitions; disallow lone expansion parameters
* Fix `this` params not getting assigned if the parameter is after a splat parameter
* Allow names of function parameters attached to `this` to be reserved words
* Always add a statement to the function body defining a variable with its default value, if it has one, if the variable `== null`; this covers the case when ES doesn’t apply the default value when `null` is passed in as a value, but CoffeeScript expects `null` and `undefined` to act interchangeably
* Aftermath of having both `undefined` and `null` trigger the use of default values for parameters with default values
* More careful parsing of destructured parameters
* Fall back to processing destructured parameters in the function body, to account for `this` or default values within destructured objects
* Clean up comments
* Restore new bare function test, minus the arrow function part of it
* Test that bound/arrow functions aren’t overwriting the `arguments` object, which should refer to the parent scope’s `arguments` (like `this`)
* Follow ES2015 spec for parameter default values: `null` gets assigned as as `null`, not the default value
* Mimic ES default parameters behavior for parameters after a splat or expansion parameter
* Bound functions cannot be generators: remove no-longer-relevant test, add check to throw error if `yield` appears inside a bound (arrow) function
* Error for bound generator functions should underline the `yield`
2016-10-26 01:26:13 -04:00
|
|
|
|
eq null, b(0,null)
|
2010-12-29 14:06:57 -05:00
|
|
|
|
eq false , b(0,false)
|
|
|
|
|
eq nonceB, b(undefined,nonceB,undefined)
|
2012-01-09 12:56:18 -05:00
|
|
|
|
c = (arg=nonceA,_,_1) -> arg
|
2010-12-29 14:06:57 -05:00
|
|
|
|
eq nonceA, c()
|
|
|
|
|
eq 0, c(0)
|
|
|
|
|
eq nonceB, c(nonceB)
|
|
|
|
|
eq nonceA, c(undefined)
|
[CS2] Output ES2015 arrow functions, default parameters, rest parameters (#4311)
* Eliminate wrapper around “bound” (arrow) functions; output `=>` for such functions
* Remove irrelevant (and breaking) tests
* Minor cleanup
* When a function parameter is a splat (i.e., it uses the ES2015 rest parameter syntax) output that parameter as ES2015
* Rearrange function parameters when one of the parameters is a splat and isn’t the last parameter (very WIP)
* Handle params like `@param`, adding assignment expressions for them when they appear; ensure splat parameter is last
* Add parameter names (not a text like `'\nValue IdentifierLiteral: a'`) to the scope, so that parameters can’t be deleted; move body-related lines together; more explanation of what’s going on
* For parameters with a default value, correctly add the parameter name to the function scope
* Handle expansions in function parameters: when an expansion is found, set the parameters to only be the original parameters left of the expansion, then an `...args` parameter; and in the function body define variables for the parameters to the right of the expansion, including setting default values
* Handle splat parameters the same way we handle expansions: if a splat parameter is found, it becomes the last parameter in the function definition, and all following parameters get declared in the function body. Fix the splat/rest parameter values after the post-splat parameters have been extracted from it. Clean up `Code.compileNode` so that we loop through the parameters only once, and we create all expressions using calls like `new IdentifierLiteral` rather than `@makeCode`.
* Fix parameter name when a parameter is a splat attached to `this` (e.g. `@param...`)
* Rather than assigning post-splat parameters based on index, use slice; passes test “Functions with splats being called with too few arguments”
* Dial back our w00t indentation
* Better parsing of parameter names (WIP)
* Refactor processing of splat/expansion parameters
* Fix assignment of default parameters for parameters that come after a splat
* Better check for whether a param is attached to `this`
* More understandable variable names
* For parameters after a splat or expansion, assign them similar to the 1.x destructuring method of using `arguments`, except only concern ourselves with the post-splat parameters instead of all parameters; and use the splat/expansion parameter name, since `arguments` in ES fat arrow functions refers to the parent function’s `arguments` rather than the fat arrow function’s arguments/parameters
* Don’t add unnamed parameters (like `[]` as a parameter) to the function scope
* Disallow multiple splat/expansion parameters in function definitions; disallow lone expansion parameters
* Fix `this` params not getting assigned if the parameter is after a splat parameter
* Allow names of function parameters attached to `this` to be reserved words
* Always add a statement to the function body defining a variable with its default value, if it has one, if the variable `== null`; this covers the case when ES doesn’t apply the default value when `null` is passed in as a value, but CoffeeScript expects `null` and `undefined` to act interchangeably
* Aftermath of having both `undefined` and `null` trigger the use of default values for parameters with default values
* More careful parsing of destructured parameters
* Fall back to processing destructured parameters in the function body, to account for `this` or default values within destructured objects
* Clean up comments
* Restore new bare function test, minus the arrow function part of it
* Test that bound/arrow functions aren’t overwriting the `arguments` object, which should refer to the parent scope’s `arguments` (like `this`)
* Follow ES2015 spec for parameter default values: `null` gets assigned as as `null`, not the default value
* Mimic ES default parameters behavior for parameters after a splat or expansion parameter
* Bound functions cannot be generators: remove no-longer-relevant test, add check to throw error if `yield` appears inside a bound (arrow) function
* Error for bound generator functions should underline the `yield`
2016-10-26 01:26:13 -04:00
|
|
|
|
eq null, c(null)
|
2010-12-29 14:06:57 -05:00
|
|
|
|
eq false , c(false)
|
|
|
|
|
eq nonceB, c(nonceB,undefined,undefined)
|
|
|
|
|
|
|
|
|
|
test "default values with @-parameters", ->
|
|
|
|
|
a = {}
|
|
|
|
|
b = {}
|
|
|
|
|
obj = f: (q = a, @p = b) -> q
|
|
|
|
|
eq a, obj.f()
|
|
|
|
|
eq b, obj.p
|
|
|
|
|
|
|
|
|
|
test "default values with splatted arguments", ->
|
|
|
|
|
withSplats = (a = 2, b..., c = 3, d = 5) -> a * (b.length + 1) * c * d
|
|
|
|
|
eq 30, withSplats()
|
|
|
|
|
eq 15, withSplats(1)
|
|
|
|
|
eq 5, withSplats(1,1)
|
|
|
|
|
eq 1, withSplats(1,1,1)
|
|
|
|
|
eq 2, withSplats(1,1,1,1)
|
2011-01-24 01:57:31 -05:00
|
|
|
|
|
2017-08-17 16:13:52 -04:00
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
|
withSplats = (a = 2, b ..., c = 3, d = 5) -> a * (b.length + 1) * c * d
|
|
|
|
|
eq 30, withSplats()
|
|
|
|
|
eq 15, withSplats(1)
|
|
|
|
|
eq 5, withSplats(1,1)
|
|
|
|
|
eq 1, withSplats(1,1,1)
|
|
|
|
|
eq 2, withSplats(1,1,1,1)
|
|
|
|
|
|
2014-01-24 11:00:34 -05:00
|
|
|
|
test "#156: parameter lists with expansion", ->
|
|
|
|
|
expandArguments = (first, ..., lastButOne, last) ->
|
|
|
|
|
eq 1, first
|
|
|
|
|
eq 4, lastButOne
|
|
|
|
|
last
|
|
|
|
|
eq 5, expandArguments 1, 2, 3, 4, 5
|
|
|
|
|
|
|
|
|
|
throws (-> CoffeeScript.compile "(..., a, b...) ->"), null, "prohibit expansion and a splat"
|
|
|
|
|
throws (-> CoffeeScript.compile "(...) ->"), null, "prohibit lone expansion"
|
|
|
|
|
|
|
|
|
|
test "#156: parameter lists with expansion in array destructuring", ->
|
|
|
|
|
expandArray = (..., [..., last]) ->
|
|
|
|
|
last
|
|
|
|
|
eq 3, expandArray 1, 2, 3, [1, 2, 3]
|
|
|
|
|
|
2015-01-13 14:54:13 -05:00
|
|
|
|
test "#3502: variable definitions and expansion", ->
|
|
|
|
|
a = b = 0
|
|
|
|
|
f = (a, ..., b) -> [a, b]
|
|
|
|
|
arrayEq [1, 5], f 1, 2, 3, 4, 5
|
|
|
|
|
eq 0, a
|
|
|
|
|
eq 0, b
|
|
|
|
|
|
|
|
|
|
test "variable definitions and splat", ->
|
|
|
|
|
a = b = 0
|
|
|
|
|
f = (a, middle..., b) -> [a, middle, b]
|
|
|
|
|
arrayEq [1, [2, 3, 4], 5], f 1, 2, 3, 4, 5
|
|
|
|
|
eq 0, a
|
|
|
|
|
eq 0, b
|
|
|
|
|
|
2017-08-17 16:13:52 -04:00
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
|
f = (a, middle ..., b) -> [a, middle, b]
|
|
|
|
|
arrayEq [1, [2, 3, 4], 5], f 1, 2, 3, 4, 5
|
|
|
|
|
eq 0, a
|
|
|
|
|
eq 0, b
|
|
|
|
|
|
2011-02-22 19:20:01 -05:00
|
|
|
|
test "default values with function calls", ->
|
|
|
|
|
doesNotThrow -> CoffeeScript.compile "(x = f()) ->"
|
|
|
|
|
|
2011-01-24 01:57:31 -05:00
|
|
|
|
test "arguments vs parameters", ->
|
|
|
|
|
doesNotThrow -> CoffeeScript.compile "f(x) ->"
|
|
|
|
|
f = (g) -> g()
|
|
|
|
|
eq 5, f (x) -> 5
|
2011-11-10 03:08:38 -05:00
|
|
|
|
|
2014-12-07 18:34:30 -05:00
|
|
|
|
test "reserved keyword as parameters", ->
|
|
|
|
|
f = (_case, @case) -> [_case, @case]
|
|
|
|
|
[a, b] = f(1, 2)
|
|
|
|
|
eq 1, a
|
|
|
|
|
eq 2, b
|
|
|
|
|
|
|
|
|
|
f = (@case, _case...) -> [@case, _case...]
|
|
|
|
|
[a, b, c] = f(1, 2, 3)
|
|
|
|
|
eq 1, a
|
|
|
|
|
eq 2, b
|
|
|
|
|
eq 3, c
|
|
|
|
|
|
Fix #1500, #1574, #3318: Name generated vars uniquely
Any variables generated by CoffeeScript are now made sure to be named to
something not present in the source code being compiled. This way you can no
longer interfere with them, either on purpose or by mistake. (#1500, #1574)
For example, `({a}, _arg) ->` now compiles correctly. (#1574)
As opposed to the somewhat complex implementations discussed in #1500, this
commit takes a very simple approach by saving all used variables names using a
single pass over the token stream. Any generated variables are then made sure
not to exist in that list.
`(@a) -> a` used to be equivalent to `(@a) -> @a`, but now throws a runtime
`ReferenceError` instead (unless `a` exists in an upper scope of course). (#3318)
`(@a) ->` used to compile to `(function(a) { this.a = a; })`. Now it compiles to
`(function(_at_a) { this.a = _at_a; })`. (But you cannot access `_at_a` either,
of course.)
Because of the above, `(@a, a) ->` is now valid; `@a` and `a` are not duplicate
parameters.
Duplicate this-parameters with a reserved word, such as `(@case, @case) ->`,
used to compile but now throws, just like regular duplicate parameters.
2015-01-10 17:04:30 -05:00
|
|
|
|
test "reserved keyword at-splat", ->
|
|
|
|
|
f = (@case...) -> @case
|
|
|
|
|
[a, b] = f(1, 2)
|
|
|
|
|
eq 1, a
|
|
|
|
|
eq 2, b
|
|
|
|
|
|
2017-08-17 16:13:52 -04:00
|
|
|
|
# Should not trigger implicit call, e.g. rest ... => rest(...)
|
|
|
|
|
f = (@case ...) -> @case
|
|
|
|
|
[a, b] = f(1, 2)
|
|
|
|
|
eq 1, a
|
|
|
|
|
eq 2, b
|
|
|
|
|
|
Fix #1500, #1574, #3318: Name generated vars uniquely
Any variables generated by CoffeeScript are now made sure to be named to
something not present in the source code being compiled. This way you can no
longer interfere with them, either on purpose or by mistake. (#1500, #1574)
For example, `({a}, _arg) ->` now compiles correctly. (#1574)
As opposed to the somewhat complex implementations discussed in #1500, this
commit takes a very simple approach by saving all used variables names using a
single pass over the token stream. Any generated variables are then made sure
not to exist in that list.
`(@a) -> a` used to be equivalent to `(@a) -> @a`, but now throws a runtime
`ReferenceError` instead (unless `a` exists in an upper scope of course). (#3318)
`(@a) ->` used to compile to `(function(a) { this.a = a; })`. Now it compiles to
`(function(_at_a) { this.a = _at_a; })`. (But you cannot access `_at_a` either,
of course.)
Because of the above, `(@a, a) ->` is now valid; `@a` and `a` are not duplicate
parameters.
Duplicate this-parameters with a reserved word, such as `(@case, @case) ->`,
used to compile but now throws, just like regular duplicate parameters.
2015-01-10 17:04:30 -05:00
|
|
|
|
test "#1574: Destructuring and a parameter named _arg", ->
|
|
|
|
|
f = ({a, b}, _arg, _arg1) -> [a, b, _arg, _arg1]
|
|
|
|
|
arrayEq [1, 2, 3, 4], f a: 1, b: 2, 3, 4
|
|
|
|
|
|
2011-11-10 03:08:38 -05:00
|
|
|
|
test "#1844: bound functions in nested comprehensions causing empty var statements", ->
|
|
|
|
|
a = ((=>) for a in [0] for b in [0])
|
|
|
|
|
eq 1, a.length
|
2011-12-24 07:04:34 -05:00
|
|
|
|
|
2011-12-18 12:55:21 -05:00
|
|
|
|
test "#1859: inline function bodies shouldn't modify prior postfix ifs", ->
|
|
|
|
|
list = [1, 2, 3]
|
|
|
|
|
ok true if list.some (x) -> x is 2
|
2012-04-24 16:56:39 -04:00
|
|
|
|
|
|
|
|
|
test "#2258: allow whitespace-style parameter lists in function definitions", ->
|
|
|
|
|
func = (
|
|
|
|
|
a, b, c
|
|
|
|
|
) -> c
|
|
|
|
|
eq func(1, 2, 3), 3
|
2013-01-05 23:32:57 -05:00
|
|
|
|
|
2012-04-24 16:56:39 -04:00
|
|
|
|
func = (
|
|
|
|
|
a
|
|
|
|
|
b
|
|
|
|
|
c
|
|
|
|
|
) -> b
|
2013-01-05 23:32:57 -05:00
|
|
|
|
eq func(1, 2, 3), 2
|
|
|
|
|
|
|
|
|
|
test "#2621: fancy destructuring in parameter lists", ->
|
|
|
|
|
func = ({ prop1: { key1 }, prop2: { key2, key3: [a, b, c] } }) ->
|
|
|
|
|
eq(key2, 'key2')
|
|
|
|
|
eq(a, 'a')
|
|
|
|
|
|
|
|
|
|
func({prop1: {key1: 'key1'}, prop2: {key2: 'key2', key3: ['a', 'b', 'c']}})
|
2013-01-18 09:08:42 -05:00
|
|
|
|
|
|
|
|
|
test "#1435 Indented property access", ->
|
|
|
|
|
rec = -> rec: rec
|
|
|
|
|
|
|
|
|
|
eq 1, do ->
|
|
|
|
|
rec()
|
|
|
|
|
.rec ->
|
|
|
|
|
rec()
|
|
|
|
|
.rec ->
|
|
|
|
|
rec.rec()
|
|
|
|
|
.rec()
|
|
|
|
|
1
|
2014-02-06 09:07:43 -05:00
|
|
|
|
|
|
|
|
|
test "#1038 Optimize trailing return statements", ->
|
|
|
|
|
compile = (code) -> CoffeeScript.compile(code, bare: yes).trim().replace(/\s+/g, " ")
|
|
|
|
|
|
|
|
|
|
eq "(function() {});", compile("->")
|
|
|
|
|
eq "(function() {});", compile("-> return")
|
|
|
|
|
eq "(function() { return void 0; });", compile("-> undefined")
|
|
|
|
|
eq "(function() { return void 0; });", compile("-> return undefined")
|
|
|
|
|
eq "(function() { foo(); });", compile("""
|
|
|
|
|
->
|
|
|
|
|
foo()
|
|
|
|
|
return
|
|
|
|
|
""")
|
2017-01-22 07:40:33 -05:00
|
|
|
|
|
2017-02-01 09:54:42 -05:00
|
|
|
|
test "#4406 Destructured parameter default evaluation order with incrementing variable", ->
|
|
|
|
|
i = 0
|
|
|
|
|
f = ({ a = ++i }, b = ++i) -> [a, b]
|
|
|
|
|
arrayEq f({}), [1, 2]
|
|
|
|
|
|
|
|
|
|
test "#4406 Destructured parameter default evaluation order with generator function", ->
|
2017-01-22 07:40:33 -05:00
|
|
|
|
current = 0
|
|
|
|
|
next = -> ++current
|
|
|
|
|
foo = ({ a = next() }, b = next()) -> [ a, b ]
|
|
|
|
|
arrayEq foo({}), [1, 2]
|
2017-04-06 13:06:45 -04:00
|
|
|
|
|
|
|
|
|
test "Destructured parameter with default value, that itself has a default value", ->
|
|
|
|
|
# Adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
|
|
|
|
|
draw = ({size = 'big', coords = {x: 0, y: 0}, radius = 25} = {}) -> "#{size}-#{coords.x}-#{coords.y}-#{radius}"
|
|
|
|
|
output = draw
|
|
|
|
|
coords:
|
|
|
|
|
x: 18
|
|
|
|
|
y: 30
|
|
|
|
|
radius: 30
|
|
|
|
|
eq output, 'big-18-30-30'
|
2017-06-21 00:53:37 -04:00
|
|
|
|
|
|
|
|
|
test "#4566: destructuring with nested default values", ->
|
|
|
|
|
f = ({a: {b = 1}}) ->
|
|
|
|
|
b
|
|
|
|
|
eq 2, f a: b: 2
|
2017-06-26 02:46:54 -04:00
|
|
|
|
|
|
|
|
|
test "#1043: comma after function glyph", ->
|
|
|
|
|
x = (a=->, b=2) ->
|
|
|
|
|
a()
|
|
|
|
|
eq x(), undefined
|
|
|
|
|
|
|
|
|
|
f = (a) -> a()
|
|
|
|
|
g = f ->, 2
|
|
|
|
|
eq g, undefined
|
|
|
|
|
h = f(=>, 2)
|
|
|
|
|
eq h, undefined
|
|
|
|
|
|
|
|
|
|
test "#3845/#3446: chain after function glyph", ->
|
|
|
|
|
angular = module: -> controller: -> controller: ->
|
|
|
|
|
|
|
|
|
|
eq undefined,
|
|
|
|
|
angular.module 'foo'
|
|
|
|
|
.controller 'EmailLoginCtrl', ->
|
|
|
|
|
.controller 'EmailSignupCtrl', ->
|
|
|
|
|
|
|
|
|
|
beforeEach = (f) -> f()
|
|
|
|
|
getPromise = -> then: -> catch: ->
|
|
|
|
|
|
|
|
|
|
eq undefined,
|
|
|
|
|
beforeEach ->
|
|
|
|
|
getPromise()
|
|
|
|
|
.then (@result) =>
|
|
|
|
|
.catch (@error) =>
|
|
|
|
|
|
|
|
|
|
doThing = -> then: -> catch: (f) -> f()
|
|
|
|
|
handleError = -> 3
|
|
|
|
|
eq 3,
|
|
|
|
|
doThing()
|
|
|
|
|
.then (@result) =>
|
|
|
|
|
.catch handleError
|
2017-08-13 22:56:58 -04:00
|
|
|
|
|
|
|
|
|
test "#4413: expressions in function parameters that create generated variables have those variables declared correctly", ->
|
|
|
|
|
'use strict'
|
|
|
|
|
# We’re in strict mode because we want an error to be thrown if the generated
|
|
|
|
|
# variable (`ref`) is assigned before being declared.
|
|
|
|
|
foo = -> null
|
|
|
|
|
bar = -> 33
|
|
|
|
|
f = (a = foo() ? bar()) -> a
|
2017-08-13 23:06:38 -04:00
|
|
|
|
g = (a = foo() ? bar()) -> a + 1
|
2017-08-13 22:56:58 -04:00
|
|
|
|
eq f(), 33
|
2017-08-13 23:06:38 -04:00
|
|
|
|
eq g(), 34
|
2017-09-07 13:06:35 -04:00
|
|
|
|
|
|
|
|
|
test "#4657: destructured array param declarations", ->
|
|
|
|
|
a = 1
|
|
|
|
|
b = 2
|
|
|
|
|
f = ([a..., b]) ->
|
|
|
|
|
f [3, 4, 5]
|
|
|
|
|
eq a, 1
|
|
|
|
|
eq b, 2
|
|
|
|
|
|
|
|
|
|
test "#4657: destructured array parameters", ->
|
|
|
|
|
f = ([a..., b]) -> {a, b}
|
|
|
|
|
result = f [1, 2, 3, 4]
|
|
|
|
|
arrayEq result.a, [1, 2, 3]
|
|
|
|
|
eq result.b, 4
|
2019-03-14 21:14:35 -04:00
|
|
|
|
|
|
|
|
|
test "#5128: default parameters of function in binary operation", ->
|
|
|
|
|
foo = yes or (a, b = {}) -> null
|
|
|
|
|
eq foo, yes
|