-
Notifications
You must be signed in to change notification settings - Fork 2k
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
([a = {}]..., b) ->
does not define a
#4005
Comments
I'm sort of amazed that this compiles. What is it trying to do, exactly? |
Get the first argument and default it to empty object, and get the last argument. Kinda cheesy... whatever = ( a = {}, ..., b )->
console.log a |
@metalim Nice solution. |
While ECMAScript 2015 does, CoffeeScript does not support default values for destructuring assignment. |
The problem is not whether or not it should be supported. |
This let's you do things like: fullName = ({first = 'John', last = 'Doe'}) -> "#{first} #{last}" Note: CoffeeScrits treats `undefined` and `null` the same, and that's true in the case of destructuring defaults as well, as opposed to ES2015 which only uses the default value if the target is `undefined`. A similar ES2015 difference already exists for function parameter defaults. It is important for CoffeeScript to be consistent with itself. fullName2 = (first = 'John', last = 'Doe') -> "#{first} #{last}" assert fullName('Bob', null) is fullName2(first: 'Bob', last: null) Fixes jashkenas#1558, jashkenas#3288 and jashkenas#4005. A test that lone expansions `[...]=a` are prohibited failed with this commit. However, the error message used to be really weird and shows that the test just _happened_ to pass: $ coffee -bpe '[...]=a' [stdin]:1:2: error: Expansion must be used inside a destructuring assignment or parameter list [...]=a ^^^ That _is_ inside a destructuring assignment. Since `[]=a` is allowed, I figured the easiest and most consistent way would be to make `[...]=a` equivalent to `[]=a`.
This let's you do things like: fullName = ({first = 'John', last = 'Doe'}) -> "#{first} #{last}" Note: CoffeeScrits treats `undefined` and `null` the same, and that's true in the case of destructuring defaults as well, as opposed to ES2015 which only uses the default value if the target is `undefined`. A similar ES2015 difference already exists for function parameter defaults. It is important for CoffeeScript to be consistent with itself. fullName2 = (first = 'John', last = 'Doe') -> "#{first} #{last}" assert fullName('Bob', null) is fullName2(first: 'Bob', last: null) Fixes jashkenas#1558, jashkenas#3288 and jashkenas#4005. A test that lone expansions `[...]=a` are prohibited failed with this commit. However, the error message used to be really weird and shows that the test just _happened_ to pass: $ coffee -bpe '[...]=a' [stdin]:1:2: error: Expansion must be used inside a destructuring assignment or parameter list [...]=a ^^^ That _is_ inside a destructuring assignment. Since `[]=a` is allowed, I figured the easiest and most consistent way would be to make `[...]=a` equivalent to `[]=a`.
This let's you do things like: fullName = ({first = 'John', last = 'Doe'}) -> "#{first} #{last}" Note: CoffeeScrits treats `undefined` and `null` the same, and that's true in the case of destructuring defaults as well, as opposed to ES2015 which only uses the default value if the target is `undefined`. A similar ES2015 difference already exists for function parameter defaults. It is important for CoffeeScript to be consistent with itself. fullName2 = (first = 'John', last = 'Doe') -> "#{first} #{last}" assert fullName('Bob', null) is fullName2(first: 'Bob', last: null) Fixes jashkenas#1558, jashkenas#3288 and jashkenas#4005.
This let's you do things like: fullName = ({first = 'John', last = 'Doe'}) -> "#{first} #{last}" Note: CoffeeScrits treats `undefined` and `null` the same, and that's true in the case of destructuring defaults as well, as opposed to ES2015 which only uses the default value if the target is `undefined`. A similar ES2015 difference already exists for function parameter defaults. It is important for CoffeeScript to be consistent with itself. fullName2 = (first = 'John', last = 'Doe') -> "#{first} #{last}" assert fullName('Bob', null) is fullName2(first: 'Bob', last: null) Fixes jashkenas#1558, jashkenas#3288 and jashkenas#4005.
This let's you do things like: fullName = ({first = 'John', last = 'Doe'}) -> "#{first} #{last}" Note: CoffeeScrits treats `undefined` and `null` the same, and that's true in the case of destructuring defaults as well, as opposed to ES2015 which only uses the default value if the target is `undefined`. A similar ES2015 difference already exists for function parameter defaults. It is important for CoffeeScript to be consistent with itself. fullName2 = (first = 'John', last = 'Doe') -> "#{first} #{last}" assert fullName('Bob', null) is fullName2(first: 'Bob', last: null) Fixes jashkenas#1558, jashkenas#3288 and jashkenas#4005.
Fixed by #4069. |
… to the function body with `var` declarations; treat splat array function parameters the legacy way to avoid rethinking jashkenas#4005
* Output simple array destructuring assignments to ES2015 * Output simple object destructured assignments to ES2015 * Compile shorthand object properties to ES2015 shorthand properties This dramatically improves the appearance of destructured imports. * Compile default values in destructured assignment to ES2015 * Rename `wrapInBraces` to `wrapInParentheses`, and `compilePatternMatch` to `compileDestructuring`, for clarity; style improvements (no `==` or `!=`, etc.) * Don’t confuse the syntax highlighter * Comment Assign::compilePatternMatch a bit * Assignment expressions in conditionals are a bad practice * Optional check for existence that only checks `!== undefined`, not `!= null`, to follow ES convention that default values only apply when a variable is undefined, not falsy * Add comments; remove unnecessary array splats in function tests * The fallback destructuring code should apply default values only if `undefined`, not falsy, to follow ES spec * Support destructuring in function parameters (first pass); catch destructured reserved words * Destructured variables in function parameter lists shouldn’t be added to the function body with `var` declarations; treat splat array function parameters the legacy way to avoid rethinking #4005 * Remove redundancy in undefined-only check for existence; fix passing option to check * Fix undefined redundancy * Simplify getting the variable name * Reimplement “check for existence if not undefined” without creating a new operator * `Obj::isAssignable` should not mutate; pass `lhs` property in from `Assign` or `Code` to child arrays and objects so that those child nodes are set as allowable for destructuring * Revert changes to tests * Restore revised test for empty destructuring assignment
With both 1.8.0 and 1.9.3, running
will produce
ReferenceError: a is not defined
.If the default assignment
= {}
is removed,a
is defined correctly.The text was updated successfully, but these errors were encountered: