Skip to content

Commit e481002

Browse files
srawlinsCommit Queue
authored andcommitted
Remove RuleState type hierarchy with an enum and condensed constructors
Change-Id: I07ee606b00467b8113f1722dba21b8c16cfe85b3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/429900 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 9d2df84 commit e481002

File tree

6 files changed

+45
-82
lines changed

6 files changed

+45
-82
lines changed

pkg/analysis_server/lib/src/services/completion/yaml/analysis_options_generator.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'package:analyzer/error/error.dart';
1010
import 'package:analyzer/file_system/file_system.dart';
1111
import 'package:analyzer/src/dart/analysis/experiments.dart';
1212
import 'package:analyzer/src/lint/registry.dart';
13-
import 'package:analyzer/src/lint/state.dart';
1413
import 'package:analyzer/src/task/options.dart';
1514

1615
/// A completion generator that can produce completion suggestions for analysis

pkg/analyzer/lib/src/error/ignore_validator.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import 'package:analyzer/source/line_info.dart';
99
import 'package:analyzer/src/error/codes.dart';
1010
import 'package:analyzer/src/ignore_comments/ignore_info.dart';
1111
import 'package:analyzer/src/lint/registry.dart';
12-
import 'package:analyzer/src/lint/state.dart';
1312

1413
/// Used to validate the ignore comments in a single file.
1514
class IgnoreValidator {
@@ -207,9 +206,9 @@ class IgnoreValidator {
207206
} else {
208207
var state = rule.state;
209208
var since = state.since.toString();
210-
if (state is DeprecatedRuleState) {
211-
// `todo`(pq): implement
212-
} else if (state is RemovedRuleState) {
209+
if (state.isDeprecated) {
210+
// TODO(pq): implement.
211+
} else if (state.isRemoved) {
213212
var replacedBy = state.replacedBy;
214213
if (replacedBy != null) {
215214
_errorReporter.atOffset(

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:analyzer/error/listener.dart';
77
import 'package:analyzer/src/analysis_options/error/option_codes.dart';
88
import 'package:analyzer/src/lint/linter.dart';
99
import 'package:analyzer/src/lint/registry.dart';
10-
import 'package:analyzer/src/lint/state.dart';
1110
import 'package:analyzer/src/plugin/options.dart';
1211
import 'package:analyzer/src/util/yaml.dart';
1312
import 'package:collection/collection.dart';
@@ -39,12 +38,11 @@ class LinterRuleOptionsValidator extends OptionsValidator {
3938
.rules
4039
.firstWhereOrNull((rule) => rule.name == value);
4140

42-
bool isDeprecatedInCurrentSdk(DeprecatedRuleState state) =>
43-
currentSdkAllows(state.since);
41+
bool isDeprecatedInCurrentSdk(RuleState state) =>
42+
state.isDeprecated && currentSdkAllows(state.since);
4443

4544
bool isRemovedInCurrentSdk(RuleState state) {
46-
if (state is! RemovedRuleState) return false;
47-
return currentSdkAllows(state.since);
45+
return state.isRemoved && currentSdkAllows(state.since);
4846
}
4947

5048
@override
@@ -103,7 +101,7 @@ class LinterRuleOptionsValidator extends OptionsValidator {
103101
// includes).
104102
if (sourceIsOptionsForContextRoot) {
105103
var state = rule.state;
106-
if (state is DeprecatedRuleState && isDeprecatedInCurrentSdk(state)) {
104+
if (state.isDeprecated && isDeprecatedInCurrentSdk(state)) {
107105
var replacedBy = state.replacedBy;
108106
if (replacedBy != null) {
109107
reporter.atSourceSpan(
@@ -120,7 +118,7 @@ class LinterRuleOptionsValidator extends OptionsValidator {
120118
}
121119
} else if (isRemovedInCurrentSdk(state)) {
122120
var since = state.since.toString();
123-
var replacedBy = (state as RemovedRuleState).replacedBy;
121+
var replacedBy = state.replacedBy;
124122
if (replacedBy != null) {
125123
reporter.atSourceSpan(
126124
node.span,

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

Lines changed: 37 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,88 +16,57 @@ final Version dart3_3 = Version(3, 3, 0);
1616
@Deprecated("Prefer to use 'RuleState'")
1717
typedef State = RuleState;
1818

19-
/// A state that marks an analysis rule as deprecated.
20-
final class DeprecatedRuleState extends RuleState {
21-
/// The optional name of an analysis rule which replaces the rule with this
22-
/// state.
23-
final String? replacedBy;
24-
25-
const DeprecatedRuleState._({super.since, this.replacedBy});
26-
27-
@override
28-
String get label => 'deprecated';
29-
}
30-
31-
/// A state that marks an analysis rule as experimental.
32-
final class ExperimentalRuleState extends RuleState {
33-
const ExperimentalRuleState._({super.since});
34-
35-
@override
36-
String get label => 'experimental';
37-
}
38-
39-
/// A state that marks an analysis rule as for internal (Dart SDK) use only.
40-
final class InternalRuleState extends RuleState {
41-
const InternalRuleState._({super.since});
19+
/// Describes the state of an analysis rule.
20+
final class RuleState {
21+
/// An Optional Dart language version that identifies the start of this state.
22+
final Version? since;
4223

43-
@override
44-
String get label => 'internal';
45-
}
24+
final _RuleStateType _type;
4625

47-
/// A state that identifies an analysis rule as having been removed.
48-
final class RemovedRuleState extends RuleState {
49-
/// An optional lint name that replaces the rule with this state.
26+
/// The optional name of an analysis rule which replaces the rule with this
27+
/// state.
5028
final String? replacedBy;
5129

52-
const RemovedRuleState._({super.since, this.replacedBy});
30+
/// Initializes a state that marks an analysis rule as deprecated.
31+
const RuleState.deprecated({this.since, this.replacedBy})
32+
: _type = _RuleStateType.deprecated;
5333

54-
@override
55-
String get label => 'removed';
56-
}
34+
/// Initializes a state that marks an analysis rule as experimental.
35+
const RuleState.experimental({this.since})
36+
: _type = _RuleStateType.experimental,
37+
replacedBy = null;
5738

58-
/// Describes the state of a lint.
59-
sealed class RuleState {
60-
/// An Optional Dart language version that identifies the start of this state.
61-
final Version? since;
39+
/// Initializes a state that marks an analysis rule as for internal (Dart SDK)
40+
/// use only.
41+
const RuleState.internal({this.since})
42+
: _type = _RuleStateType.internal,
43+
replacedBy = null;
6244

63-
/// Initialize a newly created State object.
64-
const RuleState({this.since});
45+
/// Initializes a state that identifies an analysis rule as having been removed.
46+
const RuleState.removed({this.since, this.replacedBy})
47+
: _type = _RuleStateType.removed;
6548

66-
/// Initialize a newly created deprecated state with given values.
67-
const factory RuleState.deprecated({Version? since, String? replacedBy}) =
68-
DeprecatedRuleState._;
49+
/// Initializes a state that marks an analysis rule as stable.
50+
const RuleState.stable({this.since})
51+
: _type = _RuleStateType.stable,
52+
replacedBy = null;
6953

70-
/// Initialize a newly created experimental state with given values.
71-
const factory RuleState.experimental({Version? since}) =
72-
ExperimentalRuleState._;
54+
/// Whether this state marks an analysis rule as deprecated.
55+
bool get isDeprecated => _type == _RuleStateType.deprecated;
7356

74-
/// Initialize a newly created internal state with given values.
75-
const factory RuleState.internal({Version? since}) = InternalRuleState._;
57+
/// Whether this state marks an analysis rule as experimental.
58+
bool get isExperimental => _type == _RuleStateType.experimental;
7659

77-
/// Initialize a newly created removed state with given values.
78-
const factory RuleState.removed({Version? since, String? replacedBy}) =
79-
RemovedRuleState._;
60+
/// Whether this state marks an analysis rule as internal.
61+
bool get isInternal => _type == _RuleStateType.internal;
8062

81-
/// Initialize a newly created stable state with given values.
82-
const factory RuleState.stable({Version? since}) = StableRuleState._;
63+
/// Whether this state marks an analysis rule as removed.
64+
bool get isRemoved => _type == _RuleStateType.removed;
8365

8466
/// A short description, suitable for displaying in documentation or a
8567
/// diagnostic message.
86-
String get label;
68+
String get label => _type.name;
8769
}
8870

89-
/// A state that marks an analysis rule as stable.
90-
final class StableRuleState extends RuleState {
91-
const StableRuleState._({super.since});
92-
93-
@override
94-
String get label => 'stable';
95-
}
96-
97-
extension StateExtension on RuleState {
98-
bool get isDeprecated => this is DeprecatedRuleState;
99-
bool get isExperimental => this is ExperimentalRuleState;
100-
bool get isInternal => this is InternalRuleState;
101-
bool get isRemoved => this is RemovedRuleState;
102-
bool get isStable => this is StableRuleState;
103-
}
71+
/// The type of a rule state.
72+
enum _RuleStateType { deprecated, experimental, internal, removed, stable }

pkg/linter/tool/checks/check_all_yaml.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'dart:io';
66

77
import 'package:analyzer/src/lint/io.dart';
88
import 'package:analyzer/src/lint/registry.dart';
9-
import 'package:analyzer/src/lint/state.dart';
109
import 'package:linter/src/rules.dart';
1110
import 'package:yaml/yaml.dart';
1211

pkg/linter/tool/machine.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'dart:convert';
66
import 'dart:io';
77

88
import 'package:analyzer/src/lint/registry.dart';
9-
import 'package:analyzer/src/lint/state.dart';
109
import 'package:args/args.dart';
1110
import 'package:collection/collection.dart';
1211
import 'package:linter/src/analyzer.dart';

0 commit comments

Comments
 (0)