From 1f6bc5ebcee0d5744521c0147e554f50c003dfcf Mon Sep 17 00:00:00 2001 From: Ricci Date: Fri, 25 Mar 2016 18:48:34 -0700 Subject: [PATCH] #107 --- src/errors.js | 1 + src/generator.js | 8 ++++++-- test/multi/TestErrors.oj | 12 ++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/errors.js b/src/errors.js index 559d721..823ec69 100644 --- a/src/errors.js +++ b/src/errors.js @@ -33,6 +33,7 @@ OJError.VariableAlreadyDeclared = "OJVariableAlreadyDeclaredError"; OJError.VariableNotYetDeclared = "OJVariableNotYetDeclaredError"; OJError.RestrictedUsage = "OJRestrictedUsageError"; OJError.APIMisuse = "OJAPIMisuseError"; +OJError.UseOfSelfInNonMethod = "OJUseOfSelfInNonMethodError"; const OJWarning = { }; diff --git a/src/generator.js b/src/generator.js index e77d33a..d6491da 100644 --- a/src/generator.js +++ b/src/generator.js @@ -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) { @@ -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); @@ -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) { diff --git a/test/multi/TestErrors.oj b/test/multi/TestErrors.oj index 3d94de7..3bf25d2 100644 --- a/test/multi/TestErrors.oj +++ b/test/multi/TestErrors.oj @@ -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 }