Skip to content

Commit c758441

Browse files
authored
Fix embedded SDK detection by library (#1648)
* Fix embedded SDK detection by library * pubspec to non-edge version
1 parent b5da370 commit c758441

File tree

6 files changed

+29
-27
lines changed

6 files changed

+29
-27
lines changed

lib/src/config.dart

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'dart:io';
88

99
import 'package:analyzer/dart/element/element.dart';
1010
import 'package:dartdoc/dartdoc.dart';
11-
import 'package:path/path.dart' as pathLib;
1211

1312
import 'model.dart';
1413

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

2221
factory LocalConfig.fromLibrary(LibraryElement element) {
23-
return new LocalConfig._({}, getPackageMeta(element));
24-
}
25-
26-
static PackageMeta getPackageMeta(LibraryElement element) {
27-
String sourcePath = element.source.fullName;
28-
File file = new File(pathLib.canonicalize(sourcePath));
29-
Directory dir = file.parent;
30-
while (dir.parent.path != dir.path && dir.existsSync()) {
31-
File pubspec = new File(pathLib.join(dir.path, 'pubspec.yaml'));
32-
if (pubspec.existsSync()) {
33-
return new PackageMeta.fromDir(dir);
34-
}
35-
dir = dir.parent;
36-
}
37-
return null;
22+
return new LocalConfig._({}, new PackageMeta.fromElement(element));
3823
}
3924
}
4025

lib/src/model.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,7 +2047,7 @@ class Library extends ModelElement with Categorization {
20472047
PackageMeta _packageMeta;
20482048
PackageMeta get packageMeta {
20492049
if (_packageMeta == null) {
2050-
_packageMeta = getPackageMeta(element);
2050+
_packageMeta = new PackageMeta.fromElement(element);
20512051
}
20522052
return _packageMeta;
20532053
}
@@ -2202,12 +2202,6 @@ class Library extends ModelElement with Categorization {
22022202
return name;
22032203
}
22042204

2205-
static PackageMeta getPackageMeta(Element element) {
2206-
String sourcePath = element.source.fullName;
2207-
return new PackageMeta.fromDir(
2208-
new File(pathLib.canonicalize(sourcePath)).parent);
2209-
}
2210-
22112205
static String getLibraryName(LibraryElement element) {
22122206
String name = element.name;
22132207
if (name == null || name.isEmpty) {
@@ -4582,7 +4576,7 @@ class Package extends LibraryContainer implements Comparable<Package>, Privacy {
45824576
// Workaround for mustache4dart issue where templates do not recognize
45834577
// inherited properties as being in-context.
45844578
@override
4585-
List<Library> get publicLibraries => super.publicLibraries;
4579+
Iterable<Library> get publicLibraries => super.publicLibraries;
45864580

45874581
/// A map of category name to the category itself.
45884582
Map<String, Category> get nameToCategory {
@@ -5267,7 +5261,8 @@ class PackageBuilder {
52675261
await driver.getLibraryByUri(source.uri.toString());
52685262
if (library != null) {
52695263
if (!isExcluded(Library.getLibraryName(library)) &&
5270-
!excludePackages.contains(Library.getPackageMeta(library)?.name)) {
5264+
!excludePackages
5265+
.contains(new PackageMeta.fromElement(library)?.name)) {
52715266
libraries.add(library);
52725267
sources.add(source);
52735268
}

lib/src/package_meta.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ library dartdoc.package_meta;
66

77
import 'dart:io';
88

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

2021
PackageMeta(this.dir);
2122

23+
/// Use this instead of fromDir where possible.
24+
factory PackageMeta.fromElement(LibraryElement libraryElement) {
25+
if (libraryElement.isInSdk) return new PackageMeta.fromDir(getSdkDir());
26+
return new PackageMeta.fromDir(
27+
new File(pathLib.canonicalize(libraryElement.source.fullName)).parent);
28+
}
29+
2230
/// This factory is guaranteed to return the same object for any given
2331
/// [dir.absolute.path]. Multiple [dir.absolute.path]s will resolve to the
2432
/// same object if they are part of the same package. Returns null

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,4 @@ packages:
408408
source: hosted
409409
version: "2.1.13"
410410
sdks:
411-
dart: ">=2.0.0-dev.23.0 <=2.0.0-dev.40.0"
411+
dart: ">=2.0.0-dev.23.0 <=2.0.0-dev.42.0"

test/dartdoc_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ library dartdoc.dartdoc_test;
66

77
import 'dart:io';
88

9+
import 'package:analyzer/dart/element/element.dart';
910
import 'package:dartdoc/dartdoc.dart';
1011
import 'package:dartdoc/src/model.dart';
1112
import 'package:dartdoc/src/package_meta.dart';
@@ -121,13 +122,24 @@ void main() {
121122
expect(p.libraries.map((lib) => lib.name).contains('dart:core'), isTrue);
122123
expect(p.libraries.map((lib) => lib.name).contains('dart:async'), isTrue);
123124
expect(p.libraries.map((lib) => lib.name).contains('dart:bear'), isTrue);
125+
expect(p.packages.length, equals(2));
126+
// Things that do not override the core SDK belong in their own package?
127+
expect(p.packages["Dart"].isSdk, isTrue);
128+
expect(p.packages["test_package_embedder_yaml"].isSdk, isFalse);
129+
expect(
130+
p.publicLibraries,
131+
everyElement((Library l) =>
132+
(l.element as LibraryElement).isInSdk == l.packageMeta.isSdk));
124133
// Ensure that we actually parsed some source by checking for
125134
// the 'Bear' class.
126135
Library dart_bear =
127136
p.libraries.firstWhere((lib) => lib.name == 'dart:bear');
128137
expect(dart_bear, isNotNull);
129138
expect(
130139
dart_bear.allClasses.map((cls) => cls.name).contains('Bear'), isTrue);
140+
expect(p.packages["test_package_embedder_yaml"].publicLibraries,
141+
contains(dart_bear));
142+
expect(p.packages["Dart"].publicLibraries, hasLength(2));
131143
});
132144
});
133145
}

testing/test_package/lib/fake.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class Cool {
224224
}
225225

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

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

290290
/// This class extends Future<void>
291291
abstract class ExtendsFutureVoid extends Future<void> {
292+
// ignore: missing_return
292293
factory ExtendsFutureVoid(FutureOr<void> computation()) {}
293294
}
294295

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

298299
/// This class takes a type, and it might be void.
299300
class ATypeTakingClass<T> {
301+
// ignore: missing_return
300302
T aMethodMaybeReturningVoid() {}
301303
}
302304

0 commit comments

Comments
 (0)