Skip to content

Commit 28fc634

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Move RuleContext and RuleContextUnit to public API
Change-Id: I9cca5ee27e623bfa5b878bb0277f5e12e84a745b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/431982 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 2c0fad2 commit 28fc634

File tree

11 files changed

+112
-84
lines changed

11 files changed

+112
-84
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:analysis_server_plugin/edit/fix/dart_fix_context.dart';
1717
import 'package:analysis_server_plugin/edit/fix/fix.dart';
1818
import 'package:analysis_server_plugin/src/correction/dart_change_workspace.dart';
1919
import 'package:analysis_server_plugin/src/correction/fix_generators.dart';
20+
import 'package:analyzer/analysis_rule/rule_context.dart';
2021
import 'package:analyzer/dart/analysis/analysis_context.dart';
2122
import 'package:analyzer/dart/analysis/analysis_options.dart';
2223
import 'package:analyzer/dart/analysis/results.dart';

pkg/analysis_server_plugin/lib/src/plugin_server.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:analysis_server_plugin/src/correction/assist_processor.dart';
1313
import 'package:analysis_server_plugin/src/correction/dart_change_workspace.dart';
1414
import 'package:analysis_server_plugin/src/correction/fix_processor.dart';
1515
import 'package:analysis_server_plugin/src/registry.dart';
16+
import 'package:analyzer/analysis_rule/rule_context.dart';
1617
import 'package:analyzer/dart/analysis/analysis_context.dart';
1718
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
1819
import 'package:analyzer/dart/analysis/results.dart';

pkg/analyzer/api.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
package:analyzer/analysis_rule/rule_context.dart:
2+
RuleContext (class extends Object):
3+
new (constructor: RuleContext Function())
4+
allUnits (getter: List<RuleContextUnit>)
5+
currentUnit (getter: RuleContextUnit?)
6+
definingUnit (getter: RuleContextUnit)
7+
isInLibDir (getter: bool)
8+
isInTestDirectory (getter: bool)
9+
libraryElement (getter: LibraryElement?)
10+
package (getter: WorkspacePackage?)
11+
typeProvider (getter: TypeProvider)
12+
typeSystem (getter: TypeSystem)
13+
isFeatureEnabled (method: bool Function(Feature))
14+
RuleContextUnit (class extends Object):
15+
new (constructor: RuleContextUnit Function({required String content, required ErrorReporter errorReporter, required File file, required CompilationUnit unit}))
16+
content (getter: String)
17+
errorReporter (getter: ErrorReporter)
18+
file (getter: File)
19+
unit (getter: CompilationUnit)
120
package:analyzer/analysis_rule/rule_state.dart:
221
dart2_12 (static getter: Version)
322
dart3 (static getter: Version)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/dart/analysis/features.dart';
6+
import 'package:analyzer/dart/ast/ast.dart';
7+
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/type_provider.dart';
9+
import 'package:analyzer/dart/element/type_system.dart';
10+
import 'package:analyzer/error/listener.dart';
11+
import 'package:analyzer/file_system/file_system.dart';
12+
import 'package:analyzer/workspace/workspace.dart';
13+
14+
/// Provides access to information needed by analysis rules that is not
15+
/// available from AST nodes or the element model.
16+
abstract class RuleContext {
17+
/// The list of all compilation units that make up the library under analysis,
18+
/// including the defining compilation unit, all parts, and all augmentations.
19+
List<RuleContextUnit> get allUnits;
20+
21+
/// The compilation unit being analyzed.
22+
///
23+
/// `null` when a unit is not currently being analyzed (for example when node
24+
/// processors are being registered).
25+
RuleContextUnit? get currentUnit;
26+
27+
/// The defining compilation unit of the library under analysis.
28+
RuleContextUnit get definingUnit;
29+
30+
/// Whether the [definingUnit]'s location is in a package's top-level 'lib'
31+
/// directory, including locations deeply nested, and locations in the
32+
/// package-implementation directory, 'lib/src'.
33+
bool get isInLibDir;
34+
35+
/// Whether the [definingUnit] is in a [package]'s "test" directory.
36+
bool get isInTestDirectory;
37+
38+
/// The library element representing the library that contains the compilation
39+
/// unit being analyzed.
40+
LibraryElement? get libraryElement;
41+
42+
/// The package in which the library being analyzed lives, or `null` if it
43+
/// does not live in a package.
44+
WorkspacePackage? get package;
45+
46+
TypeProvider get typeProvider;
47+
48+
TypeSystem get typeSystem;
49+
50+
/// Whether the given [feature] is enabled in this rule context.
51+
bool isFeatureEnabled(Feature feature);
52+
}
53+
54+
/// Provides access to information needed by analysis rules that is not
55+
/// available from AST nodes or the element model.
56+
class RuleContextUnit {
57+
final File file;
58+
final String content;
59+
final ErrorReporter errorReporter;
60+
final CompilationUnit unit;
61+
62+
RuleContextUnit({
63+
required this.file,
64+
required this.content,
65+
required this.errorReporter,
66+
required this.unit,
67+
});
68+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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:analyzer/analysis_rule/rule_context.dart';
56
import 'package:analyzer/dart/analysis/declared_variables.dart';
67
import 'package:analyzer/dart/analysis/features.dart';
78
import 'package:analyzer/diagnostic/diagnostic.dart';

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

Lines changed: 17 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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:analyzer/analysis_rule/rule_context.dart';
56
import 'package:analyzer/analysis_rule/rule_state.dart';
67
import 'package:analyzer/dart/analysis/features.dart';
78
import 'package:analyzer/dart/analysis/results.dart';
@@ -13,16 +14,22 @@ import 'package:analyzer/dart/element/type_system.dart';
1314
import 'package:analyzer/diagnostic/diagnostic.dart';
1415
import 'package:analyzer/error/error.dart';
1516
import 'package:analyzer/error/listener.dart';
16-
import 'package:analyzer/file_system/file_system.dart';
1717
import 'package:analyzer/src/lint/linter_visitor.dart' show RuleVisitorRegistry;
1818
import 'package:analyzer/src/lint/pub.dart';
1919
import 'package:analyzer/workspace/workspace.dart';
20-
import 'package:meta/meta.dart';
2120

2221
export 'package:analyzer/analysis_rule/rule_state.dart'
2322
show dart2_12, dart3, dart3_3, RuleState;
2423
export 'package:analyzer/src/lint/linter_visitor.dart' show NodeLintRegistry;
2524

25+
/// Returns whether [filePath] is in the top-level `lib` directory in [package].
26+
bool _isInLibDir(String? filePath, WorkspacePackage? package) {
27+
if (package == null) return false;
28+
if (filePath == null) return false;
29+
var libDir = package.root.getChildAssumingFolder('lib');
30+
return libDir.contains(filePath);
31+
}
32+
2633
/// A soon-to-be deprecated alias for [RuleContext].
2734
typedef LinterContext = RuleContext;
2835

@@ -290,70 +297,6 @@ abstract class MultiAnalysisRule extends AbstractAnalysisRule {
290297
);
291298
}
292299

