Repository navigation
'it' is not defined when referenced only by object literals? #899
Description
Activity
More complicated than just the object literal, it's something to do with the shorthand.
-> { name: it.name }compiles to =>
(function(it){ return { name: it.name }; });
Similarly,
-> { it }compiles to =>
(function(){ return { it: it }; });
From code readibility point of view I don't like
t = -> (cb) -> itimplyingitin the outermost func. IMHOitis intended for short and simple expression lambdas (that cannot be expressed with the more concise partially-applied operator syntax), not any arrow function that has a single param, and certainly not across function scopes.I wish we could:
- deliberately limit
itinference to direct function scope - emit warning/error on nested
itfunctions e.g.t = -> alert it; do -> alert itor example above.
Reacted by Ryan Hendrickson and Derek Shih- deliberately limit
My reading of the
itfeature (which, modulo this bug, seems to agree with lsc) is that it always refers to the first argument of the innermost function; so-> (cb) -> itshould haveit == cb. A warning in cases like this might be nice, but should only be reserved for cases where it's very likely the programmer is confused, IMO.(I have a fix for this bug that's part of a slightly larger unit of work; I'll be PRing it one of these days, either with that work or separately.)
Reacted by Yin Zhong@rhendric : I beg to differ.
it === cbis aliasing. You already have a name for the first parameter of the inner arrow function; it is confusing to have a second implicit name for it. It does avoid the cross-scope problem (i.e.itis always local function scope).My understanding (which of course does not agree with LSC master) had been:
the unnamed implicit single argument of the innermost function without an explicit parameter list
i.e.
-> (cb) -> itcompiles to(it) -> (cb) -> it. Note that I'm not advocating for this, nor do I write code like this -- I am trying to point out to part of the confusion.As for a stricter syntax, I believe we should emit warning/error when:
itis found in a function with an explicit parameter list but not assigned anywhere in the lexical scope (honestly I would even remove the "but" clause to make it stricter)- a function that contains implicit
itis nested in a function that also contains implicitit
Regardless I'm looking forward to your big chunk of change ;)
Upon revisiting this, it appears that I had entirely imagined this aliasing feature! I thought that was how things worked already in some cases, but I'm quite happy that it isn't. For the record, I retract my ‘should’ above:
-> (cb) -> itis already compiling exactly how I think it should (plus or minus a warning/error), with theitassumed to be a variable in an outer scope or on the global object.I'll hold further opining on issuing warnings or errors for possibly confusing uses of
ituntil that proposal gets its own issue. 😃name: it.nameparses to:Obj Prop Key name Chain Var it Index . Key name{it.name}compiles to the same, but parses differently:Obj Prop Key name Chain Key it Index . Key nameitshould beVar, but the parser givesKey. Maybe we should convert the shorthand to the same AST?thatalso does the same, as expected.
data = {that.name} if resultcompiles to:if (result) { data = { name: that.name }; }
- added a commit that references this issue
on Dec 26, 2016
compiles to:
the third function has no argument
Another example: