Skip to content

Fix embedded SDK detection by library #1648

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

Merged
merged 2 commits into from
Mar 28, 2018
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
17 changes: 1 addition & 16 deletions lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'dart:io';

import 'package:analyzer/dart/element/element.dart';
import 'package:dartdoc/dartdoc.dart';
import 'package:path/path.dart' as pathLib;

import 'model.dart';

Expand All @@ -20,21 +19,7 @@ class LocalConfig {
LocalConfig._(this.categoryMap, this.packageMeta);

factory LocalConfig.fromLibrary(LibraryElement element) {
return new LocalConfig._({}, getPackageMeta(element));
}

static PackageMeta getPackageMeta(LibraryElement element) {
String sourcePath = element.source.fullName;
File file = new File(pathLib.canonicalize(sourcePath));
Directory dir = file.parent;
while (dir.parent.path != dir.path && dir.existsSync()) {
File pubspec = new File(pathLib.join(dir.path, 'pubspec.yaml'));
if (pubspec.existsSync()) {
return new PackageMeta.fromDir(dir);
}
dir = dir.parent;
}
return null;
return new LocalConfig._({}, new PackageMeta.fromElement(element));
}
}

Expand Down
13 changes: 4 additions & 9 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,7 @@ class Library extends ModelElement with Categorization {
PackageMeta _packageMeta;
PackageMeta get packageMeta {
if (_packageMeta == null) {
_packageMeta = getPackageMeta(element);
_packageMeta = new PackageMeta.fromElement(element);
}
return _packageMeta;
}
Expand Down Expand Up @@ -2202,12 +2202,6 @@ class Library extends ModelElement with Categorization {
return name;
}

static PackageMeta getPackageMeta(Element element) {
String sourcePath = element.source.fullName;
return new PackageMeta.fromDir(
new File(pathLib.canonicalize(sourcePath)).parent);
}

static String getLibraryName(LibraryElement element) {
String name = element.name;
if (name == null || name.isEmpty) {
Expand Down Expand Up @@ -4582,7 +4576,7 @@ class Package extends LibraryContainer implements Comparable<Package>, Privacy {
// Workaround for mustache4dart issue where templates do not recognize
// inherited properties as being in-context.
@override
List<Library> get publicLibraries => super.publicLibraries;
Iterable<Library> get publicLibraries => super.publicLibraries;

/// A map of category name to the category itself.
Map<String, Category> get nameToCategory {
Expand Down Expand Up @@ -5267,7 +5261,8 @@ class PackageBuilder {
await driver.getLibraryByUri(source.uri.toString());
if (library != null) {
if (!isExcluded(Library.getLibraryName(library)) &&
!excludePackages.contains(Library.getPackageMeta(library)?.name)) {
!excludePackages
.contains(new PackageMeta.fromElement(library)?.name)) {
libraries.add(library);
sources.add(source);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/src/package_meta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ library dartdoc.package_meta;

import 'dart:io';

import 'package:analyzer/dart/element/element.dart';
import 'package:dartdoc/src/sdk.dart';
import 'package:path/path.dart' as pathLib;
import 'package:yaml/yaml.dart';
Expand All @@ -19,6 +20,13 @@ abstract class PackageMeta {

PackageMeta(this.dir);

/// Use this instead of fromDir where possible.
factory PackageMeta.fromElement(LibraryElement libraryElement) {
if (libraryElement.isInSdk) return new PackageMeta.fromDir(getSdkDir());
return new PackageMeta.fromDir(
new File(pathLib.canonicalize(libraryElement.source.fullName)).parent);
}

/// This factory is guaranteed to return the same object for any given
/// [dir.absolute.path]. Multiple [dir.absolute.path]s will resolve to the
/// same object if they are part of the same package. Returns null
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,4 @@ packages:
source: hosted
version: "2.1.13"
sdks:
dart: ">=2.0.0-dev.23.0 <=2.0.0-dev.40.0"
dart: ">=2.0.0-dev.23.0 <=2.0.0-dev.42.0"
12 changes: 12 additions & 0 deletions test/dartdoc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ library dartdoc.dartdoc_test;

import 'dart:io';

import 'package:analyzer/dart/element/element.dart';
import 'package:dartdoc/dartdoc.dart';
import 'package:dartdoc/src/model.dart';
import 'package:dartdoc/src/package_meta.dart';
Expand Down Expand Up @@ -121,13 +122,24 @@ void main() {
expect(p.libraries.map((lib) => lib.name).contains('dart:core'), isTrue);
expect(p.libraries.map((lib) => lib.name).contains('dart:async'), isTrue);
expect(p.libraries.map((lib) => lib.name).contains('dart:bear'), isTrue);
expect(p.packages.length, equals(2));
// Things that do not override the core SDK belong in their own package?
expect(p.packages["Dart"].isSdk, isTrue);
expect(p.packages["test_package_embedder_yaml"].isSdk, isFalse);
expect(
p.publicLibraries,
everyElement((Library l) =>
(l.element as LibraryElement).isInSdk == l.packageMeta.isSdk));
// Ensure that we actually parsed some source by checking for
// the 'Bear' class.
Library dart_bear =
p.libraries.firstWhere((lib) => lib.name == 'dart:bear');
expect(dart_bear, isNotNull);
expect(
dart_bear.allClasses.map((cls) => cls.name).contains('Bear'), isTrue);
expect(p.packages["test_package_embedder_yaml"].publicLibraries,
contains(dart_bear));
expect(p.packages["Dart"].publicLibraries, hasLength(2));
});
});
}
4 changes: 3 additions & 1 deletion testing/test_package/lib/fake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class Cool {
}

/// A map initialization making use of optional const.
const Map<int, String> myMap = { 1: "hello" };
const Map<int, String> myMap = {1: "hello"};

/// A variable initalization making use of optional new.
Cool aCoolVariable = Cool();
Expand Down Expand Up @@ -289,6 +289,7 @@ void aVoidParameter(Future<void> p1) {}

/// This class extends Future<void>
abstract class ExtendsFutureVoid extends Future<void> {
// ignore: missing_return
factory ExtendsFutureVoid(FutureOr<void> computation()) {}
}

Expand All @@ -297,6 +298,7 @@ abstract class ImplementsFutureVoid implements Future<void> {}

/// This class takes a type, and it might be void.
class ATypeTakingClass<T> {
// ignore: missing_return
T aMethodMaybeReturningVoid() {}
}

Expand Down