Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnamed libraries #3189

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions lib/src/model/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions test/end2end/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
Expand Down
93 changes: 88 additions & 5 deletions test/library_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down