Skip to content

Commit e986524

Browse files
authored
Cloak pragma and fix tests for 2.1.0-dev (#1738)
* Cloak pragma and fix tests for 2.1.0-dev * fix tests and rework annotations * dartfmt
1 parent 9637f21 commit e986524

File tree

119 files changed

+1470
-37
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+1470
-37
lines changed

lib/src/model.dart

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,16 +2729,42 @@ abstract class ModelElement extends Canonicalization
27292729

27302730
/// Returns linked annotations from a given metadata set, with escaping.
27312731
List<String> annotationsFromMetadata(List<ElementAnnotation> md) {
2732-
if (md == null) return <String>[];
2733-
return md.map((ElementAnnotation a) {
2732+
List<String> annotationStrings = [];
2733+
if (md == null) return annotationStrings;
2734+
for (ElementAnnotation a in md) {
27342735
String annotation = (const HtmlEscape()).convert(a.toSource());
2735-
// a.element can be null if the element can't be resolved.
2736-
var me = packageGraph
2737-
.findCanonicalModelElementFor(a.element?.enclosingElement);
2738-
if (me != null)
2739-
annotation = annotation.replaceFirst(me.name, me.linkedName);
2740-
return annotation;
2741-
}).toList(growable: false);
2736+
Element annotationElement = a.element;
2737+
2738+
ClassElement annotationClassElement;
2739+
if (annotationElement is ExecutableElement) {
2740+
annotationElement = (annotationElement as ExecutableElement)
2741+
.returnType
2742+
.element as ClassElement;
2743+
}
2744+
if (annotationElement is ClassElement) {
2745+
annotationClassElement = annotationElement;
2746+
}
2747+
ModelElement annotationModelElement =
2748+
packageGraph.findCanonicalModelElementFor(annotationElement);
2749+
// annotationElement can be null if the element can't be resolved.
2750+
Class annotationClass = packageGraph
2751+
.findCanonicalModelElementFor(annotationClassElement) as Class;
2752+
if (annotationClass == null && annotationElement != null) {
2753+
annotationClass =
2754+
new ModelElement.fromElement(annotationClassElement, packageGraph)
2755+
as Class;
2756+
}
2757+
// Some annotations are intended to be invisible (@pragma)
2758+
if (annotationClass == null ||
2759+
!packageGraph.invisibleAnnotations.contains(annotationClass)) {
2760+
if (annotationModelElement != null) {
2761+
annotation = annotation.replaceFirst(
2762+
annotationModelElement.name, annotationModelElement.linkedName);
2763+
}
2764+
annotationStrings.add(annotation);
2765+
}
2766+
}
2767+
return annotationStrings;
27422768
}
27432769

27442770
bool _isPublic;
@@ -4510,12 +4536,13 @@ class PackageGraph {
45104536
return _localPublicLibraries;
45114537
}
45124538

4513-
// Return the set of [Class]es objects should inherit through if they
4514-
// show up in the inheritance chain. Do not call before interceptorElement is
4515-
// found. Add classes here if they are similar to Interceptor in that they
4516-
// are to be ignored even when they are the implementors of [Inheritable]s,
4517-
// and the class these inherit from should instead claim implementation.
45184539
Set<Class> _inheritThrough;
4540+
4541+
/// Return the set of [Class]es objects should inherit through if they
4542+
/// show up in the inheritance chain. Do not call before interceptorElement is
4543+
/// found. Add classes here if they are similar to Interceptor in that they
4544+
/// are to be ignored even when they are the implementors of [Inheritable]s,
4545+
/// and the class these inherit from should instead claim implementation.
45194546
Set<Class> get inheritThrough {
45204547
if (_inheritThrough == null) {
45214548
_inheritThrough = new Set();
@@ -4524,6 +4551,18 @@ class PackageGraph {
45244551
return _inheritThrough;
45254552
}
45264553

4554+
Set<Class> _invisibleAnnotations;
4555+
4556+
/// Returns the set of [Class] objects that are similar to pragma
4557+
/// in that we should never count them as documentable annotations.
4558+
Set<Class> get invisibleAnnotations {
4559+
if (_invisibleAnnotations == null) {
4560+
_invisibleAnnotations = new Set();
4561+
_invisibleAnnotations.add(specialClasses[SpecialClass.pragma]);
4562+
}
4563+
return _invisibleAnnotations;
4564+
}
4565+
45274566
/// Looks up some [Library] that is reexporting this [Element]; not
45284567
/// necessarily the canonical [Library].
45294568
Library findLibraryFor(Element element) {
@@ -5007,8 +5046,10 @@ class Package extends LibraryContainer
50075046
// The full version string of the package.
50085047
case 'v':
50095048
return packageMeta.version;
5049+
default:
5050+
assert(false, 'Unsupported case: ${m.group(1)}');
5051+
return null;
50105052
}
5011-
;
50125053
});
50135054
if (!_baseHref.endsWith('/')) _baseHref = '${_baseHref}/';
50145055
} else {

lib/src/special_elements.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ enum SpecialClass {
2020

2121
/// From dart:_interceptors, Interceptor
2222
interceptor,
23+
24+
/// From dart:core, pragma
25+
pragma,
2326
}
2427

2528
/// A declaration of a special [Class] and how to find it.
@@ -66,6 +69,9 @@ final List<_SpecialClassDefinition> _specialClassDefinitions = [
6669
new _SpecialClassDefinition(SpecialClass.interceptor, 'Interceptor',
6770
'_interceptors', 'dart:_interceptors',
6871
required: false),
72+
new _SpecialClassDefinition(
73+
SpecialClass.pragma, 'pragma', 'dart.core', 'dart:core',
74+
required: false),
6975
];
7076

7177
/// Given a SDK, resolve URIs for the libraries containing our special

test/compare_output_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void main() {
8383
'--example-path-prefix',
8484
'examples',
8585
'--exclude-packages',
86-
'Dart,matcher,meta,path,stack_trace,tuple,quiver',
86+
'Dart,matcher,meta,path,stack_trace,quiver',
8787
'--hide-sdk-text',
8888
'--no-include-source',
8989
'--output',

test/dartdoc_test.dart

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,23 @@ void main() {
7474
await buildDartdoc(['--link-to-remote'], testPackageDir);
7575
DartdocResults results = await dartdoc.generateDocs();
7676
PackageGraph p = results.packageGraph;
77-
Package tuple = p.publicPackages.firstWhere((p) => p.name == 'tuple');
77+
Package meta = p.publicPackages.firstWhere((p) => p.name == 'meta');
7878
TopLevelVariable useSomethingInAnotherPackage = p.publicLibraries
7979
.firstWhere((l) => l.name == 'fake')
8080
.properties
8181
.firstWhere((p) => p.name == 'useSomethingInAnotherPackage');
82-
expect(tuple.documentedWhere, equals(DocumentLocation.remote));
83-
expect(
84-
(useSomethingInAnotherPackage.modelType.typeArguments.first
85-
as ParameterizedElementType)
86-
.element
87-
.package
88-
.documentedWhere,
89-
equals(DocumentLocation.remote));
82+
TopLevelVariable useSomethingInTheSdk = p.publicLibraries
83+
.firstWhere((l) => l.name == 'fake')
84+
.properties
85+
.firstWhere((p) => p.name == 'useSomethingInTheSdk');
86+
expect(meta.documentedWhere, equals(DocumentLocation.remote));
9087
expect(
9188
useSomethingInAnotherPackage.modelType.linkedName,
9289
startsWith(
93-
'<a href="https://pub.dartlang.org/documentation/tuple/1.0.2/tuple/Tuple2-class.html">Tuple2</a>'));
90+
'<a href=\"https://pub.dartlang.org/documentation/meta/1.1.6/meta/Required-class.html\">Required</a>'));
9491
RegExp stringLink = new RegExp(
9592
'https://api.dartlang.org/(dev|stable|edge|be)/${Platform.version.split(' ').first}/dart-core/String-class.html">String</a>');
96-
expect(useSomethingInAnotherPackage.modelType.linkedName,
97-
contains(stringLink));
93+
expect(useSomethingInTheSdk.modelType.linkedName, contains(stringLink));
9894
});
9995

