Skip to content

Commit a916e60

Browse files
authored
Add scoped lookup to Libraries (#2671)
* Add scoped lookup to Libraries * Place @OverRide adjacent to scope impl * Enable more cases, yay * post merge rebuild
1 parent 02a467b commit a916e60

File tree

8 files changed

+58
-53
lines changed

8 files changed

+58
-53
lines changed

lib/src/generator/templates.runtime_renderers.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7506,6 +7506,18 @@ class _Renderer_Library extends RendererBase<Library> {
75067506
getters: _invisibleGetters['CommentReferable']));
75077507
},
75087508
),
7509+
'scope': Property(
7510+
getValue: (CT_ c) => c.scope,
7511+
renderVariable: (CT_ c, Property<CT_> self,
7512+
List<String> remainingNames) =>
7513+
self.renderSimpleVariable(c, remainingNames, 'Scope'),
7514+
isNullValue: (CT_ c) => c.scope == null,
7515+
renderValue:
7516+
(CT_ c, RendererBase<CT_> r, List<MustachioNode> ast) {
7517+
return renderSimple(c.scope, ast, r.template,
7518+
parent: r, getters: _invisibleGetters['Scope']);
7519+
},
7520+
),
75097521
'sdkLib': Property(
75107522
getValue: (CT_ c) => c.sdkLib,
75117523
renderVariable: (CT_ c, Property<CT_> self,
@@ -11161,7 +11173,7 @@ class _Renderer_Package extends RendererBase<Package> {
1116111173
}
1116211174
}
1116311175

11164-
String renderError(PackageTemplateData context, Template template) {
11176+
String renderIndex(PackageTemplateData context, Template template) {
1116511177
return _render_PackageTemplateData(context, template.ast, template);
1116611178
}
1116711179

@@ -11361,7 +11373,7 @@ class _Renderer_PackageTemplateData extends RendererBase<PackageTemplateData> {
1136111373
}
1136211374
}
1136311375

11364-
String renderIndex(PackageTemplateData context, Template template) {
11376+
String renderError(PackageTemplateData context, Template template) {
1136511377
return _render_PackageTemplateData(context, template.ast, template);
1136611378
}
1136711379

lib/src/model/comment_referable.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,17 @@ mixin CommentReferable implements Nameable {
9090
'PrefixElement detected, override [lookupViaScope] in subclass');
9191
return null;
9292
}
93-
return recurseChildrenAndFilter(referenceLookup,
94-
ModelElement.fromElement(resultElement, packageGraph), filter);
93+
if (resultElement == null) return null;
94+
var result = ModelElement.fromElement(resultElement, packageGraph);
95+
if (result is Accessor) {
96+
result = (result as Accessor).enclosingCombo;
97+
}
98+
if (result?.enclosingElement is Container) {
99+
assert(false,
100+
'[Container] member detected, override in subclass and handle inheritance');
101+
return null;
102+
}
103+
return recurseChildrenAndFilter(referenceLookup, result, filter);
95104
}
96105

