linter: Remove a number of unused classes

* Remove unused LinterException, Reporter, PrintingReporter.
* Mark Hyperlink as visible-for-testing, and make 3 fields private.
* Make the LinterConstantEvaluationResult constructor private.
* Make LinterContextParsedImpl.package a getter

Cq-Include-Trybots: luci.dart.try:flutter-analyze-try,analyzer-win-release-try,pkg-win-release-try
Change-Id: Ib403fb7744e9454742d6555c8cdb8764ab4377be
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/369781
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Sam Rawlins 2024-06-05 14:56:48 +00:00 committed by Commit Queue
parent 1e6bc6697a
commit 149fc77208
3 changed files with 13 additions and 92 deletions

View file

@ -76,16 +76,17 @@ class Group implements Comparable<Group> {
int compareTo(Group other) => name.compareTo(other.name);
}
@visibleForTesting
class Hyperlink {
final String label;
final String href;
final bool bold;
final String _label;
final String _href;
final bool _bold;
const Hyperlink(this.label, this.href, {this.bold = false});
const Hyperlink(this._label, this._href, {bool bold = false}) : _bold = bold;
String get html => '<a href="$href">${_emph(label)}</a>';
String get html => '<a href="$_href">${_emph(_label)}</a>';
String _emph(String msg) => bold ? '<strong>$msg</strong>' : msg;
String _emph(String msg) => _bold ? '<strong>$msg</strong>' : msg;
}
/// The result of attempting to evaluate an expression.
@ -96,7 +97,7 @@ class LinterConstantEvaluationResult {
/// The errors reported during the evaluation.
final List<AnalysisError> errors;
LinterConstantEvaluationResult(this.value, this.errors);
LinterConstantEvaluationResult._(this.value, this.errors);
}
/// Provides access to information needed by lint rules that is not available
@ -170,9 +171,6 @@ class LinterContextParsedImpl implements LinterContext {
@override
final LinterContextUnit definingUnit;
@override
final WorkspacePackage? package = null;
@override
final InheritanceManager3 inheritanceManager = InheritanceManager3();
@ -191,6 +189,9 @@ class LinterContextParsedImpl implements LinterContext {
LibraryElement get libraryElement =>
throw UnsupportedError('LinterContext with parsed results');
@override
WorkspacePackage? get package => null;
@override
TypeProvider get typeProvider =>
throw UnsupportedError('LinterContext with parsed results');
@ -210,25 +211,11 @@ class LinterContextUnit {
LinterContextUnit(this.content, this.unit, this.errorReporter);
}
/// Thrown when an error occurs in linting.
class LinterException implements Exception {
/// A message describing the error.
final String? message;
/// Creates a new LinterException with an optional error [message].
const LinterException([this.message]);
@override
String toString() =>
message == null ? "LinterException" : "LinterException: $message";
}
class LinterOptions extends DriverOptions {
final Iterable<LintRule> enabledRules;
final String? analysisOptions;
LintFilter? filter;
// TODO(pq): consider migrating to named params (but note Linter dep).
LinterOptions({
Iterable<LintRule>? enabledRules,
this.analysisOptions,
@ -388,28 +375,6 @@ abstract class LintRule implements Comparable<LintRule> {
}
}
class PrintingReporter implements Reporter {
final void Function(String msg) _print;
const PrintingReporter([this._print = print]);
@override
void exception(LinterException exception) {
_print('EXCEPTION: $exception');
}
@override
void warn(String message) {
_print('WARN: $message');
}
}
abstract class Reporter {
void exception(LinterException exception);
void warn(String message);
}
/// An error listener that only records whether any constant related errors have
/// been reported.
class _ConstantAnalysisErrorListener extends AnalysisErrorListener {
@ -547,7 +512,7 @@ extension ExpressionExtension on Expression {
/// constant value, and a list of errors that occurred during the computation.
LinterConstantEvaluationResult computeConstantValue() {
var unitElement = thisOrAncestorOfType<CompilationUnit>()?.declaredElement;
if (unitElement == null) return LinterConstantEvaluationResult(null, []);
if (unitElement == null) return LinterConstantEvaluationResult._(null, []);
var libraryElement = unitElement.library as LibraryElementImpl;
var errorListener = RecordingErrorListener();
@ -575,7 +540,7 @@ extension ExpressionExtension on Expression {
var constant = visitor.evaluateAndReportInvalidConstant(this);
var dartObject = constant is DartObjectImpl ? constant : null;
return LinterConstantEvaluationResult(dartObject, errorListener.errors);
return LinterConstantEvaluationResult._(dartObject, errorListener.errors);
}
bool _canBeConstInstanceCreation(InstanceCreationExpressionImpl node) {

View file

@ -21,31 +21,6 @@ void main() {
void defineLinterEngineTests() {
group('engine', () {
setUp(setUpSharedTestEnvironment);
group('reporter', () {
void doTest(
String label, String expected, Function(PrintingReporter r) report) {
test(label, () {
String? msg;
var reporter = PrintingReporter((m) => msg = m);
report(reporter);
expect(msg, expected);
});
}
doTest('exception', 'EXCEPTION: LinterException: foo',
(r) => r.exception(LinterException('foo')));
doTest('warn', 'WARN: foo', (r) => r.warn('foo'));
});
group('exceptions', () {
test('message', () {
expect(const LinterException('foo').message, 'foo');
});
test('toString', () {
expect(const LinterException().toString(), 'LinterException');
expect(const LinterException('foo').toString(), 'LinterException: foo');
});
});
group('camel case', () {
test('humanize', () {

View file

@ -8,7 +8,6 @@ import 'dart:io';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/lint/linter.dart';
class CollectingSink extends MockIOSink {
final StringBuffer buffer = StringBuffer();
@ -84,24 +83,6 @@ class MockIOSink implements IOSink {
void writeln([Object? obj = '']) {}
}
class MockReporter extends Reporter {
List<LinterException> exceptions = <LinterException>[];
List<String> warnings = <String>[];
MockReporter();
@override
void exception(LinterException exception) {
exceptions.add(exception);
}
@override
void warn(String message) {
warnings.add(message);
}
}
class MockSource extends BasicSource {
@override
final String fullName;