Skip to content

Commit 1db40b6

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Add an AnalysisRule alias for LintRule
Since the LintRule class is the basis for writing both "lint rules" (rules that must be explicitly enabled) and "warning rules" (rules that are enabled by default), it is a misnomer to use 'LintRule' as a supertype. This change adds an alias, 'AnalysisRule'. Additionally: * Rename LinterVisitor to AnalysisRuleVisitor. * Rename lintRuleTimers to AnalysisRuleTimers. * Rename LinterExceptionHandler to AnalysisRuleExceptionHandler. * Rename LintRuleExceptionHandler to _AnalysisRuleExceptionHandler. * NodeLintRegistry is used in too many places, so I have left it for now. Change-Id: I43340cca0f9b64502960d8f5e05c6c9b81c3dfd9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392203 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 2c3ff3f commit 1db40b6

File tree

16 files changed

+453
-493
lines changed

16 files changed

+453
-493
lines changed

pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,9 @@ class BulkFixProcessor {
584584

585585
// Run lints that handle specific node types.
586586
currentUnit.unit.accept(
587-
LinterVisitor(
587+
AnalysisRuleVisitor(
588588
nodeRegistry,
589-
LinterExceptionHandler(
589+
AnalysisRuleExceptionHandler(
590590
propagateExceptions: false,
591591
).logException,
592592
),

pkg/analysis_server_plugin/lib/registry.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ abstract class PluginRegistry {
1010
void registerFixForRule(LintCode code, ProducerGenerator generator);
1111

1212
/// Register this [rule] with the analyzer's rule registry.
13-
void registerLintRule(LintRule rule);
13+
void registerLintRule(AnalysisRule rule);
1414

1515
/// Register this [rule] with the analyzer's rule registry.
16-
void registerWarningRule(LintRule rule);
16+
void registerWarningRule(AnalysisRule rule);
1717
}

pkg/analysis_server_plugin/lib/src/plugin_server.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import 'package:analyzer/src/dart/analysis/byte_store.dart';
2626
import 'package:analyzer/src/dart/analysis/file_content_cache.dart';
2727
import 'package:analyzer/src/dart/analysis/session.dart';
2828
import 'package:analyzer/src/dart/element/type_system.dart';
29-
import 'package:analyzer/src/lint/lint_rule_timers.dart';
29+
import 'package:analyzer/src/lint/analysis_rule_timers.dart';
3030
import 'package:analyzer/src/lint/linter.dart';
3131
import 'package:analyzer/src/lint/linter_visitor.dart';
3232
import 'package:analyzer/src/lint/registry.dart';
@@ -259,14 +259,14 @@ class PluginServer {
259259
var rules = Registry.ruleRegistry.enabled(configuration.ruleConfigs);
260260
for (var rule in rules) {
261261
rule.reporter = errorReporter;
262-
var timer = enableTiming ? lintRuleTimers.getTimer(rule) : null;
262+
var timer = enableTiming ? analysisRuleTimers.getTimer(rule) : null;
263263
timer?.start();
264264
rule.registerNodeProcessors(nodeRegistry, context);
265265
timer?.stop();
266266
}
267267
}
268268

269-
currentUnit.unit.accept(LinterVisitor(nodeRegistry));
269+
currentUnit.unit.accept(AnalysisRuleVisitor(nodeRegistry));
270270
// The list of the `AnalysisError`s and their associated
271271
// `protocol.AnalysisError`s.
272272
var errorsAndProtocolErrors = [

pkg/analysis_server_plugin/lib/src/registry.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ import 'package:analyzer/src/lint/registry.dart';
1010

1111
final class PluginRegistryImpl implements PluginRegistry {
1212
/// Returns currently registered lint rules.
13-
Iterable<LintRule> get registeredRules => Registry.ruleRegistry;
13+
Iterable<AnalysisRule> get registeredRules => Registry.ruleRegistry;
1414

1515
@override
1616
void registerFixForRule(LintCode code, ProducerGenerator generator) {
1717
registeredFixGenerators.registerFixForLint(code, generator);
1818
}
1919

2020
@override
21-
void registerLintRule(LintRule rule) {
21+
void registerLintRule(AnalysisRule rule) {
2222
Registry.ruleRegistry.registerLintRule(rule);
2323
}
2424

2525
@override
26-
void registerWarningRule(LintRule rule) {
26+
void registerWarningRule(AnalysisRule rule) {
2727
Registry.ruleRegistry.registerWarningRule(rule);
2828
}
2929

30-
LintRule? ruleNamed(String name) => Registry.ruleRegistry[name];
30+
AnalysisRule? ruleNamed(String name) => Registry.ruleRegistry[name];
3131
}

pkg/analysis_server_plugin/test/src/lint_rules.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'package:analyzer/dart/ast/visitor.dart';
77
import 'package:analyzer/error/error.dart';
88
import 'package:analyzer/src/lint/linter.dart';
99

10-
class NoBoolsRule extends LintRule {
10+
class NoBoolsRule extends AnalysisRule {
1111
static const LintCode code = LintCode('no_bools', 'No bools message');
1212

1313
NoBoolsRule() : super(name: 'no_bools', description: 'No bools desc');
@@ -24,7 +24,7 @@ class NoBoolsRule extends LintRule {
2424
}
2525

2626
class _NoBoolsVisitor extends SimpleAstVisitor<void> {
27-
final LintRule rule;
27+
final AnalysisRule rule;
2828

2929
_NoBoolsVisitor(this.rule);
3030

pkg/analysis_server_plugin/test/src/plugin_server_error_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class _ThrowsAsyncErrorFix extends ResolvedCorrectionProducer {
205205
}
206206
}
207207

208-
class _ThrowsAsyncErrorRule extends LintRule {
208+
class _ThrowsAsyncErrorRule extends AnalysisRule {
209209
static const LintCode code = LintCode('no_bools', 'No bools message');
210210

211211
_ThrowsAsyncErrorRule()
@@ -223,7 +223,7 @@ class _ThrowsAsyncErrorRule extends LintRule {
223223
}
224224

225225
class _ThrowsAsyncErrorVisitor extends SimpleAstVisitor<void> {
226-
final LintRule rule;
226+
final AnalysisRule rule;
227227

228228
_ThrowsAsyncErrorVisitor(this.rule);
229229

@@ -253,7 +253,7 @@ class _ThrowsSyncErrorFix extends ResolvedCorrectionProducer {
253253
}
254254
}
255255

256-
class _ThrowsSyncErrorRule extends LintRule {
256+
class _ThrowsSyncErrorRule extends AnalysisRule {
257257
static const LintCode code = LintCode('no_bools', 'No bools message');
258258

259259
_ThrowsSyncErrorRule()
@@ -271,7 +271,7 @@ class _ThrowsSyncErrorRule extends LintRule {
271271
}
272272

273273
class _ThrowsSyncErrorVisitor extends SimpleAstVisitor<void> {
274-
final LintRule rule;
274+
final AnalysisRule rule;
275275

276276
_ThrowsSyncErrorVisitor(this.rule);
277277

pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import 'package:analyzer/src/generated/ffi_verifier.dart';
4646
import 'package:analyzer/src/generated/resolver.dart';
4747
import 'package:analyzer/src/hint/sdk_constraint_verifier.dart';
4848
import 'package:analyzer/src/ignore_comments/ignore_info.dart';
49-
import 'package:analyzer/src/lint/lint_rule_timers.dart';
49+
import 'package:analyzer/src/lint/analysis_rule_timers.dart';
5050
import 'package:analyzer/src/lint/linter.dart';
5151
import 'package:analyzer/src/lint/linter_visitor.dart';
5252
import 'package:analyzer/src/util/performance/operation_performance.dart';
@@ -383,13 +383,13 @@ class LibraryAnalyzer {
383383
);
384384

385385
for (var linter in _analysisOptions.lintRules) {
386-
var timer = enableTiming ? lintRuleTimers.getTimer(linter) : null;
386+
var timer = enableTiming ? analysisRuleTimers.getTimer(linter) : null;
387387
timer?.start();
388388
linter.registerNodeProcessors(nodeRegistry, context);
389389
timer?.stop();
390390
}
391391

392-
var logException = LinterExceptionHandler(
392+
var logException = AnalysisRuleExceptionHandler(
393393
propagateExceptions: _analysisOptions.propagateLinterExceptions,
394394
).logException;
395395

@@ -408,13 +408,13 @@ class LibraryAnalyzer {
408408

409409
// Run lint rules that handle specific node types.
410410
unit.accept(
411-
LinterVisitor(nodeRegistry, logException),
411+
AnalysisRuleVisitor(nodeRegistry, logException),
412412
);
413413
}
414414

415415
// Now that all lint rules have visited the code in each of the compilation
416416
// units, we can accept each lint rule's `afterLibrary` hook.
417-
LinterVisitor(nodeRegistry, logException).afterLibrary();
417+
AnalysisRuleVisitor(nodeRegistry, logException).afterLibrary();
418418
}
419419

420420
void _computeVerifyErrors(FileAnalysis fileAnalysis) {

pkg/analyzer/lib/src/dart/ast/utilities.dart

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
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+
/// @docImport 'package:analyzer/src/lint/linter.dart';
6+
/// @docImport 'package:analyzer/src/lint/linter_visitor.dart';
7+
library;
8+
59
import 'package:analyzer/dart/ast/token.dart';
610
import 'package:analyzer/dart/ast/visitor.dart';
711
import 'package:analyzer/exception/exception.dart';
@@ -11,6 +15,44 @@ import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine;
1115

1216
export 'package:analyzer/src/dart/ast/constant_evaluator.dart';
1317

18+
/// Class capable of handling exceptions generated during the execution of an
19+
/// analysis rule.
20+
final class AnalysisRuleExceptionHandler {
21+
/// Indicates whether exceptions should be propagated to the caller (by
22+
/// re-throwing them).
23+
final bool propagateExceptions;
24+
25+
AnalysisRuleExceptionHandler({
26+
required this.propagateExceptions,
27+
});
28+
29+
/// A method that can be passed to the [AnalysisRuleVisitor] constructor to
30+
/// handle exceptions that occur during the execution of an [AnalysisRule].
31+
///
32+
/// Returns `true` if the exception was fully handled, and `false` if the
33+
/// exception should be rethrown.
34+
bool logException(
35+
AstNode node, Object visitor, Object exception, StackTrace stackTrace) {
36+
StringBuffer buffer = StringBuffer();
37+
buffer.write('Exception while using a ${visitor.runtimeType} to visit a ');
38+
AstNode? currentNode = node;
39+
bool first = true;
40+
while (currentNode != null) {
41+
if (first) {
42+
first = false;
43+
} else {
44+
buffer.write(' in ');
45+
}
46+
buffer.write(currentNode.runtimeType);
47+
currentNode = currentNode.parent;
48+
}
49+
// TODO(39284): should this exception be silent?
50+
AnalysisEngine.instance.instrumentationService.logException(
51+
SilentException(buffer.toString(), exception, stackTrace));
52+
return !propagateExceptions;
53+
}
54+
}
55+
1456
/// An AstVisitor that compares the structure of two AstNodes to see whether
1557
/// they are equal.
1658
class AstComparator implements AstVisitor<bool> {
@@ -1673,45 +1715,6 @@ class AstComparator implements AstVisitor<bool> {
16731715
}
16741716
}
16751717

1676-
/// Class capable of handling exceptions generated during linting.
1677-
///
1678-
/// Clients may not extend, implement or mix-in this class.
1679-
class LinterExceptionHandler {
1680-
/// Indicates whether linter exceptions should be propagated to the caller (by
1681-
/// re-throwing them)
1682-
final bool propagateExceptions;
1683-
1684-
LinterExceptionHandler({
1685-
required this.propagateExceptions,
1686-
});
1687-
1688-
/// A method that can be passed to the `LinterVisitor` constructor to handle
1689-
/// exceptions that occur during linting.
1690-
///
1691-
/// Returns `true` if the exception was fully handled, and `false` if the
1692-
/// exception should be rethrown.
1693-
bool logException(
1694-
AstNode node, Object visitor, Object exception, StackTrace stackTrace) {
1695-
StringBuffer buffer = StringBuffer();
1696-
buffer.write('Exception while using a ${visitor.runtimeType} to visit a ');
1697-
AstNode? currentNode = node;
1698-
bool first = true;
1699-
while (currentNode != null) {
1700-
if (first) {
1701-
first = false;
1702-
} else {
1703-
buffer.write(' in ');
1704-
}
1705-
buffer.write(currentNode.runtimeType);
1706-
currentNode = currentNode.parent;
1707-
}
1708-
// TODO(39284): should this exception be silent?
1709-
AnalysisEngine.instance.instrumentationService.logException(
1710-
SilentException(buffer.toString(), exception, stackTrace));
1711-
return !propagateExceptions;
1712-
}
1713-
}
1714-
17151718
/// An object used to locate the [AstNode] associated with a source range, given
17161719
/// the AST structure built from the source. More specifically, they will return
17171720
/// the [AstNode] with the shortest length whose source range completely

pkg/analyzer/lib/src/lint/lint_rule_timers.dart renamed to pkg/analyzer/lib/src/lint/analysis_rule_timers.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
import 'package:analyzer/src/lint/linter.dart';
66

7-
/// Shared registry of lint rule timers.
8-
final lintRuleTimers = LintRuleTimers();
7+
/// Shared registry of analysis rule timers.
8+
final analysisRuleTimers = AnalysisRuleTimers();
99

10-
/// Manages lint timing.
11-
class LintRuleTimers {
12-
/// Dictionary mapping lints (by name) to timers.
10+
/// Manages analysis rule timing.
11+
class AnalysisRuleTimers {
12+
/// Dictionary mapping rules (by name) to timers.
1313
final Map<String, Stopwatch> timers = <String, Stopwatch>{};
1414

15-
/// Get a timer associated with the given lint rule (or create one if none
15+
/// Get a timer associated with the given analysis rule (or create one if none
1616
/// exists).
17-
Stopwatch getTimer(LintRule linter) =>
17+
Stopwatch getTimer(AnalysisRule linter) =>
1818
timers.putIfAbsent(linter.name, () => Stopwatch());
1919
}

pkg/analyzer/lib/src/lint/linter.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export 'package:analyzer/src/lint/linter_visitor.dart' show NodeLintRegistry;
3636
export 'package:analyzer/src/lint/state.dart'
3737
show dart2_12, dart3, dart3_3, State;
3838

39+
/// Describes a static analysis rule, either a lint rule (which must be enabled
40+
/// via analysis options) or a warning rule (which is enabled by default).
41+
typedef AnalysisRule = LintRule;
42+
3943
/// The result of attempting to evaluate an expression as a constant.
4044
final class LinterConstantEvaluationResult {
4145
/// The value of the expression, or `null` if has [errors].

0 commit comments

Comments
 (0)