97106
CommentReferable _lookupViaReferenceChildren(

lib/src/model/documentable.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ mixin MarkdownFileDocumentation implements Documentable, Canonicalization {
8181
File get documentationFile;
8282

8383
@override
84-
String get location => packageGraph.resourceProvider.pathContext
85-
.toUri(documentationFile.path)
86-
.toString();
84+
String get location => '(${documentationFile.path})';
8785

8886
@override
8987
Set<String> get locationPieces => <String>{location};

lib/src/model/dynamic.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Dynamic extends ModelElement {
2020
ModelElement get canonicalModelElement => null;
2121

2222
@override
23-
ModelElement get enclosingElement => throw UnsupportedError('');
23+
ModelElement get enclosingElement => null;
2424

2525
/// And similarly, even if someone references it directly it can have
2626
/// no hyperlink.

lib/src/model/library.dart

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:collection';
66

77
import 'package:analyzer/dart/ast/ast.dart' hide CommentReference;
88
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/dart/element/scope.dart';
910
import 'package:analyzer/dart/element/type_system.dart';
1011
import 'package:analyzer/dart/element/visitor.dart';
1112
import 'package:analyzer/source/line_info.dart';
@@ -118,6 +119,10 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
118119
CompilationUnitElement compilationUnit) =>
119120
_getDefinedElements(compilationUnit);
120121

122+
/// Allow scope for Libraries.
123+
@override
124+
Scope get scope => element.scope;
125+
121126
List<String> __allOriginalModelElementNames;
122127

123128
/// Return true if this library is in a package configured to be treated as
@@ -577,7 +582,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
577582
/*late final*/ HashMap<String, Set<ModelElement>> _modelElementsNameMap;
578583

579584
/// Map of [fullyQualifiedNameWithoutLibrary] to all matching [ModelElement]s
580-
/// in this library. Used for code reference lookups.
585+
/// in this library. Used for legacy code reference lookups.
581586
HashMap<String, Set<ModelElement>> get modelElementsNameMap {
582587
if (_modelElementsNameMap == null) {
583588
_modelElementsNameMap = HashMap<String, Set<ModelElement>>();
@@ -656,18 +661,10 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
656661

657662
Map<String, CommentReferable> _referenceChildren;
658663
@override
659-
// TODO(jcollins-g): This should take the import/export graph
660-
// and resulting namespace into account.
661664
Map<String, CommentReferable> get referenceChildren {
662-
return _referenceChildren ??= {
663-
for (var e in constants) e.name: e,
664-
for (var e in enums) e.name: e,
665-
for (var e in extensions) e.name: e,
666-
for (var e in mixins) e.name: e,
667-
for (var e in properties) e.name: e,
668-
for (var e in typedefs) e.name: e,
669-
for (var e in classes) e.name: e,
670-
};
665+
return _referenceChildren ??= Map.fromEntries(
666+
element.exportNamespace.definedNames.entries.map((entry) => MapEntry(
667+
entry.key, ModelElement.fromElement(entry.value, packageGraph))));
671668
}
672669

673670
@override

lib/src/model/package_graph.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,7 @@ class PackageGraph with CommentReferable, Nameable {
494494
if (config.verboseWarnings && extendedDebug != null) {
495495
messageParts.addAll(extendedDebug.map((s) => ' $s'));
496496
}
497-
String fullMessage;
498-
if (messageParts.length <= 2) {
499-
fullMessage = messageParts.join(', ');
500-
} else {
501-
fullMessage = messageParts.join('\n ');
502-
}
497+
var fullMessage = messageParts.join('\n ');
503498

504499
packageWarningCounter.addWarning(warnable, kind, message, fullMessage);
505500
}

test/end2end/model_test.dart

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,16 +2217,12 @@ void main() {
22172217
MatchingLinkResult bothLookup(Warnable element, String codeRef) {
22182218
var originalLookupResult = originalLookup(element, codeRef);
22192219
var newLookupResult = newLookup(element, codeRef);
2220-
expect(newLookupResult, equals(originalLookupResult));
2220+
expect(newLookupResult.isEquivalentTo(originalLookupResult), isTrue);
22212221
return newLookupResult;
22222222
}
22232223

22242224
test('Verify basic linking inside class', () {
2225-
// parameter of [doAwesomeStuff]
2226-
// Parameter lookups are discarded with the original lookup code
2227-
expect(originalLookup(doAwesomeStuff, 'value'),
2228-
equals(MatchingLinkResult(null, warn: false)));
2229-
expect(newLookup(doAwesomeStuff, 'value'),
2225+
expect(bothLookup(doAwesomeStuff, 'value'),
22302226
equals(MatchingLinkResult(doAwesomeStuffParam)));
22312227

22322228
// Parent class of [doAwesomeStuff].
@@ -2240,37 +2236,31 @@ void main() {
22402236
equals(MatchingLinkResult(nameWithSingleUnderscore)));
22412237

22422238
// Top level class from [dart:core].
2243-
// TODO(jcollins-g): dart:core not recognized yet with new lookup code.
2244-
expect(originalLookup(doAwesomeStuff, 'String'),
2239+
expect(bothLookup(doAwesomeStuff, 'String'),
22452240
equals(MatchingLinkResult(string)));
22462241

22472242
// Another method in the same class.
22482243
expect(bothLookup(doAwesomeStuff, 'anotherMethod'),
22492244
equals(MatchingLinkResult(anotherMethod)));
22502245

22512246
// A top level function in this library.
2252-
// TODO(jcollins-g): top level functions not recognized yet with new lookup code.
2253-
expect(originalLookup(doAwesomeStuff, 'topLevelFunction'),
2247+
expect(bothLookup(doAwesomeStuff, 'topLevelFunction'),
22542248
equals(MatchingLinkResult(topLevelFunction)));
22552249

22562250
// A top level function in another library imported into this library.
2257-
// TODO(jcollins-g): namespace lookups are not yet implemented with new lookup code.
2258-
expect(originalLookup(doAwesomeStuff, 'function1'),
2251+
expect(bothLookup(doAwesomeStuff, 'function1'),
22592252
equals(MatchingLinkResult(function1)));
22602253

22612254
// A class in another library imported into this library.
2262-
// TODO(jcollins-g): namespace lookups are not yet implemented with new lookup code.
2263-
expect(originalLookup(doAwesomeStuff, 'Apple'),
2255+
expect(bothLookup(doAwesomeStuff, 'Apple'),
22642256
equals(MatchingLinkResult(Apple)));
22652257

22662258
// A top level constant in this library sharing the same name as a name in another library.
2267-
// TODO(jcollins-g): namespace lookups are not yet implemented with new lookup code.
2268-
expect(originalLookup(doAwesomeStuff, 'incorrectDocReference'),
2259+
expect(bothLookup(doAwesomeStuff, 'incorrectDocReference'),
22692260
equals(MatchingLinkResult(incorrectDocReference)));
22702261

22712262
// A top level constant in another library.
2272-
// TODO(jcollins-g): namespace lookups are not yet implemented with new lookup code.
2273-
expect(originalLookup(doAwesomeStuff, 'incorrectDocReferenceFromEx'),
2263+
expect(bothLookup(doAwesomeStuff, 'incorrectDocReferenceFromEx'),
22742264
equals(MatchingLinkResult(incorrectDocReferenceFromEx)));
22752265

22762266
// A prefixed constant in another library.
@@ -2284,7 +2274,7 @@ void main() {
22842274
equals(MatchingLinkResult(doesStuff)));
22852275

22862276
// A name of a class from an import of a library that exported that name.
2287-
expect(originalLookup(doAwesomeStuff, 'BaseClass'),
2277+
expect(bothLookup(doAwesomeStuff, 'BaseClass'),
22882278
equals(MatchingLinkResult(BaseClass)));
22892279

22902280
// A bracket operator within this class.

tool/grind.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,17 @@ void dartfmt() async {
261261
// Filter out test packages as they always have strange formatting.
262262
// Passing parameters to dartfmt for directories to search results in
263263
// filenames being stripped of the dirname so we have to filter here.
264-
void addFileToFix(String base, String fileName) {
264+
void addFileToFix(String line) {
265+
if (!line.startsWith('Changed ')) return;
266+
var fileName = line.substring(8);
265267
var pathComponents = path.split(fileName);
266268
if (pathComponents.isNotEmpty && pathComponents.first == 'testing') {
267269
return;
268270
}
269-
filesToFix.add(path.join(base, fileName));
271+
filesToFix.add(fileName);
270272
}
271273

272-
log('Validating dartfmt with version ${Platform.version}');
274+
log('Validating dart format with version ${Platform.version}');
273275
// TODO(jcollins-g): return to global once dartfmt can handle generic
274276
// type aliases
275277
for (var subDirectory in [
@@ -280,17 +282,19 @@ void dartfmt() async {
280282
path.join('testing/test_package')
281283
]) {
282284
await SubprocessLauncher('dartfmt').runStreamed(
283-
sdkBin('dartfmt'),
285+
sdkBin('dart'),
284286
[
285-
'-n',
287+
'format',
288+
'-o',
289+
'none',
286290
subDirectory,
287291
],
288-
perLine: (n) => addFileToFix(subDirectory, n));
292+
perLine: (n) => addFileToFix(n));
289293
}
290294
if (filesToFix.isNotEmpty) {
291295
fail(
292-
'dartfmt found files needing reformatting. Use this command to reformat:\n'
293-
'dartfmt -w ${filesToFix.map((f) => "\'$f\'").join(' ')}');
296+
'dart format found files needing reformatting. Use this command to reformat:\n'
297+
'dart format ${filesToFix.map((f) => "\'$f\'").join(' ')}');
294298
}
295299
} else {
296300
log('Skipping dartfmt check, requires latest dev version of SDK');

0 commit comments

Comments
 (0)