Skip to content

Commit fe8a95f

Browse files
authored
Cleanup for dart 2 and add unit testing with --preview-dart-2 (#1669)
* Cleanup for dart 2 and add unit testing with --preview-dart-2 * Try to hack around Windows failures * Clarify how MultiFutureTracker works * Remove extra attempt to find categories
1 parent d7e6ea5 commit fe8a95f

12 files changed

+141
-55
lines changed

lib/src/dartdoc_options.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ abstract class _DartdocArgOption<T> implements DartdocOption<T> {
634634
retval = double.tryParse(_argResults[argName]) as T;
635635
if (retval == null) _throwErrorForTypes(_argResults[argName]);
636636
} else if (_isMapString) {
637-
retval = {} as T;
637+
retval = <String, String>{} as T;
638638
for (String pair in _argResults[argName]) {
639639
List<String> pairList = pair.split('::');
640640
if (pairList.length != 2) {
@@ -788,7 +788,9 @@ class _FileDartdocOptions extends DartdocOptions {
788788
_categoryOrder = [];
789789
if (_dartdocOptions.containsKey('categoryOrder')) {
790790
if (_dartdocOptions['categoryOrder'] is YamlList) {
791-
_categoryOrder.addAll(_dartdocOptions['categoryOrder']);
791+
_categoryOrder.addAll(_dartdocOptions['categoryOrder']
792+
.map((c) => c.toString())
793+
.cast<String>());
792794
} else {
793795
logWarning('${_path}: categoryOrder must be a list (ignoring)');
794796
}

lib/src/element_type.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ abstract class CallableElementTypeMixin implements ParameterizedElementType {
254254
@override
255255
// TODO(jcollins-g): Rewrite this and improve object model so this doesn't
256256
// require type checking everywhere.
257-
Iterable<DefinedElementType> get typeArguments {
257+
Iterable<ElementType> get typeArguments {
258258
if (_typeArguments == null) {
259259
Iterable<DartType> dartTypeArguments;
260260
if (type.typeFormals.isEmpty &&

lib/src/html/html_generator_instance.dart

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

99
import 'package:collection/collection.dart' show compareNatural;
1010
import 'package:dartdoc/src/model_utils.dart';
11+
import 'package:mustache4dart/mustache4dart.dart';
1112
import 'package:path/path.dart' as pathLib;
1213

1314
import '../logging.dart';

lib/src/html/resource_loader.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Future<List<int>> loadAsBytes(String path) async {
2828
throw new ArgumentError('path must begin with package:');
2929
}
3030

31-
var uri = Uri.parse(path);
32-
31+
Uri uri = Uri.parse(path);
3332
return await ResourceLoader.defaultLoader.readAsBytes(uri);
3433
}

lib/src/html/templates.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import 'package:mustache4dart/mustache4dart.dart';
1111

1212
import 'resource_loader.dart' as loader;
1313

14-
typedef String TemplateRenderer(context,
15-
{bool assumeNullNonExistingProperty, bool errorOnMissingProperty});
16-
1714
const _partials = const <String>[
1815
'callable',
1916
'callable_multiline',
@@ -112,7 +109,7 @@ class Templates {
112109

113110
Future<TemplateRenderer> _loadTemplate(String templatePath) async {
114111
String templateContents = await _getTemplateFile(templatePath);
115-
return compile(templateContents, partial: _partial) as TemplateRenderer;
112+
return compile(templateContents, partial: _partial);
116113
}
117114

118115
var indexTemplate = await _loadTemplate('index.html');

lib/src/io_utils.dart

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
library dartdoc.io_utils;
77

88
import 'dart:async';
9+
import 'dart:collection';
910
import 'dart:convert';
1011
import 'dart:io';
1112

@@ -65,6 +66,36 @@ final newLinePartOfRegexp = new RegExp('\npart of ');
6566

6667
final RegExp quotables = new RegExp(r'[ "\r\n\$]');
6768

69+
/// Best used with Future<void>.
70+
class MultiFutureTracker<T> {
71+
/// Approximate maximum number of simultaneous active Futures.
72+
final int parallel;
73+
74+
final Queue<Future<T>> _queue = new Queue();
75+
76+
MultiFutureTracker(this.parallel);
77+
78+
/// Adds a Future to the queue of outstanding Futures, and returns a Future
79+
/// that completes only when the number of Futures outstanding is <= parallel.
80+
/// That can be extremely brief and there's no longer a guarantee after that
81+
/// point that another async task has not added a Future to the list.
82+
void addFuture(Future<T> future) async {
83+
_queue.add(future);
84+
future.then((f) => _queue.remove(future));
85+
await _waitUntil(parallel);
86+
}
87+
88+
/// Wait until fewer or equal to this many Futures are outstanding.
89+
void _waitUntil(int max) async {
90+
while (_queue.length > max) {
91+
await Future.any(_queue);
92+
}
93+
}
94+
95+
/// Wait until all futures added so far have completed.
96+
void wait() async => await _waitUntil(0);
97+
}
98+
6899
class SubprocessLauncher {
69100
final String context;
70101
final Map<String, String> environment;
@@ -126,6 +157,8 @@ class SubprocessLauncher {
126157
return line.split('\n');
127158
}
128159

160+
Process process = await Process.start(executable, arguments,
161+
workingDirectory: workingDirectory, environment: environment);
129162
stderr.write('$prefix+ ');
130163
if (workingDirectory != null) stderr.write('(cd "$workingDirectory" && ');
131164
if (environment != null) {
@@ -150,9 +183,6 @@ class SubprocessLauncher {
150183
}
151184
if (workingDirectory != null) stderr.write(')');
152185
stderr.write('\n');
153-
Process process = await Process.start(executable, arguments,
154-
workingDirectory: workingDirectory, environment: environment);
155-
156186
_printStream(process.stdout, stdout, prefix: prefix, filter: jsonCallback);
157187
_printStream(process.stderr, stderr, prefix: prefix, filter: jsonCallback);
158188
await process.exitCode;

lib/src/model.dart

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ class Class extends ModelElement
443443
: super(element, library, packageGraph, null) {
444444
_mixins = _cls.mixins
445445
.map((f) {
446-
ElementType t = new ElementType.from(f, packageGraph);
446+
DefinedElementType t = new ElementType.from(f, packageGraph);
447447
return t;
448448
})
449449
.where((mixin) => mixin != null)
@@ -454,7 +454,7 @@ class Class extends ModelElement
454454
}
455455

456456
_interfaces = _cls.interfaces
457-
.map((f) => new ElementType.from(f, packageGraph))
457+
.map((f) => new ElementType.from(f, packageGraph) as DefinedElementType)
458458
.toList(growable: false);
459459
}
460460

@@ -605,7 +605,7 @@ class Class extends ModelElement
605605
if (_constructors != null) return _constructors;
606606

607607
_constructors = _cls.constructors.map((e) {
608-
return new ModelElement.from(e, library, packageGraph);
608+
return new ModelElement.from(e, library, packageGraph) as Constructor;
609609
}).toList(growable: true)
610610
..sort(byName);
611611

@@ -818,9 +818,11 @@ class Class extends ModelElement
818818

819819
List<Operator> get operators {
820820
if (_operators != null) return _operators;
821-
822-
_operators = _methods.where((m) => m.isOperator).toList(growable: false)
823-
..sort(byName);
821+
_operators = _methods
822+
.where((m) => m.isOperator)
823+
.cast<Operator>()
824+
.toList(growable: false)
825+
..sort(byName);
824826
_genPageOperators.addAll(_operators);
825827

826828
return _operators;
@@ -935,7 +937,9 @@ class Class extends ModelElement
935937
if (_fields != null) return _fields;
936938
_fields = [];
937939
Set<PropertyAccessorElement> inheritedAccessors = new Set()
938-
..addAll(_inheritedElements.where((e) => e is PropertyAccessorElement));
940+
..addAll(_inheritedElements
941+
.where((e) => e is PropertyAccessorElement)
942+
.cast<PropertyAccessorElement>());
939943

940944
// This structure keeps track of inherited accessors, allowing lookup
941945
// by field name (stripping the '=' from setters).
@@ -1041,7 +1045,7 @@ class Class extends ModelElement
10411045
if (_allMethods != null) return _allMethods;
10421046

10431047
_allMethods = _cls.methods.map((e) {
1044-
return new ModelElement.from(e, library, packageGraph);
1048+
return new ModelElement.from(e, library, packageGraph) as Method;
10451049
}).toList(growable: false)
10461050
..sort(byName);
10471051

@@ -1052,7 +1056,7 @@ class Class extends ModelElement
10521056
@override
10531057
List<TypeParameter> get typeParameters => _cls.typeParameters.map((f) {
10541058
var lib = new Library(f.enclosingElement.library, packageGraph);
1055-
return new ModelElement.from(f, lib, packageGraph);
1059+
return new ModelElement.from(f, lib, packageGraph) as TypeParameter;
10561060
}).toList();
10571061

10581062
@override
@@ -1162,7 +1166,7 @@ abstract class Documentable extends Nameable {
11621166
abstract class Categorization implements ModelElement {
11631167
@override
11641168
String _buildDocumentationLocal() {
1165-
_rawDocs = super._buildDocumentationLocal();
1169+
_rawDocs = _buildDocumentationBase();
11661170
_rawDocs = _stripAndSetDartdocCategory(_rawDocs);
11671171
return _rawDocs;
11681172
}
@@ -1282,7 +1286,7 @@ class Enum extends Class {
12821286
.instanceProperties
12831287
.map((Field p) => new ModelElement.from(
12841288
p.element, p.library, p.packageGraph,
1285-
getter: p.getter, setter: p.setter))
1289+
getter: p.getter, setter: p.setter) as EnumField)
12861290
.toList(growable: false);
12871291
}
12881292

@@ -1720,7 +1724,7 @@ abstract class GetterSetterCombo implements ModelElement {
17201724

17211725
class Library extends ModelElement with Categorization {
17221726
List<Class> _classes;
1723-
List<Class> _enums;
1727+
List<Enum> _enums;
17241728
List<ModelFunction> _functions;
17251729
List<Typedef> _typeDefs;
17261730
List<TopLevelVariable> _variables;
@@ -1931,12 +1935,13 @@ class Library extends ModelElement with Categorization {
19311935

19321936
List<Class> get enums {
19331937
if (_enums != null) return _enums;
1934-
19351938
List<ClassElement> enumClasses = [];
19361939
enumClasses.addAll(_exportedNamespace.definedNames.values
1937-
.where((element) => element is ClassElement && element.isEnum));
1940+
.where((e) => e is ClassElement)
1941+
.cast<ClassElement>()
1942+
.where((element) => element.isEnum));
19381943
_enums = enumClasses
1939-
.map((e) => new ModelElement.from(e, this, packageGraph))
1944+
.map((e) => new ModelElement.from(e, this, packageGraph) as Enum)
19401945
.toList(growable: false)
19411946
..sort(byName);
19421947

@@ -1966,10 +1971,11 @@ class Library extends ModelElement with Categorization {
19661971
elements.addAll(cu.functions);
19671972
}
19681973
elements.addAll(_exportedNamespace.definedNames.values
1969-
.where((element) => element is FunctionElement));
1974+
.where((e) => e is FunctionElement)
1975+
.cast<FunctionElement>());
19701976

19711977
_functions = elements.map((e) {
1972-
return new ModelElement.from(e, this, packageGraph);
1978+
return new ModelElement.from(e, this, packageGraph) as ModelFunction;
19731979
}).toList(growable: false)
19741980
..sort(byName);
19751981

@@ -2080,9 +2086,10 @@ class Library extends ModelElement with Categorization {
20802086
}
20812087

20822088
elements.addAll(_exportedNamespace.definedNames.values
2083-
.where((element) => element is FunctionTypeAliasElement));
2089+
.where((e) => e is FunctionTypeAliasElement)
2090+
.cast<FunctionTypeAliasElement>());
20842091
_typeDefs = elements
2085-
.map((e) => new ModelElement.from(e, this, packageGraph))
2092+
.map((e) => new ModelElement.from(e, this, packageGraph) as Typedef)
20862093
.toList(growable: false)
20872094
..sort(byName);
20882095

@@ -2106,10 +2113,12 @@ class Library extends ModelElement with Categorization {
21062113
}
21072114

21082115
types.addAll(_exportedNamespace.definedNames.values
2109-
.where((element) => element is ClassElement && !element.isEnum));
2116+
.where((e) => e is ClassElement)
2117+
.cast<ClassElement>()
2118+
.where((element) => !element.isEnum));
21102119

21112120
_classes = types
2112-
.map((e) => new ModelElement.from(e, this, packageGraph))
2121+
.map((e) => new ModelElement.from(e, this, packageGraph) as Class)
21132122
.toList(growable: false)
21142123
..sort(byName);
21152124

@@ -2335,7 +2344,7 @@ class Method extends ModelElement
23352344

23362345
void _calcTypeParameters() {
23372346
typeParameters = _method.typeParameters.map((f) {
2338-
return new ModelElement.from(f, library, packageGraph);
2347+
return new ModelElement.from(f, library, packageGraph) as TypeParameter;
23392348
}).toList();
23402349
}
23412350

@@ -2845,7 +2854,10 @@ abstract class ModelElement extends Canonicalization
28452854
return docFrom;
28462855
}
28472856

2848-
String _buildDocumentationLocal() {
2857+
String _buildDocumentationLocal() => _buildDocumentationBase();
2858+
2859+
/// Separate from _buildDocumentationLocal for overriding.
2860+
String _buildDocumentationBase() {
28492861
assert(_rawDocs == null);
28502862
if (config.dropTextFrom.contains(element.library.name)) {
28512863
_rawDocs = '';
@@ -3245,8 +3257,9 @@ abstract class ModelElement extends Canonicalization
32453257
}
32463258

32473259
_parameters = new UnmodifiableListView<Parameter>(params
3248-
.map((p) => new ModelElement.from(p, library, packageGraph))
3249-
.toList() as Iterable<Parameter>);
3260+
.map((p) =>
3261+
new ModelElement.from(p, library, packageGraph) as Parameter)
3262+
.toList());
32503263
}
32513264
return _parameters;
32523265
}
@@ -3651,7 +3664,7 @@ class ModelFunctionTyped extends ModelElement
36513664

36523665
void _calcTypeParameters() {
36533666
typeParameters = _func.typeParameters.map((f) {
3654-
return new ModelElement.from(f, library, packageGraph);
3667+
return new ModelElement.from(f, library, packageGraph) as TypeParameter;
36553668
}).toList();
36563669
}
36573670

@@ -3835,16 +3848,16 @@ class PackageGraph extends Canonicalization
38353848
@override
38363849
final DartdocConfig config;
38373850

3838-
Map<String, Map<String, List<Map<String, dynamic>>>> __crossdartJson;
3851+
Map<String, Map<String, dynamic>> __crossdartJson;
38393852
// TODO(jcollins-g): move to [Package]
3840-
Map<String, Map<String, List<Map<String, dynamic>>>> get crossdartJson {
3853+
Map<String, Map<String, dynamic>> get crossdartJson {
38413854
if (__crossdartJson == null) {
38423855
// TODO(jcollins-g): allow crossdart.json location to be configurable
38433856
var crossdartFile =
38443857
new File(pathLib.join(config.inputDir.path, "crossdart.json"));
38453858
if (crossdartFile.existsSync()) {
3846-
var __crossdartJsonTmp = json.decode(crossdartFile.readAsStringSync())
3847-
as Map<String, Map<String, List<Map<String, dynamic>>>>;
3859+
Map<String, dynamic> __crossdartJsonTmp =
3860+
json.decode(crossdartFile.readAsStringSync());
38483861
__crossdartJson = {};
38493862
for (String key in __crossdartJsonTmp.keys) {
38503863
__crossdartJson[pathLib.canonicalize(key)] = __crossdartJsonTmp[key];
@@ -4480,8 +4493,10 @@ class PackageGraph extends Canonicalization
44804493
// for an inherited element whose defining Class is not canonical.
44814494
if (matches.length > 1 && preferredClass != null) {
44824495
// Search for matches inside our superchain.
4483-
List<Class> superChain =
4484-
preferredClass.superChain.map((et) => et.element).toList();
4496+
List<Class> superChain = preferredClass.superChain
4497+
.map((et) => et.element)
4498+
.cast<Class>()
4499+
.toList();
44854500
superChain.add(preferredClass);
44864501
matches.removeWhere((me) =>
44874502
!superChain.contains((me as EnclosedElement).enclosingElement));
@@ -5024,7 +5039,7 @@ abstract class TypeParameters implements ModelElement {
50245039
}
50255040

50265041
@override
5027-
DefinedElementType get modelType => super.modelType;
5042+
DefinedElementType get modelType;
50285043

50295044
List<TypeParameter> get typeParameters;
50305045
}
@@ -5158,7 +5173,7 @@ class Typedef extends ModelElement
51585173

51595174
@override
51605175
List<TypeParameter> get typeParameters => _typedef.typeParameters.map((f) {
5161-
return new ModelElement.from(f, library, packageGraph);
5176+
return new ModelElement.from(f, library, packageGraph) as TypeParameter;
51625177
}).toList();
51635178
}
51645179

lib/src/model_utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ String stripIndentFromSource(String source) {
140140
/// Add links to crossdart.info to the given source fragment
141141
String crossdartifySource(
142142
String inputPath,
143-
Map<String, Map<String, List<Map<String, dynamic>>>> json,
143+
Map<String, Map<String, dynamic>> json,
144144
String source,
145145
Element element,
146146
int start) {

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ packages:
210210
name: mustache4dart
211211
url: "https://pub.dartlang.org"
212212
source: hosted
213-
version: "2.1.0"
213+
version: "2.1.1"
214214
node_preamble:
215215
dependency: transitive
216216
description:

0 commit comments

Comments
 (0)