| 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 test.src.serialization.elements_test; | 5 library test.src.serialization.elements_test; |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 new AnalysisOptionsImpl()..enableGenericMethods = true; | 46 new AnalysisOptionsImpl()..enableGenericMethods = true; |
| 47 | 47 |
| 48 void addLibrary(String uri) { | 48 void addLibrary(String uri) { |
| 49 otherLibrarySources.add(analysisContext2.sourceFactory.forUri(uri)); | 49 otherLibrarySources.add(analysisContext2.sourceFactory.forUri(uri)); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void addLibrarySource(String filePath, String contents) { | 52 void addLibrarySource(String filePath, String contents) { |
| 53 otherLibrarySources.add(addNamedSource(filePath, contents)); | 53 otherLibrarySources.add(addNamedSource(filePath, contents)); |
| 54 } | 54 } |
| 55 | 55 |
| 56 /** |
| 57 * Verify that the given prefix is safe to elide from a resynthesized AST. |
| 58 */ |
| 59 void checkElidablePrefix(SimpleIdentifier prefix) { |
| 60 if (prefix.staticElement is! PrefixElement && |
| 61 prefix.staticElement is! ClassElement) { |
| 62 fail('Prefix of type ${prefix.staticElement.runtimeType}' |
| 63 ' should not have been elided'); |
| 64 } |
| 65 } |
| 66 |
| 56 void checkLibrary(String text, | 67 void checkLibrary(String text, |
| 57 {bool allowErrors: false, bool dumpSummaries: false}) { | 68 {bool allowErrors: false, bool dumpSummaries: false}) { |
| 58 Source source = addSource(text); | 69 Source source = addSource(text); |
| 59 LibraryElementImpl original = resolve2(source); | 70 LibraryElementImpl original = resolve2(source); |
| 60 LibraryElementImpl resynthesized = resynthesizeLibraryElement( | 71 LibraryElementImpl resynthesized = resynthesizeLibraryElement( |
| 61 encodeLibrary(original, | 72 encodeLibrary(original, |
| 62 allowErrors: allowErrors, dumpSummaries: dumpSummaries), | 73 allowErrors: allowErrors, dumpSummaries: dumpSummaries), |
| 63 source.uri.toString(), | 74 source.uri.toString(), |
| 64 original); | 75 original); |
| 65 checkLibraryElements(original, resynthesized); | 76 checkLibraryElements(original, resynthesized); |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 ' have been elided'); | 371 ' have been elided'); |
| 361 } | 372 } |
| 362 } else if (o is PropertyAccess && | 373 } else if (o is PropertyAccess && |
| 363 o.target is PrefixedIdentifier && | 374 o.target is PrefixedIdentifier && |
| 364 r is PrefixedIdentifier) { | 375 r is PrefixedIdentifier) { |
| 365 // We don't resynthesize prefixed identifiers when the prefix refers to | 376 // We don't resynthesize prefixed identifiers when the prefix refers to |
| 366 // a PrefixElement or a ClassElement. Which means that if the original | 377 // a PrefixElement or a ClassElement. Which means that if the original |
| 367 // expression was e.g. `prefix.topLevelVariableName.length`, it will get | 378 // expression was e.g. `prefix.topLevelVariableName.length`, it will get |
| 368 // resynthesized as `topLevelVariableName.length` | 379 // resynthesized as `topLevelVariableName.length` |
| 369 PrefixedIdentifier oTarget = o.target; | 380 PrefixedIdentifier oTarget = o.target; |
| 370 if (oTarget.prefix.staticElement is PrefixElement || | 381 checkElidablePrefix(oTarget.prefix); |
| 371 oTarget.prefix.staticElement is ClassElement) { | 382 compareConstAsts( |
| 372 compareConstAsts(r, | 383 r, AstFactory.identifier(oTarget.identifier, o.propertyName), desc); |
| 373 AstFactory.identifier(oTarget.identifier, o.propertyName), desc); | |
| 374 } else { | |
| 375 fail('Prefix of type ${oTarget.prefix.staticElement.runtimeType}' | |
| 376 ' should not have been elided'); | |
| 377 } | |
| 378 } else if (o is PrefixedIdentifier && r is PrefixedIdentifier) { | 384 } else if (o is PrefixedIdentifier && r is PrefixedIdentifier) { |
| 379 compareConstAsts(r.prefix, o.prefix, desc); | 385 compareConstAsts(r.prefix, o.prefix, desc); |
| 380 compareConstAsts(r.identifier, o.identifier, desc); | 386 compareConstAsts(r.identifier, o.identifier, desc); |
| 381 } else if (o is PropertyAccess && r is PropertyAccess) { | 387 } else if (o is PropertyAccess && r is PropertyAccess) { |
| 382 compareConstAsts(r.target, o.target, desc); | 388 compareConstAsts(r.target, o.target, desc); |
| 383 expect(r.propertyName.name, o.propertyName.name, reason: desc); | 389 expect(r.propertyName.name, o.propertyName.name, reason: desc); |
| 384 compareElements( | 390 compareElements( |
| 385 r.propertyName.staticElement, o.propertyName.staticElement, desc); | 391 r.propertyName.staticElement, o.propertyName.staticElement, desc); |
| 386 } else if (o is PropertyAccess && r is SimpleIdentifier) { | 392 } else if (o is PropertyAccess && |
| 387 // We don't resynthesize property access. | 393 o.target is PrefixedIdentifier && |
| 388 // We use simple identifiers with correct elements. | 394 r is SimpleIdentifier) { |
| 395 // We don't resynthesize property access when it takes the form |
| 396 // `prefixName.className.staticMember`. We just resynthesize a |
| 397 // SimpleIdentifier correctly resolved to the static member. |
| 398 PrefixedIdentifier oTarget = o.target; |
| 399 checkElidablePrefix(oTarget.prefix); |
| 400 checkElidablePrefix(oTarget.identifier); |
| 389 compareConstAsts(r, o.propertyName, desc); | 401 compareConstAsts(r, o.propertyName, desc); |
| 390 } else if (o is NullLiteral) { | 402 } else if (o is NullLiteral) { |
| 391 expect(r, new isInstanceOf<NullLiteral>(), reason: desc); | 403 expect(r, new isInstanceOf<NullLiteral>(), reason: desc); |
| 392 } else if (o is BooleanLiteral && r is BooleanLiteral) { | 404 } else if (o is BooleanLiteral && r is BooleanLiteral) { |
| 393 expect(r.value, o.value, reason: desc); | 405 expect(r.value, o.value, reason: desc); |
| 394 } else if (o is IntegerLiteral && r is IntegerLiteral) { | 406 } else if (o is IntegerLiteral && r is IntegerLiteral) { |
| 395 expect(r.value, o.value, reason: desc); | 407 expect(r.value, o.value, reason: desc); |
| 396 } else if (o is DoubleLiteral && r is DoubleLiteral) { | 408 } else if (o is DoubleLiteral && r is DoubleLiteral) { |
| 397 if (r.value != null && | 409 if (r.value != null && |
| 398 r.value.isNaN && | 410 r.value.isNaN && |
| (...skipping 3580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3979 fail('Unexpectedly tried to get unlinked summary for $uri'); | 3991 fail('Unexpectedly tried to get unlinked summary for $uri'); |
| 3980 } | 3992 } |
| 3981 return serializedUnit; | 3993 return serializedUnit; |
| 3982 } | 3994 } |
| 3983 | 3995 |
| 3984 @override | 3996 @override |
| 3985 bool hasLibrarySummary(String uri) { | 3997 bool hasLibrarySummary(String uri) { |
| 3986 return true; | 3998 return true; |
| 3987 } | 3999 } |
| 3988 } | 4000 } |
| OLD | NEW |