| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library analyzer.test.src.task.incremental_element_builder_test; | 5 library analyzer.test.src.task.incremental_element_builder_test; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/resolution_accessors.dart'; |
| 8 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
| 9 import 'package:analyzer/dart/element/visitor.dart'; | 10 import 'package:analyzer/dart/element/visitor.dart'; |
| 10 import 'package:analyzer/src/dart/ast/utilities.dart'; | 11 import 'package:analyzer/src/dart/ast/utilities.dart'; |
| 11 import 'package:analyzer/src/dart/element/element.dart'; | 12 import 'package:analyzer/src/dart/element/element.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; | 13 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 14 import 'package:analyzer/src/task/dart.dart'; | 15 import 'package:analyzer/src/task/dart.dart'; |
| 15 import 'package:analyzer/src/task/incremental_element_builder.dart'; | 16 import 'package:analyzer/src/task/incremental_element_builder.dart'; |
| 16 import 'package:analyzer/task/dart.dart'; | 17 import 'package:analyzer/task/dart.dart'; |
| 17 import 'package:test/test.dart'; | 18 import 'package:test/test.dart'; |
| (...skipping 1219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1237 test_directives_library_updateOffset() { | 1238 test_directives_library_updateOffset() { |
| 1238 _buildOldUnit(r''' | 1239 _buildOldUnit(r''' |
| 1239 #!/bin/sh | 1240 #!/bin/sh |
| 1240 library my_lib; | 1241 library my_lib; |
| 1241 class A {} | 1242 class A {} |
| 1242 '''); | 1243 '''); |
| 1243 LibraryDirective libraryDirective = oldUnit.directives.single; | 1244 LibraryDirective libraryDirective = oldUnit.directives.single; |
| 1244 // Set the LibraryElement and check that its nameOffset is correct. | 1245 // Set the LibraryElement and check that its nameOffset is correct. |
| 1245 libraryDirective.element = | 1246 libraryDirective.element = |
| 1246 new LibraryElementImpl.forNode(context, libraryDirective.name); | 1247 new LibraryElementImpl.forNode(context, libraryDirective.name); |
| 1247 expect(libraryDirective.element.nameOffset, libraryDirective.name.offset); | 1248 expect(elementForDirective(libraryDirective).nameOffset, |
| 1249 libraryDirective.name.offset); |
| 1248 // Update and check again that the nameOffset is correct. | 1250 // Update and check again that the nameOffset is correct. |
| 1249 _buildNewUnit(r''' | 1251 _buildNewUnit(r''' |
| 1250 #!/bin/sh | 1252 #!/bin/sh |
| 1251 | 1253 |
| 1252 library my_lib; | 1254 library my_lib; |
| 1253 class A {} | 1255 class A {} |
| 1254 '''); | 1256 '''); |
| 1255 expect(libraryDirective.element.nameOffset, libraryDirective.name.offset); | 1257 expect(elementForDirective(libraryDirective).nameOffset, |
| 1258 libraryDirective.name.offset); |
| 1256 } | 1259 } |
| 1257 | 1260 |
| 1258 test_directives_remove() { | 1261 test_directives_remove() { |
| 1259 _buildOldUnit(r''' | 1262 _buildOldUnit(r''' |
| 1260 library test; | 1263 library test; |
| 1261 import 'dart:async'; | 1264 import 'dart:async'; |
| 1262 import 'dart:math'; | 1265 import 'dart:math'; |
| 1263 '''); | 1266 '''); |
| 1264 List<Directive> oldDirectives = oldUnit.directives.toList(); | 1267 List<Directive> oldDirectives = oldUnit.directives.toList(); |
| 1265 _buildNewUnit(r''' | 1268 _buildNewUnit(r''' |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1343 import1.element = importElement1; | 1346 import1.element = importElement1; |
| 1344 import2.element = importElement2; | 1347 import2.element = importElement2; |
| 1345 import1.prefix.staticElement = prefixElement; | 1348 import1.prefix.staticElement = prefixElement; |
| 1346 import2.prefix.staticElement = prefixElement; | 1349 import2.prefix.staticElement = prefixElement; |
| 1347 _buildNewUnit(r''' | 1350 _buildNewUnit(r''' |
| 1348 import 'test1.dart' as m; | 1351 import 'test1.dart' as m; |
| 1349 import 'test2.dart' as m; | 1352 import 'test2.dart' as m; |
| 1350 class A {} | 1353 class A {} |
| 1351 '''); | 1354 '''); |
| 1352 int expectedPrefixOffset = 23; | 1355 int expectedPrefixOffset = 23; |
| 1353 expect(import1.prefix.staticElement.nameOffset, expectedPrefixOffset); | 1356 expect(staticElementForIdentifier(import1.prefix).nameOffset, |
| 1354 expect(import2.prefix.staticElement.nameOffset, expectedPrefixOffset); | 1357 expectedPrefixOffset); |
| 1358 expect(staticElementForIdentifier(import2.prefix).nameOffset, |
| 1359 expectedPrefixOffset); |
| 1355 expect(importElement1.prefix.nameOffset, expectedPrefixOffset); | 1360 expect(importElement1.prefix.nameOffset, expectedPrefixOffset); |
| 1356 expect(importElement2.prefix.nameOffset, expectedPrefixOffset); | 1361 expect(importElement2.prefix.nameOffset, expectedPrefixOffset); |
| 1357 } | 1362 } |
| 1358 | 1363 |
| 1359 test_directives_sameOrder_insertSpaces() { | 1364 test_directives_sameOrder_insertSpaces() { |
| 1360 _buildOldUnit(r''' | 1365 _buildOldUnit(r''' |
| 1361 library test; | 1366 library test; |
| 1362 import 'dart:async'; | 1367 import 'dart:async'; |
| 1363 import 'dart:math'; | 1368 import 'dart:math'; |
| 1364 '''); | 1369 '''); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1521 /// reference [double] and [B] types. | 1526 /// reference [double] and [B] types. |
| 1522 class C {}'''); | 1527 class C {}'''); |
| 1523 ClassElement element = newNode.element; | 1528 ClassElement element = newNode.element; |
| 1524 expect(element, isNotNull); | 1529 expect(element, isNotNull); |
| 1525 expect(element.name, 'C'); | 1530 expect(element.name, 'C'); |
| 1526 expect(element.nameOffset, newCode.indexOf('C {}')); | 1531 expect(element.nameOffset, newCode.indexOf('C {}')); |
| 1527 // [double] and [B] are still resolved | 1532 // [double] and [B] are still resolved |
| 1528 { | 1533 { |
| 1529 var docReferences = newNode.documentationComment.references; | 1534 var docReferences = newNode.documentationComment.references; |
| 1530 expect(docReferences, hasLength(2)); | 1535 expect(docReferences, hasLength(2)); |
| 1531 expect(docReferences[0].identifier.staticElement.name, 'double'); | 1536 expect(staticElementForIdentifier(docReferences[0].identifier).name, |
| 1537 'double'); |
| 1532 expect(docReferences[1].identifier.staticElement, | 1538 expect(docReferences[1].identifier.staticElement, |
| 1533 same(newNodes[2].element)); | 1539 same(newNodes[2].element)); |
| 1534 } | 1540 } |
| 1535 } | 1541 } |
| 1536 { | 1542 { |
| 1537 CompilationUnitMember newNode = newNodes[1]; | 1543 CompilationUnitMember newNode = newNodes[1]; |
| 1538 expect(newNode, same(oldNodes[0])); | 1544 expect(newNode, same(oldNodes[0])); |
| 1539 expect( | 1545 expect( |
| 1540 getNodeText(newNode), | 1546 getNodeText(newNode), |
| 1541 r''' | 1547 r''' |
| 1542 /// reference [bool] type. | 1548 /// reference [bool] type. |
| 1543 class A {}'''); | 1549 class A {}'''); |
| 1544 ClassElement element = newNode.element; | 1550 ClassElement element = newNode.element; |
| 1545 expect(element, isNotNull); | 1551 expect(element, isNotNull); |
| 1546 expect(element.name, 'A'); | 1552 expect(element.name, 'A'); |
| 1547 expect(element.nameOffset, newCode.indexOf('A {}')); | 1553 expect(element.nameOffset, newCode.indexOf('A {}')); |
| 1548 // [bool] is still resolved | 1554 // [bool] is still resolved |
| 1549 { | 1555 { |
| 1550 var docReferences = newNode.documentationComment.references; | 1556 var docReferences = newNode.documentationComment.references; |
| 1551 expect(docReferences, hasLength(1)); | 1557 expect(docReferences, hasLength(1)); |
| 1552 expect(docReferences[0].identifier.staticElement.name, 'bool'); | 1558 expect(staticElementForIdentifier(docReferences[0].identifier).name, |
| 1559 'bool'); |
| 1553 } | 1560 } |
| 1554 } | 1561 } |
| 1555 { | 1562 { |
| 1556 CompilationUnitMember newNode = newNodes[2]; | 1563 CompilationUnitMember newNode = newNodes[2]; |
| 1557 expect(newNode, same(oldNodes[1])); | 1564 expect(newNode, same(oldNodes[1])); |
| 1558 expect( | 1565 expect( |
| 1559 getNodeText(newNode), | 1566 getNodeText(newNode), |
| 1560 r''' | 1567 r''' |
| 1561 /// reference [int] type. | 1568 /// reference [int] type. |
| 1562 class B {}'''); | 1569 class B {}'''); |
| 1563 ClassElement element = newNode.element; | 1570 ClassElement element = newNode.element; |
| 1564 expect(element, isNotNull); | 1571 expect(element, isNotNull); |
| 1565 expect(element.name, 'B'); | 1572 expect(element.name, 'B'); |
| 1566 expect(element.nameOffset, newCode.indexOf('B {}')); | 1573 expect(element.nameOffset, newCode.indexOf('B {}')); |
| 1567 // [int] is still resolved | 1574 // [int] is still resolved |
| 1568 { | 1575 { |
| 1569 var docReferences = newNode.documentationComment.references; | 1576 var docReferences = newNode.documentationComment.references; |
| 1570 expect(docReferences, hasLength(1)); | 1577 expect(docReferences, hasLength(1)); |
| 1571 expect(docReferences[0].identifier.staticElement.name, 'int'); | 1578 expect(staticElementForIdentifier(docReferences[0].identifier).name, |
| 1579 'int'); |
| 1572 } | 1580 } |
| 1573 } | 1581 } |
| 1574 // verify delta | 1582 // verify delta |
| 1575 expect(unitDelta.addedDeclarations, unorderedEquals([])); | 1583 expect(unitDelta.addedDeclarations, unorderedEquals([])); |
| 1576 expect(unitDelta.removedDeclarations, unorderedEquals([])); | 1584 expect(unitDelta.removedDeclarations, unorderedEquals([])); |
| 1577 } | 1585 } |
| 1578 | 1586 |
| 1579 test_unitMembers_class_remove() { | 1587 test_unitMembers_class_remove() { |
| 1580 _buildOldUnit(r''' | 1588 _buildOldUnit(r''' |
| 1581 class A {} | 1589 class A {} |
| (...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2200 ClassDeclaration classNode = | 2208 ClassDeclaration classNode = |
| 2201 actual.getAncestor((n) => n is ClassDeclaration); | 2209 actual.getAncestor((n) => n is ClassDeclaration); |
| 2202 expect(element.enclosingElement, same(classNode.element)); | 2210 expect(element.enclosingElement, same(classNode.element)); |
| 2203 } | 2211 } |
| 2204 // ElementAnnotationImpl must use the enclosing CompilationUnitElement. | 2212 // ElementAnnotationImpl must use the enclosing CompilationUnitElement. |
| 2205 if (actual is Annotation) { | 2213 if (actual is Annotation) { |
| 2206 AstNode parent = actual.parent; | 2214 AstNode parent = actual.parent; |
| 2207 if (parent is Declaration) { | 2215 if (parent is Declaration) { |
| 2208 ElementAnnotationImpl actualElement = actual.elementAnnotation; | 2216 ElementAnnotationImpl actualElement = actual.elementAnnotation; |
| 2209 CompilationUnitElement enclosingUnitElement = | 2217 CompilationUnitElement enclosingUnitElement = |
| 2210 parent.element.getAncestor((a) => a is CompilationUnitElement); | 2218 elementForDeclaration(parent) |
| 2219 .getAncestor((a) => a is CompilationUnitElement); |
| 2211 expect(actualElement.compilationUnit, same(enclosingUnitElement)); | 2220 expect(actualElement.compilationUnit, same(enclosingUnitElement)); |
| 2212 } | 2221 } |
| 2213 } | 2222 } |
| 2214 // Identifiers like 'a.b' in 'new a.b()' might be rewritten if resolver | 2223 // Identifiers like 'a.b' in 'new a.b()' might be rewritten if resolver |
| 2215 // sees that 'a' is actually a class name, so 'b' is a constructor name. | 2224 // sees that 'a' is actually a class name, so 'b' is a constructor name. |
| 2216 // | 2225 // |
| 2217 if (expected is ConstructorName && actual is ConstructorName) { | 2226 if (expected is ConstructorName && actual is ConstructorName) { |
| 2218 Identifier expectedTypeName = expected.type.name; | 2227 Identifier expectedTypeName = expected.type.name; |
| 2219 Identifier actualTypeName = actual.type.name; | 2228 Identifier actualTypeName = actual.type.name; |
| 2220 if (expectedTypeName is PrefixedIdentifier && | 2229 if (expectedTypeName is PrefixedIdentifier && |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2340 unitMember is ClassDeclaration && unitMember.name.name == name); | 2349 unitMember is ClassDeclaration && unitMember.name.name == name); |
| 2341 } | 2350 } |
| 2342 | 2351 |
| 2343 class _MaterializeLazyElementsVisitor extends GeneralizingElementVisitor { | 2352 class _MaterializeLazyElementsVisitor extends GeneralizingElementVisitor { |
| 2344 @override | 2353 @override |
| 2345 visitExecutableElement(ExecutableElement element) { | 2354 visitExecutableElement(ExecutableElement element) { |
| 2346 element.parameters; | 2355 element.parameters; |
| 2347 super.visitExecutableElement(element); | 2356 super.visitExecutableElement(element); |
| 2348 } | 2357 } |
| 2349 } | 2358 } |
| OLD | NEW |