10096
test('generate docs for ${pathLib.basename(testPackageDir.path)} works',

test/model_test.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ void main() {
227227
});
228228

229229
test('multiple packages, sorted default', () {
230-
expect(ginormousPackageGraph.localPackages, hasLength(9));
230+
expect(ginormousPackageGraph.localPackages, hasLength(4));
231231
expect(ginormousPackageGraph.localPackages.first.name,
232232
equals('test_package'));
233233
});
@@ -322,6 +322,15 @@ void main() {
322322
.any((et) => et.name == 'Interceptor'),
323323
isFalse);
324324
});
325+
326+
test('Verify pragma is hidden in docs', () {
327+
Class pragmaModelElement =
328+
sdkAsPackageGraph.specialClasses[SpecialClass.pragma];
329+
Class HasPragma = fakeLibrary.allClasses
330+
.firstWhere((Class c) => c.name == 'HasPragma');
331+
expect(pragmaModelElement.name, equals('pragma'));
332+
expect(HasPragma.annotations, isEmpty);
333+
});
325334
});
326335
});
327336

testing/test_package/lib/fake.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ library fake;
4848

4949
import 'dart:async';
5050
import 'dart:collection';
51-
52-
import 'package:tuple/tuple.dart';
53-
51+
import 'package:meta/meta.dart' show Required;
5452
import 'css.dart' as css;
5553
import 'example.dart';
5654
import 'two_exports.dart' show BaseClass;
@@ -91,6 +89,10 @@ class HasGenerics<X, Y, Z> {
9189
Map<X, Y> convertToMap() => null;
9290
}
9391

