musictheory / NilScript

Objective-C-style language superset of JavaScript with a tiny, simple runtime
Other
50 stars 5 forks source link

self shouldn't be allowed inside of a non-method #107

Closed jmarr closed 8 years ago

jmarr commented 8 years ago

Example:

@implementation Foo
 var foobar = function( ) {
  return [self bar];
}
@end

The [self bar] call should not be allowed.

iccir commented 8 years ago

That should be a compiler error, as self makes no sense in that context. Implementation-wise, it currently compiles to this, but that has never been guaranteed.

In vanilla browser-based JavaScript, self is a property on the window object which returns a WindowProxy.

Hence, there may be situations where you'd want to use self in a function inside of an @implementation block (although this wouldn't be valid in your example, as we'd be messaging the WindowProxy with an oj msgSend). I don't feel comfortable preventing this use as an error.

However, I could see the case for a "warn when self is used inside of an @implementation block but not inside of a method". Or possibly even "warn when self is used in a non-method inside of an oj file".

But probably not "always warn about the usage of self", as it may be validly used in a JavaScript library or pure-JS file.

iccir commented 8 years ago

Added a new --warn-self-in-non-methods option, which warns on any usage of self outside of a non-method. For JS libraries or pure-JS files, see #113