Skip to content

Migrate a few more peripheral libraries to NNBD #2783

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 1 commit into from
Sep 16, 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
3 changes: 0 additions & 3 deletions lib/src/experiment_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

///
/// Implementation of Dart language experiment option handling for dartdoc.
/// See https://github.com/dart-lang/sdk/blob/main/docs/process/experimental-flags.md.
Expand All @@ -30,7 +28,6 @@ Future<List<DartdocOption<Object>>> createExperimentOptions(
'enable-experiment', ['non-nullable'], resourceProvider,
help: 'Enable or disable listed experiments.\n' +
ExperimentStatus.knownFeatures.values
.where((e) => e.documentation != null)
.map((e) =>
' [no-]${e.enableString}: ${e.documentation} (default: ${e.isEnabledByDefault})')
.join('\n')),
Expand Down
10 changes: 4 additions & 6 deletions lib/src/generator/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

/// A library containing an abstract documentation generator.
library dartdoc.generator;

Expand Down Expand Up @@ -45,7 +43,7 @@ abstract class Generator {
Future<void> generate(PackageGraph packageGraph, FileWriter writer);
}

Future<List<DartdocOption<Object>>> createGeneratorOptions(
Future<List<DartdocOption>> createGeneratorOptions(
PackageMetaProvider packageMetaProvider) async {
var resourceProvider = packageMetaProvider.resourceProvider;
return [
Expand Down Expand Up @@ -73,16 +71,16 @@ Future<List<DartdocOption<Object>>> createGeneratorOptions(
'Generates `index.json` with indentation and newlines. The file is '
'larger, but it\'s also easier to diff.',
negatable: false),
DartdocOptionArgFile<String>('favicon', null, resourceProvider,
DartdocOptionArgFile<String?>('favicon', null, resourceProvider,
optionIs: OptionKind.file,
help: 'A path to a favicon for the generated docs.',
mustExist: true),
DartdocOptionArgOnly<String>('relCanonicalPrefix', null, resourceProvider,
DartdocOptionArgOnly<String?>('relCanonicalPrefix', null, resourceProvider,
help:
'If provided, add a rel="canonical" prefixed with provided value. '
'Consider using if building many versions of the docs for public '
'SEO; learn more at https://goo.gl/gktN6F.'),
DartdocOptionArgOnly<String>('templatesDir', null, resourceProvider,
DartdocOptionArgOnly<String?>('templatesDir', null, resourceProvider,
optionIs: OptionKind.dir,
mustExist: true,
hide: true,
Expand Down
48 changes: 22 additions & 26 deletions lib/src/source_linker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,31 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

/// A library for getting external source code links for Dartdoc.
library dartdoc.source_linker;

import 'package:analyzer/file_system/file_system.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;

final _uriTemplateRegExp = RegExp(r'(%[frl]%)');
@Deprecated('Public variable intended to be private; will be removed as early '
'as Dartdoc 1.0.0')
RegExp get uriTemplateRegexp => _uriTemplateRegExp;

abstract class SourceLinkerOptionContext implements DartdocOptionContextBase {
List<String> get linkToSourceExcludes =>
optionSet['linkToSource']['excludes'].valueAt(context);

String get linkToSourceRevision =>
String? get linkToSourceRevision =>
optionSet['linkToSource']['revision'].valueAt(context);

String get linkToSourceRoot =>
String? get linkToSourceRoot =>
optionSet['linkToSource']['root'].valueAt(context);

String get linkToSourceUriTemplate =>
String? get linkToSourceUriTemplate =>
optionSet['linkToSource']['uriTemplate'].valueAt(context);
}

Future<List<DartdocOption<Object>>> createSourceLinkerOptions(
Future<List<DartdocOption<Object?>>> createSourceLinkerOptions(
ResourceProvider resourceProvider) async {
return [
DartdocOptionSet('linkToSource', resourceProvider)
Expand All @@ -42,13 +36,13 @@ Future<List<DartdocOption<Object>>> createSourceLinkerOptions(
help:
'A list of directories to exclude from linking to a source code repository.'),
// TODO(jcollins-g): Use [DartdocOptionArgSynth], possibly in combination with a repository type and the root directory, and get revision number automatically
DartdocOptionArgOnly<String>('revision', null, resourceProvider,
DartdocOptionArgOnly<String?>('revision', null, resourceProvider,
help: 'Revision number to insert into the URI.'),
DartdocOptionArgFile<String>('root', null, resourceProvider,
DartdocOptionArgFile<String?>('root', null, resourceProvider,
optionIs: OptionKind.dir,
help:
'Path to a local directory that is the root of the repository we link to. All source code files under this directory will be linked.'),
DartdocOptionArgFile<String>('uriTemplate', null, resourceProvider,
DartdocOptionArgFile<String?>('uriTemplate', null, resourceProvider,
help:
'''Substitute into this template to generate a uri for an element's source code.
Dartdoc dynamically substitutes the following fields into the template:
Expand All @@ -63,16 +57,16 @@ class SourceLinker {
final List<String> excludes;
final int lineNumber;
final String sourceFileName;
final String revision;
final String root;
final String uriTemplate;
final String? revision;
final String? root;
final String? uriTemplate;

/// Most users of this class should use the [SourceLinker.fromElement] factory
/// instead. This constructor is public for testing.
SourceLinker(
{@required this.excludes,
this.lineNumber,
this.sourceFileName,
{required this.excludes,
required this.lineNumber,
required this.sourceFileName,
this.revision,
this.root,
this.uriTemplate}) {
Expand All @@ -81,7 +75,10 @@ class SourceLinker {
throw DartdocOptionError(
'linkToSource root and uriTemplate must both be specified to generate repository links');
}
if (uriTemplate.contains('%r%') && revision == null) {
var uriTemplateValue = uriTemplate;
if (uriTemplateValue != null &&
uriTemplateValue.contains('%r%') &&
revision == null) {
throw DartdocOptionError(
r'%r% specified in uriTemplate, but no revision available');
}
Expand All @@ -104,7 +101,9 @@ class SourceLinker {
}

String href() {
if (sourceFileName == null || root == null || uriTemplate == null) {
var root = this.root;
var uriTemplate = this.uriTemplate;
if (root == null || uriTemplate == null) {
return '';
}
if (!path.isWithin(root, sourceFileName) ||
Expand All @@ -118,15 +117,12 @@ class SourceLinker {
var urlContext = path.Context(style: path.Style.url);
return urlContext
.joinAll(path.split(path.relative(sourceFileName, from: root)));
break;
case '%r%':
return revision;
break;
return revision!;
case '%l%':
return lineNumber.toString();
break;
default:
return null;
return '';
}
});
}
Expand Down
45 changes: 18 additions & 27 deletions lib/src/tool_definition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

import 'dart:async';

import 'package:analyzer/file_system/file_system.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/io_utils.dart';
import 'package:dartdoc/src/tool_runner.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p show extension;

/// Defines the attributes of a tool in the options file, corresponding to
Expand All @@ -19,15 +16,15 @@ import 'package:path/path.dart' as p show extension;
class ToolDefinition {
/// A list containing the command and options to be run for this tool. The
/// first argument in the command is the tool executable, and will have its
/// path evaluated relative to the `dartdoc_options.yaml` location. Must not
/// be an empty list, or be null.
/// path evaluated relative to the `dartdoc_options.yaml` location. Must
/// not be empty.
final List<String> command;

/// A list containing the command and options to setup phase for this tool.
/// The first argument in the command is the tool executable, and will have
/// its path evaluated relative to the `dartdoc_options.yaml` location. May
/// be null or empty, in which case it will be ignored at setup time.
final List<String> setupCommand;
final List<String>? setupCommand;

/// A description of the defined tool. Must not be null.
final String description;
Expand All @@ -46,13 +43,11 @@ class ToolDefinition {
/// given.
factory ToolDefinition.fromCommand(
List<String> command,
List<String> setupCommand,
List<String>? setupCommand,
String description,
ResourceProvider resourceProvider,
{List<String> compileArgs}) {
assert(command != null);
{List<String>? compileArgs}) {
assert(command.isNotEmpty);
assert(description != null);
if (isDartExecutable(command[0])) {
return DartToolDefinition(
command, setupCommand, description, resourceProvider,
Expand All @@ -69,12 +64,11 @@ class ToolDefinition {
}

ToolDefinition(this.command, this.setupCommand, this.description)
: assert(command != null),
assert(command.isNotEmpty),
assert(description != null);
: assert(command.isNotEmpty);

@override
String toString() {
var setupCommand = this.setupCommand;
final commandString =
'${this is DartToolDefinition ? '(Dart) ' : ''}"${command.join(' ')}"';
if (setupCommand == null) {
Expand All @@ -86,7 +80,7 @@ class ToolDefinition {
}

Future<ToolStateForArgs> toolStateForArgs(String toolName, List<String> args,
{@required ToolErrorCallback toolErrorCallback}) async {
{required ToolErrorCallback toolErrorCallback}) async {
var commandPath = args.removeAt(0);
return ToolStateForArgs(commandPath, args, null);
}
Expand All @@ -107,7 +101,7 @@ class DartToolDefinition extends ToolDefinition {
/// built.
@override
Future<ToolStateForArgs> toolStateForArgs(String toolName, List<String> args,
{@required ToolErrorCallback toolErrorCallback}) async {
{required ToolErrorCallback toolErrorCallback}) async {
assert(args[0] == command.first);
// Set up flags to create a new snapshot, if needed, and use the first run
// as the training run.
Expand Down Expand Up @@ -135,11 +129,9 @@ class DartToolDefinition extends ToolDefinition {
} else {
await snapshot._snapshotValid();
if (!snapshotFile.exists) {
if (toolErrorCallback != null) {
toolErrorCallback(
'Snapshot creation failed for $toolName tool. $snapshotPath does '
'not exist. Will execute tool without snapshot.');
}
toolErrorCallback(
'Snapshot creation failed for $toolName tool. $snapshotPath does '
'not exist. Will execute tool without snapshot.');
} else {
// replace the first argument with the path to the snapshot.
args[0] = snapshotPath;
Expand All @@ -148,11 +140,10 @@ class DartToolDefinition extends ToolDefinition {
}
}

DartToolDefinition(List<String> command, List<String> setupCommand,
DartToolDefinition(List<String> command, List<String>? setupCommand,
String description, this._resourceProvider,
{this.compileArgs = const []})
: assert(compileArgs != null),
super(command, setupCommand, description);
: super(command, setupCommand, description);
}

/// Manages the creation of a single snapshot file in a context where multiple
Expand Down Expand Up @@ -238,17 +229,17 @@ class SnapshotCache {

_Snapshot getSnapshot(String toolPath) {
if (snapshots.containsKey(toolPath)) {
return snapshots[toolPath];
return snapshots[toolPath]!;
}
snapshots[toolPath] =
_Snapshot(snapshotCache, toolPath, _serial, _resourceProvider);
_serial++;
return snapshots[toolPath];
return snapshots[toolPath]!;
}

void dispose() {
_instances.remove(_resourceProvider);
if (snapshotCache != null && snapshotCache.exists) {
if (snapshotCache.exists) {
return snapshotCache.delete();
}
return null;
Expand All @@ -258,7 +249,7 @@ class SnapshotCache {
class ToolStateForArgs {
final String commandPath;
final List<String> args;
final void Function() onProcessComplete;
final void Function()? onProcessComplete;

ToolStateForArgs(this.commandPath, this.args, this.onProcessComplete);
}
1 change: 1 addition & 0 deletions test/source_linker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void main() {
excludes: [],
lineNumber: 20,
revision: '12345',
sourceFileName: 'path/to/file.dart',
).href();
expect(sourceLinkerHref, throwsA(TypeMatcher<DartdocOptionError>()));
});
Expand Down