Comments (8)
Definitely using the issue tracker -- this is the right place.
For future reference, you can load "coffee-script" like any other Narwhal module. To check what a function compiles as from within the REPL, you can do this:
cs> coffee = require('coffee-script')
[object Object]
cs> coffee.compile("(square: x => x * x.)(4)")
square = function(x) {
return x * x;
}(4);
In the REPL, things compile a little differently, without "var" keywords at the top level, or the function wrapper, in order to add every input line to global scope by default.
If you want to quickly check compilation, I'd use coffee-script -e "(square: x => x * x.)(4)"
Unfortunately, your problem is a little bit deeper than at first glance. Your parenthesis are getting ignored because they contain an assignment, which is considered to be a statement (because it might have to declare "var", which would be a syntax error in JS). You can't do (var square = function ... )
. It would be great if we could fix this in CoffeeScript by pulling out the variable declaration outside the parenthesis, but the code generation doesn't support that at this point. It might also be better to throw a syntax error when parenthesis enclose a statement.
If you have ideas for how best to fix it, I'm all ears. Perhaps we can special case it, just for assignment, in the same manner as custom_return and custom_assign.
from coffeescript.
You're right this does get a little messy.
Off the top of my head, here is the best I can think up for special casing it:
js> coffee.compile("(square: x * x.)(4)")
var square;
(square = function (x) {
return x * x;
})(4);
What do you think of that?
from coffeescript.
Yeah, that would be ideal, but it's going to get complicated. Assignment can't be used as an expression in the general case unless the variable is already declared and it's wrapped in parenthesis. Take this case:
1 + 2 + (func: => 3.)()
Right now it compiles into a JS syntax error:
1 + 2 + var func = function() {
return 3;
}();
Ideally, it would compile into this:
var func;
1 + 2 + (func = function() {
return 3;
})();
In order to accomplish this, the inner assignment would have to push back the variable declaration through the previous ParenNode and OpNode, to tuck it in as the expression prior to the current one. Either backtracking or a multi-pass compiler is required for that (I think). Neither of which we have at the moment.
Maybe something with throw :declaration, and catching it at the Expressions level....
from coffeescript.
I would say just throw an exception then. This is pretty edge case, and its not like CoffeeScript has to match JS one-to-one for every little detail.
Maybe this can be revisited after more important things have been completed? I think there definitely should be some type of alert to the user about the current ambiguities if they try to compile something like this, though.
from coffeescript.
Sure, you got it. I'll throw an exception if a parenthesis wraps a statement, instead of swallowing it silently. And leave this ticket open as an enhancement for future reference. If we could push back the declaration (ensuring it gets declared in proper scope), then all assignment could be used as an expression, which would be pretty cool.
from coffeescript.
Idea!
Instead of pushing back the "var" declaration, can we push it forward? I know that JS looks ahead for all vars declared and named functions (not "var foo = function ..." but "function foo () ....") and will actually read those first before executing any code. Thats why you can define functions inside other functions after the return! Very wierd.
Check out this code I just ran through firebug's console:
(function () {
alert(1 + 2 + (foo = 3) + 4);
alert(foo);
alert(window.foo);
var foo;
})();
Does this make it easier?
from coffeescript.
Thanks for pushing this issue. It made a real positive change to the language. I just pushed a commit that pulls out all variable declarations to the top of the current scope, allowing assignment to act as an expression. Pull from master and give it a try:
six: (one: 1) + (two: 2) + (three: 3)
Compiles into:
var one, six, three, two;
six = (one = 1) + (two = 2) + (three = 3);
Your original example:
(square: x => x * x.)(4)
Compiles into:
var square;
(square = function(x) {
return x * x;
})(4);
As it turns out, we already had the Scope object handy, which knew exactly which variables were declared inside of it (as opposed to being declared within an external scope). This commit has the changes:
http://github.com/jashkenas/coffee-script/commit/9adf2e2d306bcd36eebd9e162adf6e30a374b9c8
Closing the ticket...
from coffeescript.
Don't thank me, I didn't do anything. Thank you!
from coffeescript.
Related Issues (20)
- Bug: Chained comparison doesn't work with `in` binary operator HOT 1
- Bug?: Cannot Base64 encode value: 0 (Legacy browser/WSH) HOT 2
- Unnecessary `splice` ref added for Array destructuring with rest element not in last position HOT 4
- Bug: Re-ordered nested non-end BindingRestElement doesn't get transpiled HOT 1
- CoffeeScript is fantastic, please donβt give it up HOT 1
- How to imitate `let` behavior in loops? HOT 5
- Proposal: Alternative file extension HOT 1
- Bug: Excessive variable and shallow copy for leading or middle rest parameter
- Proposal: Introduce `let` statement. HOT 6
- Proposal: Document Existential Operator Assignment
- Site issue: code blocks twitch on hover HOT 2
- Proposal: cake command should support ES6 modules HOT 2
- Need help understanding class member meanings HOT 1
- CLI `npm` `scripts` and input `.coffee` file/s as last argument conflicting with `--watch` HOT 2
- Bug: wrong code is transpiled for function call without parentheses HOT 2
- Proposal: Add end word to close method or class HOT 2
- Bug: Invalid indentation allowed after `do`
- Bug: Remove checkShebangLine multi arguments check HOT 1
- feature_request(html): backend CoffeeScript compilation inside HTML files HOT 3
- Bug: yield cannot be used in do -> expressions reliably? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coffeescript.