From 7bff366b96f35027f7368bf8410721888696846a Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Fri, 30 Apr 2021 05:34:04 +0000 Subject: [PATCH] Convert snippets tool to null safety (#78646) --- dev/bots/analyze_sample_code.dart | 5 +- dev/snippets/lib/configuration.dart | 12 ++-- dev/snippets/lib/main.dart | 32 +++++----- dev/snippets/lib/snippets.dart | 76 ++++++++++++----------- dev/snippets/pubspec.yaml | 2 +- dev/snippets/test/configuration_test.dart | 2 +- dev/snippets/test/snippets_test.dart | 8 +-- 7 files changed, 72 insertions(+), 65 deletions(-) diff --git a/dev/bots/analyze_sample_code.dart b/dev/bots/analyze_sample_code.dart index 94574a4a09..2e7830819d 100644 --- a/dev/bots/analyze_sample_code.dart +++ b/dev/bots/analyze_sample_code.dart @@ -355,7 +355,10 @@ class SampleChecker { } else { return Process.runSync( _dartExecutable, - [path.canonicalize(_snippetsSnapshotPath!), ...args], + [ + path.canonicalize(_snippetsSnapshotPath!), + ...args, + ], workingDirectory: workingDirectory, ); } diff --git a/dev/snippets/lib/configuration.dart b/dev/snippets/lib/configuration.dart index efbe8ca19f..d3729ba4c2 100644 --- a/dev/snippets/lib/configuration.dart +++ b/dev/snippets/lib/configuration.dart @@ -27,7 +27,7 @@ String getEnumName(dynamic enumItem) { /// A class to compute the configuration of the snippets input and output /// locations based in the current location of the snippets main.dart. class Configuration { - Configuration({@required this.flutterRoot}) : assert(flutterRoot != null); + Configuration({required this.flutterRoot}) : assert(flutterRoot != null); final Directory flutterRoot; @@ -37,20 +37,22 @@ class Configuration { Directory get configDirectory { _configPath ??= Directory( path.canonicalize(path.join(flutterRoot.absolute.path, 'dev', 'snippets', 'config'))); - return _configPath; + return _configPath!; } - Directory _configPath; + // Nullable so that we can use it as a lazy cache. + Directory? _configPath; /// This is where the snippets themselves will be written, in order to be /// uploaded to the docs site. Directory get outputDirectory { _docsDirectory ??= Directory( path.canonicalize(path.join(flutterRoot.absolute.path, 'dev', 'docs', 'doc', 'snippets'))); - return _docsDirectory; + return _docsDirectory!; } - Directory _docsDirectory; + // Nullable so that we can use it as a lazy cache. + Directory? _docsDirectory; /// This makes sure that the output directory exists. void createOutputDirectory() { diff --git a/dev/snippets/lib/main.dart b/dev/snippets/lib/main.dart index e0539440fc..7f1729a24d 100644 --- a/dev/snippets/lib/main.dart +++ b/dev/snippets/lib/main.dart @@ -22,13 +22,13 @@ const String _kTypeOption = 'type'; const String _kShowDartPad = 'dartpad'; String getChannelName() { - final RegExp gitBranchRegexp = RegExp(r'^## (.*)'); + final RegExp gitBranchRegexp = RegExp(r'^## (?.*)'); final ProcessResult gitResult = Process.runSync('git', ['status', '-b', '--porcelain']); if (gitResult.exitCode != 0) throw 'git status exit with non-zero exit code: ${gitResult.exitCode}'; - final Match gitBranchMatch = gitBranchRegexp.firstMatch( + final RegExpMatch? gitBranchMatch = gitBranchRegexp.firstMatch( (gitResult.stdout as String).trim().split('\n').first); - return gitBranchMatch == null ? '' : gitBranchMatch.group(1).split('...').first; + return gitBranchMatch == null ? '' : gitBranchMatch.namedGroup('branch')!.split('...').first; } /// Generates snippet dartdoc output for a given input, and creates any sample @@ -113,8 +113,7 @@ void main(List argList) { } final SnippetType snippetType = SnippetType.values - .firstWhere((SnippetType type) => getEnumName(type) == args[_kTypeOption], orElse: () => null); - assert(snippetType != null, "Unable to find '${args[_kTypeOption]}' in SnippetType enum."); + .firstWhere((SnippetType type) => getEnumName(type) == args[_kTypeOption]); if (args[_kShowDartPad] == true && snippetType != SnippetType.sample) { errorExit('${args[_kTypeOption]} was selected, but the --dartpad flag is only valid ' @@ -132,7 +131,7 @@ void main(List argList) { errorExit('The input file ${input.path} does not exist.'); } - String template; + String? template; if (snippetType == SnippetType.sample) { final String templateArg = args[_kTemplateOption] as String; if (templateArg == null || templateArg.isEmpty) { @@ -143,25 +142,24 @@ void main(List argList) { template = templateArg.replaceAll(RegExp(r'.tmpl$'), ''); } - String emptyToNull(String value) => value?.isEmpty ?? true ? null : value; - final String packageName = emptyToNull(args[_kPackageOption] as String); - final String libraryName = emptyToNull(args[_kLibraryOption] as String); - final String elementName = emptyToNull(args[_kElementOption] as String); - final String serial = emptyToNull(args[_kSerialOption] as String); + final String packageName = args[_kPackageOption] as String? ?? ''; + final String libraryName = args[_kLibraryOption] as String? ?? ''; + final String elementName = args[_kElementOption] as String? ?? ''; + final String serial = args[_kSerialOption] as String? ?? ''; final List id = []; if (args[_kOutputOption] != null) { id.add(path.basename(path.basenameWithoutExtension(args[_kOutputOption] as String))); } else { - if (packageName != null && packageName != 'flutter') { + if (packageName.isNotEmpty && packageName != 'flutter') { id.add(packageName); } - if (libraryName != null) { + if (libraryName.isNotEmpty) { id.add(libraryName); } - if (elementName != null) { + if (elementName.isNotEmpty) { id.add(elementName); } - if (serial != null) { + if (serial.isNotEmpty) { id.add(serial); } if (id.isEmpty) { @@ -178,10 +176,10 @@ void main(List argList) { showDartPad: args[_kShowDartPad] as bool, template: template, output: args[_kOutputOption] != null ? File(args[_kOutputOption] as String) : null, - metadata: { + metadata: { 'sourcePath': environment['SOURCE_PATH'], 'sourceLine': environment['SOURCE_LINE'] != null - ? int.tryParse(environment['SOURCE_LINE']) + ? int.tryParse(environment['SOURCE_LINE']!) : null, 'id': id.join('.'), 'channel': getChannelName(), diff --git a/dev/snippets/lib/snippets.dart b/dev/snippets/lib/snippets.dart index 4816cc5a07..0b12ce125b 100644 --- a/dev/snippets/lib/snippets.dart +++ b/dev/snippets/lib/snippets.dart @@ -6,7 +6,6 @@ import 'dart:convert'; import 'dart:io'; import 'package:dart_style/dart_style.dart'; -import 'package:meta/meta.dart'; import 'package:path/path.dart' as path; import 'configuration.dart'; @@ -19,7 +18,7 @@ void errorExit(String message) { // A Tuple containing the name and contents associated with a code block in a // snippet. class _ComponentTuple { - _ComponentTuple(this.name, this.contents, {String language}) : language = language ?? ''; + _ComponentTuple(this.name, this.contents, {this.language = ''}); final String name; final List contents; final String language; @@ -29,7 +28,7 @@ class _ComponentTuple { /// Generates the snippet HTML, as well as saving the output snippet main to /// the output directory. class SnippetGenerator { - SnippetGenerator({Configuration configuration}) + SnippetGenerator({Configuration? configuration}) : configuration = configuration ?? // Flutter's root is four directories up from this script. Configuration(flutterRoot: Directory(Platform.environment['FLUTTER_ROOT'] @@ -52,7 +51,7 @@ class SnippetGenerator { File getOutputFile(String id) => File(path.join(configuration.outputDirectory.path, '$id.dart')); /// Gets the path to the template file requested. - File getTemplatePath(String templateName, {Directory templatesDir}) { + File? getTemplatePath(String templateName, {Directory? templatesDir}) { final Directory templateDir = templatesDir ?? configuration.templatesDirectory; final File templateFile = File(path.join(templateDir.path, '$templateName.tmpl')); return templateFile.existsSync() ? templateFile : null; @@ -61,7 +60,7 @@ class SnippetGenerator { /// Injects the [injections] into the [template], and turning the /// "description" injection into a comment. Only used for /// [SnippetType.sample] snippets. - String interpolateTemplate(List<_ComponentTuple> injections, String template, Map metadata) { + String interpolateTemplate(List<_ComponentTuple> injections, String template, Map metadata) { final RegExp moustacheRegExp = RegExp('{{([^}]+)}}'); return template.replaceAllMapped(moustacheRegExp, (Match match) { if (match[1] == 'description') { @@ -86,9 +85,12 @@ class SnippetGenerator { // mustache reference, since we want to allow the sections to be // "optional" in the input: users shouldn't be forced to add an empty // "```dart preamble" section if that section would be empty. - final _ComponentTuple result = injections - .firstWhere((_ComponentTuple tuple) => tuple.name == match[1], orElse: () => null); - return result?.mergedContent ?? (metadata[match[1]] ?? '').toString(); + final int componentIndex = injections + .indexWhere((_ComponentTuple tuple) => tuple.name == match[1]); + if (componentIndex == -1) { + return (metadata[match[1]] ?? '').toString(); + } + return injections[componentIndex].mergedContent; } }).trim(); } @@ -100,10 +102,15 @@ class SnippetGenerator { /// /// Takes into account the [type] and doesn't substitute in the id and the app /// if not a [SnippetType.sample] snippet. - String interpolateSkeleton(SnippetType type, List<_ComponentTuple> injections, String skeleton, Map metadata) { + String interpolateSkeleton( + SnippetType type, + List<_ComponentTuple> injections, + String skeleton, + Map metadata, + ) { final List result = []; const HtmlEscape htmlEscape = HtmlEscape(); - String language; + String language = 'dart'; for (final _ComponentTuple injection in injections) { if (!injection.name.startsWith('code')) { continue; @@ -133,11 +140,11 @@ class SnippetGenerator { final Map substitutions = { 'description': description, 'code': htmlEscape.convert(result.join('\n')), - 'language': language ?? 'dart', + 'language': language, 'serial': '', - 'id': metadata['id'] as String, + 'id': metadata['id']! as String, 'channel': channel, - 'element': metadata['element'] as String ?? '', + 'element': (metadata['element'] ?? '') as String, 'app': '', }; if (type == SnippetType.sample) { @@ -146,7 +153,7 @@ class SnippetGenerator { ..['app'] = htmlEscape.convert(injections.firstWhere((_ComponentTuple tuple) => tuple.name == 'app').mergedContent); } return skeleton.replaceAllMapped(RegExp('{{(${substitutions.keys.join('|')})}}'), (Match match) { - return substitutions[match[1]]; + return substitutions[match[1]] ?? ''; }); } @@ -157,16 +164,16 @@ class SnippetGenerator { input = input.trim(); final List description = []; final List<_ComponentTuple> components = <_ComponentTuple>[]; - String language; - final RegExp codeStartEnd = RegExp(r'^\s*```([-\w]+|[-\w]+ ([-\w]+))?\s*$'); + String? language; + final RegExp codeStartEnd = RegExp(r'^\s*```(?[-\w]+|[-\w]+ (?
[-\w]+))?\s*$'); for (final String line in input.split('\n')) { - final Match match = codeStartEnd.firstMatch(line); + final RegExpMatch? match = codeStartEnd.firstMatch(line); if (match != null) { // If we saw the start or end of a code block inCodeBlock = !inCodeBlock; - if (match[1] != null) { - language = match[1]; - if (match[2] != null) { - components.add(_ComponentTuple('code-${match[2]}', [], language: language)); + if (match.namedGroup('language') != null) { + language = match[1]!; + if (match.namedGroup('section') != null) { + components.add(_ComponentTuple('code-${match.namedGroup('section')}', [], language: language)); } else { components.add(_ComponentTuple('code', [], language: language)); } @@ -191,7 +198,7 @@ class SnippetGenerator { } String _loadFileAsUtf8(File file) { - return file.readAsStringSync(encoding: Encoding.getByName('utf-8')); + return file.readAsStringSync(encoding: utf8); } String _addLineNumbers(String app) { @@ -228,13 +235,12 @@ class SnippetGenerator { File input, SnippetType type, { bool showDartPad = false, - String template, - File output, - @required Map metadata, + String? template, + File? output, + required Map metadata, }) { assert(template != null || type != SnippetType.sample); - assert(metadata != null && metadata['id'] != null); - assert(input != null); + assert(metadata['id'] != null); assert(!showDartPad || type == SnippetType.sample, 'Only application samples work with dartpad.'); final List<_ComponentTuple> snippetData = parseInput(_loadFileAsUtf8(input)); switch (type) { @@ -244,10 +250,9 @@ class SnippetGenerator { stderr.writeln('Unable to find the templates directory.'); exit(1); } - final File templateFile = getTemplatePath(template, templatesDir: templatesDir); + final File? templateFile = getTemplatePath(template!, templatesDir: templatesDir); if (templateFile == null) { - stderr.writeln( - 'The template $template was not found in the templates directory ${templatesDir.path}'); + stderr.writeln('The template $template was not found in the templates directory ${templatesDir.path}'); exit(1); } final String templateContents = _loadFileAsUtf8(templateFile); @@ -261,20 +266,19 @@ class SnippetGenerator { } snippetData.add(_ComponentTuple('app', app.split('\n'))); - final File outputFile = output ?? getOutputFile(metadata['id'] as String); + final File outputFile = output ?? getOutputFile(metadata['id']! as String); stderr.writeln('Writing to ${outputFile.absolute.path}'); outputFile.writeAsStringSync(app); final File metadataFile = File(path.join(path.dirname(outputFile.path), '${path.basenameWithoutExtension(outputFile.path)}.json')); stderr.writeln('Writing metadata to ${metadataFile.absolute.path}'); - final _ComponentTuple description = snippetData.firstWhere( - (_ComponentTuple data) => data.name == 'description', - orElse: () => null, - ); + final int descriptionIndex = snippetData.indexWhere( + (_ComponentTuple data) => data.name == 'description'); + final String descriptionString = descriptionIndex == -1 ? '' : snippetData[descriptionIndex].mergedContent; metadata.addAll({ 'file': path.basename(outputFile.path), - 'description': description?.mergedContent, + 'description': descriptionString, }); metadataFile.writeAsStringSync(jsonEncoder.convert(metadata)); break; diff --git a/dev/snippets/pubspec.yaml b/dev/snippets/pubspec.yaml index a1fe347aa6..d79a53d290 100644 --- a/dev/snippets/pubspec.yaml +++ b/dev/snippets/pubspec.yaml @@ -5,7 +5,7 @@ description: A code snippet dartdoc extension for Flutter API docs. homepage: https://github.com/flutter/flutter environment: - sdk: ">=2.2.2 <3.0.0" + sdk: ">=2.12.1 <3.0.0" dartdoc: # Exclude this package from the hosted API docs (Ironically...). diff --git a/dev/snippets/test/configuration_test.dart b/dev/snippets/test/configuration_test.dart index c3e6d521f1..26131ab655 100644 --- a/dev/snippets/test/configuration_test.dart +++ b/dev/snippets/test/configuration_test.dart @@ -9,7 +9,7 @@ import 'package:test/test.dart' hide TypeMatcher, isInstanceOf; void main() { group('Configuration', () { - Configuration config; + late Configuration config; setUp(() { config = Configuration(flutterRoot: Directory('/flutter sdk')); diff --git a/dev/snippets/test/snippets_test.dart b/dev/snippets/test/snippets_test.dart index c16d3f2d2c..e5590b8a96 100644 --- a/dev/snippets/test/snippets_test.dart +++ b/dev/snippets/test/snippets_test.dart @@ -12,10 +12,10 @@ import 'package:test/test.dart' hide TypeMatcher, isInstanceOf; void main() { group('Generator', () { - Configuration configuration; - SnippetGenerator generator; - Directory tmpDir; - File template; + late Configuration configuration; + late SnippetGenerator generator; + late Directory tmpDir; + late File template; setUp(() { tmpDir = Directory.systemTemp.createTempSync('flutter_snippets_test.');