From 10700c78f2648d4337dcc58b465310abb9e88347 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Tue, 3 Sep 2024 13:06:45 -0700 Subject: [PATCH 1/2] Correct the wildcard tests for import prefixes. An unrelated fix, https://github.com/dart-lang/dartdoc/pull/3865, revealed that the wildcard-variables experiment was not actually enabled in tests; the SDK constraint used in test packages needs to be '^3.6.0'. With that fix, there are some questions about how much dartdoc should be doing to keep wildcards "non-binding" and how much the analyzer should be doing. So that is raised in https://github.com/dart-lang/dartdoc/issues/3769#issuecomment-2327129506. --- lib/src/model/comment_referable.dart | 8 ++++++++ lib/src/model/library.dart | 6 ++++++ test/dartdoc_test_base.dart | 5 ++++- test/prefixes_test.dart | 14 +++++++++----- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/src/model/comment_referable.dart b/lib/src/model/comment_referable.dart index 1b86636b3d..0a0015e15d 100644 --- a/lib/src/model/comment_referable.dart +++ b/lib/src/model/comment_referable.dart @@ -7,12 +7,14 @@ library; import 'dart:core'; +import 'package:analyzer/dart/analysis/features.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/scope.dart'; import 'package:collection/collection.dart'; import 'package:dartdoc/src/model/library.dart'; import 'package:dartdoc/src/model/model_element.dart'; import 'package:dartdoc/src/model/nameable.dart'; +import 'package:dartdoc/src/model/prefix.dart'; import 'package:meta/meta.dart'; class _ReferenceChildrenLookup { @@ -55,6 +57,12 @@ mixin CommentReferable implements Nameable { // First attempt: Ask analyzer's `Scope.lookup` API. var result = _lookupViaScope(referenceLookup, filter: filter); if (result != null) { + if (result is Prefix && + result.name == '_' && + library!.element.featureSet.isEnabled(Feature.wildcard_variables)) { + // A wildcard import prefix is non-binding. + continue; + } return result; } diff --git a/lib/src/model/library.dart b/lib/src/model/library.dart index ac8a50d31b..0cb9045858 100644 --- a/lib/src/model/library.dart +++ b/lib/src/model/library.dart @@ -2,6 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'package:analyzer/dart/analysis/features.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/scope.dart'; import 'package:analyzer/source/line_info.dart'; @@ -442,6 +443,11 @@ class Library extends ModelElement // ambiguous. dart-lang/dartdoc#2683. for (var MapEntry(key: prefix, value: libraries) in _prefixToLibrary.entries) { + if (prefix == '_' && + element.featureSet.isEnabled(Feature.wildcard_variables)) { + // A wildcard import prefix is non-binding. + continue; + } referenceChildrenBuilder.putIfAbsent(prefix, () => libraries.first); } return referenceChildrenBuilder; diff --git a/test/dartdoc_test_base.dart b/test/dartdoc_test_base.dart index da629b2cd8..f65a7c76a0 100644 --- a/test/dartdoc_test_base.dart +++ b/test/dartdoc_test_base.dart @@ -36,9 +36,12 @@ abstract class DartdocTestBase { String get linkPrefix => '$placeholder$libraryName'; + String get dartAsyncUrlPrefix => + 'https://api.dart.dev/stable/3.2.0/dart-async'; + String get dartCoreUrlPrefix => 'https://api.dart.dev/stable/3.2.0/dart-core'; - String get sdkConstraint => '>=3.3.0 <4.0.0'; + String get sdkConstraint => '>=3.6.0 <4.0.0'; List get experiments => ['wildcard-variables']; diff --git a/test/prefixes_test.dart b/test/prefixes_test.dart index f7d10c26bc..845311c3ba 100644 --- a/test/prefixes_test.dart +++ b/test/prefixes_test.dart @@ -19,6 +19,8 @@ class PrefixesTest extends DartdocTestBase { @override String get libraryName => 'prefixes'; + @FailingTest( + reason: 'requires https://github.com/dart-lang/dartdoc/pull/3865') void test_referenced() async { var library = await bootPackageWithLibrary( ''' @@ -29,9 +31,11 @@ int x = 0; ''', additionalArguments: ['--link-to-remote'], ); - var f = library.properties.named('x'); - // There is no link, but also no wrong link or crash. - expect(f.documentationAsHtml, '

Text async.

'); + var x = library.properties.named('x'); + expect( + x.documentationAsHtml, + '

Text async.

', + ); } void test_referenced_wildcard() async { @@ -44,8 +48,8 @@ int x = 0; ''', additionalArguments: ['--link-to-remote'], ); - var f = library.properties.named('x'); + var x = library.properties.named('x'); // There is no link, but also no wrong link or crash. - expect(f.documentationAsHtml, '

Text _.

'); + expect(x.documentationAsHtml, '

Text _.

'); } } From bcfb67265002e10204feac2163cb6077653d8226 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Tue, 3 Sep 2024 15:12:52 -0700 Subject: [PATCH 2/2] Tests now pass --- test/prefixes_test.dart | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/prefixes_test.dart b/test/prefixes_test.dart index 04c6921559..e6f4de8e7c 100644 --- a/test/prefixes_test.dart +++ b/test/prefixes_test.dart @@ -19,8 +19,6 @@ class PrefixesTest extends DartdocTestBase { @override String get libraryName => 'prefixes'; - @FailingTest( - reason: 'requires https://github.com/dart-lang/dartdoc/pull/3865') void test_referenced() async { var library = await bootPackageWithLibrary( ''' @@ -38,7 +36,6 @@ int x = 0; ); } - @FailingTest(issue: 'https://github.com/dart-lang/dartdoc/issues/3769') void test_referenced_wildcard() async { var library = await bootPackageWithLibrary( '''