293-
/// Provides access to information needed by analysis rules that is not
294-
/// available from AST nodes or the element model.
295-
abstract class RuleContext {
296-
/// The list of all compilation units that make up the library under analysis,
297-
/// including the defining compilation unit, all parts, and all augmentations.
298-
List<RuleContextUnit> get allUnits;
299-
300-
/// The compilation unit being analyzed.
301-
///
302-
/// `null` when a unit is not currently being analyzed (for example when node
303-
/// processors are being registered).
304-
RuleContextUnit? get currentUnit;
305-
306-
/// The defining compilation unit of the library under analysis.
307-
RuleContextUnit get definingUnit;
308-
309-
/// Whether the [definingUnit]'s location is in a package's top-level 'lib'
310-
/// directory, including locations deeply nested, and locations in the
311-
/// package-implementation directory, 'lib/src'.
312-
bool get isInLibDir;
313-
314-
/// Whether the [definingUnit] is in a [package]'s "test" directory.
315-
bool get isInTestDirectory;
316-
317-
/// The library element representing the library that contains the compilation
318-
/// unit being analyzed.
319-
@experimental
320-
LibraryElement? get libraryElement2;
321-
322-
/// The package in which the library being analyzed lives, or `null` if it
323-
/// does not live in a package.
324-
WorkspacePackage? get package;
325-
326-
TypeProvider get typeProvider;
327-
328-
TypeSystem get typeSystem;
329-
330-
/// Whether the given [feature] is enabled in this rule context.
331-
bool isFeatureEnabled(Feature feature);
332-
333-
static bool _isInLibDir(String? filePath, WorkspacePackage? package) {
334-
if (package == null) return false;
335-
if (filePath == null) return false;
336-
var libDir = package.root.getChildAssumingFolder('lib');
337-
return libDir.contains(filePath);
338-
}
339-
}
340-
341-
/// Provides access to information needed by analysis rules that is not
342-
/// available from AST nodes or the element model.
343-
class RuleContextUnit {
344-
final File _file;
345-
final String content;
346-
final ErrorReporter errorReporter;
347-
final CompilationUnit unit;
348-
349-
RuleContextUnit({
350-
required File file,
351-
required this.content,
352-
required this.errorReporter,
353-
required this.unit,
354-
}) : _file = file;
355-
}
356-
357300
/// A [RuleContext] for a library, parsed into [ParsedUnitResult]s.
358301
///
359302
/// This is available for analysis rules that can operate on parsed,
@@ -371,17 +314,14 @@ final class RuleContextWithParsedResults implements RuleContext {
371314
RuleContextWithParsedResults(this.allUnits, this.definingUnit);
372315

373316
@override
374-
bool get isInLibDir => RuleContext._isInLibDir(
375-
definingUnit.unit.declaredFragment?.source.fullName,
376-
package,
377-
);
317+
bool get isInLibDir =>
318+
_isInLibDir(definingUnit.unit.declaredFragment?.source.fullName, package);
378319

379320
@override
380321
bool get isInTestDirectory => false;
381322

382-
@experimental
383323
@override
384-
LibraryElement get libraryElement2 =>
324+
LibraryElement get libraryElement =>
385325
throw UnsupportedError(
386326
'RuleContext with parsed results does not include a LibraryElement',
387327
);
@@ -437,26 +377,23 @@ final class RuleContextWithResolvedResults implements RuleContext {
437377
);
438378

439379
@override
440-
bool get isInLibDir => RuleContext._isInLibDir(
441-
definingUnit.unit.declaredFragment?.source.fullName,
442-
package,
443-
);
380+
bool get isInLibDir =>
381+
_isInLibDir(definingUnit.unit.declaredFragment?.source.fullName, package);
444382

445383
@override
446384
bool get isInTestDirectory {
447385
if (package case var package?) {
448-
var file = definingUnit._file;
386+
var file = definingUnit.file;
449387
return package.isInTestDirectory(file);
450388
}
451389
return false;
452390
}
453391

454-
@experimental
455392
@override
456-
LibraryElement get libraryElement2 =>
393+
LibraryElement get libraryElement =>
457394
definingUnit.unit.declaredFragment!.element;
458395

459396
@override
460397
bool isFeatureEnabled(Feature feature) =>
461-
libraryElement2.featureSet.isEnabled(feature);
398+
libraryElement.featureSet.isEnabled(feature);
462399
}

