Skip to content

Commit

Permalink
[element model] migrate unnecessary_breaks
Browse files Browse the repository at this point in the history
Bug: dart-lang/linter#5099
Change-Id: I745a770eecd901a1eb56acc5043939eccb8ebb91
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/387660
Auto-Submit: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
  • Loading branch information
pq authored and Commit Queue committed Sep 30, 2024
1 parent dba344b commit 72ee3e9
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
7 changes: 7 additions & 0 deletions pkg/linter/lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion pkg/linter/lib/src/rules/unnecessary_breaks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/linter/lib/src/rules/unnecessary_lambdas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class _Visitor extends SimpleAstVisitor<void> {
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
Expand Down
6 changes: 2 additions & 4 deletions pkg/linter/lib/src/rules/unnecessary_library_name.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand All @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions pkg/linter/lib/src/rules/use_enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down Expand Up @@ -48,9 +49,7 @@ class _Visitor extends SimpleAstVisitor<void> {
}

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);
}
6 changes: 2 additions & 4 deletions pkg/linter/lib/src/rules/use_super_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 72ee3e9

Please sign in to comment.