@@ -10,7 +10,6 @@ import 'package:analysis_server/plugin/edit/fix/fix_core.dart';
1010import 'package:analysis_server/plugin/edit/fix/fix_dart.dart' ;
1111import 'package:analysis_server/src/services/completion/dart/utilities.dart' ;
1212import 'package:analysis_server/src/services/correction/fix.dart' ;
13- import 'package:analysis_server/src/services/correction/fix/dart/top_level_declarations.dart' ;
1413import 'package:analysis_server/src/services/correction/levenshtein.dart' ;
1514import 'package:analysis_server/src/services/correction/namespace.dart' ;
1615import 'package:analysis_server/src/services/correction/strings.dart' ;
@@ -27,6 +26,7 @@ import 'package:analyzer/dart/element/type.dart';
2726import 'package:analyzer/error/error.dart' ;
2827import 'package:analyzer/file_system/file_system.dart' ;
2928import 'package:analyzer/src/dart/analysis/session_helper.dart' ;
29+ import 'package:analyzer/src/dart/analysis/top_level_declaration.dart' ;
3030import 'package:analyzer/src/dart/ast/ast.dart' ;
3131import 'package:analyzer/src/dart/ast/token.dart' ;
3232import 'package:analyzer/src/dart/ast/utilities.dart' ;
@@ -105,11 +105,7 @@ class DartFixContributor implements FixContributor {
105105 // For each fix, put the fix into the HashMap.
106106 for (int i = 0 ; i < allAnalysisErrors.length; i++ ) {
107107 final FixContext fixContextI = new DartFixContextImpl (
108- context.workspace,
109- context.resolveResult,
110- allAnalysisErrors[i],
111- (name) => [],
112- );
108+ context.workspace, context.resolveResult, allAnalysisErrors[i]);
113109 final FixProcessor processorI = new FixProcessor (fixContextI);
114110 final List <Fix > fixesListI = await processorI.compute ();
115111 for (Fix f in fixesListI) {
@@ -2416,7 +2412,7 @@ class FixProcessor {
24162412 }
24172413 // may be there is an existing import,
24182414 // but it is with prefix and we don't use this prefix
2419- var alreadyImportedWithPrefix = new Set <String >();
2415+ Set < Source > alreadyImportedWithPrefix = new Set <Source >();
24202416 for (ImportElement imp in unitLibraryElement.imports) {
24212417 // prepare element
24222418 LibraryElement libraryElement = imp.importedLibrary;
@@ -2458,7 +2454,7 @@ class FixProcessor {
24582454 libraryName = libraryElement.source.shortName;
24592455 }
24602456 // don't add this library again
2461- alreadyImportedWithPrefix.add (libraryElement.source.fullName );
2457+ alreadyImportedWithPrefix.add (libraryElement.source);
24622458 // update library
24632459 String newShowCode = 'show ${showNames .join (', ' )}' ;
24642460 int offset = showCombinator.offset;
@@ -2477,21 +2473,25 @@ class FixProcessor {
24772473 }
24782474 // Find new top-level declarations.
24792475 {
2480- var declarations = await context .getTopLevelDeclarations (name);
2481- for (var declaration in declarations) {
2476+ var declarations = await session .getTopLevelDeclarations (name);
2477+ for (TopLevelDeclarationInSource declaration in declarations) {
24822478 // Check the kind.
2483- if (! kinds2.contains (declaration.kind)) {
2479+ if (! kinds2.contains (declaration.declaration. kind)) {
24842480 continue ;
24852481 }
24862482 // Check the source.
2487- if (alreadyImportedWithPrefix.contains (declaration.path)) {
2483+ Source librarySource = declaration.source;
2484+ if (alreadyImportedWithPrefix.contains (librarySource)) {
2485+ continue ;
2486+ }
2487+ if (! _isSourceVisibleToLibrary (librarySource)) {
24882488 continue ;
24892489 }
24902490 // Compute the fix kind.
24912491 FixKind fixKind;
2492- if (declaration.uri. isScheme ( 'dart' ) ) {
2492+ if (librarySource.isInSystemLibrary ) {
24932493 fixKind = DartFixKind .IMPORT_LIBRARY_SDK ;
2494- } else if (_isLibSrcPath (declaration.path )) {
2494+ } else if (_isLibSrcPath (librarySource.fullName )) {
24952495 // Bad: non-API.
24962496 fixKind = DartFixKind .IMPORT_LIBRARY_PROJECT3 ;
24972497 } else if (declaration.isExported) {
@@ -2503,8 +2503,8 @@ class FixProcessor {
25032503 }
25042504 // Add the fix.
25052505 var relativeURI =
2506- _getRelativeURIFromLibrary (unitLibraryElement, declaration.path );
2507- await _addFix_importLibrary (fixKind, declaration .uri, relativeURI);
2506+ _getRelativeURIFromLibrary (unitLibraryElement, librarySource );
2507+ await _addFix_importLibrary (fixKind, librarySource .uri, relativeURI);
25082508 }
25092509 }
25102510 }
@@ -4246,20 +4246,21 @@ class FixProcessor {
42464246 }
42474247
42484248 /**
4249- * Return the relative uri from the passed [library] to the given [path] .
4250- * If the [path ] is not in the LibraryElement, `null` is returned.
4249+ * Return the relative uri from the passed [library] to the passed
4250+ * [source] . If the [source ] is not in the LibraryElement, `null` is returned.
42514251 */
4252- String _getRelativeURIFromLibrary (LibraryElement library, String path ) {
4252+ String _getRelativeURIFromLibrary (LibraryElement library, Source source ) {
42534253 var librarySource = library? .librarySource;
42544254 if (librarySource == null ) {
42554255 return null ;
42564256 }
42574257 var pathCtx = resourceProvider.pathContext;
42584258 var libraryDirectory = pathCtx.dirname (librarySource.fullName);
4259- var sourceDirectory = pathCtx.dirname (path );
4260- if (pathCtx.isWithin (libraryDirectory, path ) ||
4259+ var sourceDirectory = pathCtx.dirname (source.fullName );
4260+ if (pathCtx.isWithin (libraryDirectory, source.fullName ) ||
42614261 pathCtx.isWithin (sourceDirectory, libraryDirectory)) {
4262- String relativeFile = pathCtx.relative (path, from: libraryDirectory);
4262+ String relativeFile =
4263+ pathCtx.relative (source.fullName, from: libraryDirectory);
42634264 return pathCtx.split (relativeFile).join ('/' );
42644265 }
42654266 return null ;
@@ -4468,6 +4469,30 @@ class FixProcessor {
44684469 return false ;
44694470 }
44704471
4472+ /**
4473+ * Return `true` if the [source] can be imported into current library.
4474+ */
4475+ bool _isSourceVisibleToLibrary (Source source) {
4476+ String path = source.fullName;
4477+
4478+ var contextRoot = context.resolveResult.session.analysisContext.contextRoot;
4479+ if (contextRoot == null ) {
4480+ return true ;
4481+ }
4482+
4483+ // We don't want to use private libraries of other packages.
4484+ if (source.uri.isScheme ('package' ) && _isLibSrcPath (path)) {
4485+ return contextRoot.root.contains (path);
4486+ }
4487+
4488+ // We cannot use relative URIs to reference files outside of our package.
4489+ if (source.uri.isScheme ('file' )) {
4490+ return contextRoot.root.contains (path);
4491+ }
4492+
4493+ return true ;
4494+ }
4495+
44714496 bool _isToListMethodElement (MethodElement method) {
44724497 if (method.name != 'toList' ) {
44734498 return false ;
0 commit comments