Skip to content

Commit 50f38ec

Browse files
authored
Merge master into nnbd branch (#2747)
* Switch to using CompilationUnitElement.classes (#2743) * Prepare dartdoc_options for migration (#2745) * more specific imports in options * do not allow autoinitialization * dartfmt * parameterize the valueWithContext type * Use alternative constructors instead of externalizing, didn't realize the autodetect was so widely used * more subclasses * Empty commit to straighten out GitHub Actions
1 parent 7e202b9 commit 50f38ec

File tree

6 files changed

+76
-67
lines changed

6 files changed

+76
-67
lines changed

lib/options.dart

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ import 'dart:io' show stderr, exitCode;
44

55
import 'package:analyzer/file_system/file_system.dart';
66
import 'package:args/args.dart';
7-
import 'package:dartdoc/dartdoc.dart';
7+
import 'package:dartdoc/dartdoc.dart' show dartdocVersion, programName;
8+
import 'package:dartdoc/src/dartdoc_options.dart';
9+
import 'package:dartdoc/src/generator/generator.dart';
810
import 'package:dartdoc/src/logging.dart';
11+
import 'package:dartdoc/src/package_meta.dart';
912

1013
/// Helper class that consolidates option contexts for instantiating generators.
1114
class DartdocGeneratorOptionContext extends DartdocOptionContext {
1215
DartdocGeneratorOptionContext(
1316
DartdocOptionSet optionSet, Folder dir, ResourceProvider resourceProvider)
1417
: super(optionSet, dir, resourceProvider);
1518

19+
DartdocGeneratorOptionContext.fromDefaultContextLocation(
20+
DartdocOptionSet optionSet, ResourceProvider resourceProvider)
21+
: super.fromDefaultContextLocation(optionSet, resourceProvider);
22+
1623
// TODO(migration): Make late final with initializer when Null Safe.
1724
String _header;
1825

@@ -59,6 +66,9 @@ class DartdocProgramOptionContext extends DartdocGeneratorOptionContext
5966
DartdocProgramOptionContext(
6067
DartdocOptionSet optionSet, Folder dir, ResourceProvider resourceProvider)
6168
: super(optionSet, dir, resourceProvider);
69+
DartdocProgramOptionContext.fromDefaultContextLocation(
70+
DartdocOptionSet optionSet, ResourceProvider resourceProvider)
71+
: super.fromDefaultContextLocation(optionSet, resourceProvider);
6272

6373
bool get generateDocs => optionSet['generateDocs'].valueAt(context);
6474
bool get help => optionSet['help'].valueAt(context);
@@ -120,8 +130,8 @@ Future<DartdocProgramOptionContext> parseOptions(
120130

121131
DartdocProgramOptionContext config;
122132
try {
123-
config = DartdocProgramOptionContext(
124-
optionSet, null, packageMetaProvider.resourceProvider);
133+
config = DartdocProgramOptionContext.fromDefaultContextLocation(
134+
optionSet, packageMetaProvider.resourceProvider);
125135
} on DartdocOptionError catch (e) {
126136
stderr.writeln(' fatal error: ${e.message}');
127137
stderr.writeln('');

lib/src/dartdoc_options.dart

+56-54
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,10 @@ abstract class DartdocOption<T> {
398398

399399
bool get _isDouble => _kDoubleVal is T;
400400

401-
DartdocOption<Object> _parent;
401+
DartdocOption _parent;
402402

403403
/// The parent of this DartdocOption, or null if this is the root.
404-
DartdocOption<Object> get parent => _parent;
404+
DartdocOption get parent => _parent;
405405

406406
final Map<String, _YamlFileData> __yamlAtCanonicalPathCache = {};
407407

@@ -426,18 +426,18 @@ abstract class DartdocOption<T> {
426426
/// Throw [DartdocFileMissing] with a detailed error message indicating where
427427
/// the error came from when a file or directory option is missing.
428428
void _onMissing(
429-
_OptionValueWithContext<Object> valueWithContext, String missingFilename);
429+
_OptionValueWithContext<T> valueWithContext, String missingFilename);
430430

431431
/// Call [_onMissing] for every path that does not exist.
432-
void _validatePaths(_OptionValueWithContext<Object> valueWithContext) {
432+
void _validatePaths(_OptionValueWithContext<T> valueWithContext) {
433433
if (!mustExist) return;
434434
assert(isDir || isFile);
435435
List<String> resolvedPaths;
436436
var value = valueWithContext.value;
437437
if (value is String) {
438-
resolvedPaths = [valueWithContext.resolvedValue];
438+
resolvedPaths = [valueWithContext.resolvedValue as String];
439439
} else if (value is List<String>) {
440-
resolvedPaths = valueWithContext.resolvedValue as List;
440+
resolvedPaths = valueWithContext.resolvedValue as List<String>;
441441
} else if (value is Map<String, String>) {
442442
resolvedPaths = (valueWithContext.resolvedValue as Map).values.toList();
443443
} else {
@@ -458,7 +458,7 @@ abstract class DartdocOption<T> {
458458

459459
/// For a [List<String>] or [String] value, if [isDir] or [isFile] is set,
460460
/// resolve paths in value relative to canonicalPath.
461-
T _handlePathsInContext(_OptionValueWithContext<Object> valueWithContext) {
461+
T _handlePathsInContext(_OptionValueWithContext<T> valueWithContext) {
462462
if (valueWithContext?.value == null || !(isDir || isFile || isGlob)) {
463463
return valueWithContext?.value;
464464
}
@@ -474,15 +474,15 @@ abstract class DartdocOption<T> {
474474
ArgResults get _argResults => root.__argResults;
475475

476476
/// Set the parent of this [DartdocOption]. Do not call more than once.
477-
set parent(DartdocOption<Object> newParent) {
477+
set parent(DartdocOption newParent) {
478478
assert(_parent == null);
479479
_parent = newParent;
480480
}
481481

482482
/// The root [DartdocOption] containing this object, or [this] if the object
483483
/// has no parent.
484-
DartdocOption<Object> get root {
485-
DartdocOption<Object> p = this;
484+
DartdocOption get root {
485+
DartdocOption p = this;
486486
while (p.parent != null) {
487487
p = p.parent;
488488
}
@@ -492,7 +492,7 @@ abstract class DartdocOption<T> {
492492
/// All object names starting at the root.
493493
Iterable<String> get keys {
494494
var keyList = <String>[];
495-
DartdocOption<Object> option = this;
495+
DartdocOption option = this;
496496
while (option?.name != null) {
497497
keyList.add(option.name);
498498
option = option.parent;
@@ -501,7 +501,7 @@ abstract class DartdocOption<T> {
501501
}
502502

503503
/// Direct children of this node, mapped by name.
504-
final Map<String, DartdocOption<Object>> _children = {};
504+
final Map<String, DartdocOption> _children = {};
505505

506506
/// Return the calculated value of this option, given the directory as
507507
/// context.
@@ -533,7 +533,7 @@ abstract class DartdocOption<T> {
533533
resourceProvider.pathContext.basename(element.source.fullName))));
534534

535535
/// Adds a DartdocOption to the children of this DartdocOption.
536-
void add(DartdocOption<Object> option) {
536+
void add(DartdocOption option) {
537537
if (_children.containsKey(option.name)) {
538538
throw DartdocOptionError(
539539
'Tried to add two children with the same name: ${option.name}');
@@ -547,15 +547,15 @@ abstract class DartdocOption<T> {
547547
void _onAdd() {}
548548

549549
/// Adds a list of dartdoc options to the children of this DartdocOption.
550-
void addAll(Iterable<DartdocOption<Object>> options) => options.forEach(add);
550+
void addAll(Iterable<DartdocOption> options) => options.forEach(add);
551551

552552
/// Get the immediate child of this node named [name].
553553
DartdocOption<dynamic> operator [](String name) {
554554
return _children[name];
555555
}
556556

557557
/// Apply the function [visit] to [this] and all children.
558-
void traverse(void Function(DartdocOption<Object> option) visit) {
558+
void traverse(void Function(DartdocOption option) visit) {
559559
visit(this);
560560
_children.values.forEach((d) => d.traverse(visit));
561561
}
@@ -592,7 +592,7 @@ class DartdocOptionFileSynth<T> extends DartdocOption<T>
592592

593593
@override
594594
void _onMissing(
595-
_OptionValueWithContext<Object> valueWithContext, String missingPath) {
595+
_OptionValueWithContext<T> valueWithContext, String missingPath) {
596596
if (valueWithContext.definingFile != null) {
597597
_onMissingFromFiles(valueWithContext, missingPath);
598598
} else {
@@ -634,7 +634,7 @@ class DartdocOptionArgSynth<T> extends DartdocOption<T>
634634

635635
@override
636636
void _onMissing(
637-
_OptionValueWithContext<Object> valueWithContext, String missingPath) {
637+
_OptionValueWithContext<T> valueWithContext, String missingPath) {
638638
_onMissingFromArgs(valueWithContext, missingPath);
639639
}
640640

@@ -689,20 +689,20 @@ abstract class DartdocSyntheticOption<T> implements DartdocOption<T> {
689689
}
690690

691691
@override
692-
void _onMissing(_OptionValueWithContext<Object> valueWithContext,
693-
String missingPath) =>
692+
void _onMissing(
693+
_OptionValueWithContext<T> valueWithContext, String missingPath) =>
694694
_onMissingFromSynthetic(valueWithContext, missingPath);
695695

696696
void _onMissingFromSynthetic(
697-
_OptionValueWithContext<Object> valueWithContext, String missingPath) {
697+
_OptionValueWithContext<T> valueWithContext, String missingPath) {
698698
var description = 'Synthetic configuration option $name from <internal>';
699699
throw DartdocFileMissing(
700700
'$description, computed as ${valueWithContext.value}, resolves to '
701701
'missing path: "$missingPath"');
702702
}
703703
}
704704

705-
typedef OptionGenerator = Future<List<DartdocOption<Object>>> Function(
705+
typedef OptionGenerator = Future<List<DartdocOption>> Function(
706706
PackageMetaProvider);
707707

708708
/// A [DartdocOption] that only contains other [DartdocOption]s and is not an
@@ -736,13 +736,13 @@ class DartdocOptionSet extends DartdocOption<void> {
736736

737737
/// Since we have no value, [_onMissing] does nothing.
738738
@override
739-
void _onMissing(_OptionValueWithContext<Object> valueWithContext,
740-
String missingFilename) {}
739+
void _onMissing(
740+
_OptionValueWithContext<void> valueWithContext, String missingFilename) {}
741741

742742
/// Traverse skips this node, because it doesn't represent a real
743743
/// configuration object.
744744
@override
745-
void traverse(void Function(DartdocOption<Object> option) visitor) {
745+
void traverse(void Function(DartdocOption option) visitor) {
746746
_children.values.forEach((d) => d.traverse(visitor));
747747
}
748748
}
@@ -817,7 +817,7 @@ class DartdocOptionArgFile<T> extends DartdocOption<T>
817817

818818
@override
819819
void _onMissing(
820-
_OptionValueWithContext<Object> valueWithContext, String missingPath) {
820+
_OptionValueWithContext<T> valueWithContext, String missingPath) {
821821
if (valueWithContext.definingFile != null) {
822822
_onMissingFromFiles(valueWithContext, missingPath);
823823
} else {
@@ -889,12 +889,12 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {
889889
String get fieldName => keys.join('.');
890890

891891
@override
892-
void _onMissing(_OptionValueWithContext<Object> valueWithContext,
893-
String missingPath) =>
892+
void _onMissing(
893+
_OptionValueWithContext<T> valueWithContext, String missingPath) =>
894894
_onMissingFromFiles(valueWithContext, missingPath);
895895

896896
void _onMissingFromFiles(
897-
_OptionValueWithContext<Object> valueWithContext, String missingPath) {
897+
_OptionValueWithContext<T> valueWithContext, String missingPath) {
898898
var dartdocYaml = resourceProvider.pathContext.join(
899899
valueWithContext.canonicalDirectoryPath, valueWithContext.definingFile);
900900
throw DartdocFileMissing('Field $fieldName from $dartdocYaml, set to '
@@ -916,7 +916,7 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {
916916
T _valueAtFromFiles(Folder dir) {
917917
var key = resourceProvider.pathContext.canonicalize(dir.path);
918918
if (!__valueAtFromFiles.containsKey(key)) {
919-
_OptionValueWithContext<Object> valueWithContext;
919+
_OptionValueWithContext<T> valueWithContext;
920920
if (parentDirOverridesChild) {
921921
valueWithContext = _valueAtFromFilesLastFound(dir);
922922
} else {
@@ -929,8 +929,8 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {
929929

930930
/// Searches all dartdoc_options files through parent directories, starting at
931931
/// [dir], for the option and returns one once found.
932-
_OptionValueWithContext<Object> _valueAtFromFilesFirstFound(Folder folder) {
933-
_OptionValueWithContext<Object> value;
932+
_OptionValueWithContext<T> _valueAtFromFilesFirstFound(Folder folder) {
933+
_OptionValueWithContext<T> value;
934934
for (var dir in folder.withAncestors) {
935935
value = _valueAtFromFile(dir);
936936
if (value != null) break;
@@ -941,8 +941,8 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {
941941
/// Searches all dartdoc_options files for the option, and returns the value
942942
/// in the top-most parent directory `dartdoc_options.yaml` file it is
943943
/// mentioned in.
944-
_OptionValueWithContext<Object> _valueAtFromFilesLastFound(Folder folder) {
945-
_OptionValueWithContext<Object> value;
944+
_OptionValueWithContext<T> _valueAtFromFilesLastFound(Folder folder) {
945+
_OptionValueWithContext<T> value;
946946
for (var dir in folder.withAncestors) {
947947
var tmpValue = _valueAtFromFile(dir);
948948
if (tmpValue != null) value = tmpValue;
@@ -952,7 +952,7 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {
952952

953953
/// Returns null if not set in the YAML file in this directory (or its
954954
/// parents).
955-
_OptionValueWithContext<Object> _valueAtFromFile(Folder dir) {
955+
_OptionValueWithContext<T> _valueAtFromFile(Folder dir) {
956956
var yamlFileData = _yamlAtDirectory(dir);
957957
var contextPath = yamlFileData.canonicalDirectoryPath;
958958
Object yamlData = yamlFileData.data ?? {};
@@ -1083,12 +1083,12 @@ abstract class _DartdocArgOption<T> implements DartdocOption<T> {
10831083
}
10841084

10851085
@override
1086-
void _onMissing(_OptionValueWithContext<Object> valueWithContext,
1087-
String missingPath) =>
1086+
void _onMissing(
1087+
_OptionValueWithContext<T> valueWithContext, String missingPath) =>
10881088
_onMissingFromArgs(valueWithContext, missingPath);
10891089

10901090
void _onMissingFromArgs(
1091-
_OptionValueWithContext<Object> valueWithContext, String missingPath) {
1091+
_OptionValueWithContext<T> valueWithContext, String missingPath) {
10921092
throw DartdocFileMissing(
10931093
'Argument --$argName, set to ${valueWithContext.value}, resolves to '
10941094
'missing path: "$missingPath"');
@@ -1098,7 +1098,7 @@ abstract class _DartdocArgOption<T> implements DartdocOption<T> {
10981098
/// the [argParser] and the working directory from [_directoryCurrent].
10991099
///
11001100
/// Throws [UnsupportedError] if [T] is not a supported type.
1101-
_OptionValueWithContext<Object> _valueAtFromArgsWithContext() {
1101+
_OptionValueWithContext<T> _valueAtFromArgsWithContext() {
11021102
if (!_argResults.wasParsed(argName)) return null;
11031103

11041104
T retval;
@@ -1217,21 +1217,23 @@ class DartdocOptionContext extends DartdocOptionContextBase
12171217
// TODO(jcollins-g): Allow passing in structured data to initialize a
12181218
// [DartdocOptionContext]'s arguments instead of having to parse strings
12191219
// via optionSet.
1220-
/// If [entity] is null, assume this is the initialization case and use
1221-
/// the inputDir flag to determine the context.
1222-
DartdocOptionContext(
1223-
this.optionSet, Resource resource, ResourceProvider resourceProvider) {
1224-
if (resource == null) {
1225-
var current = resourceProvider.pathContext.current;
1226-
String inputDir =
1227-
optionSet['inputDir'].valueAt(resourceProvider.getFolder(current)) ??
1228-
current;
1229-
context = resourceProvider.getFolder(inputDir);
1230-
} else {
1231-
context = resourceProvider.getFolder(resourceProvider.pathContext
1232-
.canonicalize(
1233-
resource is File ? resource.parent2.path : resource.path));
1234-
}
1220+
DartdocOptionContext(this.optionSet, Resource contextLocation,
1221+
ResourceProvider resourceProvider) {
1222+
assert(contextLocation != null);
1223+
context = resourceProvider.getFolder(resourceProvider.pathContext
1224+
.canonicalize(contextLocation is File
1225+
? contextLocation.parent2.path
1226+
: contextLocation.path));
1227+
}
1228+
1229+
/// Build a DartdocOptionContext via the 'inputDir' command line option.
1230+
DartdocOptionContext.fromDefaultContextLocation(
1231+
this.optionSet, ResourceProvider resourceProvider) {
1232+
var current = resourceProvider.pathContext.current;
1233+
String inputDir =
1234+
optionSet['inputDir'].valueAt(resourceProvider.getFolder(current)) ??
1235+
current;
1236+
context = resourceProvider.getFolder(inputDir);
12351237
}
12361238

12371239
/// Build a DartdocOptionContext from an analyzer element (using its source
@@ -1368,7 +1370,7 @@ class DartdocOptionContext extends DartdocOptionContextBase
13681370

13691371
/// Instantiate dartdoc's configuration file and options parser with the
13701372
/// given command line arguments.
1371-
Future<List<DartdocOption<Object>>> createDartdocOptions(
1373+
Future<List<DartdocOption>> createDartdocOptions(
13721374
PackageMetaProvider packageMetaProvider,
13731375
) async {
13741376
var resourceProvider = packageMetaProvider.resourceProvider;

lib/src/model/library.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
104104
CompilationUnitElement compilationUnit) {
105105
return quiver.concat([
106106
compilationUnit.accessors,
107+
compilationUnit.classes,
107108
compilationUnit.enums,
108109
compilationUnit.extensions,
109110
compilationUnit.functions,
110111
compilationUnit.mixins,
111112
compilationUnit.topLevelVariables,
112113
compilationUnit.typeAliases,
113-
compilationUnit.types,
114114
]);
115115
}
116116

test/end2end/dartdoc_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ Future<DartdocGeneratorOptionContext> _generatorContextFromArgv(
5757
],
5858
pubPackageMetaProvider);
5959
optionSet.parseArguments(argv);
60-
return DartdocGeneratorOptionContext(
61-
optionSet, null, pubPackageMetaProvider.resourceProvider);
60+
return DartdocGeneratorOptionContext.fromDefaultContextLocation(
61+
optionSet, pubPackageMetaProvider.resourceProvider);
6262
}
6363

6464
class DartdocLoggingOptionContext extends DartdocGeneratorOptionContext

test/mustachio/renderers_output_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ Future<DartdocGeneratorOptionContext> _generatorContextFromArgv(
2929
],
3030
pubPackageMetaProvider);
3131
optionSet.parseArguments(argv);
32-
return DartdocGeneratorOptionContext(
33-
optionSet, null, pubPackageMetaProvider.resourceProvider);
32+
return DartdocGeneratorOptionContext.fromDefaultContextLocation(
33+
optionSet, pubPackageMetaProvider.resourceProvider);
3434
}
3535

3636
void main() {

0 commit comments

Comments
 (0)