pkg/analyzer/test/src/lint/linter/linter_context_impl_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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:analyzer/analysis_rule/rule_context.dart';
56
import 'package:analyzer/error/listener.dart';
67
import 'package:analyzer/src/dart/ast/ast.dart';
78
import 'package:analyzer/src/lint/constants.dart';

pkg/linter/lib/src/rules/implementation_imports.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ImplementationImports extends LintRule {
2424
NodeLintRegistry registry,
2525
LinterContext context,
2626
) {
27-
var libraryUri = context.libraryElement2?.uri;
27+
var libraryUri = context.libraryElement?.uri;
2828
if (libraryUri == null) return;
2929

3030
// If the source URI is not a `package` URI, bail out.

pkg/linter/lib/src/rules/no_wildcard_variable_uses.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class NoWildcardVariableUses extends LintRule {
2525
NodeLintRegistry registry,
2626
LinterContext context,
2727
) {
28-
if (context.libraryElement2.hasWildcardVariablesFeatureEnabled) return;
28+
if (context.libraryElement.hasWildcardVariablesFeatureEnabled) return;
2929

3030
var visitor = _Visitor(this);
3131
registry.addSimpleIdentifier(this, visitor);

pkg/linter/lib/src/rules/prefer_relative_imports.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PreferRelativeImports extends LintRule {
3232
) {
3333
if (!context.isInLibDir) return;
3434

35-
var sourceUri = context.libraryElement2?.uri;
35+
var sourceUri = context.libraryElement?.uri;
3636
if (sourceUri == null) return;
3737

3838
var visitor = _Visitor(this, sourceUri, context);

0 commit comments

Comments
 (0)