| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library analyzer.test.generated.non_hint_code_test; |
| 6 |
| 7 import 'package:analyzer/error/error.dart'; |
| 8 import 'package:analyzer/src/error/codes.dart'; |
| 9 import 'package:analyzer/src/generated/source_io.dart'; |
| 10 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 11 |
| 12 import '../utils.dart'; |
| 13 import 'resolver_test_case.dart'; |
| 14 |
| 15 main() { |
| 16 initializeTestEnvironment(); |
| 17 defineReflectiveTests(NonHintCodeTest); |
| 18 } |
| 19 |
| 20 @reflectiveTest |
| 21 class NonHintCodeTest extends ResolverTestCase { |
| 22 void test_deadCode_afterTryCatch() { |
| 23 Source source = addSource(''' |
| 24 main() { |
| 25 try { |
| 26 return f(); |
| 27 } catch (e) { |
| 28 print(e); |
| 29 } |
| 30 print('not dead'); |
| 31 } |
| 32 f() { |
| 33 throw 'foo'; |
| 34 } |
| 35 '''); |
| 36 computeLibrarySourceErrors(source); |
| 37 assertNoErrors(source); |
| 38 verify([source]); |
| 39 } |
| 40 |
| 41 void test_deadCode_deadBlock_conditionalElse_debugConst() { |
| 42 Source source = addSource(r''' |
| 43 const bool DEBUG = true; |
| 44 f() { |
| 45 DEBUG ? 1 : 2; |
| 46 }'''); |
| 47 computeLibrarySourceErrors(source); |
| 48 assertNoErrors(source); |
| 49 verify([source]); |
| 50 } |
| 51 |
| 52 void test_deadCode_deadBlock_conditionalIf_debugConst() { |
| 53 Source source = addSource(r''' |
| 54 const bool DEBUG = false; |
| 55 f() { |
| 56 DEBUG ? 1 : 2; |
| 57 }'''); |
| 58 computeLibrarySourceErrors(source); |
| 59 assertNoErrors(source); |
| 60 verify([source]); |
| 61 } |
| 62 |
| 63 void test_deadCode_deadBlock_else() { |
| 64 Source source = addSource(r''' |
| 65 const bool DEBUG = true; |
| 66 f() { |
| 67 if(DEBUG) {} else {} |
| 68 }'''); |
| 69 computeLibrarySourceErrors(source); |
| 70 assertNoErrors(source); |
| 71 verify([source]); |
| 72 } |
| 73 |
| 74 void test_deadCode_deadBlock_if_debugConst_prefixedIdentifier() { |
| 75 Source source = addSource(r''' |
| 76 class A { |
| 77 static const bool DEBUG = false; |
| 78 } |
| 79 f() { |
| 80 if(A.DEBUG) {} |
| 81 }'''); |
| 82 computeLibrarySourceErrors(source); |
| 83 assertNoErrors(source); |
| 84 verify([source]); |
| 85 } |
| 86 |
| 87 void test_deadCode_deadBlock_if_debugConst_prefixedIdentifier2() { |
| 88 Source source = addSource(r''' |
| 89 library L; |
| 90 import 'lib2.dart'; |
| 91 f() { |
| 92 if(A.DEBUG) {} |
| 93 }'''); |
| 94 addNamedSource( |
| 95 "/lib2.dart", |
| 96 r''' |
| 97 library lib2; |
| 98 class A { |
| 99 static const bool DEBUG = false; |
| 100 }'''); |
| 101 computeLibrarySourceErrors(source); |
| 102 assertNoErrors(source); |
| 103 verify([source]); |
| 104 } |
| 105 |
| 106 void test_deadCode_deadBlock_if_debugConst_propertyAccessor() { |
| 107 Source source = addSource(r''' |
| 108 library L; |
| 109 import 'lib2.dart' as LIB; |
| 110 f() { |
| 111 if(LIB.A.DEBUG) {} |
| 112 }'''); |
| 113 addNamedSource( |
| 114 "/lib2.dart", |
| 115 r''' |
| 116 library lib2; |
| 117 class A { |
| 118 static const bool DEBUG = false; |
| 119 }'''); |
| 120 computeLibrarySourceErrors(source); |
| 121 assertNoErrors(source); |
| 122 verify([source]); |
| 123 } |
| 124 |
| 125 void test_deadCode_deadBlock_if_debugConst_simpleIdentifier() { |
| 126 Source source = addSource(r''' |
| 127 const bool DEBUG = false; |
| 128 f() { |
| 129 if(DEBUG) {} |
| 130 }'''); |
| 131 computeLibrarySourceErrors(source); |
| 132 assertNoErrors(source); |
| 133 verify([source]); |
| 134 } |
| 135 |
| 136 void test_deadCode_deadBlock_while_debugConst() { |
| 137 Source source = addSource(r''' |
| 138 const bool DEBUG = false; |
| 139 f() { |
| 140 while(DEBUG) {} |
| 141 }'''); |
| 142 computeLibrarySourceErrors(source); |
| 143 assertNoErrors(source); |
| 144 verify([source]); |
| 145 } |
| 146 |
| 147 void test_deadCode_deadCatch_onCatchSubtype() { |
| 148 Source source = addSource(r''' |
| 149 class A {} |
| 150 class B extends A {} |
| 151 f() { |
| 152 try {} on B catch (e) {} on A catch (e) {} catch (e) {} |
| 153 }'''); |
| 154 computeLibrarySourceErrors(source); |
| 155 assertNoErrors(source); |
| 156 verify([source]); |
| 157 } |
| 158 |
| 159 void test_deadCode_deadFinalBreakInCase() { |
| 160 Source source = addSource(r''' |
| 161 f() { |
| 162 switch (true) { |
| 163 case true: |
| 164 try { |
| 165 int a = 1; |
| 166 } finally { |
| 167 return; |
| 168 } |
| 169 break; |
| 170 default: |
| 171 break; |
| 172 } |
| 173 }'''); |
| 174 computeLibrarySourceErrors(source); |
| 175 assertNoErrors(source); |
| 176 verify([source]); |
| 177 } |
| 178 |
| 179 void test_deadCode_deadOperandLHS_and_debugConst() { |
| 180 Source source = addSource(r''' |
| 181 const bool DEBUG = false; |
| 182 f() { |
| 183 bool b = DEBUG && false; |
| 184 }'''); |
| 185 computeLibrarySourceErrors(source); |
| 186 assertNoErrors(source); |
| 187 verify([source]); |
| 188 } |
| 189 |
| 190 void test_deadCode_deadOperandLHS_or_debugConst() { |
| 191 Source source = addSource(r''' |
| 192 const bool DEBUG = true; |
| 193 f() { |
| 194 bool b = DEBUG || true; |
| 195 }'''); |
| 196 computeLibrarySourceErrors(source); |
| 197 assertNoErrors(source); |
| 198 verify([source]); |
| 199 } |
| 200 |
| 201 void test_deadCode_statementAfterIfWithoutElse() { |
| 202 Source source = addSource(r''' |
| 203 f() { |
| 204 if (1 < 0) { |
| 205 return; |
| 206 } |
| 207 int a = 1; |
| 208 }'''); |
| 209 computeLibrarySourceErrors(source); |
| 210 assertNoErrors(source); |
| 211 verify([source]); |
| 212 } |
| 213 |
| 214 void test_deprecatedMemberUse_inDeprecatedClass() { |
| 215 Source source = addSource(r''' |
| 216 @deprecated |
| 217 f() {} |
| 218 |
| 219 @deprecated |
| 220 class C { |
| 221 m() { |
| 222 f(); |
| 223 } |
| 224 } |
| 225 '''); |
| 226 computeLibrarySourceErrors(source); |
| 227 assertNoErrors(source); |
| 228 verify([source]); |
| 229 } |
| 230 |
| 231 void test_deprecatedMemberUse_inDeprecatedFunction() { |
| 232 Source source = addSource(r''' |
| 233 @deprecated |
| 234 f() {} |
| 235 |
| 236 @deprecated |
| 237 g() { |
| 238 f(); |
| 239 } |
| 240 '''); |
| 241 computeLibrarySourceErrors(source); |
| 242 assertNoErrors(source); |
| 243 verify([source]); |
| 244 } |
| 245 |
| 246 void test_deprecatedMemberUse_inDeprecatedLibrary() { |
| 247 Source source = addSource(r''' |
| 248 @deprecated |
| 249 library lib; |
| 250 |
| 251 @deprecated |
| 252 f() {} |
| 253 |
| 254 class C { |
| 255 m() { |
| 256 f(); |
| 257 } |
| 258 } |
| 259 '''); |
| 260 computeLibrarySourceErrors(source); |
| 261 assertNoErrors(source); |
| 262 verify([source]); |
| 263 } |
| 264 |
| 265 void test_deprecatedMemberUse_inDeprecatedMethod() { |
| 266 Source source = addSource(r''' |
| 267 @deprecated |
| 268 f() {} |
| 269 |
| 270 class C { |
| 271 @deprecated |
| 272 m() { |
| 273 f(); |
| 274 } |
| 275 } |
| 276 '''); |
| 277 computeLibrarySourceErrors(source); |
| 278 assertNoErrors(source); |
| 279 verify([source]); |
| 280 } |
| 281 |
| 282 void test_deprecatedMemberUse_inDeprecatedMethod_inDeprecatedClass() { |
| 283 Source source = addSource(r''' |
| 284 @deprecated |
| 285 f() {} |
| 286 |
| 287 @deprecated |
| 288 class C { |
| 289 @deprecated |
| 290 m() { |
| 291 f(); |
| 292 } |
| 293 } |
| 294 '''); |
| 295 computeLibrarySourceErrors(source); |
| 296 assertNoErrors(source); |
| 297 verify([source]); |
| 298 } |
| 299 |
| 300 void test_divisionOptimization() { |
| 301 Source source = addSource(r''' |
| 302 f(int x, int y) { |
| 303 var v = x / y.toInt(); |
| 304 }'''); |
| 305 computeLibrarySourceErrors(source); |
| 306 assertNoErrors(source); |
| 307 verify([source]); |
| 308 } |
| 309 |
| 310 void test_divisionOptimization_supressIfDivisionNotDefinedInCore() { |
| 311 Source source = addSource(r''' |
| 312 f(x, y) { |
| 313 var v = (x / y).toInt(); |
| 314 }'''); |
| 315 computeLibrarySourceErrors(source); |
| 316 assertNoErrors(source); |
| 317 verify([source]); |
| 318 } |
| 319 |
| 320 void test_divisionOptimization_supressIfDivisionOverridden() { |
| 321 Source source = addSource(r''' |
| 322 class A { |
| 323 num operator /(x) { return x; } |
| 324 } |
| 325 f(A x, A y) { |
| 326 var v = (x / y).toInt(); |
| 327 }'''); |
| 328 computeLibrarySourceErrors(source); |
| 329 assertNoErrors(source); |
| 330 verify([source]); |
| 331 } |
| 332 |
| 333 void test_duplicateImport_as() { |
| 334 Source source = addSource(r''' |
| 335 library L; |
| 336 import 'lib1.dart'; |
| 337 import 'lib1.dart' as one; |
| 338 A a; |
| 339 one.A a2;'''); |
| 340 addNamedSource( |
| 341 "/lib1.dart", |
| 342 r''' |
| 343 library lib1; |
| 344 class A {}'''); |
| 345 computeLibrarySourceErrors(source); |
| 346 assertNoErrors(source); |
| 347 verify([source]); |
| 348 } |
| 349 |
| 350 void test_duplicateImport_hide() { |
| 351 Source source = addSource(r''' |
| 352 library L; |
| 353 import 'lib1.dart'; |
| 354 import 'lib1.dart' hide A; |
| 355 A a; |
| 356 B b;'''); |
| 357 addNamedSource( |
| 358 "/lib1.dart", |
| 359 r''' |
| 360 library lib1; |
| 361 class A {} |
| 362 class B {}'''); |
| 363 computeLibrarySourceErrors(source); |
| 364 assertNoErrors(source); |
| 365 verify([source]); |
| 366 } |
| 367 |
| 368 void test_duplicateImport_show() { |
| 369 Source source = addSource(r''' |
| 370 library L; |
| 371 import 'lib1.dart'; |
| 372 import 'lib1.dart' show A; |
| 373 A a; |
| 374 B b;'''); |
| 375 addNamedSource( |
| 376 "/lib1.dart", |
| 377 r''' |
| 378 library lib1; |
| 379 class A {} |
| 380 class B {}'''); |
| 381 computeLibrarySourceErrors(source); |
| 382 assertNoErrors(source); |
| 383 verify([source]); |
| 384 } |
| 385 |
| 386 void test_importDeferredLibraryWithLoadFunction() { |
| 387 resolveWithErrors(<String>[ |
| 388 r''' |
| 389 library lib1; |
| 390 f() {}''', |
| 391 r''' |
| 392 library root; |
| 393 import 'lib1.dart' deferred as lib1; |
| 394 main() { lib1.f(); }''' |
| 395 ], ErrorCode.EMPTY_LIST); |
| 396 } |
| 397 |
| 398 void test_issue20904BuggyTypePromotionAtIfJoin_1() { |
| 399 // https://code.google.com/p/dart/issues/detail?id=20904 |
| 400 Source source = addSource(r''' |
| 401 f(var message, var dynamic_) { |
| 402 if (message is Function) { |
| 403 message = dynamic_; |
| 404 } |
| 405 int s = message; |
| 406 }'''); |
| 407 computeLibrarySourceErrors(source); |
| 408 assertNoErrors(source); |
| 409 verify([source]); |
| 410 } |
| 411 |
| 412 void test_issue20904BuggyTypePromotionAtIfJoin_3() { |
| 413 // https://code.google.com/p/dart/issues/detail?id=20904 |
| 414 Source source = addSource(r''' |
| 415 f(var message) { |
| 416 var dynamic_; |
| 417 if (message is Function) { |
| 418 message = dynamic_; |
| 419 } else { |
| 420 return; |
| 421 } |
| 422 int s = message; |
| 423 }'''); |
| 424 computeLibrarySourceErrors(source); |
| 425 assertNoErrors(source); |
| 426 verify([source]); |
| 427 } |
| 428 |
| 429 void test_issue20904BuggyTypePromotionAtIfJoin_4() { |
| 430 // https://code.google.com/p/dart/issues/detail?id=20904 |
| 431 Source source = addSource(r''' |
| 432 f(var message) { |
| 433 if (message is Function) { |
| 434 message = ''; |
| 435 } else { |
| 436 return; |
| 437 } |
| 438 String s = message; |
| 439 }'''); |
| 440 computeLibrarySourceErrors(source); |
| 441 assertNoErrors(source); |
| 442 verify([source]); |
| 443 } |
| 444 |
| 445 void test_missingReturn_emptyFunctionBody() { |
| 446 Source source = addSource(r''' |
| 447 abstract class A { |
| 448 int m(); |
| 449 }'''); |
| 450 computeLibrarySourceErrors(source); |
| 451 assertNoErrors(source); |
| 452 verify([source]); |
| 453 } |
| 454 |
| 455 void test_missingReturn_expressionFunctionBody() { |
| 456 Source source = addSource("int f() => 0;"); |
| 457 computeLibrarySourceErrors(source); |
| 458 assertNoErrors(source); |
| 459 verify([source]); |
| 460 } |
| 461 |
| 462 void test_missingReturn_noReturnType() { |
| 463 Source source = addSource("f() {}"); |
| 464 computeLibrarySourceErrors(source); |
| 465 assertNoErrors(source); |
| 466 verify([source]); |
| 467 } |
| 468 |
| 469 void test_missingReturn_voidReturnType() { |
| 470 Source source = addSource("void f() {}"); |
| 471 computeLibrarySourceErrors(source); |
| 472 assertNoErrors(source); |
| 473 verify([source]); |
| 474 } |
| 475 |
| 476 void test_nullAwareInCondition_for_noCondition() { |
| 477 Source source = addSource(r''' |
| 478 m(x) { |
| 479 for (var v = x; ; v++) {} |
| 480 } |
| 481 '''); |
| 482 computeLibrarySourceErrors(source); |
| 483 assertNoErrors(source); |
| 484 verify([source]); |
| 485 } |
| 486 |
| 487 void test_nullAwareInCondition_if_notTopLevel() { |
| 488 Source source = addSource(r''' |
| 489 m(x) { |
| 490 if (x?.y == null) {} |
| 491 } |
| 492 '''); |
| 493 computeLibrarySourceErrors(source); |
| 494 assertNoErrors(source); |
| 495 verify([source]); |
| 496 } |
| 497 |
| 498 void test_overrideEqualsButNotHashCode() { |
| 499 Source source = addSource(r''' |
| 500 class A { |
| 501 bool operator ==(x) { return x; } |
| 502 get hashCode => 0; |
| 503 }'''); |
| 504 computeLibrarySourceErrors(source); |
| 505 assertNoErrors(source); |
| 506 verify([source]); |
| 507 } |
| 508 |
| 509 void test_overrideOnNonOverridingField_inInterface() { |
| 510 Source source = addSource(r''' |
| 511 class A { |
| 512 int get a => 0; |
| 513 void set b(_) {} |
| 514 int c; |
| 515 } |
| 516 class B implements A { |
| 517 @override |
| 518 final int a = 1; |
| 519 @override |
| 520 int b; |
| 521 @override |
| 522 int c; |
| 523 }'''); |
| 524 computeLibrarySourceErrors(source); |
| 525 assertNoErrors(source); |
| 526 verify([source]); |
| 527 } |
| 528 |
| 529 void test_overrideOnNonOverridingField_inSuperclass() { |
| 530 Source source = addSource(r''' |
| 531 class A { |
| 532 int get a => 0; |
| 533 void set b(_) {} |
| 534 int c; |
| 535 } |
| 536 class B extends A { |
| 537 @override |
| 538 final int a = 1; |
| 539 @override |
| 540 int b; |
| 541 @override |
| 542 int c; |
| 543 }'''); |
| 544 computeLibrarySourceErrors(source); |
| 545 assertNoErrors(source); |
| 546 verify([source]); |
| 547 } |
| 548 |
| 549 void test_overrideOnNonOverridingGetter_inInterface() { |
| 550 Source source = addSource(r''' |
| 551 class A { |
| 552 int get m => 0; |
| 553 } |
| 554 class B implements A { |
| 555 @override |
| 556 int get m => 1; |
| 557 }'''); |
| 558 computeLibrarySourceErrors(source); |
| 559 assertNoErrors(source); |
| 560 verify([source]); |
| 561 } |
| 562 |
| 563 void test_overrideOnNonOverridingGetter_inSuperclass() { |
| 564 Source source = addSource(r''' |
| 565 class A { |
| 566 int get m => 0; |
| 567 } |
| 568 class B extends A { |
| 569 @override |
| 570 int get m => 1; |
| 571 }'''); |
| 572 computeLibrarySourceErrors(source); |
| 573 assertNoErrors(source); |
| 574 verify([source]); |
| 575 } |
| 576 |
| 577 void test_overrideOnNonOverridingMethod_inInterface() { |
| 578 Source source = addSource(r''' |
| 579 class A { |
| 580 int m() => 0; |
| 581 } |
| 582 class B implements A { |
| 583 @override |
| 584 int m() => 1; |
| 585 }'''); |
| 586 computeLibrarySourceErrors(source); |
| 587 assertNoErrors(source); |
| 588 verify([source]); |
| 589 } |
| 590 |
| 591 void test_overrideOnNonOverridingMethod_inSuperclass() { |
| 592 Source source = addSource(r''' |
| 593 class A { |
| 594 int m() => 0; |
| 595 } |
| 596 class B extends A { |
| 597 @override |
| 598 int m() => 1; |
| 599 }'''); |
| 600 computeLibrarySourceErrors(source); |
| 601 assertNoErrors(source); |
| 602 verify([source]); |
| 603 } |
| 604 |
| 605 void test_overrideOnNonOverridingMethod_inSuperclass_abstract() { |
| 606 Source source = addSource(r''' |
| 607 abstract class A { |
| 608 int m(); |
| 609 } |
| 610 class B extends A { |
| 611 @override |
| 612 int m() => 1; |
| 613 }'''); |
| 614 computeLibrarySourceErrors(source); |
| 615 assertNoErrors(source); |
| 616 verify([source]); |
| 617 } |
| 618 |
| 619 void test_overrideOnNonOverridingSetter_inInterface() { |
| 620 Source source = addSource(r''' |
| 621 class A { |
| 622 set m(int x) {} |
| 623 } |
| 624 class B implements A { |
| 625 @override |
| 626 set m(int x) {} |
| 627 }'''); |
| 628 computeLibrarySourceErrors(source); |
| 629 assertNoErrors(source); |
| 630 verify([source]); |
| 631 } |
| 632 |
| 633 void test_overrideOnNonOverridingSetter_inSuperclass() { |
| 634 Source source = addSource(r''' |
| 635 class A { |
| 636 set m(int x) {} |
| 637 } |
| 638 class B extends A { |
| 639 @override |
| 640 set m(int x) {} |
| 641 }'''); |
| 642 computeLibrarySourceErrors(source); |
| 643 assertNoErrors(source); |
| 644 verify([source]); |
| 645 } |
| 646 |
| 647 void test_propagatedFieldType() { |
| 648 Source source = addSource(r''' |
| 649 class A { } |
| 650 class X<T> { |
| 651 final x = new List<T>(); |
| 652 } |
| 653 class Z { |
| 654 final X<A> y = new X<A>(); |
| 655 foo() { |
| 656 y.x.add(new A()); |
| 657 } |
| 658 }'''); |
| 659 computeLibrarySourceErrors(source); |
| 660 assertNoErrors(source); |
| 661 verify([source]); |
| 662 } |
| 663 |
| 664 void test_proxy_annotation_prefixed() { |
| 665 Source source = addSource(r''' |
| 666 library L; |
| 667 @proxy |
| 668 class A {} |
| 669 f(var a) { |
| 670 a = new A(); |
| 671 a.m(); |
| 672 var x = a.g; |
| 673 a.s = 1; |
| 674 var y = a + a; |
| 675 a++; |
| 676 ++a; |
| 677 }'''); |
| 678 computeLibrarySourceErrors(source); |
| 679 assertNoErrors(source); |
| 680 } |
| 681 |
| 682 void test_proxy_annotation_prefixed2() { |
| 683 Source source = addSource(r''' |
| 684 library L; |
| 685 @proxy |
| 686 class A {} |
| 687 class B { |
| 688 f(var a) { |
| 689 a = new A(); |
| 690 a.m(); |
| 691 var x = a.g; |
| 692 a.s = 1; |
| 693 var y = a + a; |
| 694 a++; |
| 695 ++a; |
| 696 } |
| 697 }'''); |
| 698 computeLibrarySourceErrors(source); |
| 699 assertNoErrors(source); |
| 700 } |
| 701 |
| 702 void test_proxy_annotation_prefixed3() { |
| 703 Source source = addSource(r''' |
| 704 library L; |
| 705 class B { |
| 706 f(var a) { |
| 707 a = new A(); |
| 708 a.m(); |
| 709 var x = a.g; |
| 710 a.s = 1; |
| 711 var y = a + a; |
| 712 a++; |
| 713 ++a; |
| 714 } |
| 715 } |
| 716 @proxy |
| 717 class A {}'''); |
| 718 computeLibrarySourceErrors(source); |
| 719 assertNoErrors(source); |
| 720 } |
| 721 |
| 722 void test_undefinedGetter_inSubtype() { |
| 723 Source source = addSource(r''' |
| 724 class A {} |
| 725 class B extends A { |
| 726 get b => 0; |
| 727 } |
| 728 f(var a) { |
| 729 if(a is A) { |
| 730 return a.b; |
| 731 } |
| 732 }'''); |
| 733 computeLibrarySourceErrors(source); |
| 734 assertNoErrors(source); |
| 735 } |
| 736 |
| 737 void test_undefinedMethod_assignmentExpression_inSubtype() { |
| 738 Source source = addSource(r''' |
| 739 class A {} |
| 740 class B extends A { |
| 741 operator +(B b) {return new B();} |
| 742 } |
| 743 f(var a, var a2) { |
| 744 a = new A(); |
| 745 a2 = new A(); |
| 746 a += a2; |
| 747 }'''); |
| 748 computeLibrarySourceErrors(source); |
| 749 assertNoErrors(source); |
| 750 } |
| 751 |
| 752 void test_undefinedMethod_dynamic() { |
| 753 Source source = addSource(r''' |
| 754 class D<T extends dynamic> { |
| 755 fieldAccess(T t) => t.abc; |
| 756 methodAccess(T t) => t.xyz(1, 2, 'three'); |
| 757 }'''); |
| 758 computeLibrarySourceErrors(source); |
| 759 assertNoErrors(source); |
| 760 } |
| 761 |
| 762 void test_undefinedMethod_inSubtype() { |
| 763 Source source = addSource(r''' |
| 764 class A {} |
| 765 class B extends A { |
| 766 b() {} |
| 767 } |
| 768 f() { |
| 769 var a = new A(); |
| 770 a.b(); |
| 771 }'''); |
| 772 computeLibrarySourceErrors(source); |
| 773 assertNoErrors(source); |
| 774 } |
| 775 |
| 776 void test_undefinedMethod_unionType_all() { |
| 777 Source source = addSource(r''' |
| 778 class A { |
| 779 int m(int x) => 0; |
| 780 } |
| 781 class B { |
| 782 String m() => '0'; |
| 783 } |
| 784 f(A a, B b) { |
| 785 var ab; |
| 786 if (0 < 1) { |
| 787 ab = a; |
| 788 } else { |
| 789 ab = b; |
| 790 } |
| 791 ab.m(); |
| 792 }'''); |
| 793 computeLibrarySourceErrors(source); |
| 794 assertNoErrors(source); |
| 795 } |
| 796 |
| 797 void test_undefinedMethod_unionType_some() { |
| 798 Source source = addSource(r''' |
| 799 class A { |
| 800 int m(int x) => 0; |
| 801 } |
| 802 class B {} |
| 803 f(A a, B b) { |
| 804 var ab; |
| 805 if (0 < 1) { |
| 806 ab = a; |
| 807 } else { |
| 808 ab = b; |
| 809 } |
| 810 ab.m(0); |
| 811 }'''); |
| 812 computeLibrarySourceErrors(source); |
| 813 assertNoErrors(source); |
| 814 } |
| 815 |
| 816 void test_undefinedOperator_binaryExpression_inSubtype() { |
| 817 Source source = addSource(r''' |
| 818 class A {} |
| 819 class B extends A { |
| 820 operator +(B b) {} |
| 821 } |
| 822 f(var a) { |
| 823 if(a is A) { |
| 824 a + 1; |
| 825 } |
| 826 }'''); |
| 827 computeLibrarySourceErrors(source); |
| 828 assertNoErrors(source); |
| 829 } |
| 830 |
| 831 void test_undefinedOperator_indexBoth_inSubtype() { |
| 832 Source source = addSource(r''' |
| 833 class A {} |
| 834 class B extends A { |
| 835 operator [](int index) {} |
| 836 } |
| 837 f(var a) { |
| 838 if(a is A) { |
| 839 a[0]++; |
| 840 } |
| 841 }'''); |
| 842 computeLibrarySourceErrors(source); |
| 843 assertNoErrors(source); |
| 844 } |
| 845 |
| 846 void test_undefinedOperator_indexGetter_inSubtype() { |
| 847 Source source = addSource(r''' |
| 848 class A {} |
| 849 class B extends A { |
| 850 operator [](int index) {} |
| 851 } |
| 852 f(var a) { |
| 853 if(a is A) { |
| 854 a[0]; |
| 855 } |
| 856 }'''); |
| 857 computeLibrarySourceErrors(source); |
| 858 assertNoErrors(source); |
| 859 } |
| 860 |
| 861 void test_undefinedOperator_indexSetter_inSubtype() { |
| 862 Source source = addSource(r''' |
| 863 class A {} |
| 864 class B extends A { |
| 865 operator []=(i, v) {} |
| 866 } |
| 867 f(var a) { |
| 868 if(a is A) { |
| 869 a[0] = 1; |
| 870 } |
| 871 }'''); |
| 872 computeLibrarySourceErrors(source); |
| 873 assertNoErrors(source); |
| 874 } |
| 875 |
| 876 void test_undefinedOperator_postfixExpression() { |
| 877 Source source = addSource(r''' |
| 878 class A {} |
| 879 class B extends A { |
| 880 operator +(B b) {return new B();} |
| 881 } |
| 882 f(var a) { |
| 883 if(a is A) { |
| 884 a++; |
| 885 } |
| 886 }'''); |
| 887 computeLibrarySourceErrors(source); |
| 888 assertNoErrors(source); |
| 889 } |
| 890 |
| 891 void test_undefinedOperator_prefixExpression() { |
| 892 Source source = addSource(r''' |
| 893 class A {} |
| 894 class B extends A { |
| 895 operator +(B b) {return new B();} |
| 896 } |
| 897 f(var a) { |
| 898 if(a is A) { |
| 899 ++a; |
| 900 } |
| 901 }'''); |
| 902 computeLibrarySourceErrors(source); |
| 903 assertNoErrors(source); |
| 904 } |
| 905 |
| 906 void test_undefinedSetter_inSubtype() { |
| 907 Source source = addSource(r''' |
| 908 class A {} |
| 909 class B extends A { |
| 910 set b(x) {} |
| 911 } |
| 912 f(var a) { |
| 913 if(a is A) { |
| 914 a.b = 0; |
| 915 } |
| 916 }'''); |
| 917 computeLibrarySourceErrors(source); |
| 918 assertNoErrors(source); |
| 919 } |
| 920 |
| 921 void test_unnecessaryCast_13855_parameter_A() { |
| 922 // dartbug.com/13855, dartbug.com/13732 |
| 923 Source source = addSource(r''' |
| 924 class A{ |
| 925 a() {} |
| 926 } |
| 927 class B<E> { |
| 928 E e; |
| 929 m() { |
| 930 (e as A).a(); |
| 931 } |
| 932 }'''); |
| 933 computeLibrarySourceErrors(source); |
| 934 assertNoErrors(source); |
| 935 verify([source]); |
| 936 } |
| 937 |
| 938 void test_unnecessaryCast_conditionalExpression() { |
| 939 Source source = addSource(r''' |
| 940 abstract class I {} |
| 941 class A implements I {} |
| 942 class B implements I {} |
| 943 I m(A a, B b) { |
| 944 return a == null ? b as I : a as I; |
| 945 }'''); |
| 946 computeLibrarySourceErrors(source); |
| 947 assertNoErrors(source); |
| 948 verify([source]); |
| 949 } |
| 950 |
| 951 void test_unnecessaryCast_dynamic_type() { |
| 952 Source source = addSource(r''' |
| 953 m(v) { |
| 954 var b = v as Object; |
| 955 }'''); |
| 956 computeLibrarySourceErrors(source); |
| 957 assertNoErrors(source); |
| 958 verify([source]); |
| 959 } |
| 960 |
| 961 void test_unnecessaryCast_generics() { |
| 962 // dartbug.com/18953 |
| 963 Source source = addSource(r''' |
| 964 import 'dart:async'; |
| 965 Future<int> f() => new Future.value(0); |
| 966 void g(bool c) { |
| 967 (c ? f(): new Future.value(0) as Future<int>).then((int value) {}); |
| 968 }'''); |
| 969 computeLibrarySourceErrors(source); |
| 970 assertNoErrors(source); |
| 971 verify([source]); |
| 972 } |
| 973 |
| 974 void test_unnecessaryCast_type_dynamic() { |
| 975 Source source = addSource(r''' |
| 976 m(v) { |
| 977 var b = Object as dynamic; |
| 978 }'''); |
| 979 computeLibrarySourceErrors(source); |
| 980 assertNoErrors(source); |
| 981 verify([source]); |
| 982 } |
| 983 |
| 984 void test_unnecessaryNoSuchMethod_blockBody_notReturnStatement() { |
| 985 Source source = addSource(r''' |
| 986 class A { |
| 987 noSuchMethod(x) => super.noSuchMethod(x); |
| 988 } |
| 989 class B extends A { |
| 990 mmm(); |
| 991 noSuchMethod(y) { |
| 992 print(y); |
| 993 } |
| 994 }'''); |
| 995 computeLibrarySourceErrors(source); |
| 996 assertNoErrors(source); |
| 997 verify([source]); |
| 998 } |
| 999 |
| 1000 void test_unnecessaryNoSuchMethod_blockBody_notSingleStatement() { |
| 1001 Source source = addSource(r''' |
| 1002 class A { |
| 1003 noSuchMethod(x) => super.noSuchMethod(x); |
| 1004 } |
| 1005 class B extends A { |
| 1006 mmm(); |
| 1007 noSuchMethod(y) { |
| 1008 print(y); |
| 1009 return super.noSuchMethod(y); |
| 1010 } |
| 1011 }'''); |
| 1012 computeLibrarySourceErrors(source); |
| 1013 assertNoErrors(source); |
| 1014 verify([source]); |
| 1015 } |
| 1016 |
| 1017 void test_unnecessaryNoSuchMethod_expressionBody_notNoSuchMethod() { |
| 1018 Source source = addSource(r''' |
| 1019 class A { |
| 1020 noSuchMethod(x) => super.noSuchMethod(x); |
| 1021 } |
| 1022 class B extends A { |
| 1023 mmm(); |
| 1024 noSuchMethod(y) => super.hashCode; |
| 1025 }'''); |
| 1026 computeLibrarySourceErrors(source); |
| 1027 assertNoErrors(source); |
| 1028 verify([source]); |
| 1029 } |
| 1030 |
| 1031 void test_unnecessaryNoSuchMethod_expressionBody_notSuper() { |
| 1032 Source source = addSource(r''' |
| 1033 class A { |
| 1034 noSuchMethod(x) => super.noSuchMethod(x); |
| 1035 } |
| 1036 class B extends A { |
| 1037 mmm(); |
| 1038 noSuchMethod(y) => 42; |
| 1039 }'''); |
| 1040 computeLibrarySourceErrors(source); |
| 1041 assertNoErrors(source); |
| 1042 verify([source]); |
| 1043 } |
| 1044 |
| 1045 void test_unusedImport_annotationOnDirective() { |
| 1046 Source source = addSource(r''' |
| 1047 library L; |
| 1048 @A() |
| 1049 import 'lib1.dart';'''); |
| 1050 Source source2 = addNamedSource( |
| 1051 "/lib1.dart", |
| 1052 r''' |
| 1053 library lib1; |
| 1054 class A { |
| 1055 const A() {} |
| 1056 }'''); |
| 1057 computeLibrarySourceErrors(source); |
| 1058 assertErrors(source); |
| 1059 verify([source, source2]); |
| 1060 } |
| 1061 |
| 1062 void test_unusedImport_as_equalPrefixes() { |
| 1063 // 18818 |
| 1064 Source source = addSource(r''' |
| 1065 library L; |
| 1066 import 'lib1.dart' as one; |
| 1067 import 'lib2.dart' as one; |
| 1068 one.A a; |
| 1069 one.B b;'''); |
| 1070 Source source2 = addNamedSource( |
| 1071 "/lib1.dart", |
| 1072 r''' |
| 1073 library lib1; |
| 1074 class A {}'''); |
| 1075 Source source3 = addNamedSource( |
| 1076 "/lib2.dart", |
| 1077 r''' |
| 1078 library lib2; |
| 1079 class B {}'''); |
| 1080 computeLibrarySourceErrors(source); |
| 1081 assertErrors(source); |
| 1082 assertNoErrors(source2); |
| 1083 assertNoErrors(source3); |
| 1084 verify([source, source2, source3]); |
| 1085 } |
| 1086 |
| 1087 void test_unusedImport_core_library() { |
| 1088 Source source = addSource(r''' |
| 1089 library L; |
| 1090 import 'dart:core';'''); |
| 1091 computeLibrarySourceErrors(source); |
| 1092 assertNoErrors(source); |
| 1093 verify([source]); |
| 1094 } |
| 1095 |
| 1096 void test_unusedImport_export() { |
| 1097 Source source = addSource(r''' |
| 1098 library L; |
| 1099 import 'lib1.dart'; |
| 1100 Two two;'''); |
| 1101 addNamedSource( |
| 1102 "/lib1.dart", |
| 1103 r''' |
| 1104 library lib1; |
| 1105 export 'lib2.dart'; |
| 1106 class One {}'''); |
| 1107 addNamedSource( |
| 1108 "/lib2.dart", |
| 1109 r''' |
| 1110 library lib2; |
| 1111 class Two {}'''); |
| 1112 computeLibrarySourceErrors(source); |
| 1113 assertNoErrors(source); |
| 1114 verify([source]); |
| 1115 } |
| 1116 |
| 1117 void test_unusedImport_export2() { |
| 1118 Source source = addSource(r''' |
| 1119 library L; |
| 1120 import 'lib1.dart'; |
| 1121 Three three;'''); |
| 1122 addNamedSource( |
| 1123 "/lib1.dart", |
| 1124 r''' |
| 1125 library lib1; |
| 1126 export 'lib2.dart'; |
| 1127 class One {}'''); |
| 1128 addNamedSource( |
| 1129 "/lib2.dart", |
| 1130 r''' |
| 1131 library lib2; |
| 1132 export 'lib3.dart'; |
| 1133 class Two {}'''); |
| 1134 addNamedSource( |
| 1135 "/lib3.dart", |
| 1136 r''' |
| 1137 library lib3; |
| 1138 class Three {}'''); |
| 1139 computeLibrarySourceErrors(source); |
| 1140 assertNoErrors(source); |
| 1141 verify([source]); |
| 1142 } |
| 1143 |
| 1144 void test_unusedImport_export_infiniteLoop() { |
| 1145 Source source = addSource(r''' |
| 1146 library L; |
| 1147 import 'lib1.dart'; |
| 1148 Two two;'''); |
| 1149 addNamedSource( |
| 1150 "/lib1.dart", |
| 1151 r''' |
| 1152 library lib1; |
| 1153 export 'lib2.dart'; |
| 1154 class One {}'''); |
| 1155 addNamedSource( |
| 1156 "/lib2.dart", |
| 1157 r''' |
| 1158 library lib2; |
| 1159 export 'lib3.dart'; |
| 1160 class Two {}'''); |
| 1161 addNamedSource( |
| 1162 "/lib3.dart", |
| 1163 r''' |
| 1164 library lib3; |
| 1165 export 'lib2.dart'; |
| 1166 class Three {}'''); |
| 1167 computeLibrarySourceErrors(source); |
| 1168 assertNoErrors(source); |
| 1169 verify([source]); |
| 1170 } |
| 1171 |
| 1172 void test_unusedImport_metadata() { |
| 1173 Source source = addSource(r''' |
| 1174 library L; |
| 1175 @A(x) |
| 1176 import 'lib1.dart'; |
| 1177 class A { |
| 1178 final int value; |
| 1179 const A(this.value); |
| 1180 }'''); |
| 1181 addNamedSource( |
| 1182 "/lib1.dart", |
| 1183 r''' |
| 1184 library lib1; |
| 1185 const x = 0;'''); |
| 1186 computeLibrarySourceErrors(source); |
| 1187 assertNoErrors(source); |
| 1188 verify([source]); |
| 1189 } |
| 1190 |
| 1191 void test_unusedImport_prefix_topLevelFunction() { |
| 1192 Source source = addSource(r''' |
| 1193 library L; |
| 1194 import 'lib1.dart' hide topLevelFunction; |
| 1195 import 'lib1.dart' as one show topLevelFunction; |
| 1196 class A { |
| 1197 static void x() { |
| 1198 One o; |
| 1199 one.topLevelFunction(); |
| 1200 } |
| 1201 }'''); |
| 1202 addNamedSource( |
| 1203 "/lib1.dart", |
| 1204 r''' |
| 1205 library lib1; |
| 1206 class One {} |
| 1207 topLevelFunction() {}'''); |
| 1208 computeLibrarySourceErrors(source); |
| 1209 assertNoErrors(source); |
| 1210 verify([source]); |
| 1211 } |
| 1212 |
| 1213 void test_unusedImport_prefix_topLevelFunction2() { |
| 1214 Source source = addSource(r''' |
| 1215 library L; |
| 1216 import 'lib1.dart' hide topLevelFunction; |
| 1217 import 'lib1.dart' as one show topLevelFunction; |
| 1218 import 'lib1.dart' as two show topLevelFunction; |
| 1219 class A { |
| 1220 static void x() { |
| 1221 One o; |
| 1222 one.topLevelFunction(); |
| 1223 two.topLevelFunction(); |
| 1224 } |
| 1225 }'''); |
| 1226 addNamedSource( |
| 1227 "/lib1.dart", |
| 1228 r''' |
| 1229 library lib1; |
| 1230 class One {} |
| 1231 topLevelFunction() {}'''); |
| 1232 computeLibrarySourceErrors(source); |
| 1233 assertNoErrors(source); |
| 1234 verify([source]); |
| 1235 } |
| 1236 |
| 1237 void test_useOfVoidResult_implicitReturnValue() { |
| 1238 Source source = addSource(r''' |
| 1239 f() {} |
| 1240 class A { |
| 1241 n() { |
| 1242 var a = f(); |
| 1243 } |
| 1244 }'''); |
| 1245 computeLibrarySourceErrors(source); |
| 1246 assertNoErrors(source); |
| 1247 verify([source]); |
| 1248 } |
| 1249 |
| 1250 void test_useOfVoidResult_nonVoidReturnValue() { |
| 1251 Source source = addSource(r''' |
| 1252 int f() => 1; |
| 1253 g() { |
| 1254 var a = f(); |
| 1255 }'''); |
| 1256 computeLibrarySourceErrors(source); |
| 1257 assertNoErrors(source); |
| 1258 verify([source]); |
| 1259 } |
| 1260 } |
| 1261 |
| 1262 class PubSuggestionCodeTest extends ResolverTestCase { |
| 1263 void test_import_package() { |
| 1264 Source source = addSource("import 'package:somepackage/other.dart';"); |
| 1265 computeLibrarySourceErrors(source); |
| 1266 assertErrors(source, [CompileTimeErrorCode.URI_DOES_NOT_EXIST]); |
| 1267 } |
| 1268 |
| 1269 void test_import_packageWithDotDot() { |
| 1270 Source source = addSource("import 'package:somepackage/../other.dart';"); |
| 1271 computeLibrarySourceErrors(source); |
| 1272 assertErrors(source, [ |
| 1273 CompileTimeErrorCode.URI_DOES_NOT_EXIST, |
| 1274 HintCode.PACKAGE_IMPORT_CONTAINS_DOT_DOT |
| 1275 ]); |
| 1276 } |
| 1277 |
| 1278 void test_import_packageWithLeadingDotDot() { |
| 1279 Source source = addSource("import 'package:../other.dart';"); |
| 1280 computeLibrarySourceErrors(source); |
| 1281 assertErrors(source, [ |
| 1282 CompileTimeErrorCode.URI_DOES_NOT_EXIST, |
| 1283 HintCode.PACKAGE_IMPORT_CONTAINS_DOT_DOT |
| 1284 ]); |
| 1285 } |
| 1286 |
| 1287 void test_import_referenceIntoLibDirectory() { |
| 1288 cacheSource("/myproj/pubspec.yaml", ""); |
| 1289 cacheSource("/myproj/lib/other.dart", ""); |
| 1290 Source source = |
| 1291 addNamedSource("/myproj/web/test.dart", "import '../lib/other.dart';"); |
| 1292 computeLibrarySourceErrors(source); |
| 1293 assertErrors( |
| 1294 source, [HintCode.FILE_IMPORT_OUTSIDE_LIB_REFERENCES_FILE_INSIDE]); |
| 1295 } |
| 1296 |
| 1297 void test_import_referenceIntoLibDirectory_no_pubspec() { |
| 1298 cacheSource("/myproj/lib/other.dart", ""); |
| 1299 Source source = |
| 1300 addNamedSource("/myproj/web/test.dart", "import '../lib/other.dart';"); |
| 1301 computeLibrarySourceErrors(source); |
| 1302 assertNoErrors(source); |
| 1303 } |
| 1304 |
| 1305 void test_import_referenceOutOfLibDirectory() { |
| 1306 cacheSource("/myproj/pubspec.yaml", ""); |
| 1307 cacheSource("/myproj/web/other.dart", ""); |
| 1308 Source source = |
| 1309 addNamedSource("/myproj/lib/test.dart", "import '../web/other.dart';"); |
| 1310 computeLibrarySourceErrors(source); |
| 1311 assertErrors( |
| 1312 source, [HintCode.FILE_IMPORT_INSIDE_LIB_REFERENCES_FILE_OUTSIDE]); |
| 1313 } |
| 1314 |
| 1315 void test_import_referenceOutOfLibDirectory_no_pubspec() { |
| 1316 cacheSource("/myproj/web/other.dart", ""); |
| 1317 Source source = |
| 1318 addNamedSource("/myproj/lib/test.dart", "import '../web/other.dart';"); |
| 1319 computeLibrarySourceErrors(source); |
| 1320 assertNoErrors(source); |
| 1321 } |
| 1322 |
| 1323 void test_import_valid_inside_lib1() { |
| 1324 cacheSource("/myproj/pubspec.yaml", ""); |
| 1325 cacheSource("/myproj/lib/other.dart", ""); |
| 1326 Source source = |
| 1327 addNamedSource("/myproj/lib/test.dart", "import 'other.dart';"); |
| 1328 computeLibrarySourceErrors(source); |
| 1329 assertNoErrors(source); |
| 1330 } |
| 1331 |
| 1332 void test_import_valid_inside_lib2() { |
| 1333 cacheSource("/myproj/pubspec.yaml", ""); |
| 1334 cacheSource("/myproj/lib/bar/other.dart", ""); |
| 1335 Source source = addNamedSource( |
| 1336 "/myproj/lib/foo/test.dart", "import '../bar/other.dart';"); |
| 1337 computeLibrarySourceErrors(source); |
| 1338 assertNoErrors(source); |
| 1339 } |
| 1340 |
| 1341 void test_import_valid_outside_lib() { |
| 1342 cacheSource("/myproj/pubspec.yaml", ""); |
| 1343 cacheSource("/myproj/web/other.dart", ""); |
| 1344 Source source = |
| 1345 addNamedSource("/myproj/lib2/test.dart", "import '../web/other.dart';"); |
| 1346 computeLibrarySourceErrors(source); |
| 1347 assertNoErrors(source); |
| 1348 } |
| 1349 } |
| OLD | NEW |