OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 engine.incremental_resolution_validator; | 5 library engine.incremental_resolution_validator; |
6 | 6 |
7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
9 | 9 |
10 | |
11 /** | 10 /** |
12 * Validates that the [actual] and the [expected] units have the same structure | 11 * Validates that the [actual] and the [expected] units have the same structure |
13 * and resolution. Throws [IncrementalResolutionMismatch] otherwise. | 12 * and resolution. Throws [IncrementalResolutionMismatch] otherwise. |
14 */ | 13 */ |
15 void assertSameResolution(CompilationUnit actual, CompilationUnit expected, | 14 void assertSameResolution(CompilationUnit actual, CompilationUnit expected, |
16 {bool validateTypes: false}) { | 15 {bool validateTypes: false}) { |
17 _SameResolutionValidator validator = | 16 _SameResolutionValidator validator = |
18 new _SameResolutionValidator(validateTypes, expected); | 17 new _SameResolutionValidator(validateTypes, expected); |
19 actual.accept(validator); | 18 actual.accept(validator); |
20 } | 19 } |
21 | 20 |
22 | |
23 /** | 21 /** |
24 * This exception is thrown when a mismatch between actual and expected AST | 22 * This exception is thrown when a mismatch between actual and expected AST |
25 * or resolution is found. | 23 * or resolution is found. |
26 */ | 24 */ |
27 class IncrementalResolutionMismatch { | 25 class IncrementalResolutionMismatch { |
28 final String message; | 26 final String message; |
29 IncrementalResolutionMismatch(this.message); | 27 IncrementalResolutionMismatch(this.message); |
30 } | 28 } |
31 | 29 |
32 | |
33 class _SameResolutionValidator implements AstVisitor { | 30 class _SameResolutionValidator implements AstVisitor { |
34 final bool validateTypes; | 31 final bool validateTypes; |
35 AstNode other; | 32 AstNode other; |
36 | 33 |
37 _SameResolutionValidator(this.validateTypes, this.other); | 34 _SameResolutionValidator(this.validateTypes, this.other); |
38 | 35 |
39 @override | 36 @override |
40 visitAdjacentStrings(AdjacentStrings node) { | 37 visitAdjacentStrings(AdjacentStrings node) {} |
41 } | |
42 | 38 |
43 @override | 39 @override |
44 visitAnnotation(Annotation node) { | 40 visitAnnotation(Annotation node) { |
45 Annotation other = this.other; | 41 Annotation other = this.other; |
46 _visitNode(node.name, other.name); | 42 _visitNode(node.name, other.name); |
47 _visitNode(node.constructorName, other.constructorName); | 43 _visitNode(node.constructorName, other.constructorName); |
48 _visitNode(node.arguments, other.arguments); | 44 _visitNode(node.arguments, other.arguments); |
49 _verifyElement(node.element, other.element); | 45 _verifyElement(node.element, other.element); |
50 } | 46 } |
51 | 47 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 _visitNode(node.body, other.body); | 237 _visitNode(node.body, other.body); |
242 } | 238 } |
243 | 239 |
244 @override | 240 @override |
245 visitDoubleLiteral(DoubleLiteral node) { | 241 visitDoubleLiteral(DoubleLiteral node) { |
246 DoubleLiteral other = this.other; | 242 DoubleLiteral other = this.other; |
247 _visitExpression(node, other); | 243 _visitExpression(node, other); |
248 } | 244 } |
249 | 245 |
250 @override | 246 @override |
251 visitEmptyFunctionBody(EmptyFunctionBody node) { | 247 visitEmptyFunctionBody(EmptyFunctionBody node) {} |
252 } | |
253 | 248 |
254 @override | 249 @override |
255 visitEmptyStatement(EmptyStatement node) { | 250 visitEmptyStatement(EmptyStatement node) {} |
256 } | |
257 | 251 |
258 @override | 252 @override |
259 visitEnumConstantDeclaration(EnumConstantDeclaration node) { | 253 visitEnumConstantDeclaration(EnumConstantDeclaration node) { |
260 EnumConstantDeclaration other = this.other; | 254 EnumConstantDeclaration other = this.other; |
261 _visitDeclaration(node, other); | 255 _visitDeclaration(node, other); |
262 _visitNode(node.name, other.name); | 256 _visitNode(node.name, other.name); |
263 } | 257 } |
264 | 258 |
265 @override | 259 @override |
266 visitEnumDeclaration(EnumDeclaration node) { | 260 visitEnumDeclaration(EnumDeclaration node) { |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 _visitExpression(node, other); | 432 _visitExpression(node, other); |
439 } | 433 } |
440 | 434 |
441 @override | 435 @override |
442 visitInterpolationExpression(InterpolationExpression node) { | 436 visitInterpolationExpression(InterpolationExpression node) { |
443 InterpolationExpression other = this.other; | 437 InterpolationExpression other = this.other; |
444 _visitNode(node.expression, other.expression); | 438 _visitNode(node.expression, other.expression); |
445 } | 439 } |
446 | 440 |
447 @override | 441 @override |
448 visitInterpolationString(InterpolationString node) { | 442 visitInterpolationString(InterpolationString node) {} |
449 } | |
450 | 443 |
451 @override | 444 @override |
452 visitIsExpression(IsExpression node) { | 445 visitIsExpression(IsExpression node) { |
453 IsExpression other = this.other; | 446 IsExpression other = this.other; |
454 _visitExpression(node, other); | 447 _visitExpression(node, other); |
455 _visitNode(node.expression, other.expression); | 448 _visitNode(node.expression, other.expression); |
456 _visitNode(node.type, other.type); | 449 _visitNode(node.type, other.type); |
457 } | 450 } |
458 | 451 |
459 @override | 452 @override |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 } | 514 } |
522 | 515 |
523 @override | 516 @override |
524 visitNamedExpression(NamedExpression node) { | 517 visitNamedExpression(NamedExpression node) { |
525 NamedExpression other = this.other; | 518 NamedExpression other = this.other; |
526 _visitNode(node.name, other.name); | 519 _visitNode(node.name, other.name); |
527 _visitNode(node.expression, other.expression); | 520 _visitNode(node.expression, other.expression); |
528 } | 521 } |
529 | 522 |
530 @override | 523 @override |
531 visitNativeClause(NativeClause node) { | 524 visitNativeClause(NativeClause node) {} |
532 } | |
533 | 525 |
534 @override | 526 @override |
535 visitNativeFunctionBody(NativeFunctionBody node) { | 527 visitNativeFunctionBody(NativeFunctionBody node) {} |
536 } | |
537 | 528 |
538 @override | 529 @override |
539 visitNullLiteral(NullLiteral node) { | 530 visitNullLiteral(NullLiteral node) { |
540 NullLiteral other = this.other; | 531 NullLiteral other = this.other; |
541 _visitExpression(node, other); | 532 _visitExpression(node, other); |
542 } | 533 } |
543 | 534 |
544 @override | 535 @override |
545 visitParenthesizedExpression(ParenthesizedExpression node) { | 536 visitParenthesizedExpression(ParenthesizedExpression node) { |
546 ParenthesizedExpression other = this.other; | 537 ParenthesizedExpression other = this.other; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
608 _visitExpression(node, other); | 599 _visitExpression(node, other); |
609 } | 600 } |
610 | 601 |
611 @override | 602 @override |
612 visitReturnStatement(ReturnStatement node) { | 603 visitReturnStatement(ReturnStatement node) { |
613 ReturnStatement other = this.other; | 604 ReturnStatement other = this.other; |
614 _visitNode(node.expression, other.expression); | 605 _visitNode(node.expression, other.expression); |
615 } | 606 } |
616 | 607 |
617 @override | 608 @override |
618 visitScriptTag(ScriptTag node) { | 609 visitScriptTag(ScriptTag node) {} |
619 } | |
620 | 610 |
621 @override | 611 @override |
622 visitShowCombinator(ShowCombinator node) { | 612 visitShowCombinator(ShowCombinator node) { |
623 ShowCombinator other = this.other; | 613 ShowCombinator other = this.other; |
624 _visitList(node.shownNames, other.shownNames); | 614 _visitList(node.shownNames, other.shownNames); |
625 } | 615 } |
626 | 616 |
627 @override | 617 @override |
628 visitSimpleFormalParameter(SimpleFormalParameter node) { | 618 visitSimpleFormalParameter(SimpleFormalParameter node) { |
629 SimpleFormalParameter other = this.other; | 619 SimpleFormalParameter other = this.other; |
630 _visitNormalFormalParameter(node, other); | 620 _visitNormalFormalParameter(node, other); |
631 _visitNode(node.type, other.type); | 621 _visitNode(node.type, other.type); |
632 } | 622 } |
633 | 623 |
634 @override | 624 @override |
635 visitSimpleIdentifier(SimpleIdentifier node) { | 625 visitSimpleIdentifier(SimpleIdentifier node) { |
636 SimpleIdentifier other = this.other; | 626 SimpleIdentifier other = this.other; |
637 _verifyElement(node.staticElement, other.staticElement); | 627 _verifyElement(node.staticElement, other.staticElement); |
638 _verifyElement(node.propagatedElement, other.propagatedElement); | 628 _verifyElement(node.propagatedElement, other.propagatedElement); |
639 _visitExpression(node, other); | 629 _visitExpression(node, other); |
640 } | 630 } |
641 | 631 |
642 @override | 632 @override |
643 visitSimpleStringLiteral(SimpleStringLiteral node) { | 633 visitSimpleStringLiteral(SimpleStringLiteral node) {} |
644 } | |
645 | 634 |
646 @override | 635 @override |
647 visitStringInterpolation(StringInterpolation node) { | 636 visitStringInterpolation(StringInterpolation node) { |
648 StringInterpolation other = this.other; | 637 StringInterpolation other = this.other; |
649 _visitList(node.elements, other.elements); | 638 _visitList(node.elements, other.elements); |
650 } | 639 } |
651 | 640 |
652 @override | 641 @override |
653 visitSuperConstructorInvocation(SuperConstructorInvocation node) { | 642 visitSuperConstructorInvocation(SuperConstructorInvocation node) { |
654 SuperConstructorInvocation other = this.other; | 643 SuperConstructorInvocation other = this.other; |
(...skipping 23 matching lines...) Expand all Loading... |
678 } | 667 } |
679 | 668 |
680 @override | 669 @override |
681 visitSwitchStatement(SwitchStatement node) { | 670 visitSwitchStatement(SwitchStatement node) { |
682 SwitchStatement other = this.other; | 671 SwitchStatement other = this.other; |
683 _visitNode(node.expression, other.expression); | 672 _visitNode(node.expression, other.expression); |
684 _visitList(node.members, other.members); | 673 _visitList(node.members, other.members); |
685 } | 674 } |
686 | 675 |
687 @override | 676 @override |
688 visitSymbolLiteral(SymbolLiteral node) { | 677 visitSymbolLiteral(SymbolLiteral node) {} |
689 } | |
690 | 678 |
691 @override | 679 @override |
692 visitThisExpression(ThisExpression node) { | 680 visitThisExpression(ThisExpression node) { |
693 ThisExpression other = this.other; | 681 ThisExpression other = this.other; |
694 _visitExpression(node, other); | 682 _visitExpression(node, other); |
695 } | 683 } |
696 | 684 |
697 @override | 685 @override |
698 visitThrowExpression(ThrowExpression node) { | 686 visitThrowExpression(ThrowExpression node) { |
699 ThrowExpression other = this.other; | 687 ThrowExpression other = this.other; |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
887 void _visitNode(AstNode node, AstNode other) { | 875 void _visitNode(AstNode node, AstNode other) { |
888 if (node == null) { | 876 if (node == null) { |
889 _expectIsNull(other); | 877 _expectIsNull(other); |
890 } else { | 878 } else { |
891 this.other = other; | 879 this.other = other; |
892 _assertNode(node, other); | 880 _assertNode(node, other); |
893 node.accept(this); | 881 node.accept(this); |
894 } | 882 } |
895 } | 883 } |
896 | 884 |
897 void _visitNormalFormalParameter(NormalFormalParameter node, | 885 void _visitNormalFormalParameter( |
898 NormalFormalParameter other) { | 886 NormalFormalParameter node, NormalFormalParameter other) { |
899 _verifyElement(node.element, other.element); | 887 _verifyElement(node.element, other.element); |
900 _visitNode(node.documentationComment, other.documentationComment); | 888 _visitNode(node.documentationComment, other.documentationComment); |
901 _visitList(node.metadata, other.metadata); | 889 _visitList(node.metadata, other.metadata); |
902 _visitNode(node.identifier, other.identifier); | 890 _visitNode(node.identifier, other.identifier); |
903 } | 891 } |
904 | 892 |
905 /** | 893 /** |
906 * Returns an URI scheme independent version of the [element] location. | 894 * Returns an URI scheme independent version of the [element] location. |
907 */ | 895 */ |
908 static String _getElementLocationWithoutUri(Element element) { | 896 static String _getElementLocationWithoutUri(Element element) { |
909 if (element == null) { | 897 if (element == null) { |
910 return '<null>'; | 898 return '<null>'; |
911 } | 899 } |
912 if (element is UriReferencedElementImpl) { | 900 if (element is UriReferencedElementImpl) { |
913 return '<ignored>'; | 901 return '<ignored>'; |
914 } | 902 } |
915 ElementLocation location = element.location; | 903 ElementLocation location = element.location; |
916 List<String> components = location.components; | 904 List<String> components = location.components; |
917 String uriPrefix = ''; | 905 String uriPrefix = ''; |
918 Element unit = element is CompilationUnitElement ? | 906 Element unit = element is CompilationUnitElement |
919 element : | 907 ? element |
920 element.getAncestor((e) => e is CompilationUnitElement); | 908 : element.getAncestor((e) => e is CompilationUnitElement); |
921 if (unit != null) { | 909 if (unit != null) { |
922 String libComponent = components[0]; | 910 String libComponent = components[0]; |
923 String unitComponent = components[1]; | 911 String unitComponent = components[1]; |
924 components = components.sublist(2); | 912 components = components.sublist(2); |
925 uriPrefix = _getShortElementLocationUri(libComponent) + | 913 uriPrefix = _getShortElementLocationUri(libComponent) + |
926 ':' + | 914 ':' + |
927 _getShortElementLocationUri(unitComponent); | 915 _getShortElementLocationUri(unitComponent); |
928 } else { | 916 } else { |
929 String libComponent = components[0]; | 917 String libComponent = components[0]; |
930 components = components.sublist(1); | 918 components = components.sublist(1); |
(...skipping 10 matching lines...) Expand all Loading... |
941 * package:project/my_lib.dart -> my_lib.dart | 929 * package:project/my_lib.dart -> my_lib.dart |
942 */ | 930 */ |
943 static String _getShortElementLocationUri(String uri) { | 931 static String _getShortElementLocationUri(String uri) { |
944 int index = uri.lastIndexOf('/'); | 932 int index = uri.lastIndexOf('/'); |
945 if (index == -1) { | 933 if (index == -1) { |
946 return uri; | 934 return uri; |
947 } | 935 } |
948 return uri.substring(index + 1); | 936 return uri.substring(index + 1); |
949 } | 937 } |
950 } | 938 } |
OLD | NEW |