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

Side by Side Diff: pkg/analysis_server/test/services/correction/fix_test.dart

Issue 2377803002: Order suggested Quick Fix imports: direct, export, non-API. (Closed)
Patch Set: Created 4 years, 2 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 unified diff | Download patch
« no previous file with comments | « pkg/analysis_server/lib/src/services/correction/fix_internal.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 test.services.correction.fix; 5 library test.services.correction.fix;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/plugin/edit/fix/fix_core.dart'; 9 import 'package:analysis_server/plugin/edit/fix/fix_core.dart';
10 import 'package:analysis_server/plugin/edit/fix/fix_dart.dart'; 10 import 'package:analysis_server/plugin/edit/fix/fix_dart.dart';
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 DartFixContext dartContext = new DartFixContextImpl( 145 DartFixContext dartContext = new DartFixContextImpl(
146 new FixContextImpl(provider, context, error), testUnit); 146 new FixContextImpl(provider, context, error), testUnit);
147 FixProcessor processor = new FixProcessor(dartContext); 147 FixProcessor processor = new FixProcessor(dartContext);
148 return processor.compute(); 148 return processor.compute();
149 } 149 }
150 150
151 /** 151 /**
152 * Configures the [SourceFactory] to have the `my_pkg` package in 152 * Configures the [SourceFactory] to have the `my_pkg` package in
153 * `/packages/my_pkg/lib` folder. 153 * `/packages/my_pkg/lib` folder.
154 */ 154 */
155 void _configureMyPkg(String myLibCode) { 155 void _configureMyPkg(Map<String, String> pathToCode) {
156 provider.newFile('/packages/my_pkg/lib/my_lib.dart', myLibCode); 156 pathToCode.forEach((path, code) {
157 provider.newFile('/packages/my_pkg/lib/$path', code);
158 });
157 // configure SourceFactory 159 // configure SourceFactory
158 Folder myPkgFolder = provider.getResource('/packages/my_pkg/lib'); 160 Folder myPkgFolder = provider.getResource('/packages/my_pkg/lib');
159 UriResolver pkgResolver = new PackageMapUriResolver(provider, { 161 UriResolver pkgResolver = new PackageMapUriResolver(provider, {
160 'my_pkg': [myPkgFolder] 162 'my_pkg': [myPkgFolder]
161 }); 163 });
162 context.sourceFactory = new SourceFactory( 164 context.sourceFactory = new SourceFactory(
163 [AbstractContextTest.SDK_RESOLVER, pkgResolver, resourceResolver]); 165 [AbstractContextTest.SDK_RESOLVER, pkgResolver, resourceResolver]);
164 // force 'my_pkg' resolution 166 // force 'my_pkg' resolution
165 addSource('/tmp/other.dart', "import 'package:my_pkg/my_lib.dart';"); 167 addSource(
168 '/tmp/other.dart',
169 pathToCode.keys
170 .map((path) => "import 'package:my_pkg/$path';")
171 .join('\n'));
166 } 172 }
167 173
168 AnalysisError _findErrorToFix() { 174 AnalysisError _findErrorToFix() {
169 List<AnalysisError> errors = context.computeErrors(testSource); 175 List<AnalysisError> errors = context.computeErrors(testSource);
170 if (errorFilter != null) { 176 if (errorFilter != null) {
171 errors = errors.where(errorFilter).toList(); 177 errors = errors.where(errorFilter).toList();
172 } 178 }
173 expect(errors, hasLength(1)); 179 expect(errors, hasLength(1));
174 return errors[0]; 180 return errors[0];
175 } 181 }
(...skipping 2899 matching lines...) Expand 10 before | Expand all | Expand 10 after
3075 '''); 3081 ''');
3076 await assertHasFix( 3082 await assertHasFix(
3077 DartFixKind.REPLACE_RETURN_TYPE_FUTURE, 3083 DartFixKind.REPLACE_RETURN_TYPE_FUTURE,
3078 ''' 3084 '''
3079 import 'dart:async'; 3085 import 'dart:async';
3080 Future main() async { 3086 Future main() async {
3081 } 3087 }
3082 '''); 3088 ''');
3083 } 3089 }
3084 3090
3085 test_importLibraryPackage_withClass() async { 3091 test_importLibraryPackage_preferDirectOverExport() async {
3086 _configureMyPkg(''' 3092 _configureMyPkg({'b.dart': 'class Test {}', 'a.dart': "export 'b.dart';"});
3087 library my_lib;
3088 class Test {}
3089 ''');
3090 // try to find a fix
3091 resolveTestUnit(''' 3093 resolveTestUnit('''
3092 main() { 3094 main() {
3093 Test test = null; 3095 Test test = null;
3096 }
3097 ''');
3098 performAllAnalysisTasks();
3099 await assertHasFix(
3100 DartFixKind.IMPORT_LIBRARY_PROJECT1,
3101 '''
3102 import 'package:my_pkg/b.dart';
3103
3104 main() {
3105 Test test = null;
3106 }
3107 ''');
3108 await assertHasFix(
3109 DartFixKind.IMPORT_LIBRARY_PROJECT2,
3110 '''
3111 import 'package:my_pkg/a.dart';
3112
3113 main() {
3114 Test test = null;
3115 }
3116 ''');
3117 }
3118
3119 test_importLibraryPackage_preferPublicOverPrivate() async {
3120 _configureMyPkg(
3121 {'src/a.dart': 'class Test {}', 'b.dart': "export 'src/a.dart';"});
3122 resolveTestUnit('''
3123 main() {
3124 Test test = null;
3094 } 3125 }
3095 '''); 3126 ''');
3096 performAllAnalysisTasks(); 3127 performAllAnalysisTasks();
3097 await assertHasFix( 3128 await assertHasFix(
3098 DartFixKind.IMPORT_LIBRARY_PROJECT, 3129 DartFixKind.IMPORT_LIBRARY_PROJECT2,
3099 ''' 3130 '''
3100 import 'package:my_pkg/my_lib.dart'; 3131 import 'package:my_pkg/b.dart';
3101 3132
3102 main() { 3133 main() {
3103 Test test = null; 3134 Test test = null;
3135 }
3136 ''');
3137 await assertHasFix(
3138 DartFixKind.IMPORT_LIBRARY_PROJECT3,
3139 '''
3140 import 'package:my_pkg/src/a.dart';
3141
3142 main() {
3143 Test test = null;
3104 } 3144 }
3105 '''); 3145 ''');
3106 } 3146 }
3107 3147
3108 test_importLibraryProject_withClass_annotation() async { 3148 test_importLibraryProject_withClass_annotation() async {
3109 addSource( 3149 addSource(
3110 '/lib.dart', 3150 '/lib.dart',
3111 ''' 3151 '''
3112 library lib; 3152 library lib;
3113 class Test { 3153 class Test {
3114 const Test(int p); 3154 const Test(int p);
3115 } 3155 }
3116 '''); 3156 ''');
3117 resolveTestUnit(''' 3157 resolveTestUnit('''
3118 @Test(0) 3158 @Test(0)
3119 main() { 3159 main() {
3120 } 3160 }
3121 '''); 3161 ''');
3122 performAllAnalysisTasks(); 3162 performAllAnalysisTasks();
3123 await assertHasFix( 3163 await assertHasFix(
3124 DartFixKind.IMPORT_LIBRARY_PROJECT, 3164 DartFixKind.IMPORT_LIBRARY_PROJECT1,
3125 ''' 3165 '''
3126 import 'lib.dart'; 3166 import 'lib.dart';
3127 3167
3128 @Test(0) 3168 @Test(0)
3129 main() { 3169 main() {
3130 } 3170 }
3131 '''); 3171 ''');
3132 } 3172 }
3133 3173
3134 test_importLibraryProject_withClass_hasOtherLibraryWithPrefix() async { 3174 test_importLibraryProject_withClass_hasOtherLibraryWithPrefix() async {
(...skipping 13 matching lines...) Expand all
3148 '''); 3188 ''');
3149 resolveTestUnit(''' 3189 resolveTestUnit('''
3150 import 'b.dart' show Two; 3190 import 'b.dart' show Two;
3151 main () { 3191 main () {
3152 new Two(); 3192 new Two();
3153 new One(); 3193 new One();
3154 } 3194 }
3155 '''); 3195 ''');
3156 performAllAnalysisTasks(); 3196 performAllAnalysisTasks();
3157 await assertHasFix( 3197 await assertHasFix(
3158 DartFixKind.IMPORT_LIBRARY_PROJECT, 3198 DartFixKind.IMPORT_LIBRARY_PROJECT1,
3159 ''' 3199 '''
3160 import 'a.dart'; 3200 import 'a.dart';
3161 import 'b.dart' show Two; 3201 import 'b.dart' show Two;
3162 main () { 3202 main () {
3163 new Two(); 3203 new Two();
3164 new One(); 3204 new One();
3165 } 3205 }
3166 '''); 3206 ''');
3167 } 3207 }
3168 3208
3169 test_importLibraryProject_withClass_inParentFolder() async { 3209 test_importLibraryProject_withClass_inParentFolder() async {
3170 testFile = '/project/bin/test.dart'; 3210 testFile = '/project/bin/test.dart';
3171 addSource( 3211 addSource(
3172 '/project/lib.dart', 3212 '/project/lib.dart',
3173 ''' 3213 '''
3174 library lib; 3214 library lib;
3175 class Test {} 3215 class Test {}
3176 '''); 3216 ''');
3177 resolveTestUnit(''' 3217 resolveTestUnit('''
3178 main() { 3218 main() {
3179 Test t = null; 3219 Test t = null;
3180 } 3220 }
3181 '''); 3221 ''');
3182 performAllAnalysisTasks(); 3222 performAllAnalysisTasks();
3183 await assertHasFix( 3223 await assertHasFix(
3184 DartFixKind.IMPORT_LIBRARY_PROJECT, 3224 DartFixKind.IMPORT_LIBRARY_PROJECT1,
3185 ''' 3225 '''
3186 import '../lib.dart'; 3226 import '../lib.dart';
3187 3227
3188 main() { 3228 main() {
3189 Test t = null; 3229 Test t = null;
3190 } 3230 }
3191 '''); 3231 ''');
3192 } 3232 }
3193 3233
3194 test_importLibraryProject_withClass_inRelativeFolder() async { 3234 test_importLibraryProject_withClass_inRelativeFolder() async {
3195 testFile = '/project/bin/test.dart'; 3235 testFile = '/project/bin/test.dart';
3196 addSource( 3236 addSource(
3197 '/project/lib/sub/folder/lib.dart', 3237 '/project/lib/sub/folder/lib.dart',
3198 ''' 3238 '''
3199 library lib; 3239 library lib;
3200 class Test {} 3240 class Test {}
3201 '''); 3241 ''');
3202 resolveTestUnit(''' 3242 resolveTestUnit('''
3203 main() { 3243 main() {
3204 Test t = null; 3244 Test t = null;
3205 } 3245 }
3206 '''); 3246 ''');
3207 performAllAnalysisTasks(); 3247 performAllAnalysisTasks();
3208 await assertHasFix( 3248 await assertHasFix(
3209 DartFixKind.IMPORT_LIBRARY_PROJECT, 3249 DartFixKind.IMPORT_LIBRARY_PROJECT1,
3210 ''' 3250 '''
3211 import '../lib/sub/folder/lib.dart'; 3251 import '../lib/sub/folder/lib.dart';
3212 3252
3213 main() { 3253 main() {
3214 Test t = null; 3254 Test t = null;
3215 } 3255 }
3216 '''); 3256 ''');
3217 } 3257 }
3218 3258
3219 test_importLibraryProject_withClass_inSameFolder() async { 3259 test_importLibraryProject_withClass_inSameFolder() async {
3220 testFile = '/project/bin/test.dart'; 3260 testFile = '/project/bin/test.dart';
3221 addSource( 3261 addSource(
3222 '/project/bin/lib.dart', 3262 '/project/bin/lib.dart',
3223 ''' 3263 '''
3224 library lib; 3264 library lib;
3225 class Test {} 3265 class Test {}
3226 '''); 3266 ''');
3227 resolveTestUnit(''' 3267 resolveTestUnit('''
3228 main() { 3268 main() {
3229 Test t = null; 3269 Test t = null;
3230 } 3270 }
3231 '''); 3271 ''');
3232 performAllAnalysisTasks(); 3272 performAllAnalysisTasks();
3233 await assertHasFix( 3273 await assertHasFix(
3234 DartFixKind.IMPORT_LIBRARY_PROJECT, 3274 DartFixKind.IMPORT_LIBRARY_PROJECT1,
3235 ''' 3275 '''
3236 import 'lib.dart'; 3276 import 'lib.dart';
3237 3277
3238 main() { 3278 main() {
3239 Test t = null; 3279 Test t = null;
3240 } 3280 }
3241 '''); 3281 ''');
3242 } 3282 }
3243 3283
3244 test_importLibraryProject_withFunction() async { 3284 test_importLibraryProject_withFunction() async {
3245 addSource( 3285 addSource(
3246 '/lib.dart', 3286 '/lib.dart',
3247 ''' 3287 '''
3248 library lib; 3288 library lib;
3249 myFunction() {} 3289 myFunction() {}
3250 '''); 3290 ''');
3251 resolveTestUnit(''' 3291 resolveTestUnit('''
3252 main() { 3292 main() {
3253 myFunction(); 3293 myFunction();
3254 } 3294 }
3255 '''); 3295 ''');
3256 performAllAnalysisTasks(); 3296 performAllAnalysisTasks();
3257 await assertHasFix( 3297 await assertHasFix(
3258 DartFixKind.IMPORT_LIBRARY_PROJECT, 3298 DartFixKind.IMPORT_LIBRARY_PROJECT1,
3259 ''' 3299 '''
3260 import 'lib.dart'; 3300 import 'lib.dart';
3261 3301
3262 main() { 3302 main() {
3263 myFunction(); 3303 myFunction();
3264 } 3304 }
3265 '''); 3305 ''');
3266 } 3306 }
3267 3307
3268 test_importLibraryProject_withFunction_unresolvedMethod() async { 3308 test_importLibraryProject_withFunction_unresolvedMethod() async {
3269 addSource( 3309 addSource(
3270 '/lib.dart', 3310 '/lib.dart',
3271 ''' 3311 '''
3272 library lib; 3312 library lib;
3273 myFunction() {} 3313 myFunction() {}
3274 '''); 3314 ''');
3275 resolveTestUnit(''' 3315 resolveTestUnit('''
3276 class A { 3316 class A {
3277 main() { 3317 main() {
3278 myFunction(); 3318 myFunction();
3279 } 3319 }
3280 } 3320 }
3281 '''); 3321 ''');
3282 performAllAnalysisTasks(); 3322 performAllAnalysisTasks();
3283 await assertHasFix( 3323 await assertHasFix(
3284 DartFixKind.IMPORT_LIBRARY_PROJECT, 3324 DartFixKind.IMPORT_LIBRARY_PROJECT1,
3285 ''' 3325 '''
3286 import 'lib.dart'; 3326 import 'lib.dart';
3287 3327
3288 class A { 3328 class A {
3289 main() { 3329 main() {
3290 myFunction(); 3330 myFunction();
3291 } 3331 }
3292 } 3332 }
3293 '''); 3333 ''');
3294 } 3334 }
3295 3335
3296 test_importLibraryProject_withFunctionTypeAlias() async { 3336 test_importLibraryProject_withFunctionTypeAlias() async {
3297 testFile = '/project/bin/test.dart'; 3337 testFile = '/project/bin/test.dart';
3298 addSource( 3338 addSource(
3299 '/project/bin/lib.dart', 3339 '/project/bin/lib.dart',
3300 ''' 3340 '''
3301 library lib; 3341 library lib;
3302 typedef MyFunction(); 3342 typedef MyFunction();
3303 '''); 3343 ''');
3304 resolveTestUnit(''' 3344 resolveTestUnit('''
3305 main() { 3345 main() {
3306 MyFunction t = null; 3346 MyFunction t = null;
3307 } 3347 }
3308 '''); 3348 ''');
3309 performAllAnalysisTasks(); 3349 performAllAnalysisTasks();
3310 await assertHasFix( 3350 await assertHasFix(
3311 DartFixKind.IMPORT_LIBRARY_PROJECT, 3351 DartFixKind.IMPORT_LIBRARY_PROJECT1,
3312 ''' 3352 '''
3313 import 'lib.dart'; 3353 import 'lib.dart';
3314 3354
3315 main() { 3355 main() {
3316 MyFunction t = null; 3356 MyFunction t = null;
3317 } 3357 }
3318 '''); 3358 ''');
3319 } 3359 }
3320 3360
3321 test_importLibraryProject_withTopLevelVariable() async { 3361 test_importLibraryProject_withTopLevelVariable() async {
3322 addSource( 3362 addSource(
3323 '/lib.dart', 3363 '/lib.dart',
3324 ''' 3364 '''
3325 library lib; 3365 library lib;
3326 int MY_VAR = 42; 3366 int MY_VAR = 42;
3327 '''); 3367 ''');
3328 resolveTestUnit(''' 3368 resolveTestUnit('''
3329 main() { 3369 main() {
3330 print(MY_VAR); 3370 print(MY_VAR);
3331 } 3371 }
3332 '''); 3372 ''');
3333 performAllAnalysisTasks(); 3373 performAllAnalysisTasks();
3334 await assertHasFix( 3374 await assertHasFix(
3335 DartFixKind.IMPORT_LIBRARY_PROJECT, 3375 DartFixKind.IMPORT_LIBRARY_PROJECT1,
3336 ''' 3376 '''
3337 import 'lib.dart'; 3377 import 'lib.dart';
3338 3378
3339 main() { 3379 main() {
3340 print(MY_VAR); 3380 print(MY_VAR);
3341 } 3381 }
3342 '''); 3382 ''');
3343 } 3383 }
3344 3384
3345 test_importLibrarySdk_withClass_AsExpression() async { 3385 test_importLibrarySdk_withClass_AsExpression() async {
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
3531 class B {} 3571 class B {}
3532 '''); 3572 ''');
3533 resolveTestUnit(''' 3573 resolveTestUnit('''
3534 import 'lib.dart' show A; 3574 import 'lib.dart' show A;
3535 main() { 3575 main() {
3536 A a; 3576 A a;
3537 B b; 3577 B b;
3538 } 3578 }
3539 '''); 3579 ''');
3540 performAllAnalysisTasks(); 3580 performAllAnalysisTasks();
3541 await assertNoFix(DartFixKind.IMPORT_LIBRARY_PROJECT); 3581 await assertNoFix(DartFixKind.IMPORT_LIBRARY_PROJECT1);
3542 await assertHasFix( 3582 await assertHasFix(
3543 DartFixKind.IMPORT_LIBRARY_SHOW, 3583 DartFixKind.IMPORT_LIBRARY_SHOW,
3544 ''' 3584 '''
3545 import 'lib.dart' show A, B; 3585 import 'lib.dart' show A, B;
3546 main() { 3586 main() {
3547 A a; 3587 A a;
3548 B b; 3588 B b;
3549 } 3589 }
3550 '''); 3590 ''');
3551 } 3591 }
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
3917 '''); 3957 ''');
3918 performAllAnalysisTasks(); 3958 performAllAnalysisTasks();
3919 await assertHasFix( 3959 await assertHasFix(
3920 DartFixKind.REPLACE_IMPORT_URI, 3960 DartFixKind.REPLACE_IMPORT_URI,
3921 ''' 3961 '''
3922 import '../foo/bar/lib.dart'; 3962 import '../foo/bar/lib.dart';
3923 '''); 3963 ''');
3924 } 3964 }
3925 3965
3926 test_replaceImportUri_package() async { 3966 test_replaceImportUri_package() async {
3927 _configureMyPkg(''); 3967 _configureMyPkg({'my_lib.dart': ''});
3928 resolveTestUnit(''' 3968 resolveTestUnit('''
3929 import 'no/matter/my_lib.dart'; 3969 import 'no/matter/my_lib.dart';
3930 '''); 3970 ''');
3931 performAllAnalysisTasks(); 3971 performAllAnalysisTasks();
3932 await assertHasFix( 3972 await assertHasFix(
3933 DartFixKind.REPLACE_IMPORT_URI, 3973 DartFixKind.REPLACE_IMPORT_URI,
3934 ''' 3974 '''
3935 import 'package:my_pkg/my_lib.dart'; 3975 import 'package:my_pkg/my_lib.dart';
3936 '''); 3976 ''');
3937 } 3977 }
(...skipping 1481 matching lines...) Expand 10 before | Expand all | Expand 10 after
5419 @override 5459 @override
5420 void t() { } 5460 void t() { }
5421 } 5461 }
5422 '''); 5462 ''');
5423 } 5463 }
5424 5464
5425 void verifyResult(String expectedResult) { 5465 void verifyResult(String expectedResult) {
5426 expect(resultCode, expectedResult); 5466 expect(resultCode, expectedResult);
5427 } 5467 }
5428 } 5468 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/lib/src/services/correction/fix_internal.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698