diff --git a/pkg/linter/lib/src/extensions.dart b/pkg/linter/lib/src/extensions.dart index 68722c87f6fa..cd8f621e4954 100644 --- a/pkg/linter/lib/src/extensions.dart +++ b/pkg/linter/lib/src/extensions.dart @@ -2,6 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'package:analyzer/dart/analysis/features.dart'; import 'package:analyzer/dart/ast/token.dart'; import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.dart'; @@ -546,6 +547,12 @@ extension InterfaceTypeExtension on InterfaceType { } } +extension LinterContextExtension on LinterContext { + /// Whether the given [feature] is enabled in this linter context. + bool isEnabled(Feature feature) => + libraryElement2!.featureSet.isEnabled(feature); +} + extension MethodDeclarationExtension on MethodDeclaration { bool get hasInheritedMethod => lookUpInheritedMethod() != null; diff --git a/pkg/linter/lib/src/rules/unnecessary_breaks.dart b/pkg/linter/lib/src/rules/unnecessary_breaks.dart index bb51f924eb6c..042c21b8ab56 100644 --- a/pkg/linter/lib/src/rules/unnecessary_breaks.dart +++ b/pkg/linter/lib/src/rules/unnecessary_breaks.dart @@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/visitor.dart'; import '../analyzer.dart'; +import '../extensions.dart'; import '../linter_lint_codes.dart'; const _desc = r"Don't use explicit `break`s when a break is implied."; @@ -24,7 +25,8 @@ class UnnecessaryBreaks extends LintRule { @override void registerNodeProcessors( NodeLintRegistry registry, LinterContext context) { - if (!context.libraryElement!.featureSet.isEnabled(Feature.patterns)) return; + if (!context.isEnabled(Feature.patterns)) return; + var visitor = _Visitor(this); registry.addBreakStatement(this, visitor); } diff --git a/pkg/linter/lib/src/rules/unnecessary_lambdas.dart b/pkg/linter/lib/src/rules/unnecessary_lambdas.dart index 44ce80efd5f7..62549b3135a6 100644 --- a/pkg/linter/lib/src/rules/unnecessary_lambdas.dart +++ b/pkg/linter/lib/src/rules/unnecessary_lambdas.dart @@ -95,8 +95,8 @@ class _Visitor extends SimpleAstVisitor { final TypeSystem typeSystem; _Visitor(this.rule, LinterContext context) - : constructorTearOffsEnabled = context.libraryElement!.featureSet - .isEnabled(Feature.constructor_tearoffs), + : constructorTearOffsEnabled = + context.isEnabled(Feature.constructor_tearoffs), typeSystem = context.typeSystem; @override diff --git a/pkg/linter/lib/src/rules/unnecessary_library_name.dart b/pkg/linter/lib/src/rules/unnecessary_library_name.dart index cfaa6a3333cd..65984f5b40aa 100644 --- a/pkg/linter/lib/src/rules/unnecessary_library_name.dart +++ b/pkg/linter/lib/src/rules/unnecessary_library_name.dart @@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/visitor.dart'; import '../analyzer.dart'; +import '../extensions.dart'; import '../linter_lint_codes.dart'; const _desc = r"Don't have a library name in a `library` declaration."; @@ -24,10 +25,7 @@ class UnnecessaryLibraryName extends LintRule { @override void registerNodeProcessors( NodeLintRegistry registry, LinterContext context) { - if (!context.libraryElement!.featureSet - .isEnabled(Feature.unnamedLibraries)) { - return; - } + if (!context.isEnabled(Feature.unnamedLibraries)) return; var visitor = _Visitor(this); registry.addLibraryDirective(this, visitor); diff --git a/pkg/linter/lib/src/rules/use_enums.dart b/pkg/linter/lib/src/rules/use_enums.dart index 76f09b1d4030..27e19af63472 100644 --- a/pkg/linter/lib/src/rules/use_enums.dart +++ b/pkg/linter/lib/src/rules/use_enums.dart @@ -27,9 +27,7 @@ class UseEnums extends LintRule { @override void registerNodeProcessors( NodeLintRegistry registry, LinterContext context) { - if (!context.libraryElement!.featureSet.isEnabled(Feature.enhanced_enums)) { - return; - } + if (!context.isEnabled(Feature.enhanced_enums)) return; var visitor = _Visitor(this, context); registry.addClassDeclaration(this, visitor); diff --git a/pkg/linter/lib/src/rules/use_string_in_part_of_directives.dart b/pkg/linter/lib/src/rules/use_string_in_part_of_directives.dart index b3de77fb5a86..f8735876bca9 100644 --- a/pkg/linter/lib/src/rules/use_string_in_part_of_directives.dart +++ b/pkg/linter/lib/src/rules/use_string_in_part_of_directives.dart @@ -8,6 +8,7 @@ import 'package:analyzer/dart/ast/visitor.dart'; import 'package:analyzer/src/lint/linter.dart'; //ignore: implementation_imports import '../analyzer.dart'; +import '../extensions.dart'; import '../linter_lint_codes.dart'; const _desc = r'Use string in part of directives.'; @@ -48,9 +49,7 @@ class _Visitor extends SimpleAstVisitor { } extension on LinterContext { - bool get hasEnancedPartsFeatureEnabled { - var self = this; - return self is LinterContextWithResolvedResults && - self.libraryElement.featureSet.isEnabled(Feature.enhanced_parts); - } + bool get hasEnancedPartsFeatureEnabled => + this is LinterContextWithResolvedResults && + isEnabled(Feature.enhanced_parts); } diff --git a/pkg/linter/lib/src/rules/use_super_parameters.dart b/pkg/linter/lib/src/rules/use_super_parameters.dart index 6e7a223e2dce..c916acce21fd 100644 --- a/pkg/linter/lib/src/rules/use_super_parameters.dart +++ b/pkg/linter/lib/src/rules/use_super_parameters.dart @@ -9,6 +9,7 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/src/utilities/extensions/string.dart'; // ignore: implementation_imports import '../analyzer.dart'; +import '../extensions.dart'; import '../linter_lint_codes.dart'; const _desc = r'Use super-initializer parameters where possible.'; @@ -39,10 +40,7 @@ class UseSuperParameters extends LintRule { @override void registerNodeProcessors( NodeLintRegistry registry, LinterContext context) { - if (!context.libraryElement!.featureSet - .isEnabled(Feature.super_parameters)) { - return; - } + if (!context.isEnabled(Feature.super_parameters)) return; var visitor = _Visitor(this, context); registry.addConstructorDeclaration(this, visitor);