Repository: dart-lang/dartdoc

Issue:
Unnamed libraries support
This is the dartdoc issue for https://github.com/dart-lang/language/issues/1073.



Fix patch:
diff --git a/lib/src/model/library.dart b/lib/src/model/library.dart
index e366c6be1c..dff239650f 100644
--- a/lib/src/model/library.dart
+++ b/lib/src/model/library.dart
@@ -127,12 +127,13 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
   @override
   Scope get scope => element.scope;
 
-  /// Return true if this library is in a package configured to be treated as
-  /// as using Null safety and itself uses Null safety.
+  /// Whether this library is in a package configured to be treated as using
+  /// null safety and itself uses null safety.
   bool get _allowsNullSafety => element.isNonNullableByDefault;
 
-  /// Return true if this library should be documented as using Null safety.
-  /// A library may use Null safety but not documented that way.
+  /// Whether this library should be documented as using null safety.
+  ///
+  /// A library may use null safety but not be documented that way.
   @override
   bool get isNullSafety =>
       config.enableExperiment.contains('non-nullable') && _allowsNullSafety;
@@ -193,12 +194,8 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
       .map((e) => modelBuilder.from(e, this) as Extension)
       .toList(growable: false);
 
-  SdkLibrary? get sdkLib {
-    if (packageGraph.sdkLibrarySources.containsKey(element.librarySource)) {
-      return packageGraph.sdkLibrarySources[element.librarySource];
-    }
-    return null;
-  }
+  SdkLibrary? get sdkLib =>
+      packageGraph.sdkLibrarySources[element.librarySource];
 
   @override
   bool get isPublic {
@@ -335,18 +332,22 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
   late final String name = () {
     var source = element.source;
     if (source.uri.isScheme('dart')) {
-      // There are inconsistencies in library naming + URIs for the dart
-      // internal libraries; rationalize them here.
+      // There are inconsistencies in library naming + URIs for the Dart
+      // SDK libraries; we rationalize them here.
       if (source.uri.toString().contains('/')) {
         return element.name.replaceFirst('dart.', 'dart:');
       }
       return source.uri.toString();
     } else if (element.name.isNotEmpty) {
+      // An empty name indicates that the library is "implicitly named" with the
+      // empty string. That is, it either has no `library` directive, or it has
+      // a `library` directive with no name.
       return element.name;
     }
     var baseName = pathContext.basename(source.fullName);
     if (baseName.endsWith('.dart')) {
-      return baseName.substring(0, baseName.length - '.dart'.length);
+      const dartExtensionLength = '.dart'.length;
+      return baseName.substring(0, baseName.length - dartExtensionLength);
     }
     return baseName;
   }();
diff --git a/pubspec.yaml b/pubspec.yaml
index 8498f87e20..fa49531c37 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -8,7 +8,7 @@ environment:
   sdk: '>=2.17.0 <3.0.0'
 
 dependencies:
-  analyzer: ">=4.7.0 <6.0.0"
+  analyzer: "^5.1.0"
   args: ^2.3.0
   cli_util: ^0.3.5
   collection: ^1.15.0


Test patch:
diff --git a/test/end2end/model_test.dart b/test/end2end/model_test.dart
index 33577b16fc..02d74f4e84 100644
--- a/test/end2end/model_test.dart
+++ b/test/end2end/model_test.dart
@@ -917,14 +917,6 @@ void main() {
       expect(exLibrary.characterLocation, isNotNull);
     });
 
