Skip to content

Commit 0afc9eb

Browse files
authored
Add Dart Code Metrics (#2055)
1 parent 5fcbb80 commit 0afc9eb

24 files changed

+112
-69
lines changed

dwds/analysis_options.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,45 @@ linter:
2222
- require_trailing_commas
2323
- unawaited_futures
2424
- unnecessary_lambdas
25+
26+
# TODO(https://github.com/dart-lang/webdev/issues/2053): Enable commented-out rules with fixes.
27+
dart_code_metrics:
28+
metrics:
29+
# cyclomatic-complexity: 20
30+
# number-of-parameters: 5
31+
# maximum-nesting-level: 5
32+
metrics-exclude:
33+
- test/**
34+
rules:
35+
- avoid-cascade-after-if-null
36+
- avoid-collection-methods-with-unrelated-types
37+
- avoid-duplicate-exports
38+
# - avoid-global-state
39+
- avoid-missing-enum-constant-in-map
40+
- avoid-nested-conditional-expressions
41+
- avoid-non-ascii-symbols
42+
# - avoid-non-null-assertion
43+
# - avoid-passing-async-when-sync-expected
44+
- avoid-redundant-async
45+
# - avoid-throw-in-catch-block
46+
- avoid-unnecessary-type-assertions
47+
- avoid-unnecessary-type-casts
48+
- avoid-unrelated-type-assertions
49+
- avoid-unused-parameters
50+
- binary-expression-operand-order
51+
- double-literal-format
52+
# - format-comment
53+
# - member-ordering
54+
- no-boolean-literal-compare
55+
# - no-empty-block
56+
- no-equal-then-else
57+
# - prefer-async-await
58+
# - prefer-commenting-analyzer-ignores
59+
# - prefer-correct-test-file-name
60+
- prefer-correct-type-name
61+
- prefer-enums-by-name
62+
# - prefer-immediate-return
63+
- prefer-iterable-of
64+
- prefer-last
65+
# - prefer-moving-to-variable:
66+
# allow-duplicated-chains: 2

dwds/debug_extension_mv3/tool/build_extension.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Future<int> run({required bool isProd, required bool isMV3}) async {
7474
return updateExitCode;
7575
}
7676

77-
Future<int> _handleProcess(Process process) async {
77+
Future<int> _handleProcess(Process process) {
7878
_handleOutput(process.stdout, isStdout: true);
7979
_handleOutput(process.stderr, isStdout: false);
8080
return process.exitCode;

dwds/debug_extension_mv3/web/background.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void _registerListeners() {
6262
Future<void> _handleRuntimeMessages(
6363
dynamic jsRequest,
6464
MessageSender sender,
65-
Function sendResponse,
65+
Function _,
6666
) async {
6767
if (jsRequest is! String) return;
6868

dwds/debug_extension_mv3/web/cross_extension_communication.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final _eventsForAngularDartDevTools = {
2929

3030
Future<void> handleMessagesFromAngularDartDevTools(
3131
dynamic jsRequest,
32-
MessageSender sender,
32+
MessageSender _,
3333
Function sendResponse,
3434
) async {
3535
if (jsRequest == null) return;
@@ -107,7 +107,8 @@ void _forwardMessageToAngularDartDevTools(ExternalExtensionMessage message) {
107107
chrome.runtime.sendMessage(
108108
_angularDartDevToolsId,
109109
message,
110-
/* options */ null,
110+
/* options */
111+
null,
111112
allowInterop(([result]) => _checkForErrors(result, message.name)),
112113
);
113114
}

