Skip to content

Commit 588a89f

Browse files
srawlinsCommit Queue
authored andcommitted
linter: Move LintDriver into linter package
The only remaining use of LintDriver was in `test_linter.dart` in the linter package. Moving this code unveiled a bunch of stuff that could be removed or made private: * The only use of DriverOptions was the class LinterOptions which subclasses it; so just combine the two classes. * Once combined, we also see that none of `cacheSize, `enabledLints`, or `packageConfigPath` are every used; remove them. This also simplifies the code in `LintDriver.analyze`, in the call to create the `AnalysisContextCollection`. * Move the top-level function `createSource` to `test_linter.dart`. * Remove unused `TestLinter.numSourcesAnalyzed`. * Remove unused `LintDriver.numSourcesAnalyzed`. * The StdInstrumentation is only used locally; privatize it. * test_linter.dart does not need to export any libraries. Change-Id: I5eee9570eefa2ecb11e72852f5d46b2e6c12ea11 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391321 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent da14b16 commit 588a89f

File tree

3 files changed

+32
-83
lines changed

3 files changed

+32
-83
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import 'package:analyzer/src/dart/element/element.dart';
2525
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
2626
import 'package:analyzer/src/dart/element/type_system.dart';
2727
import 'package:analyzer/src/error/codes.dart';
28-
import 'package:analyzer/src/lint/analysis.dart';
2928
import 'package:analyzer/src/lint/linter_visitor.dart' show NodeLintRegistry;
3029
import 'package:analyzer/src/lint/pub.dart';
3130
import 'package:analyzer/src/lint/registry.dart';
@@ -184,9 +183,15 @@ final class LinterContextWithResolvedResults implements LinterContext {
184183
LibraryElement2 get libraryElement2 => libraryElement as LibraryElement2;
185184
}
186185

187-
class LinterOptions extends DriverOptions {
186+
class LinterOptions {
188187
final Iterable<LintRule> enabledRules;
189188

189+
/// The path to the Dart SDK.
190+
String? dartSdkPath;
191+
192+
/// Whether to gather timing data during analysis.
193+
bool enableTiming = false;
194+
190195
LinterOptions({
191196
Iterable<LintRule>? enabledRules,
192197
}) : enabledRules = enabledRules ?? Registry.ruleRegistry;
Lines changed: 14 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,21 @@
1-
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
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:linter/src/test_utilities/test_linter.dart';
6-
library;
7-
85
import 'dart:io' as io;
96

107
import 'package:analyzer/dart/analysis/results.dart';
118
import 'package:analyzer/file_system/file_system.dart';
12-
import 'package:analyzer/file_system/physical_file_system.dart';
139
import 'package:analyzer/instrumentation/instrumentation.dart';
14-
import 'package:analyzer/source/file_source.dart';
15-
import 'package:analyzer/source/source.dart';
10+
// ignore: implementation_imports
1611
import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart';
12+
// ignore: implementation_imports
1713
import 'package:analyzer/src/generated/engine.dart';
14+
// ignore: implementation_imports
1815
import 'package:analyzer/src/lint/io.dart';
16+
// ignore: implementation_imports
1917
import 'package:analyzer/src/lint/linter.dart';
2018

21-
Source createSource(Uri uri) {
22-
var filePath = uri.toFilePath();
23-
var file = PhysicalResourceProvider.INSTANCE.getFile(filePath);
24-
return FileSource(file, uri);
25-
}
26-
27-
/// Print the given message and exit with the given [exitCode]
28-
void printAndFail(String message, {int exitCode = 15}) {
29-
print(message);
30-
io.exit(exitCode);
31-
}
32-
33-
class DriverOptions {
34-
/// The maximum number of sources for which AST structures should be kept
35-
/// in the cache. The default is 512.
36-
int cacheSize = 512;
37-
38-
/// The path to the dart SDK.
39-
String? dartSdkPath;
40-
41-
/// Whether to show lint warnings.
42-
bool enableLints = true;
43-
44-
/// Whether to gather timing data during analysis.
45-
bool enableTiming = false;
46-
47-
/// The path to a `.packages` configuration file
48-
String? packageConfigPath;
49-
}
50-
51-
/// A driver _only used_ by [TestLinter], which is only used by package:linter
52-
/// tests and tools.
5319
class LintDriver {
5420
/// The files which have been analyzed so far. This is used to compute the
5521
/// total number of files analyzed for statistics.
@@ -61,19 +27,11 @@ class LintDriver {
6127

6228
LintDriver(this.options, this._resourceProvider);
6329

64-
/// Return the number of sources that have been analyzed so far.
65-
int get numSourcesAnalyzed => _filesAnalyzed.length;
66-
6730
Future<List<AnalysisErrorInfo>> analyze(Iterable<io.File> files) async {
68-
AnalysisEngine.instance.instrumentationService = StdInstrumentation();
69-
70-
// TODO(scheglov): Enforce normalized absolute paths in the config.
71-
var packageConfigPath = options.packageConfigPath;
72-
packageConfigPath = _absoluteNormalizedPath.ifNotNull(packageConfigPath);
31+
AnalysisEngine.instance.instrumentationService = _StdInstrumentation();
7332

7433
var contextCollection = AnalysisContextCollectionImpl(
7534
resourceProvider: _resourceProvider,
76-
packagesFile: packageConfigPath,
7735
sdkPath: options.dartSdkPath,
7836
includedPaths:
7937
files.map((file) => _absoluteNormalizedPath(file.path)).toList(),
@@ -82,7 +40,7 @@ class LintDriver {
8240
required contextRoot,
8341
required sdk,
8442
}) {
85-
analysisOptions.lint = options.enableLints;
43+
analysisOptions.lint = true;
8644
analysisOptions.warning = false;
8745
analysisOptions.enableTiming = options.enableTiming;
8846
analysisOptions.lintRules =
@@ -114,15 +72,13 @@ class LintDriver {
11472

11573
String _absoluteNormalizedPath(String path) {
11674
var pathContext = _resourceProvider.pathContext;
117-
path = pathContext.absolute(path);
118-
path = pathContext.normalize(path);
119-
return path;
75+
return pathContext.normalize(pathContext.absolute(path));
12076
}
12177
}
12278

12379
/// Prints logging information comments to the [outSink] and error messages to
12480
/// [errorSink].
125-
class StdInstrumentation extends NoopInstrumentationService {
81+
class _StdInstrumentation extends NoopInstrumentationService {
12682
@override
12783
void logError(String message, [Object? exception]) {
12884
errorSink.writeln(message);
@@ -132,9 +88,11 @@ class StdInstrumentation extends NoopInstrumentationService {
13288
}
13389

13490
@override
135-
void logException(dynamic exception,
136-
[StackTrace? stackTrace,
137-
List<InstrumentationServiceAttachment>? attachments]) {
91+
void logException(
92+
exception, [
93+
StackTrace? stackTrace,
94+
List<InstrumentationServiceAttachment>? attachments,
95+
]) {
13896
errorSink.writeln(exception);
13997
errorSink.writeln(stackTrace);
14098
}
@@ -147,14 +105,3 @@ class StdInstrumentation extends NoopInstrumentationService {
147105
}
148106
}
149107
}
150-
151-
extension _UnaryFunctionExtension<T, R> on R Function(T) {
152-
/// Invoke this function if [t] is not `null`, otherwise return `null`.
153-
R? ifNotNull(T? t) {
154-
if (t != null) {
155-
return this(t);
156-
} else {
157-
return null;
158-
}
159-
}
160-
}

pkg/linter/lib/src/test_utilities/test_linter.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,26 @@ import 'package:analyzer/error/error.dart';
88
import 'package:analyzer/error/listener.dart';
99
import 'package:analyzer/file_system/file_system.dart' as file_system;
1010
import 'package:analyzer/file_system/physical_file_system.dart' as file_system;
11+
import 'package:analyzer/source/file_source.dart';
12+
import 'package:analyzer/source/source.dart';
1113
// ignore: implementation_imports
1214
import 'package:analyzer/src/generated/engine.dart' show AnalysisErrorInfo;
1315
// ignore: implementation_imports
14-
import 'package:analyzer/src/lint/analysis.dart';
15-
// ignore: implementation_imports
1616
import 'package:analyzer/src/lint/io.dart';
1717
// ignore: implementation_imports
1818
import 'package:analyzer/src/lint/linter.dart';
1919
// ignore: implementation_imports
2020
import 'package:analyzer/src/lint/pub.dart';
2121
import 'package:meta/meta.dart';
22-
import 'package:path/path.dart' as p;
22+
import 'package:path/path.dart' as path;
23+
24+
import 'lint_driver.dart';
2325

24-
export 'package:analyzer/src/lint/linter_visitor.dart' show NodeLintRegistry;
25-
export 'package:analyzer/src/lint/state.dart'
26-
show dart2_12, dart3, dart3_3, State;
26+
Source createSource(Uri uri) {
27+
var filePath = uri.toFilePath();
28+
var file = file_system.PhysicalResourceProvider.INSTANCE.getFile(filePath);
29+
return FileSource(file, uri);
30+
}
2731

2832
/// Dart source linter, only for package:linter's tools and tests.
2933
class TestLinter implements AnalysisErrorListener {
@@ -32,11 +36,6 @@ class TestLinter implements AnalysisErrorListener {
3236
final LinterOptions options;
3337
final file_system.ResourceProvider _resourceProvider;
3438

35-
/// The total number of sources that were analyzed.
36-
///
37-
/// Only valid after [lintFiles] has been called.
38-
int numSourcesAnalyzed = 0;
39-
4039
TestLinter(
4140
this.options, {
4241
file_system.ResourceProvider? resourceProvider,
@@ -47,9 +46,7 @@ class TestLinter implements AnalysisErrorListener {
4746
var errors = <AnalysisErrorInfo>[];
4847
var lintDriver = LintDriver(options, _resourceProvider);
4948
errors.addAll(await lintDriver.analyze(files.where(isDartFile)));
50-
numSourcesAnalyzed = lintDriver.numSourcesAnalyzed;
5149
files.where(isPubspecFile).forEach((file) {
52-
numSourcesAnalyzed++;
5350
var errorsForFile = lintPubspecSource(
5451
contents: file.readAsStringSync(),
5552
sourcePath: _resourceProvider.pathContext.normalize(file.absolute.path),
@@ -63,7 +60,7 @@ class TestLinter implements AnalysisErrorListener {
6360
Iterable<AnalysisErrorInfo> lintPubspecSource(
6461
{required String contents, String? sourcePath}) {
6562
var results = <AnalysisErrorInfo>[];
66-
var sourceUrl = sourcePath == null ? null : p.toUri(sourcePath);
63+
var sourceUrl = sourcePath == null ? null : path.toUri(sourcePath);
6764
var spec = Pubspec.parse(contents, sourceUrl: sourceUrl);
6865

6966
for (var rule in options.enabledRules) {

0 commit comments

Comments
 (0)