Skip to content

Merge master into nnbd branch #2747

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 5 commits into from
Aug 18, 2021
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
16 changes: 13 additions & 3 deletions lib/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import 'dart:io' show stderr, exitCode;

import 'package:analyzer/file_system/file_system.dart';
import 'package:args/args.dart';
import 'package:dartdoc/dartdoc.dart';
import 'package:dartdoc/dartdoc.dart' show dartdocVersion, programName;
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/generator/generator.dart';
import 'package:dartdoc/src/logging.dart';
import 'package:dartdoc/src/package_meta.dart';

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

DartdocGeneratorOptionContext.fromDefaultContextLocation(
DartdocOptionSet optionSet, ResourceProvider resourceProvider)
: super.fromDefaultContextLocation(optionSet, resourceProvider);

// TODO(migration): Make late final with initializer when Null Safe.
String _header;

Expand Down Expand Up @@ -59,6 +66,9 @@ class DartdocProgramOptionContext extends DartdocGeneratorOptionContext
DartdocProgramOptionContext(
DartdocOptionSet optionSet, Folder dir, ResourceProvider resourceProvider)
: super(optionSet, dir, resourceProvider);
DartdocProgramOptionContext.fromDefaultContextLocation(
DartdocOptionSet optionSet, ResourceProvider resourceProvider)
: super.fromDefaultContextLocation(optionSet, resourceProvider);

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

