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

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

Issue 15004020: Report StaticWarningCode.IMPORT_DUPLICATED_LIBRARY_NAME (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use multiline test sources 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 18d00a087f0bc6edee3e08a7e2725bc69259a809..b54e04e8e95964912aa5e5b57cbfe4dcf87ccca1 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
@@ -44,6 +44,7 @@ import com.google.dart.engine.ast.FunctionTypeAlias;
import com.google.dart.engine.ast.Identifier;
import com.google.dart.engine.ast.IfStatement;
import com.google.dart.engine.ast.ImplementsClause;
+import com.google.dart.engine.ast.ImportDirective;
import com.google.dart.engine.ast.InstanceCreationExpression;
import com.google.dart.engine.ast.ListLiteral;
import com.google.dart.engine.ast.MapLiteral;
@@ -77,6 +78,7 @@ import com.google.dart.engine.element.ConstructorElement;
import com.google.dart.engine.element.Element;
import com.google.dart.engine.element.ExecutableElement;
import com.google.dart.engine.element.FieldElement;
+import com.google.dart.engine.element.ImportElement;
import com.google.dart.engine.element.LibraryElement;
import com.google.dart.engine.element.MethodElement;
import com.google.dart.engine.element.ParameterElement;
@@ -201,6 +203,11 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
private HashMap<FieldElement, INIT_STATE> initialFieldElementsMap;
/**
+ * A table mapping name of the library to the import directive which import this library.
+ */
+ private HashMap<String, LibraryElement> nameToImportElement = new HashMap<String, LibraryElement>();
+
+ /**
* A list of types used by the {@link CompileTimeErrorCode#EXTENDS_DISALLOWED_CLASS} and
* {@link CompileTimeErrorCode#IMPLEMENTS_DISALLOWED_CLASS} error codes.
*/
@@ -391,6 +398,12 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
}
@Override
+ public Void visitImportDirective(ImportDirective node) {
+ checkForImportDuplicateLibraryName(node);
+ return super.visitImportDirective(node);
+ }
+
+ @Override
public Void visitInstanceCreationExpression(InstanceCreationExpression node) {
ConstructorName constructorName = node.getConstructorName();
TypeName typeName = constructorName.getType();
@@ -1326,6 +1339,46 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
}
/**
+ * This verifies the passed import has unique name among other imported libraries.
+ *
+ * @param node the import directive to evaluate
+ * @param library the imported library
+ * @return {@code true} if and only if an error code is generated on the passed node
+ * @see CompileTimeErrorCode#IMPORT_DUPLICATED_LIBRARY_NAME
+ */
+ private boolean checkForImportDuplicateLibraryName(ImportDirective node) {
+ // prepare import element
+ Element nodeElement = node.getElement();
+ if (!(nodeElement instanceof ImportElement)) {
+ return false;
+ }
+ ImportElement nodeImportElement = (ImportElement) nodeElement;
+ // prepare imported library
+ LibraryElement nodeLibrary = nodeImportElement.getImportedLibrary();
+ if (nodeLibrary == null) {
+ return false;
+ }
+ String name = nodeLibrary.getName();
+ // check if there is other imported library with the same name
+ LibraryElement prevLibrary = nameToImportElement.get(name);
+ if (prevLibrary != null) {
+ if (!prevLibrary.equals(nodeLibrary)) {
+ errorReporter.reportError(
+ StaticWarningCode.IMPORT_DUPLICATED_LIBRARY_NAME,
+ node,
+ prevLibrary.getDefiningCompilationUnit().getDisplayName(),
+ nodeLibrary.getDefiningCompilationUnit().getDisplayName(),
+ name);
+ return true;
+ }
+ } else {
+ nameToImportElement.put(name, nodeLibrary);
+ }
+ // OK
+ return false;
+ }
+
+ /**
* This verifies that the passed switch statement case expressions all have the same type.
*
* @param node the switch statement to evaluate

Powered by Google App Engine
This is Rietveld 408576698