92+
/// This class uses a pragma annotation.
93+
@pragma('Hello world')
94+
class HasPragma {}
95+
9496
/// This is a class with a table.
9597
///
9698
/// It has multiple sentences before the table. Because testing is a good
@@ -130,7 +132,8 @@ dynamic get mustGetThis => null;
130132

131133
Map<dynamic, String> mapWithDynamicKeys = {};
132134

133-
Tuple2<String, String> useSomethingInAnotherPackage;
135+
Required useSomethingInAnotherPackage;
136+
String useSomethingInTheSdk;
134137

135138
/// Useful for annotations.
136139
class Annotation {

testing/test_package/pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ description: Best package ever.
44
version: 0.0.1
55
dependencies:
66
meta: ^1.0.0
7-
tuple: 1.0.2
87
test_package_imported:
98
path: "../test_package_imported"

testing/test_package_docs/fake/ABaseClass-class.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ <h5>fake library</h5>
6060
<li><a href="fake/Foo2-class.html">Foo2</a></li>
6161
<li><a href="fake/HasGenerics-class.html">HasGenerics</a></li>
6262
<li><a href="fake/HasGenericWithExtends-class.html">HasGenericWithExtends</a></li>
63+
<li><a href="fake/HasPragma-class.html">HasPragma</a></li>
6364
<li><a href="fake/ImplementingThingy-class.html">ImplementingThingy</a></li>
6465
<li><a href="fake/ImplementingThingy2-class.html">ImplementingThingy2</a></li>
6566
<li><a href="fake/ImplementsFutureVoid-class.html">ImplementsFutureVoid</a></li>
@@ -114,6 +115,7 @@ <h5>fake library</h5>
114115
<li><a href="fake/setAndGet.html">setAndGet</a></li>
115116
<li><a href="fake/simpleProperty.html">simpleProperty</a></li>
116117
<li><a href="fake/useSomethingInAnotherPackage.html">useSomethingInAnotherPackage</a></li>
118+
<li><a href="fake/useSomethingInTheSdk.html">useSomethingInTheSdk</a></li>
117119

118120
<li class="section-title"><a href="fake/fake-library.html#functions">Functions</a></li>
119121
<li><a href="fake/addCallback.html">addCallback</a></li>

testing/test_package_docs/fake/AClassUsingASuperMixin-class.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ <h5>fake library</h5>
6060
<li><a href="fake/Foo2-class.html">Foo2</a></li>
6161
<li><a href="fake/HasGenerics-class.html">HasGenerics</a></li>
6262
<li><a href="fake/HasGenericWithExtends-class.html">HasGenericWithExtends</a></li>
63+
<li><a href="fake/HasPragma-class.html">HasPragma</a></li>
6364
<li><a href="fake/ImplementingThingy-class.html">ImplementingThingy</a></li>
6465
<li><a href="fake/ImplementingThingy2-class.html">ImplementingThingy2</a></li>
6566
<li><a href="fake/ImplementsFutureVoid-class.html">ImplementsFutureVoid</a></li>
@@ -114,6 +115,7 @@ <h5>fake library</h5>
114115
<li><a href="fake/setAndGet.html">setAndGet</a></li>
115116
<li><a href="fake/simpleProperty.html">simpleProperty</a></li>
116117
<li><a href="fake/useSomethingInAnotherPackage.html">useSomethingInAnotherPackage</a></li>
118+
<li><a href="fake/useSomethingInTheSdk.html">useSomethingInTheSdk</a></li>
117119

118120
<li class="section-title"><a href="fake/fake-library.html#functions">Functions</a></li>
119121
<li><a href="fake/addCallback.html">addCallback</a></li>

testing/test_package_docs/fake/AClassWithFancyProperties-class.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ <h5>fake library</h5>
6060
<li><a href="fake/Foo2-class.html">Foo2</a></li>
6161
<li><a href="fake/HasGenerics-class.html">HasGenerics</a></li>
6262
<li><a href="fake/HasGenericWithExtends-class.html">HasGenericWithExtends</a></li>
63+
<li><a href="fake/HasPragma-class.html">HasPragma</a></li>
6364
<li><a href="fake/ImplementingThingy-class.html">ImplementingThingy</a></li>
6465
<li><a href="fake/ImplementingThingy2-class.html">ImplementingThingy2</a></li>
6566
<li><a href="fake/ImplementsFutureVoid-class.html">ImplementsFutureVoid</a></li>
@@ -114,6 +115,7 @@ <h5>fake library</h5>
114115
<li><a href="fake/setAndGet.html">setAndGet</a></li>
115116
<li><a href="fake/simpleProperty.html">simpleProperty</a></li>
116117
<li><a href="fake/useSomethingInAnotherPackage.html">useSomethingInAnotherPackage</a></li>
118+
<li><a href="fake/useSomethingInTheSdk.html">useSomethingInTheSdk</a></li>
117119

118120
<li class="section-title"><a href="fake/fake-library.html#functions">Functions</a></li>
119121
<li><a href="fake/addCallback.html">addCallback</a></li>

0 commit comments

Comments
 (0)