From c33b4f7f4e3be4f9a4ef4b1f0197f76e90cc5b2a Mon Sep 17 00:00:00 2001 From: Janice Collins Date: Tue, 14 Sep 2021 09:21:39 -0700 Subject: [PATCH] A few more libraries migrated --- lib/src/experiment_options.dart | 3 -- lib/src/generator/generator.dart | 10 +++---- lib/src/source_linker.dart | 48 +++++++++++++++----------------- lib/src/tool_definition.dart | 45 ++++++++++++------------------ test/source_linker_test.dart | 1 + 5 files changed, 45 insertions(+), 62 deletions(-) diff --git a/lib/src/experiment_options.dart b/lib/src/experiment_options.dart index fa22f5da2d..5201b86515 100644 --- a/lib/src/experiment_options.dart +++ b/lib/src/experiment_options.dart @@ -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. @@ -30,7 +28,6 @@ Future>> 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')), diff --git a/lib/src/generator/generator.dart b/lib/src/generator/generator.dart index 2a5c2f319e..7246e61184 100644 --- a/lib/src/generator/generator.dart +++ b/lib/src/generator/generator.dart @@ -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; @@ -45,7 +43,7 @@ abstract class Generator { Future generate(PackageGraph packageGraph, FileWriter writer); } -Future>> createGeneratorOptions( +Future> createGeneratorOptions( PackageMetaProvider packageMetaProvider) async { var resourceProvider = packageMetaProvider.resourceProvider; return [ @@ -73,16 +71,16 @@ Future>> createGeneratorOptions( 'Generates `index.json` with indentation and newlines. The file is ' 'larger, but it\'s also easier to diff.', negatable: false), - DartdocOptionArgFile('favicon', null, resourceProvider, + DartdocOptionArgFile('favicon', null, resourceProvider, optionIs: OptionKind.file, help: 'A path to a favicon for the generated docs.', mustExist: true), - DartdocOptionArgOnly('relCanonicalPrefix', null, resourceProvider, + DartdocOptionArgOnly('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('templatesDir', null, resourceProvider, + DartdocOptionArgOnly('templatesDir', null, resourceProvider, optionIs: OptionKind.dir, mustExist: true, hide: true, diff --git a/lib/src/source_linker.dart b/lib/src/source_linker.dart index 5fc2313ce1..97b8660136 100644 --- a/lib/src/source_linker.dart +++ b/lib/src/source_linker.dart @@ -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 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>> createSourceLinkerOptions( +Future>> createSourceLinkerOptions( ResourceProvider resourceProvider) async { return [ DartdocOptionSet('linkToSource', resourceProvider) @@ -42,13 +36,13 @@ Future>> 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('revision', null, resourceProvider, + DartdocOptionArgOnly('revision', null, resourceProvider, help: 'Revision number to insert into the URI.'), - DartdocOptionArgFile('root', null, resourceProvider, + DartdocOptionArgFile('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('uriTemplate', null, resourceProvider, + DartdocOptionArgFile('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: @@ -63,16 +57,16 @@ class SourceLinker { final List 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}) { @@ -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'); } @@ -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) || @@ -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 ''; } }); } diff --git a/lib/src/tool_definition.dart b/lib/src/tool_definition.dart index ba39692c45..9885dcd876 100644 --- a/lib/src/tool_definition.dart +++ b/lib/src/tool_definition.dart @@ -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 @@ -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 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 setupCommand; + final List? setupCommand; /// A description of the defined tool. Must not be null. final String description; @@ -46,13 +43,11 @@ class ToolDefinition { /// given. factory ToolDefinition.fromCommand( List command, - List setupCommand, + List? setupCommand, String description, ResourceProvider resourceProvider, - {List compileArgs}) { - assert(command != null); + {List? compileArgs}) { assert(command.isNotEmpty); - assert(description != null); if (isDartExecutable(command[0])) { return DartToolDefinition( command, setupCommand, description, resourceProvider, @@ -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) { @@ -86,7 +80,7 @@ class ToolDefinition { } Future toolStateForArgs(String toolName, List args, - {@required ToolErrorCallback toolErrorCallback}) async { + {required ToolErrorCallback toolErrorCallback}) async { var commandPath = args.removeAt(0); return ToolStateForArgs(commandPath, args, null); } @@ -107,7 +101,7 @@ class DartToolDefinition extends ToolDefinition { /// built. @override Future toolStateForArgs(String toolName, List 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. @@ -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; @@ -148,11 +140,10 @@ class DartToolDefinition extends ToolDefinition { } } - DartToolDefinition(List command, List setupCommand, + DartToolDefinition(List command, List? 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 @@ -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; @@ -258,7 +249,7 @@ class SnapshotCache { class ToolStateForArgs { final String commandPath; final List args; - final void Function() onProcessComplete; + final void Function()? onProcessComplete; ToolStateForArgs(this.commandPath, this.args, this.onProcessComplete); } diff --git a/test/source_linker_test.dart b/test/source_linker_test.dart index adc591e34a..bf581e710a 100644 --- a/test/source_linker_test.dart +++ b/test/source_linker_test.dart @@ -53,6 +53,7 @@ void main() { excludes: [], lineNumber: 20, revision: '12345', + sourceFileName: 'path/to/file.dart', ).href(); expect(sourceLinkerHref, throwsA(TypeMatcher())); });