Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix compiler error when compiling 'super()' #2721

Closed
wants to merge 1 commit into from

3 participants

@epidemian

I stumbled upon an unexpected compiler error while compiling super():

$ coffee -cs <<<'super()'
TypeError: Cannot read property 'name' of null
    at Scope.exports.Scope.Scope.namedMethod (/usr/lib/node_modules/coffee-script/lib/coffee-script/scope.js:42:22)
    at Call.exports.Call.Call.superReference (/usr/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:805:24)
    at Call.exports.Call.Call.compileNode (/usr/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:927:21)
    at Call.exports.Base.Base.compile (/usr/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:48:21)
...

It seems the error has been there for quite some time (since 1.3.2), introduced in commit 34be878.

The error is gone after adding a little ? in the right place, but really...

The error message for super() now seems good:

$ bin/coffee -cs <<<'super()'
SyntaxError: cannot call super outside of a function.
    at SyntaxError (unknown source)
    at Call.exports.Call.Call.superReference (/home/demian/Programming/coffee-script/lib/coffee-script/nodes.js:807:15)
    at Call.exports.Call.Call.compileNode (/home/demian/Programming/coffee-script/lib/coffee-script/nodes.js:927:21)
    at Call.exports.Base.Base.compile (/home/demian/Programming/coffee-script/lib/coffee-script/nodes.js:48:21)
...

But the error message for -> super() is also "cannot call super outside of a function", which doesn't make any sense, because it's inside a function. And it seems it should say "cannot call super on an anonymous function" instead, which would make much more sense.

So, i have no idea how Scope works or if it's really that class the source of the problem. Could someone more seasoned have a look this?

@vendethiel
Collaborator

Fix probably is okay :p. But I think it's a bit more complicated. For instance,

foo =
  a: -> super

will fail because it will call a.__super__, it should probably be disallowed here too.

@epidemian

Ok, but that compiles. Wrongly. But compiles. I think it's another super-related issue, isn't it?

I think if we fix the error message for -> super() this PR could be merged.

That being said, how can we avoid this kind of issues in the future? Should we have tests for error messages? Because the compiler was failing, as expected, but not with the expected error message.

@vendethiel
Collaborator

Something here, I suppose

@epidemian

Nice. I might get into testing the error messages later :)

@jashkenas jashkenas closed this in 6b79af2
@jashkenas
Owner

Whoops, wrong ticket.

@jashkenas jashkenas reopened this
@jashkenas jashkenas closed this in 5dea70b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 25, 2013
  1. @epidemian
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 2 deletions.
  1. +2 −1  lib/coffee-script/scope.js
  2. +1 −1  src/scope.litcoffee
View
3  lib/coffee-script/scope.js
@@ -39,7 +39,8 @@
};
Scope.prototype.namedMethod = function() {
- if (this.method.name || !this.parent) {
+ var _ref1;
+ if (((_ref1 = this.method) != null ? _ref1.name : void 0) || !this.parent) {
return this.method;
}
return this.parent.namedMethod();
View
2  src/scope.litcoffee
@@ -41,7 +41,7 @@ can get complicated if super is being called from an inner function.
function object that has a name filled in, or bottoms out.
namedMethod: ->
- return @method if @method.name or !@parent
+ return @method if @method?.name or !@parent
@parent.namedMethod()
Look up a variable name in lexical scope, and declare it if it does not
Something went wrong with that request. Please try again.