Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
iccir committed Mar 26, 2016
1 parent 2192096 commit 1f6bc5e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ OJError.VariableAlreadyDeclared = "OJVariableAlreadyDeclaredError";
OJError.VariableNotYetDeclared = "OJVariableNotYetDeclaredError";
OJError.RestrictedUsage = "OJRestrictedUsageError";
OJError.APIMisuse = "OJAPIMisuseError";
OJError.UseOfSelfInNonMethod = "OJUseOfSelfInNonMethodError";

const OJWarning = { };

Expand Down
8 changes: 6 additions & 2 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,8 @@ generate()

function handleIdentifier(node)
{
let name = node.name;
let name = node.name;
let isSelf = (name == "self");

if (name[0] === "$") {
if (name.indexOf("$oj") == 0) {
Expand Down Expand Up @@ -740,7 +741,7 @@ generate()
if (currentClass.isIvar(name) || name == "self") {
let usesSelf = currentMethodNode && (methodUsesSelfVar || (language === LanguageTypechecker));

if (name == "self") {
if (isSelf) {
replacement = usesSelf ? "self" : "this";
} else {
replacement = generateThisIvar(currentClass.name, name, usesSelf);
Expand All @@ -761,6 +762,9 @@ generate()
warnings.push(Utils.makeError(OJWarning.UndeclaredInstanceVariable, "Use of undeclared instance variable " + node.name, node));
}
}

} else if (isSelf && !currentMethodNode) {
Utils.throwError(OJError.UseOfSelfInNonMethod, "Use of self in non-method", node);
}

if (inlines) {
Expand Down
12 changes: 12 additions & 0 deletions test/multi/TestErrors.oj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
@end


// --------------------------------------------------------------------
// @name self-in-non-method

@implementation UseOfThisInMethod

function usesSelf() {
self; //@error OJUseOfSelfInNonMethodError
}

@end


// --------------------------------------------------------------------
// @name this-in-method
// @opts { "warn-this-in-methods": true }
Expand Down

0 comments on commit 1f6bc5e

Please sign in to comment.