-    test('packageName', () {
-      expect(exLibrary.packageName, 'test_package');
-    });
-
-    test('has a fully qualified name', () {
-      expect(exLibrary.fullyQualifiedName, 'ex');
-    });
-
     test('can be deprecated', () {
       expect(isDeprecated.isDeprecated, isTrue);
       expect(anonLib.isDeprecated, isFalse);
@@ -939,18 +931,6 @@ void main() {
       expect(isDeprecated.linkedName, contains('class="deprecated"'));
     });
 
-    test('has documentation', () {
-      expect(exLibrary.documentation,
-          'a library. testing string escaping: `var s = \'a string\'` <cool>\n');
-    });
-
-    test('has one line docs', () {
-      expect(
-          fakeLibrary.oneLineDoc,
-          equals(
-              'WOW FAKE PACKAGE IS <strong>BEST</strong> <a href="http://example.org">PACKAGE</a>'));
-    });
-
     test('has properties', () {
       expect(exLibrary.hasPublicProperties, isTrue);
     });
diff --git a/test/library_test.dart b/test/library_test.dart
index 003825dfee..ddc8db8cb2 100644
--- a/test/library_test.dart
+++ b/test/library_test.dart
@@ -2,19 +2,102 @@
 // 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/file_system/memory_file_system.dart';
 import 'package:dartdoc/src/model/model.dart';
 import 'package:test/test.dart';
 
-import 'src/utils.dart' as utils;
+import 'src/test_descriptor_utils.dart' as d;
+import 'src/utils.dart';
 
 void main() {
+  test('A named library', () async {
+    var packageMetaProvider = testPackageMetaProvider;
+
+    var packagePath = await d.createPackage(
+      'test_package',
+      libFiles: [
+        d.file('lib.dart', '''
+/// A doc comment.
+///
+/// **Details**.
+library name.and.dots;
+'''),
+      ],
+      resourceProvider:
+          packageMetaProvider.resourceProvider as MemoryResourceProvider,
+    );
+    final packageConfigProvider =
+        getTestPackageConfigProvider(packageMetaProvider.defaultSdkDir.path);
+    packageConfigProvider.addPackageToConfigFor(
+        packagePath, 'library_test', Uri.file('$packagePath/'));
+
+    final packageGraph = await bootBasicPackage(
+      packagePath,
+      packageMetaProvider,
+      packageConfigProvider,
+    );
+    final library = packageGraph.libraries.named('name.and.dots');
+    expect(library.name, 'name.and.dots');
+    expect(library.fullyQualifiedName, 'name.and.dots');
+    expect(library.packageName, 'test_package');
+    expect(library.documentationComment, '''
+/// A doc comment.
+///
+/// **Details**.''');
+    expect(library.documentation, '''
+A doc comment.
+
+**Details**.''');
+    expect(library.oneLineDoc, 'A doc comment.');
+  });
+
+  test('An unnamed library', () async {
+    var packageMetaProvider = testPackageMetaProvider;
+
+    var packagePath = await d.createPackage(
+      'test_package',
+      libFiles: [
+        d.file('lib.dart', '''
+/// A doc comment.
+///
+/// **Details**.
+library;
+'''),
+      ],
+      resourceProvider:
+          packageMetaProvider.resourceProvider as MemoryResourceProvider,
+    );
+    final packageConfigProvider =
+        getTestPackageConfigProvider(packageMetaProvider.defaultSdkDir.path);
+    packageConfigProvider.addPackageToConfigFor(
+        packagePath, 'library_test', Uri.file('$packagePath/'));
+
+    final packageGraph = await bootBasicPackage(
+      packagePath,
+      packageMetaProvider,
+      packageConfigProvider,
+    );
+    final library = packageGraph.libraries.named('lib');
+    expect(library.name, 'lib');
+    expect(library.fullyQualifiedName, 'lib');
+    expect(library.packageName, 'test_package');
+    expect(library.documentationComment, '''
+/// A doc comment.
+///
+/// **Details**.''');
+    expect(library.documentation, '''
+A doc comment.
+
+**Details**.''');
+    expect(library.oneLineDoc, 'A doc comment.');
+  });
+
   test('libraries in SDK package have appropriate data', () async {
-    var packageMetaProvider = utils.testPackageMetaProvider;
+    var packageMetaProvider = testPackageMetaProvider;
     var sdkFolder = packageMetaProvider.defaultSdkDir;
-    var packageConfigProvider =
-        utils.getTestPackageConfigProvider(sdkFolder.path);
+    var packageConfigProvider = getTestPackageConfigProvider(sdkFolder.path);
 
-    var packageGraph = await utils.bootBasicPackage(
+    var packageGraph = await bootBasicPackage(
         sdkFolder.path, packageMetaProvider, packageConfigProvider,
         additionalArguments: [
           '--input',