From 4fc81c6b5b9597542d803e00c3a31ff58cfbb519 Mon Sep 17 00:00:00 2001 From: pq Date: Tue, 16 Feb 2021 21:17:46 -0800 Subject: [PATCH 1/2] upgrade to nullable http and github --- lib/src/util/score_utils.dart | 10 +++--- pubspec.yaml | 4 +-- tool/bot/rule_doc_check.dart | 7 ++--- tool/bot/version_check.dart | 8 ++--- tool/canonical/scorecard.dart | 13 ++++---- tool/crawl.dart | 59 ++++++++++++++++++++--------------- tool/doc.dart | 40 +++++++++++++----------- tool/scorecard.dart | 18 ++++++----- tool/since.dart | 33 ++++++++++++-------- 9 files changed, 105 insertions(+), 87 deletions(-) diff --git a/lib/src/util/score_utils.dart b/lib/src/util/score_utils.dart index 77c075fcc..98a2ea7d5 100644 --- a/lib/src/util/score_utils.dart +++ b/lib/src/util/score_utils.dart @@ -3,7 +3,6 @@ // BSD-style license that can be found in the LICENSE file. import 'package:analyzer/src/lint/config.dart'; // ignore: implementation_imports -// ignore: import_of_legacy_library_into_null_safe import 'package:http/http.dart' as http; final _pedanticOptionsRootUrl = @@ -16,15 +15,18 @@ List? _pedanticRules; Future> get pedanticRules async => _pedanticRules ??= await _fetchPedanticRules(); -Future> fetchRules(Uri optionsUrl) async { +Future> fetchRules(Uri optionsUrl) async { final config = await _fetchConfig(optionsUrl); if (config == null) { print('no config found for: $optionsUrl (SKIPPED)'); return []; } - final rules = []; + final rules = []; for (var ruleConfig in config.ruleConfigs) { - rules.add(ruleConfig.name); + var name = ruleConfig.name; + if (name != null) { + rules.add(name); + } } return rules; } diff --git a/pubspec.yaml b/pubspec.yaml index bf5916eb4..7fcab22e9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,7 +17,7 @@ dependencies: charcode: ^1.2.0 collection: ^1.15.0 glob: ^2.0.0 - http: ^0.12.0 + http: ^0.13.0 meta: ^1.3.0 path: ^1.8.0 source_span: ^1.8.0 @@ -29,7 +29,7 @@ dev_dependencies: cli_util: ^0.3.0 fixnum: path: test/mock_packages/fixnum - github: '>=6.0.5 <8.0.0' + github: ^7.1.0-nullsafe.0 grinder: ^0.9.0-nullsafety markdown: ^4.0.0 matcher: ^0.12.10 diff --git a/tool/bot/rule_doc_check.dart b/tool/bot/rule_doc_check.dart index 334fa6222..66f8dc22f 100644 --- a/tool/bot/rule_doc_check.dart +++ b/tool/bot/rule_doc_check.dart @@ -4,7 +4,6 @@ import 'dart:convert'; -// ignore: import_of_legacy_library_into_null_safe import 'package:http/http.dart' as http; import 'package:test/test.dart'; @@ -28,8 +27,8 @@ void main() async { test(rule, () async { // todo (pq): consider replacing w/ lintCode.url // see: https://github.com/dart-lang/linter/issues/2034 - final response = await http - .head('https://dart-lang.github.io/linter/lints/$rule.html'); + final response = await http.head( + Uri.parse('https://dart-lang.github.io/linter/lints/$rule.html')); expect(response.statusCode, 200); }); } @@ -43,4 +42,4 @@ final _client = http.Client(); Future getBody(String url) async => (await getResponse(url)).body; -Future getResponse(String url) async => _client.get(url); +Future getResponse(String url) async => _client.get(Uri.parse(url)); diff --git a/tool/bot/version_check.dart b/tool/bot/version_check.dart index 5afb941e7..a179aec7a 100644 --- a/tool/bot/version_check.dart +++ b/tool/bot/version_check.dart @@ -5,7 +5,6 @@ import 'dart:convert'; import 'dart:io'; -// ignore: import_of_legacy_library_into_null_safe import 'package:http/http.dart' as http; void main() async { @@ -17,8 +16,8 @@ void main() async { print('Checking for a git release tag corresponding to $latestVersion...'); var client = http.Client(); - var req = await client - .get('https://github.com/dart-lang/linter/releases/tag/$latestVersion'); + var req = await client.get(Uri.parse( + 'https://github.com/dart-lang/linter/releases/tag/$latestVersion')); if (req.statusCode == 404) { print( @@ -35,4 +34,5 @@ final _client = http.Client(); Future getBody(String url) async => (await getResponse(url)).body; -Future getResponse(String url) async => _client.get(url); +Future getResponse(String url) async => + _client.get(Uri.parse(url)); diff --git a/tool/canonical/scorecard.dart b/tool/canonical/scorecard.dart index f68e1dd4a..da0162cca 100644 --- a/tool/canonical/scorecard.dart +++ b/tool/canonical/scorecard.dart @@ -9,9 +9,7 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/visitor.dart'; import 'package:analyzer/src/lint/config.dart'; // ignore: implementation_imports import 'package:analyzer/src/lint/registry.dart'; // ignore: implementation_imports -// ignore: import_of_legacy_library_into_null_safe import 'package:github/github.dart'; -// ignore: import_of_legacy_library_into_null_safe import 'package:http/http.dart' as http; import 'package:linter/src/analyzer.dart'; import 'package:linter/src/rules.dart'; @@ -285,7 +283,8 @@ class ScoreCard { var bugReferences = []; for (var bug in bugs) { - if (bug.title.contains(lint.name)) { + var title = bug.title; + if (title != null && title.contains(lint.name)) { bugReferences.add('#${bug.number.toString()}'); } } @@ -331,8 +330,8 @@ class ScoreCard { static Future> _getLintsWithBulkFixes() async { var client = http.Client(); - var req = await client.get( - 'https://raw.githubusercontent.com/dart-lang/sdk/master/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart'); + var req = await client.get(Uri.parse( + 'https://raw.githubusercontent.com/dart-lang/sdk/master/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart')); var parser = CompilationUnitParser(); var cu = parser.parse(contents: req.body, name: 'bulk_fix_processor.dart'); @@ -346,8 +345,8 @@ class ScoreCard { static Future> _getLintsWithFixes() async { var client = http.Client(); - var req = await client.get( - 'https://raw.githubusercontent.com/dart-lang/sdk/master/pkg/analysis_server/lib/src/services/linter/lint_names.dart'); + var req = await client.get(Uri.parse( + 'https://raw.githubusercontent.com/dart-lang/sdk/master/pkg/analysis_server/lib/src/services/linter/lint_names.dart')); var parser = CompilationUnitParser(); var cu = parser.parse(contents: req.body, name: 'lint_names.dart'); diff --git a/tool/crawl.dart b/tool/crawl.dart index 6bfb82c80..3d7deb6c9 100644 --- a/tool/crawl.dart +++ b/tool/crawl.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. -// ignore_for_file: import_of_legacy_library_into_null_safe - import 'dart:async'; import 'dart:io'; @@ -16,6 +14,8 @@ import 'package:linter/src/util/score_utils.dart' as score_utils; import 'package:pub_semver/pub_semver.dart'; import 'package:yaml/yaml.dart'; +/// todo (pq): reign in the nullable types + const _allPathSuffix = '/example/all.yaml'; final _effectiveDartOptionsRootUrl = Uri.https( 'raw.githubusercontent.com', '/tenhobi/effective_dart/master/lib/'); @@ -36,7 +36,7 @@ final _stagehandOptionsUrl = Uri.https('raw.githubusercontent.com', /// We don't care about SDKs previous to this bottom. final Version bottomDartSdk = Version(2, 0, 0); -Map _dartSdkToLinterMap = {}; +Map _dartSdkToLinterMap = {}; List? _effectiveDartRules; List? _flutterRepoRules; @@ -45,11 +45,11 @@ int? _latestMinor; Iterable? _registeredLints; -List? _sdkTags; +List? _sdkTags; -Map> _sinceMap = >{}; +Map> _sinceMap = >{}; -List? _stagehandRules; +List? _stagehandRules; Future> get effectiveDartRules async => _effectiveDartRules ??= await _fetchEffectiveDartRules(); @@ -65,26 +65,28 @@ Future get latestMinor async => Future> get pedanticRules async => score_utils.pedanticRules; -Iterable? get registeredLints { +Iterable get registeredLints { if (_registeredLints == null) { registerLintRules(); _registeredLints = Registry.ruleRegistry; } - return _registeredLints; + return _registeredLints!; } -Future> get sdkTags async => _sdkTags ??= await _fetchSdkTags(); +Future> get sdkTags async => _sdkTags ??= await _fetchSdkTags(); -Future> get stagehandRules async => +Future> get stagehandRules async => _stagehandRules ??= await score_utils.fetchRules(_stagehandOptionsUrl); Future dartSdkForLinter(String version) async { var sdkVersions = []; var sdks = await sdkTags; for (var sdk in sdks) { - var linterVersion = await linterForDartSdk(sdk); - if (linterVersion == version) { - sdkVersions.add(sdk); + if (sdk != null) { + var linterVersion = await linterForDartSdk(sdk); + if (linterVersion == version) { + sdkVersions.add(sdk); + } } } @@ -92,7 +94,7 @@ Future dartSdkForLinter(String version) async { return sdkVersions.isNotEmpty ? sdkVersions.first : null; } -Future> fetchRulesForVersion(String version) async => +Future> fetchRulesForVersion(String version) async => score_utils.fetchRules(_repoPathPrefix.resolve('$version$_allPathSuffix')); Future findSinceDartSdk(String linterVersion) async => @@ -100,11 +102,13 @@ Future findSinceDartSdk(String linterVersion) async => Future findSinceLinter(String lint) async { // History recorded in `all.yaml` starts in minor 31. - var rules_31 = await (rulesForVersion(31) as FutureOr>); - if (rules_31.contains(lint)) { - var version = await _crawlForVersion(lint); - if (version != null) { - return version; + var rules_31 = await rulesForVersion(31); + if (rules_31 != null) { + if (rules_31.contains(lint)) { + var version = await _crawlForVersion(lint); + if (version != null) { + return version; + } } } @@ -121,15 +125,14 @@ Future findSinceLinter(String lint) async { return null; } -Future linterForDartSdk(String sdk) async => - _dartSdkToLinterMap[sdk] ??= - await (_fetchLinterForVersion(sdk) as FutureOr); +Future linterForDartSdk(String sdk) async => + _dartSdkToLinterMap[sdk] ??= await _fetchLinterForVersion(sdk); -Future?> rulesForVersion(int minor) async { +Future?> rulesForVersion(int minor) async { var version = '0.1.$minor'; if (minor >= 31) { - return _sinceMap[version] ??= - await (fetchRulesForVersion(version) as FutureOr>); + var rules = await fetchRulesForVersion(version); + return _sinceMap[version] ??= rules; } return null; } @@ -179,7 +182,7 @@ Future _fetchLinterForVersion(String version) async { return null; } -Future> _fetchSdkTags() { +Future> _fetchSdkTags() { final github = GitHub(); final slug = RepositorySlug('dart-lang', 'sdk'); @@ -188,7 +191,11 @@ Future> _fetchSdkTags() { return github.repositories .listTags(slug) .map((t) => t.name) + .where((t) => t != null) .where((t) { + if (t == null) { + return false; + } // Filter on numeric release tags. if (!t.startsWith(RegExp(r'\d+'))) { return false; diff --git a/tool/doc.dart b/tool/doc.dart index 98ea51b6e..8945c44a2 100644 --- a/tool/doc.dart +++ b/tool/doc.dart @@ -8,7 +8,6 @@ import 'dart:io'; import 'package:analyzer/src/lint/config.dart'; import 'package:analyzer/src/lint/registry.dart'; import 'package:args/args.dart'; -// ignore: import_of_legacy_library_into_null_safe import 'package:http/http.dart' as http; import 'package:linter/src/analyzer.dart'; import 'package:linter/src/rules.dart'; @@ -79,7 +78,7 @@ Future get effectiveDartLatestVersion async { 'https://raw.githubusercontent.com/tenhobi/effective_dart/master/lib/analysis_options.yaml'; var client = http.Client(); print('loading $url...'); - var req = await client.get(url); + var req = await client.get(Uri.parse(url)); var parts = req.body.split('package:effective_dart/analysis_options.'); return parts[1].split('.yaml')[0]; } @@ -109,7 +108,7 @@ Future get pedanticLatestVersion async { 'https://raw.githubusercontent.com/dart-lang/pedantic/master/lib/analysis_options.yaml'; var client = http.Client(); print('loading $url...'); - var req = await client.get(url); + var req = await client.get(Uri.parse(url)); var parts = req.body.split('package:pedantic/analysis_options.'); return parts[1].split('.yaml')[0]; } @@ -121,32 +120,35 @@ Future fetchBadgeInfo() async { var latestPedantic = await pedanticLatestVersion; var latestEffectiveDart = await effectiveDartLatestVersion; - var pedantic = await (fetchConfig( - 'https://raw.githubusercontent.com/dart-lang/pedantic/master/lib/analysis_options.$latestPedantic.yaml') - as FutureOr); - for (var ruleConfig in pedantic.ruleConfigs) { - pedanticRules.add(ruleConfig.name); + var pedantic = await fetchConfig( + 'https://raw.githubusercontent.com/dart-lang/pedantic/master/lib/analysis_options.$latestPedantic.yaml'); + if (pedantic != null) { + for (var ruleConfig in pedantic.ruleConfigs) { + pedanticRules.add(ruleConfig.name); + } } - var effectiveDart = await (fetchConfig( - 'https://raw.githubusercontent.com/tenhobi/effective_dart/master/lib/analysis_options.$latestEffectiveDart.yaml') - as FutureOr); - for (var ruleConfig in effectiveDart.ruleConfigs) { - effectiveDartRules.add(ruleConfig.name); + var effectiveDart = await fetchConfig( + 'https://raw.githubusercontent.com/tenhobi/effective_dart/master/lib/analysis_options.$latestEffectiveDart.yaml'); + if (effectiveDart != null) { + for (var ruleConfig in effectiveDart.ruleConfigs) { + effectiveDartRules.add(ruleConfig.name); + } } - var flutter = await (fetchConfig( - 'https://raw.githubusercontent.com/flutter/flutter/master/packages/flutter/lib/analysis_options_user.yaml') - as FutureOr); - for (var ruleConfig in flutter.ruleConfigs) { - flutterRules.add(ruleConfig.name); + var flutter = await fetchConfig( + 'https://raw.githubusercontent.com/flutter/flutter/master/packages/flutter/lib/analysis_options_user.yaml'); + if (flutter != null) { + for (var ruleConfig in flutter.ruleConfigs) { + flutterRules.add(ruleConfig.name); + } } } Future fetchConfig(String url) async { var client = http.Client(); print('loading $url...'); - var req = await client.get(url); + var req = await client.get(Uri.parse(url)); return processAnalysisOptionsFile(req.body); } diff --git a/tool/scorecard.dart b/tool/scorecard.dart index c766a37ee..83cffed53 100644 --- a/tool/scorecard.dart +++ b/tool/scorecard.dart @@ -7,9 +7,7 @@ import 'dart:async'; import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/visitor.dart'; import 'package:analyzer/src/lint/registry.dart'; -// ignore: import_of_legacy_library_into_null_safe import 'package:github/github.dart'; -// ignore: import_of_legacy_library_into_null_safe import 'package:http/http.dart' as http; import 'package:linter/src/analyzer.dart'; import 'package:linter/src/rules.dart'; @@ -205,8 +203,8 @@ class ScoreCard { static Future> _getLintsWithFixes() async { var client = http.Client(); - var req = await client.get( - 'https://raw.githubusercontent.com/dart-lang/sdk/master/pkg/analysis_server/lib/src/services/linter/lint_names.dart'); + var req = await client.get(Uri.parse( + 'https://raw.githubusercontent.com/dart-lang/sdk/master/pkg/analysis_server/lib/src/services/linter/lint_names.dart')); var parser = CompilationUnitParser(); var cu = parser.parse(contents: req.body, name: 'lint_names.dart'); @@ -220,8 +218,8 @@ class ScoreCard { static Future> _getLintsWithAssists() async { var client = http.Client(); - var req = await client.get( - 'https://raw.githubusercontent.com/dart-lang/sdk/master/pkg/analysis_server/lib/src/services/correction/assist.dart'); + var req = await client.get(Uri.parse( + 'https://raw.githubusercontent.com/dart-lang/sdk/master/pkg/analysis_server/lib/src/services/correction/assist.dart')); var parser = CompilationUnitParser(); var cu = parser.parse(contents: req.body, name: 'assist.dart'); var assistKindClass = cu.declarations.firstWhere( @@ -274,7 +272,8 @@ class ScoreCard { } var bugReferences = []; for (var bug in bugs) { - if (bug.title.contains(lint.name)) { + var title = bug.title; + if (title != null && title.contains(lint.name)) { bugReferences.add('#${bug.number.toString()}'); } } @@ -297,7 +296,10 @@ class ScoreCard { } } -bool _isBug(Issue issue) => issue.labels.map((l) => l.name).contains('bug'); +bool _isBug(Issue issue) { + var labels = issue.labels; + return labels != null && labels.map((l) => l.name).contains('bug'); +} class LintScore { String? name; diff --git a/tool/since.dart b/tool/since.dart index e3a615a8f..643f832d9 100644 --- a/tool/since.dart +++ b/tool/since.dart @@ -42,7 +42,7 @@ Future> _getSinceInfo() async { final linterVersionCache = loadYamlNode(linterCache) as YamlMap; var sinceMap = {}; - for (var lint in registeredLints!.map((l) => l.name)) { + for (var lint in registeredLints.map((l) => l.name)) { var linterVersion = linterVersionCache[lint] as String?; if (linterVersion == null) { linterVersion = await findSinceLinter(lint); @@ -69,12 +69,14 @@ Future?> get dartSdkMap async { var sdks = await sdkTags; for (var sdk in sdks) { - if (!_dartSdkMap!.containsKey(sdk)) { + if (sdk != null && !_dartSdkMap!.containsKey(sdk)) { var linterVersion = await linterForDartSdk(sdk); - _dartSdkMap![sdk] = linterVersion; - print('fetched...'); - print('$sdk : $linterVersion'); - print('(consider caching in tool/since/dart_sdk.yaml)'); + if (linterVersion != null) { + _dartSdkMap![sdk] = linterVersion; + print('fetched...'); + print('$sdk : $linterVersion'); + print('(consider caching in tool/since/dart_sdk.yaml)'); + } } } } @@ -94,10 +96,12 @@ Future _sinceSdkForLinter(String? linterVersionString) async { } var sdkVersions = []; - var sdkCache = await (dartSdkMap as FutureOr>); - for (var sdkEntry in sdkCache.entries) { - if (Version.parse(sdkEntry.value) == linterVersion) { - sdkVersions.add(sdkEntry.key); + var sdkCache = await dartSdkMap; + if (sdkCache != null) { + for (var sdkEntry in sdkCache.entries) { + if (Version.parse(sdkEntry.value) == linterVersion) { + sdkVersions.add(sdkEntry.key); + } } } if (sdkVersions.isEmpty) { @@ -110,9 +114,12 @@ Future _sinceSdkForLinter(String? linterVersionString) async { } Future _nextLinterVersion(Version linterVersion) async { - for (final version in await (linterVersions as FutureOr>)) { - if (Version.parse(version).compareTo(linterVersion) > 0) { - return version; + var versions = await linterVersions; + if (versions != null) { + for (final version in versions) { + if (Version.parse(version).compareTo(linterVersion) > 0) { + return version; + } } } return null; From 4e63588a7be88496bdabfdbfdb319a68116237d8 Mon Sep 17 00:00:00 2001 From: pq Date: Tue, 16 Feb 2021 21:40:39 -0800 Subject: [PATCH 2/2] cleanup --- tool/crawl.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/tool/crawl.dart b/tool/crawl.dart index 3d7deb6c9..f08fd56de 100644 --- a/tool/crawl.dart +++ b/tool/crawl.dart @@ -191,7 +191,6 @@ Future> _fetchSdkTags() { return github.repositories .listTags(slug) .map((t) => t.name) - .where((t) => t != null) .where((t) { if (t == null) { return false;