Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1665)

Unified Diff: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java

Issue 15085012: Report CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Tweak for review comments. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
diff --git a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
index 2d29686f08133f646600572ef80d64a3453fc89e..72c004edac39af2ba820e6ba6a7ad4315f90d510 100644
--- a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
+++ b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
@@ -22,6 +22,7 @@ import com.google.dart.engine.ast.CatchClause;
import com.google.dart.engine.ast.ClassDeclaration;
import com.google.dart.engine.ast.ClassMember;
import com.google.dart.engine.ast.ClassTypeAlias;
+import com.google.dart.engine.ast.CompilationUnit;
import com.google.dart.engine.ast.ConditionalExpression;
import com.google.dart.engine.ast.ConstructorDeclaration;
import com.google.dart.engine.ast.ConstructorFieldInitializer;
@@ -59,6 +60,7 @@ import com.google.dart.engine.ast.SuperConstructorInvocation;
import com.google.dart.engine.ast.SwitchCase;
import com.google.dart.engine.ast.SwitchMember;
import com.google.dart.engine.ast.SwitchStatement;
+import com.google.dart.engine.ast.ThisExpression;
import com.google.dart.engine.ast.ThrowExpression;
import com.google.dart.engine.ast.TopLevelVariableDeclaration;
import com.google.dart.engine.ast.TypeArgumentList;
@@ -496,6 +498,12 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
}
@Override
+ public Void visitThisExpression(ThisExpression node) {
+ checkForInvalidReferenceToThis(node);
+ return super.visitThisExpression(node);
+ }
+
+ @Override
public Void visitThrowExpression(ThrowExpression node) {
checkForConstEvalThrowsException(node);
return super.visitThrowExpression(node);
@@ -1390,6 +1398,21 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
}
/**
+ * This verifies that the usage of the passed 'this' is valid.
+ *
+ * @param node the 'this' expression to evaluate
+ * @return {@code true} if and only if an error code is generated on the passed node
+ * @see CompileTimeErrorCode#INVALID_REFERENCE_TO_THIS
+ */
+ private boolean checkForInvalidReferenceToThis(ThisExpression node) {
+ if (!isThisInValidContext(node)) {
+ errorReporter.reportError(CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS, node);
+ return true;
+ }
+ return false;
+ }
+
+ /**
* Checks to ensure that first type argument to a map literal must be the 'String' type.
*
* @param arguments a non-{@code null}, non-empty {@link TypeName} node list from the respective
@@ -1847,4 +1870,28 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
}
return null;
}
+
+ /**
+ * @param node the 'this' expression to analyze
+ * @return {@code true} if the given 'this' expression is in the valid context
+ */
+ private boolean isThisInValidContext(ThisExpression node) {
+ for (ASTNode n = node; n != null; n = n.getParent()) {
+ if (n instanceof CompilationUnit) {
+ return false;
+ }
+ if (n instanceof ConstructorDeclaration) {
+ ConstructorDeclaration constructor = (ConstructorDeclaration) n;
+ return constructor.getFactoryKeyword() == null;
+ }
+ if (n instanceof ConstructorFieldInitializer) {
+ return false;
+ }
+ if (n instanceof MethodDeclaration) {
+ MethodDeclaration method = (MethodDeclaration) n;
+ return !method.isStatic();
+ }
+ }
+ return false;
+ }
}

Powered by Google App Engine
This is Rietveld 408576698