Skip to content

Commit e0820bb

Browse files
bwilkersonCommit Bot
authored and
Commit Bot
committed
Enable the prefer_function_declarations_over_variables lint in the analysis server
Change-Id: I53a6cd3c5d3be1c66173f52a7f01d8df63fd9350 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249727 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 9f5942d commit e0820bb

File tree

9 files changed

+50
-50
lines changed

9 files changed

+50
-50
lines changed

pkg/analysis_server/analysis_options.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ analyzer:
1818
implementation_imports: ignore
1919
non_constant_identifier_names: ignore
2020
overridden_fields: ignore
21-
prefer_function_declarations_over_variables: ignore
2221
prefer_void_to_null: ignore
2322
provide_deprecation_message: ignore
2423

pkg/analysis_server/lib/plugin/edit/assist/assist_core.dart

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ import 'package:analyzer_plugin/utilities/assist/assist.dart';
1010
///
1111
/// Clients may not extend, implement or mix-in this class.
1212
class Assist {
13-
/// A comparator that can be used to sort assists by their relevance. The most
14-
/// relevant assists will be sorted before assists with a lower relevance.
15-
/// Assists with the same relevance are sorted alphabetically.
16-
static final Comparator<Assist> SORT_BY_RELEVANCE = (Assist a, Assist b) {
17-
if (a.kind.priority != b.kind.priority) {
18-
// A higher priority indicates a higher relevance
19-
// and should be sorted before a lower priority.
20-
return b.kind.priority - a.kind.priority;
21-
}
22-
return a.change.message.compareTo(b.change.message);
23-
};
24-
2513
/// A description of the assist being proposed.
2614
final AssistKind kind;
2715

@@ -35,4 +23,17 @@ class Assist {
3523
String toString() {
3624
return 'Assist(kind=$kind, change=$change)';
3725
}
26+
27+
/// A function that can be used to sort assists by their relevance.
28+
///
29+
/// The most relevant assists will be sorted before assists with a lower
30+
/// relevance. Assists with the same relevance are sorted alphabetically.
31+
static int compareAssists(Assist a, Assist b) {
32+
if (a.kind.priority != b.kind.priority) {
33+
// A higher priority indicates a higher relevance
34+
// and should be sorted before a lower priority.
35+
return b.kind.priority - a.kind.priority;
36+
}
37+
return a.change.message.compareTo(b.change.message);
38+
}
3839
}

pkg/analysis_server/lib/plugin/edit/fix/fix_core.dart

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,6 @@ import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1111
///
1212
/// Clients may not extend, implement or mix-in this class.
1313
class Fix {
14-
/// A comparator that can be used to sort fixes by their relevance. The most
15-
/// relevant fixes will be sorted before fixes with a lower relevance. Fixes
16-
/// with the same relevance are sorted alphabetically.
17-
static final Comparator<Fix> SORT_BY_RELEVANCE = (Fix a, Fix b) {
18-
if (a.kind.priority != b.kind.priority) {
19-
// A higher priority indicates a higher relevance
20-
// and should be sorted before a lower priority.
21-
return b.kind.priority - a.kind.priority;
22-
}
23-
return a.change.message.compareTo(b.change.message);
24-
};
25-
2614
/// A description of the fix being proposed.
2715
final FixKind kind;
2816

@@ -36,6 +24,19 @@ class Fix {
3624
String toString() {
3725
return 'Fix(kind=$kind, change=$change)';
3826
}
27+
28+
/// A finction that can be used to sort fixes by their relevance.
29+
///
30+
/// The most relevant fixes will be sorted before fixes with a lower
31+
/// relevance. Fixes with the same relevance are sorted alphabetically.
32+
static int compareFixes(Fix a, Fix b) {
33+
if (a.kind.priority != b.kind.priority) {
34+
// A higher priority indicates a higher relevance
35+
// and should be sorted before a lower priority.
36+
return b.kind.priority - a.kind.priority;
37+
}
38+
return a.change.message.compareTo(b.change.message);
39+
}
3940
}
4041

4142
/// An object used to provide context information for [FixContributor]s.

pkg/analysis_server/lib/src/cider/assists.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CiderAssistsComputer {
3737
);
3838
final processor = AssistProcessor(context);
3939
final assists = await processor.compute();
40-
assists.sort(Assist.SORT_BY_RELEVANCE);
40+
assists.sort(Assist.compareAssists);
4141
result.addAll(assists);
4242
} on InconsistentAnalysisException {
4343
// If an InconsistentAnalysisException occurs, it's likely the user modified

pkg/analysis_server/lib/src/cider/fixes.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class CiderFixesComputer {
5858
);
5959

6060
var fixes = await DartFixContributor().computeFixes(context);
61-
fixes.sort(Fix.SORT_BY_RELEVANCE);
61+
fixes.sort(Fix.compareFixes);
6262

6363
result.add(
6464
CiderErrorFixes(error: error, fixes: fixes, lineInfo: lineInfo),

pkg/analysis_server/lib/src/handler/legacy/edit_get_assists.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class EditGetAssistsHandler extends LegacyHandler
101101
try {
102102
var processor = AssistProcessor(context);
103103
var assists = await processor.compute();
104-
assists.sort(Assist.SORT_BY_RELEVANCE);
104+
assists.sort(Assist.compareAssists);
105105
for (var assist in assists) {
106106
changes.add(assist.change);
107107
}

pkg/analysis_server/lib/src/handler/legacy/edit_get_fixes.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class EditGetFixesHandler extends LegacyHandler
127127
resourceProvider, error, content, options);
128128
var fixes = await generator.computeFixes();
129129
if (fixes.isNotEmpty) {
130-
fixes.sort(Fix.SORT_BY_RELEVANCE);
130+
fixes.sort(Fix.compareFixes);
131131
var lineInfo = LineInfo.fromContent(content);
132132
var result = engine.ErrorsResultImpl(
133133
session, file, Uri.file(file), lineInfo, false, errors);
@@ -179,7 +179,7 @@ error.errorCode: ${error.errorCode}
179179
}
180180

181181
if (fixes.isNotEmpty) {
182-
fixes.sort(Fix.SORT_BY_RELEVANCE);
182+
fixes.sort(Fix.compareFixes);
183183
var serverError = newAnalysisError_fromEngine(result, error);
184184
var errorFixes = AnalysisErrorFixes(serverError);
185185
errorFixesList.add(errorFixes);
@@ -227,7 +227,7 @@ error.errorCode: ${error.errorCode}
227227
PubspecFixGenerator(resourceProvider, error, content, document);
228228
var fixes = await generator.computeFixes();
229229
if (fixes.isNotEmpty) {
230-
fixes.sort(Fix.SORT_BY_RELEVANCE);
230+
fixes.sort(Fix.compareFixes);
231231
var lineInfo = LineInfo.fromContent(content);
232232
var result = engine.ErrorsResultImpl(
233233
session, file, Uri.file(file), lineInfo, false, errors);

pkg/analysis_server/lib/src/lsp/handlers/handler_code_actions.dart

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,6 @@ class CodeActionHandler
3535
// CodeAction class).
3636
final codeActionPriorities = Expando<int>();
3737

38-
/// A comparator that can be used to sort [CodeActions]s using priorities
39-
/// in [codeActionPriorities].
40-
///
41-
/// The highest number priority will be sorted before lower number priorities.
42-
/// Items with the same priority are sorted alphabetically by their title.
43-
late final Comparator<CodeAction> _codeActionComparator =
44-
(CodeAction a, CodeAction b) {
45-
// We should never be sorting actions without priorities.
46-
final aPriority = codeActionPriorities[a] ?? 0;
47-
final bPriority = codeActionPriorities[b] ?? 0;
48-
if (aPriority != bPriority) {
49-
return bPriority - aPriority;
50-
}
51-
return a.title.compareTo(b.title);
52-
};
53-
5438
CodeActionHandler(super.server);
5539

5640
@override
@@ -200,6 +184,21 @@ class CodeActionHandler
200184
: Either2<CodeAction, Command>.t2(command);
201185
}
202186

187+
/// A function that can be used to sort [CodeActions]s using priorities
188+
/// in [codeActionPriorities].
189+
///
190+
/// The highest number priority will be sorted before lower number priorities.
191+
/// Items with the same priority are sorted alphabetically by their title.
192+
int _compareCodeActions(CodeAction a, CodeAction b) {
193+
// We should never be sorting actions without priorities.
194+
final aPriority = codeActionPriorities[a] ?? 0;
195+
final bPriority = codeActionPriorities[b] ?? 0;
196+
if (aPriority != bPriority) {
197+
return bPriority - aPriority;
198+
}
199+
return a.title.compareTo(b.title);
200+
}
201+
203202
/// Creates a CodeAction to apply this assist. Note: This code will fetch the
204203
/// version of each document being modified so it's important to call this
205204
/// immediately after computing edits to ensure the document is not modified
@@ -318,7 +317,7 @@ class CodeActionHandler
318317
}));
319318

320319
final dedupedCodeActions = _dedupeActions(codeActions, range.start);
321-
dedupedCodeActions.sort(_codeActionComparator);
320+
dedupedCodeActions.sort(_compareCodeActions);
322321

323322
return dedupedCodeActions
324323
.where((action) => shouldIncludeKind(action.kind))
@@ -450,7 +449,7 @@ class CodeActionHandler
450449
codeActions.addAll(pluginFixActions);
451450

452451
final dedupedActions = _dedupeActions(codeActions, range.start);
453-
dedupedActions.sort(_codeActionComparator);
452+
dedupedActions.sort(_compareCodeActions);
454453

455454
return dedupedActions
456455
.where((action) => shouldIncludeKind(action.kind))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1433,7 +1433,7 @@ class FixProcessor extends BaseProcessor {
14331433

14341434
Future<Fix?> computeFix() async {
14351435
await _addFromProducers();
1436-
fixes.sort(Fix.SORT_BY_RELEVANCE);
1436+
fixes.sort(Fix.compareFixes);
14371437
return fixes.isNotEmpty ? fixes.first : null;
14381438
}
14391439

0 commit comments

Comments
 (0)