dwds/debug_extension_mv3/web/debug_session.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,11 @@ _enableExecutionContextReporting(int tabId) {
234234
if (chromeError != null) {
235235
final errorMessage = _translateChromeError(chromeError.message);
236236
chrome.notifications.create(
237-
/*notificationId*/ null,
237+
/*notificationId*/
238+
null,
238239
NotificationOptions(message: errorMessage),
239-
/*callback*/ null,
240+
/*callback*/
241+
null,
240242
);
241243
return;
242244
}
@@ -682,7 +684,8 @@ Future<bool> _sendAuthRequest(String authUrl) async {
682684
Future<bool> _showWarningNotification(String message) {
683685
final completer = Completer<bool>();
684686
chrome.notifications.create(
685-
/*notificationId*/ null,
687+
/*notificationId*/
688+
null,
686689
NotificationOptions(
687690
title: '[Error] Dart Debug Extension',
688691
message: message,

dwds/debug_extension_mv3/web/lifeline_connection.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'chrome_api.dart';
66
import 'logger.dart';
77

8-
void main() async {
8+
void main() {
99
_connectToLifelinePort();
1010
}
1111

@@ -15,7 +15,8 @@ void _connectToLifelinePort() {
1515
prefix: 'Dart Debug Extension',
1616
);
1717
chrome.runtime.connect(
18-
/*extensionId=*/ null,
18+
/*extensionId=*/
19+
null,
1920
ConnectInfo(name: 'keepAlive'),
2021
);
2122
}

dwds/debug_extension_mv3/web/messaging.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ Future<bool> sendRuntimeMessage({
118118
);
119119
final completer = Completer<bool>();
120120
chrome.runtime.sendMessage(
121-
/*id*/ null,
121+
/*id*/
122+
null,
122123
message.toJSON(),
123-
/*options*/ null,
124+
/*options*/
125+
null,
124126
allowInterop(() {
125127
final error = chrome.runtime.lastError;
126128
if (error != null) {

dwds/debug_extension_mv3/web/panel.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ Future<void> _registerListeners() async {
7777

7878
void _handleRuntimeMessages(
7979
dynamic jsRequest,
80-
MessageSender sender,
81-
Function sendResponse,
80+
MessageSender _,
81+
Function __,
8282
) {
8383
if (jsRequest is! String) return;
8484

@@ -120,7 +120,7 @@ void _handleRuntimeMessages(
120120
);
121121
}
122122

123-
void _handleStorageChanges(Object storageObj, String storageArea) {
123+
void _handleStorageChanges(Object storageObj, String _) {
124124
interceptStorageChange<DebugInfo>(
125125
storageObj: storageObj,
126126
expectedType: StorageObject.debugInfo,
@@ -252,7 +252,7 @@ bool _warningBannerIsVisible({String? message}) {
252252
final warningBanner = document.getElementById(_warningBannerId);
253253
final isVisible =
254254
warningBanner != null && warningBanner.classes.contains(_showClass);
255-
if (message == null || isVisible == false) return isVisible;
255+
if (message == null || !isVisible) return isVisible;
256256
final warningMsg = document.getElementById(_warningMsgId);
257257
return warningMsg?.innerHtml == message;
258258
}
@@ -293,7 +293,7 @@ Future<void> _launchDebugConnection(Event _) async {
293293
Future<void> _maybeHandleConnectionTimeout() async {
294294
_connecting = true;
295295
await Future.delayed(Duration(seconds: 10));
296-
if (_connecting == true) {
296+
if (_connecting) {
297297
_handleConnectFailure(ConnectFailureReason.timeout);
298298
}
299299
}

dwds/debug_extension_mv3/web/utils.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Future<Tab?> getTab(int tabId) {
4242
return completer.future;
4343
}
4444

45-
Future<Tab?> get activeTab async {
45+
Future<Tab?> get activeTab {
4646
final completer = Completer<Tab?>();
4747
final query = QueryInfo(active: true, currentWindow: true);
4848
chrome.tabs.query(
@@ -96,9 +96,17 @@ void onExtensionIconClicked(void Function(Tab) callback) {
9696

9797
void setExtensionIcon(IconInfo info) {
9898
if (isMV3) {
99-
_setExtensionIconMV3(info, /*callback*/ null);
99+
_setExtensionIconMV3(
100+
info,
101+
/*callback*/
102+
null,
103+
);
100104
} else {
101-
_setExtensionIconMV2(info, /*callback*/ null);
105+
_setExtensionIconMV2(
106+
info,
107+
/*callback*/
108+
null,
109+
);
102110
}
103111
}
104112

dwds/lib/dart_web_debug_service.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ class Dwds {
141141
buildResults,
142142
devTools,
143143
assetReader,
144-
loadStrategy,
145144
hostname,
146145
extensionBackend,
147146
urlEncoder,

dwds/lib/shared/batched_stream.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ class BatchedStreamController<T> {
6666
if (now > lastSendTime + _batchDelayMilliseconds) {
6767
lastSendTime = now;
6868
if (buffer.isNotEmpty) {
69-
_outputController.sink.add(List.from(buffer));
69+
_outputController.sink.add(List.of(buffer));
7070
buffer.clear();
7171
}
7272
}
7373
}
7474

7575
if (buffer.isNotEmpty) {
76-
_outputController.sink.add(List.from(buffer));
76+
_outputController.sink.add(List.of(buffer));
7777
}
7878
_completer.complete(true);
7979
}

dwds/lib/src/debugging/debugger.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class Debugger extends Domain {
222222
///
223223
/// The JS VM is technically not paused at the start of the Isolate so there
224224
/// will not be a corresponding [DebuggerResumedEvent].
225-
Future<void> resumeFromStart() => _resumeHandler(null);
225+
void resumeFromStart() => _resumeHandler(null);
226226

227227
/// Notify the debugger the [Isolate] is paused at the application start.
228228
void notifyPausedAtStart() {
@@ -443,7 +443,6 @@ class Debugger extends Domain {
443443
}
444444

445445
static bool _isSubRange({
446-
required int length,
447446
int? offset,
448447
int? count,
449448
}) {
@@ -552,7 +551,7 @@ class Debugger extends Domain {
552551
if (_isEmptyRange(offset: offset, count: count, length: length)) {
553552
return [];
554553
}
555-
if (_isSubRange(offset: offset, count: count, length: length)) {
554+
if (_isSubRange(offset: offset, count: count)) {
556555
final range = await _subRange(
557556
objectId,
558557
offset: offset ?? 0,
@@ -743,7 +742,7 @@ class Debugger extends Domain {
743742
}
744743

745744
/// Handles resume events coming from the Chrome connection.
746-
Future<void> _resumeHandler(DebuggerResumedEvent? _) async {
745+
void _resumeHandler(DebuggerResumedEvent? _) {
747746
// We can receive a resume event in the middle of a reload which will result
748747
// in a null isolate.
749748
final isolate = inspector.isolate;
@@ -764,7 +763,7 @@ class Debugger extends Domain {
764763
}
765764

766765
/// Handles targetCrashed events coming from the Chrome connection.
767-
Future<void> _crashHandler(TargetCrashedEvent _) async {
766+
void _crashHandler(TargetCrashedEvent _) {
768767
// We can receive a resume event in the middle of a reload which will result
769768
// in a null isolate.
770769
final isolate = inspector.isolate;
@@ -947,7 +946,7 @@ class _Breakpoints extends Domain {
947946

948947
/// Adds a breakpoint at [scriptId] and [line] or returns an existing one if
949948
/// present.
950-
Future<Breakpoint> add(String scriptId, int line, int column) async {
949+
Future<Breakpoint> add(String scriptId, int line, int column) {
951950
final id = breakpointIdFor(scriptId, line, column);
952951
return _bpByDartId.putIfAbsent(
953952
id,

dwds/lib/src/debugging/inspector.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,16 +579,13 @@ class AppInspector implements AppInspectorInterface {
579579
/// Returns the list of scripts refs cached.
580580
Future<List<ScriptRef>> _populateScriptCaches() async {
581581
return _scriptCacheMemoizer.runOnce(() async {
582-
final libraryUris = [
583-
for (var library in isolate.libraries ?? []) library.uri
584-
];
585582
final scripts = await globalLoadStrategy
586583
.metadataProviderFor(appConnection.request.entrypointPath)
587584
.scripts;
588585
// For all the non-dart: libraries, find their parts and create scriptRefs
589586
// for them.
590587
final userLibraries =
591-
libraryUris.where((uri) => !uri.startsWith('dart:'));
588+
_userLibraryUris(isolate.libraries ?? <LibraryRef>[]);
592589
for (var uri in userLibraries) {
593590
final parts = scripts[uri];
594591
final scriptRefs = [
@@ -619,6 +616,12 @@ class AppInspector implements AppInspectorInterface {
619616
});
620617
}
621618

619+
Iterable<String> _userLibraryUris(Iterable<LibraryRef> libraries) {
620+
return libraries
621+
.map((library) => library.uri ?? '')
622+
.where((uri) => uri.isNotEmpty && !uri.startsWith('dart:'));
623+
}
624+
622625
/// Look up the script by id in an isolate.
623626
@override
624627
ScriptRef? scriptWithId(String? scriptId) =>

dwds/lib/src/debugging/instance.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ class InstanceHelper extends Domain {
121121
}
122122

123123
final metaData = await ClassMetaData.metaDataFor(
124-
inspector.remoteDebugger,
125124
remoteObject,
126125
inspector,
127126
);
@@ -748,7 +747,6 @@ class InstanceHelper extends Domain {
748747
return _primitiveInstanceRef(InstanceKind.kNull, remoteObject);
749748
}
750749
final metaData = await ClassMetaData.metaDataFor(
751-
inspector.remoteDebugger,
752750
remoteObject,
753751
inspector,
754752
);

dwds/lib/src/debugging/metadata/class.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:dwds/src/debugging/remote_debugger.dart';
65
import 'package:dwds/src/loaders/strategy.dart';
76
import 'package:dwds/src/services/chrome_debug_exception.dart';
87
import 'package:dwds/src/utilities/domain.dart';
@@ -128,7 +127,6 @@ class ClassMetaData {
128127
///
129128
/// Returns null if the [remoteObject] is not a Dart class.
130129
static Future<ClassMetaData?> metaDataFor(
131-
RemoteDebugger remoteDebugger,
132130
RemoteObject remoteObject,
133131
AppInspectorInterface inspector,
134132
) async {

dwds/lib/src/dwds_vm_client.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class DwdsVmClient {
121121
await client.registerService('ext.dwds.screenshot', 'DWDS');
122122

123123
client.registerServiceCallback('ext.dwds.sendEvent', (event) async {
124-
_processSendEvent(event, chromeProxyService, dwdsStats);
124+
_processSendEvent(event, dwdsStats);
125125
return {'result': Success().toJson()};
126126
});
127127
await client.registerService('ext.dwds.sendEvent', 'DWDS');
@@ -172,7 +172,6 @@ class DwdsVmClient {
172172

173173
void _processSendEvent(
174174
Map<String, dynamic> event,
175-
ChromeProxyService chromeProxyService,
176175
DwdsStats dwdsStats,
177176
) {
178177
final type = event['type'] as String?;

0 commit comments

Comments
 (0)