diff --git a/pkg/analyzer/lib/src/dart/analysis/file_state.dart b/pkg/analyzer/lib/src/dart/analysis/file_state.dart index ae15012012e..74cf10a0a32 100644 --- a/pkg/analyzer/lib/src/dart/analysis/file_state.dart +++ b/pkg/analyzer/lib/src/dart/analysis/file_state.dart @@ -101,8 +101,8 @@ class FileState { Set _transitiveFiles; String _transitiveSignature; - List _topLevelDeclarations; - List _exportedTopLevelDeclarations; + Map _topLevelDeclarations; + Map _exportedTopLevelDeclarations; FileState._(this._fsState, this.path, this.uri, this.source); @@ -133,47 +133,43 @@ class FileState { List get exportedFiles => _exportedFiles; /** - * Return [TopLevelDeclaration]s exported from the this library file. + * Return [TopLevelDeclaration]s exported from the this library file. The + * keys to the map are names of declarations. */ - List get exportedTopLevelDeclarations { + Map get exportedTopLevelDeclarations { if (_exportedTopLevelDeclarations == null) { - _exportedTopLevelDeclarations = []; + _exportedTopLevelDeclarations = {}; Set seenLibraries = new Set(); /** * Compute [TopLevelDeclaration]s exported from the [library]. */ - List computeExported(FileState library) { + Map computeExported(FileState library) { var declarations = {}; if (seenLibraries.add(library)) { - // Append the library declarations. - for (TopLevelDeclaration t in library.topLevelDeclarations) { - declarations[t.name] = t; - } - for (FileState part in library.partedFiles) { - for (TopLevelDeclaration t in part.topLevelDeclarations) { - declarations[t.name] = t; - } - } - // Append the exported declarations. for (int i = 0; i < library._exportedFiles.length; i++) { - List exported = + Map exported = computeExported(library._exportedFiles[i]); - for (TopLevelDeclaration t in exported) { - if (!declarations.containsKey(t.name) && - library._exportFilters[i].accepts(t.name)) { + for (TopLevelDeclaration t in exported.values) { + if (library._exportFilters[i].accepts(t.name)) { declarations[t.name] = t; } } } + // Append the library declarations. + declarations.addAll(library.topLevelDeclarations); + for (FileState part in library.partedFiles) { + declarations.addAll(part.topLevelDeclarations); + } + // We're done with this library. seenLibraries.remove(library); } - return declarations.values.toList(); + return declarations; } _exportedTopLevelDeclarations = computeExported(this); @@ -224,15 +220,16 @@ class FileState { Set get referencedNames => _referencedNames; /** - * Return public top-level declarations declared in the file. + * Return public top-level declarations declared in the file. The keys to the + * map are names of declarations. */ - List get topLevelDeclarations { + Map get topLevelDeclarations { if (_topLevelDeclarations == null) { - _topLevelDeclarations = []; + _topLevelDeclarations = {}; void addDeclaration(TopLevelDeclarationKind kind, String name) { if (!name.startsWith('_')) { - _topLevelDeclarations.add(new TopLevelDeclaration(kind, name)); + _topLevelDeclarations[name] = new TopLevelDeclaration(kind, name); } } diff --git a/pkg/analyzer/test/src/dart/analysis/file_state_test.dart b/pkg/analyzer/test/src/dart/analysis/file_state_test.dart index 8a3a8da40a8..bb45ad1dd7f 100644 --- a/pkg/analyzer/test/src/dart/analysis/file_state_test.dart +++ b/pkg/analyzer/test/src/dart/analysis/file_state_test.dart @@ -73,8 +73,9 @@ export 'a.dart'; class B {} '''); FileState file = fileSystemState.getFileForPath(b); - List declarations = file.exportedTopLevelDeclarations; - expect(declarations.map((t) => t.name), unorderedEquals(['A', 'B'])); + Map declarations = + file.exportedTopLevelDeclarations; + expect(declarations.keys, unorderedEquals(['A', 'B'])); } test_exportedTopLevelDeclarations_export2_show() { @@ -162,9 +163,10 @@ export 'a.dart'; int V; '''); FileState file = fileSystemState.getFileForPath(b); - List declarations = file.exportedTopLevelDeclarations; - expect(declarations.map((t) => t.name), unorderedEquals(['V'])); - expect(declarations.single.kind, TopLevelDeclarationKind.variable); + Map declarations = + file.exportedTopLevelDeclarations; + expect(declarations.keys, unorderedEquals(['V'])); + expect(declarations['V'].kind, TopLevelDeclarationKind.variable); } test_exportedTopLevelDeclarations_export_show() { @@ -499,16 +501,13 @@ set _V3(_) {} '''); FileState file = fileSystemState.getFileForPath(path); - List declarations = file.topLevelDeclarations; + Map declarations = file.topLevelDeclarations; void assertHas(String name, TopLevelDeclarationKind kind) { - expect( - declarations, - contains(predicate( - (TopLevelDeclaration t) => t.name == name && t.kind == kind))); + expect(declarations[name]?.kind, kind); } - expect(declarations.map((t) => t.name), + expect(declarations.keys, unorderedEquals(['C', 'F', 'E', 'f', 'V1', 'V2', 'V3', 'V4'])); assertHas('C', TopLevelDeclarationKind.type); assertHas('F', TopLevelDeclarationKind.type); @@ -636,8 +635,9 @@ set _V3(_) {} void _assertExportedTopLevelDeclarations(String path, List expected) { FileState file = fileSystemState.getFileForPath(path); - List declarations = file.exportedTopLevelDeclarations; - expect(declarations.map((t) => t.name), unorderedEquals(expected)); + Map declarations = + file.exportedTopLevelDeclarations; + expect(declarations.keys, unorderedEquals(expected)); } void _assertFilesWithoutTransitiveFiles(List expected) {