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

Side by Side Diff: pkg/analyzer/test/src/dart/analysis/driver_test.dart

Issue 2469323004: Mix file: and package: URIs during analysis. (Closed)
Patch Set: Refresh all FileState instances for a path. Created 4 years, 1 month 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/analyzer/lib/src/dart/analysis/file_state.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) 2016, the Dart project authors. Please see the AUTHORS file 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 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.driver; 5 library analyzer.test.driver;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 9
10 import 'package:analyzer/dart/ast/ast.dart'; 10 import 'package:analyzer/dart/ast/ast.dart';
11 import 'package:analyzer/dart/element/element.dart';
11 import 'package:analyzer/error/error.dart'; 12 import 'package:analyzer/error/error.dart';
12 import 'package:analyzer/file_system/file_system.dart'; 13 import 'package:analyzer/file_system/file_system.dart';
13 import 'package:analyzer/file_system/memory_file_system.dart'; 14 import 'package:analyzer/file_system/memory_file_system.dart';
14 import 'package:analyzer/source/package_map_resolver.dart'; 15 import 'package:analyzer/source/package_map_resolver.dart';
15 import 'package:analyzer/src/dart/analysis/byte_store.dart'; 16 import 'package:analyzer/src/dart/analysis/byte_store.dart';
16 import 'package:analyzer/src/dart/analysis/driver.dart'; 17 import 'package:analyzer/src/dart/analysis/driver.dart';
17 import 'package:analyzer/src/dart/analysis/file_state.dart'; 18 import 'package:analyzer/src/dart/analysis/file_state.dart';
18 import 'package:analyzer/src/error/codes.dart'; 19 import 'package:analyzer/src/error/codes.dart';
19 import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl; 20 import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
20 import 'package:analyzer/src/generated/source.dart'; 21 import 'package:analyzer/src/generated/source.dart';
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 testFile = _p('/test/lib/test.dart'); 69 testFile = _p('/test/lib/test.dart');
69 driver = new AnalysisDriver( 70 driver = new AnalysisDriver(
70 new PerformanceLog(logBuffer), 71 new PerformanceLog(logBuffer),
71 provider, 72 provider,
72 byteStore, 73 byteStore,
73 contentOverlay, 74 contentOverlay,
74 new SourceFactory([ 75 new SourceFactory([
75 new DartUriResolver(sdk), 76 new DartUriResolver(sdk),
76 new PackageMapUriResolver(provider, <String, List<Folder>>{ 77 new PackageMapUriResolver(provider, <String, List<Folder>>{
77 'test': [provider.getFolder(testProject)] 78 'test': [provider.getFolder(testProject)]
78 }) 79 }),
80 new ResourceUriResolver(provider)
79 ], null, provider), 81 ], null, provider),
80 new AnalysisOptionsImpl()..strongMode = true); 82 new AnalysisOptionsImpl()..strongMode = true);
81 driver.status.lastWhere((status) { 83 driver.status.lastWhere((status) {
82 allStatuses.add(status); 84 allStatuses.add(status);
83 if (status.isIdle) { 85 if (status.isIdle) {
84 idleStatusMonitor.notify(); 86 idleStatusMonitor.notify();
85 } 87 }
86 }); 88 });
87 driver.results.listen(allResults.add); 89 driver.results.listen(allResults.add);
88 } 90 }
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 final f = 42; 282 final f = 42;
281 } 283 }
282 ''', 284 ''',
283 priority: true); 285 priority: true);
284 await _waitForIdle(); 286 await _waitForIdle();
285 287
286 AnalysisResult result = await driver.getResult(testFile); 288 AnalysisResult result = await driver.getResult(testFile);
287 expect(_getClassFieldType(result.unit, 'C', 'f'), 'int'); 289 expect(_getClassFieldType(result.unit, 'C', 'f'), 'int');
288 } 290 }
289 291
292 test_getResult_sameFile_twoUris() async {
293 var a = _p('/test/lib/a.dart');
294 var b = _p('/test/lib/b.dart');
295 var c = _p('/test/test/c.dart');
296 provider.newFile(a, 'class A<T> {}');
297 provider.newFile(
298 b,
299 r'''
300 import 'a.dart';
301 var VB = new A<int>();
302 ''');
303 provider.newFile(
304 c,
305 r'''
306 import '../lib/a.dart';
307 var VC = new A<double>();
308 ''');
309
310 driver.addFile(a);
311 driver.addFile(b);
312 await _waitForIdle();
313
314 {
315 AnalysisResult result = await driver.getResult(b);
316 expect(_getImportSource(result.unit, 0).uri.toString(),
317 'package:test/a.dart');
318 expect(_getTopLevelVarType(result.unit, 'VB'), 'A<int>');
319 }
320
321 {
322 AnalysisResult result = await driver.getResult(c);
323 expect(_getImportSource(result.unit, 0).uri.toString(),
324 'file:///test/lib/a.dart');
325 expect(_getTopLevelVarType(result.unit, 'VC'), 'A<double>');
326 }
327 }
328
329 test_getResult_mix_fileAndPackageUris() async {
330 var a = _p('/test/bin/a.dart');
331 var b = _p('/test/bin/b.dart');
332 var c = _p('/test/lib/c.dart');
333 var d = _p('/test/test/d.dart');
334 provider.newFile(
335 a,
336 r'''
337 import 'package:test/c.dart';
338 int x = y;
339 ''');
340 provider.newFile(
341 b,
342 r'''
343 import '../lib/c.dart';
344 int x = y;
345 ''');
346 provider.newFile(
347 c,
348 r'''
349 import '../test/d.dart';
350 var y = z;
351 ''');
352 provider.newFile(
353 d,
354 r'''
355 String z = "string";
356 ''');
357
358 // Analysis of my_pkg/bin/a.dart produces no error because
359 // file:///my_pkg/bin/a.dart imports package:my_pkg/c.dart, and
360 // package:my_pkg/c.dart's import is erroneous, causing y's reference to z
361 // to be unresolved (and therefore have type dynamic).
362 {
363 AnalysisResult result = await driver.getResult(a);
364 expect(result.errors, isEmpty);
365 }
366
367 // Analysis of my_pkg/bin/b.dart produces the error "A value of type
368 // 'String' can't be assigned to a variable of type 'int'", because
369 // file:///my_pkg/bin/b.dart imports file:///my_pkg/lib/c.dart, which
370 // successfully imports file:///my_pkg/test/d.dart, causing y to have an
371 // inferred type of String.
372 {
373 AnalysisResult result = await driver.getResult(b);
374 List<AnalysisError> errors = result.errors;
375 expect(errors, hasLength(1));
376 expect(errors[0].errorCode, StaticTypeWarningCode.INVALID_ASSIGNMENT);
377 }
378 }
379
290 test_getResult_selfConsistent() async { 380 test_getResult_selfConsistent() async {
291 var a = _p('/test/lib/a.dart'); 381 var a = _p('/test/lib/a.dart');
292 var b = _p('/test/lib/b.dart'); 382 var b = _p('/test/lib/b.dart');
293 provider.newFile( 383 provider.newFile(
294 a, 384 a,
295 r''' 385 r'''
296 import 'b.dart'; 386 import 'b.dart';
297 var A1 = 1; 387 var A1 = 1;
298 var A2 = B1; 388 var A2 = B1;
299 '''); 389 ''');
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 } 632 }
543 fail('Cannot find the field $fieldName in the class $className in\n$unit'); 633 fail('Cannot find the field $fieldName in the class $className in\n$unit');
544 return null; 634 return null;
545 } 635 }
546 636
547 String _getClassFieldType( 637 String _getClassFieldType(
548 CompilationUnit unit, String className, String fieldName) { 638 CompilationUnit unit, String className, String fieldName) {
549 return _getClassField(unit, className, fieldName).element.type.toString(); 639 return _getClassField(unit, className, fieldName).element.type.toString();
550 } 640 }
551 641
642 ImportElement _getImportElement(CompilationUnit unit, int directiveIndex) {
643 var import = unit.directives[directiveIndex] as ImportDirective;
644 return import.element as ImportElement;
645 }
646
647 Source _getImportSource(CompilationUnit unit, int directiveIndex) {
648 return _getImportElement(unit, directiveIndex).importedLibrary.source;
649 }
650
552 VariableDeclaration _getTopLevelVar(CompilationUnit unit, String name) { 651 VariableDeclaration _getTopLevelVar(CompilationUnit unit, String name) {
553 for (CompilationUnitMember declaration in unit.declarations) { 652 for (CompilationUnitMember declaration in unit.declarations) {
554 if (declaration is TopLevelVariableDeclaration) { 653 if (declaration is TopLevelVariableDeclaration) {
555 for (VariableDeclaration variable in declaration.variables.variables) { 654 for (VariableDeclaration variable in declaration.variables.variables) {
556 if (variable.name.name == name) { 655 if (variable.name.name == name) {
557 return variable; 656 return variable;
558 } 657 }
559 } 658 }
560 } 659 }
561 } 660 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 @override 701 @override
603 List<int> get(String key) { 702 List<int> get(String key) {
604 return map[key]; 703 return map[key];
605 } 704 }
606 705
607 @override 706 @override
608 void put(String key, List<int> bytes) { 707 void put(String key, List<int> bytes) {
609 map[key] = bytes; 708 map[key] = bytes;
610 } 709 }
611 } 710 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/analysis/file_state.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698