DartdocProgramOptionContext config;
try {
config = DartdocProgramOptionContext(
optionSet, null, packageMetaProvider.resourceProvider);
config = DartdocProgramOptionContext.fromDefaultContextLocation(
optionSet, packageMetaProvider.resourceProvider);
} on DartdocOptionError catch (e) {
stderr.writeln(' fatal error: ${e.message}');
stderr.writeln('');
Expand Down
110 changes: 56 additions & 54 deletions lib/src/dartdoc_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,10 @@ abstract class DartdocOption<T> {

bool get _isDouble => _kDoubleVal is T;

DartdocOption<Object> _parent;
DartdocOption _parent;

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

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

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

/// Call [_onMissing] for every path that does not exist.
void _validatePaths(_OptionValueWithContext<Object> valueWithContext) {
void _validatePaths(_OptionValueWithContext<T> valueWithContext) {
if (!mustExist) return;
assert(isDir || isFile);
List<String> resolvedPaths;
var value = valueWithContext.value;
if (value is String) {
resolvedPaths = [valueWithContext.resolvedValue];
resolvedPaths = [valueWithContext.resolvedValue as String];
} else if (value is List<String>) {
resolvedPaths = valueWithContext.resolvedValue as List;
resolvedPaths = valueWithContext.resolvedValue as List<String>;
} else if (value is Map<String, String>) {
resolvedPaths = (valueWithContext.resolvedValue as Map).values.toList();
} else {
Expand All @@ -458,7 +458,7 @@ abstract class DartdocOption<T> {

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

/// Set the parent of this [DartdocOption]. Do not call more than once.
set parent(DartdocOption<Object> newParent) {
set parent(DartdocOption newParent) {
assert(_parent == null);
_parent = newParent;
}

/// The root [DartdocOption] containing this object, or [this] if the object
/// has no parent.
DartdocOption<Object> get root {
DartdocOption<Object> p = this;
DartdocOption get root {
DartdocOption p = this;
while (p.parent != null) {
p = p.parent;
}
Expand All @@ -492,7 +492,7 @@ abstract class DartdocOption<T> {
/// All object names starting at the root.
Iterable<String> get keys {
var keyList = <String>[];
DartdocOption<Object> option = this;
DartdocOption option = this;
while (option?.name != null) {
keyList.add(option.name);
option = option.parent;
Expand All @@ -501,7 +501,7 @@ abstract class DartdocOption<T> {
}

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

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

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

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

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

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

@override
void _onMissing(
_OptionValueWithContext<Object> valueWithContext, String missingPath) {
_OptionValueWithContext<T> valueWithContext, String missingPath) {
if (valueWithContext.definingFile != null) {
_onMissingFromFiles(valueWithContext, missingPath);
} else {
Expand Down Expand Up @@ -634,7 +634,7 @@ class DartdocOptionArgSynth<T> extends DartdocOption<T>

@override
void _onMissing(
_OptionValueWithContext<Object> valueWithContext, String missingPath) {
_OptionValueWithContext<T> valueWithContext, String missingPath) {
_onMissingFromArgs(valueWithContext, missingPath);
}

Expand Down Expand Up @@ -689,20 +689,20 @@ abstract class DartdocSyntheticOption<T> implements DartdocOption<T> {
}

@override
void _onMissing(_OptionValueWithContext<Object> valueWithContext,
String missingPath) =>
void _onMissing(
_OptionValueWithContext<T> valueWithContext, String missingPath) =>
_onMissingFromSynthetic(valueWithContext, missingPath);

void _onMissingFromSynthetic(
_OptionValueWithContext<Object> valueWithContext, String missingPath) {
_OptionValueWithContext<T> valueWithContext, String missingPath) {
var description = 'Synthetic configuration option $name from <internal>';
throw DartdocFileMissing(
'$description, computed as ${valueWithContext.value}, resolves to '
'missing path: "$missingPath"');
}
}

typedef OptionGenerator = Future<List<DartdocOption<Object>>> Function(
typedef OptionGenerator = Future<List<DartdocOption>> Function(
PackageMetaProvider);

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

/// Since we have no value, [_onMissing] does nothing.
@override
void _onMissing(_OptionValueWithContext<Object> valueWithContext,
String missingFilename) {}
void _onMissing(
_OptionValueWithContext<void> valueWithContext, String missingFilename) {}

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

@override
void _onMissing(
_OptionValueWithContext<Object> valueWithContext, String missingPath) {
_OptionValueWithContext<T> valueWithContext, String missingPath) {
if (valueWithContext.definingFile != null) {
_onMissingFromFiles(valueWithContext, missingPath);
} else {
Expand Down Expand Up @@ -889,12 +889,12 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {
String get fieldName => keys.join('.');

@override
void _onMissing(_OptionValueWithContext<Object> valueWithContext,
String missingPath) =>
void _onMissing(
_OptionValueWithContext<T> valueWithContext, String missingPath) =>
_onMissingFromFiles(valueWithContext, missingPath);

void _onMissingFromFiles(
_OptionValueWithContext<Object> valueWithContext, String missingPath) {
_OptionValueWithContext<T> valueWithContext, String missingPath) {
var dartdocYaml = resourceProvider.pathContext.join(
valueWithContext.canonicalDirectoryPath, valueWithContext.definingFile);
throw DartdocFileMissing('Field $fieldName from $dartdocYaml, set to '
Expand All @@ -916,7 +916,7 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {
T _valueAtFromFiles(Folder dir) {
var key = resourceProvider.pathContext.canonicalize(dir.path);
if (!__valueAtFromFiles.containsKey(key)) {
_OptionValueWithContext<Object> valueWithContext;
_OptionValueWithContext<T> valueWithContext;
if (parentDirOverridesChild) {
valueWithContext = _valueAtFromFilesLastFound(dir);
} else {
Expand All @@ -929,8 +929,8 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {

/// Searches all dartdoc_options files through parent directories, starting at
/// [dir], for the option and returns one once found.
_OptionValueWithContext<Object> _valueAtFromFilesFirstFound(Folder folder) {
_OptionValueWithContext<Object> value;
_OptionValueWithContext<T> _valueAtFromFilesFirstFound(Folder folder) {
_OptionValueWithContext<T> value;
for (var dir in folder.withAncestors) {
value = _valueAtFromFile(dir);
if (value != null) break;
Expand All @@ -941,8 +941,8 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {
/// Searches all dartdoc_options files for the option, and returns the value
/// in the top-most parent directory `dartdoc_options.yaml` file it is
/// mentioned in.
_OptionValueWithContext<Object> _valueAtFromFilesLastFound(Folder folder) {
_OptionValueWithContext<Object> value;
_OptionValueWithContext<T> _valueAtFromFilesLastFound(Folder folder) {
_OptionValueWithContext<T> value;
for (var dir in folder.withAncestors) {
var tmpValue = _valueAtFromFile(dir);
if (tmpValue != null) value = tmpValue;
Expand All @@ -952,7 +952,7 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {

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

@override
void _onMissing(_OptionValueWithContext<Object> valueWithContext,
String missingPath) =>
void _onMissing(
_OptionValueWithContext<T> valueWithContext, String missingPath) =>
_onMissingFromArgs(valueWithContext, missingPath);

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

T retval;
Expand Down Expand Up @@ -1217,21 +1217,23 @@ class DartdocOptionContext extends DartdocOptionContextBase
// TODO(jcollins-g): Allow passing in structured data to initialize a
// [DartdocOptionContext]'s arguments instead of having to parse strings
// via optionSet.
/// If [entity] is null, assume this is the initialization case and use
/// the inputDir flag to determine the context.
DartdocOptionContext(
this.optionSet, Resource resource, ResourceProvider resourceProvider) {
if (resource == null) {
var current = resourceProvider.pathContext.current;
String inputDir =
optionSet['inputDir'].valueAt(resourceProvider.getFolder(current)) ??
current;
context = resourceProvider.getFolder(inputDir);
} else {
context = resourceProvider.getFolder(resourceProvider.pathContext
.canonicalize(
resource is File ? resource.parent2.path : resource.path));
}
DartdocOptionContext(this.optionSet, Resource contextLocation,
ResourceProvider resourceProvider) {
assert(contextLocation != null);
context = resourceProvider.getFolder(resourceProvider.pathContext
.canonicalize(contextLocation is File
? contextLocation.parent2.path
: contextLocation.path));
}

/// Build a DartdocOptionContext via the 'inputDir' command line option.
DartdocOptionContext.fromDefaultContextLocation(
this.optionSet, ResourceProvider resourceProvider) {
var current = resourceProvider.pathContext.current;
String inputDir =
optionSet['inputDir'].valueAt(resourceProvider.getFolder(current)) ??
current;
context = resourceProvider.getFolder(inputDir);
}

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

/// Instantiate dartdoc's configuration file and options parser with the
/// given command line arguments.
Future<List<DartdocOption<Object>>> createDartdocOptions(
Future<List<DartdocOption>> createDartdocOptions(
PackageMetaProvider packageMetaProvider,
) async {
var resourceProvider = packageMetaProvider.resourceProvider;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
CompilationUnitElement compilationUnit) {
return quiver.concat([
compilationUnit.accessors,
compilationUnit.classes,
compilationUnit.enums,
compilationUnit.extensions,
compilationUnit.functions,
compilationUnit.mixins,
compilationUnit.topLevelVariables,
compilationUnit.typeAliases,
compilationUnit.types,
]);
}

Expand Down
4 changes: 2 additions & 2 deletions test/end2end/dartdoc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ Future<DartdocGeneratorOptionContext> _generatorContextFromArgv(
],
pubPackageMetaProvider);
optionSet.parseArguments(argv);
return DartdocGeneratorOptionContext(
optionSet, null, pubPackageMetaProvider.resourceProvider);
return DartdocGeneratorOptionContext.fromDefaultContextLocation(
optionSet, pubPackageMetaProvider.resourceProvider);
}

class DartdocLoggingOptionContext extends DartdocGeneratorOptionContext
Expand Down
4 changes: 2 additions & 2 deletions test/mustachio/renderers_output_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Future<DartdocGeneratorOptionContext> _generatorContextFromArgv(
],
pubPackageMetaProvider);
optionSet.parseArguments(argv);
return DartdocGeneratorOptionContext(
optionSet, null, pubPackageMetaProvider.resourceProvider);
return DartdocGeneratorOptionContext.fromDefaultContextLocation(
optionSet, pubPackageMetaProvider.resourceProvider);
}

void main() {
Expand Down
Loading