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' ;
56import 'package:analyzer/analysis_rule/rule_state.dart' ;
67import 'package:analyzer/dart/analysis/features.dart' ;
78import 'package:analyzer/dart/analysis/results.dart' ;
@@ -13,16 +14,22 @@ import 'package:analyzer/dart/element/type_system.dart';
1314import 'package:analyzer/diagnostic/diagnostic.dart' ;
1415import 'package:analyzer/error/error.dart' ;
1516import 'package:analyzer/error/listener.dart' ;
16- import 'package:analyzer/file_system/file_system.dart' ;
1717import 'package:analyzer/src/lint/linter_visitor.dart' show RuleVisitorRegistry;
1818import 'package:analyzer/src/lint/pub.dart' ;
1919import 'package:analyzer/workspace/workspace.dart' ;
20- import 'package:meta/meta.dart' ;
2120
2221export 'package:analyzer/analysis_rule/rule_state.dart'
2322 show dart2_12, dart3, dart3_3, RuleState;
2423export '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] .
2734typedef 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}
0 commit comments