From e0820bb508c7f8cbf00a56cdb7904fa4dac82bf8 Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Mon, 27 Jun 2022 18:27:23 +0000 Subject: [PATCH] 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 Reviewed-by: Samuel Rawlins --- pkg/analysis_server/analysis_options.yaml | 1 - .../lib/plugin/edit/assist/assist_core.dart | 25 ++++++------- .../lib/plugin/edit/fix/fix_core.dart | 25 ++++++------- .../lib/src/cider/assists.dart | 2 +- pkg/analysis_server/lib/src/cider/fixes.dart | 2 +- .../src/handler/legacy/edit_get_assists.dart | 2 +- .../src/handler/legacy/edit_get_fixes.dart | 6 ++-- .../lsp/handlers/handler_code_actions.dart | 35 +++++++++---------- .../src/services/correction/fix_internal.dart | 2 +- 9 files changed, 50 insertions(+), 50 deletions(-) diff --git a/pkg/analysis_server/analysis_options.yaml b/pkg/analysis_server/analysis_options.yaml index 54612a2958f..9e70161fb5c 100644 --- a/pkg/analysis_server/analysis_options.yaml +++ b/pkg/analysis_server/analysis_options.yaml @@ -18,7 +18,6 @@ analyzer: implementation_imports: ignore non_constant_identifier_names: ignore overridden_fields: ignore - prefer_function_declarations_over_variables: ignore prefer_void_to_null: ignore provide_deprecation_message: ignore diff --git a/pkg/analysis_server/lib/plugin/edit/assist/assist_core.dart b/pkg/analysis_server/lib/plugin/edit/assist/assist_core.dart index c56e634ac00..5606b508cda 100644 --- a/pkg/analysis_server/lib/plugin/edit/assist/assist_core.dart +++ b/pkg/analysis_server/lib/plugin/edit/assist/assist_core.dart @@ -10,18 +10,6 @@ import 'package:analyzer_plugin/utilities/assist/assist.dart'; /// /// Clients may not extend, implement or mix-in this class. class Assist { - /// A comparator that can be used to sort assists by their relevance. The most - /// relevant assists will be sorted before assists with a lower relevance. - /// Assists with the same relevance are sorted alphabetically. - static final Comparator SORT_BY_RELEVANCE = (Assist a, Assist b) { - if (a.kind.priority != b.kind.priority) { - // A higher priority indicates a higher relevance - // and should be sorted before a lower priority. - return b.kind.priority - a.kind.priority; - } - return a.change.message.compareTo(b.change.message); - }; - /// A description of the assist being proposed. final AssistKind kind; @@ -35,4 +23,17 @@ class Assist { String toString() { return 'Assist(kind=$kind, change=$change)'; } + + /// A function that can be used to sort assists by their relevance. + /// + /// The most relevant assists will be sorted before assists with a lower + /// relevance. Assists with the same relevance are sorted alphabetically. + static int compareAssists(Assist a, Assist b) { + if (a.kind.priority != b.kind.priority) { + // A higher priority indicates a higher relevance + // and should be sorted before a lower priority. + return b.kind.priority - a.kind.priority; + } + return a.change.message.compareTo(b.change.message); + } } diff --git a/pkg/analysis_server/lib/plugin/edit/fix/fix_core.dart b/pkg/analysis_server/lib/plugin/edit/fix/fix_core.dart index f3873dc4e91..7f14241ee6b 100644 --- a/pkg/analysis_server/lib/plugin/edit/fix/fix_core.dart +++ b/pkg/analysis_server/lib/plugin/edit/fix/fix_core.dart @@ -11,18 +11,6 @@ import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; /// /// Clients may not extend, implement or mix-in this class. class Fix { - /// A comparator that can be used to sort fixes by their relevance. The most - /// relevant fixes will be sorted before fixes with a lower relevance. Fixes - /// with the same relevance are sorted alphabetically. - static final Comparator SORT_BY_RELEVANCE = (Fix a, Fix b) { - if (a.kind.priority != b.kind.priority) { - // A higher priority indicates a higher relevance - // and should be sorted before a lower priority. - return b.kind.priority - a.kind.priority; - } - return a.change.message.compareTo(b.change.message); - }; - /// A description of the fix being proposed. final FixKind kind; @@ -36,6 +24,19 @@ class Fix { String toString() { return 'Fix(kind=$kind, change=$change)'; } + + /// A finction that can be used to sort fixes by their relevance. + /// + /// The most relevant fixes will be sorted before fixes with a lower + /// relevance. Fixes with the same relevance are sorted alphabetically. + static int compareFixes(Fix a, Fix b) { + if (a.kind.priority != b.kind.priority) { + // A higher priority indicates a higher relevance + // and should be sorted before a lower priority. + return b.kind.priority - a.kind.priority; + } + return a.change.message.compareTo(b.change.message); + } } /// An object used to provide context information for [FixContributor]s. diff --git a/pkg/analysis_server/lib/src/cider/assists.dart b/pkg/analysis_server/lib/src/cider/assists.dart index 6b51f056a08..76a0d6b3840 100644 --- a/pkg/analysis_server/lib/src/cider/assists.dart +++ b/pkg/analysis_server/lib/src/cider/assists.dart @@ -37,7 +37,7 @@ class CiderAssistsComputer { ); final processor = AssistProcessor(context); final assists = await processor.compute(); - assists.sort(Assist.SORT_BY_RELEVANCE); + assists.sort(Assist.compareAssists); result.addAll(assists); } on InconsistentAnalysisException { // If an InconsistentAnalysisException occurs, it's likely the user modified diff --git a/pkg/analysis_server/lib/src/cider/fixes.dart b/pkg/analysis_server/lib/src/cider/fixes.dart index fe44877c3c6..b14c6ff0d9d 100644 --- a/pkg/analysis_server/lib/src/cider/fixes.dart +++ b/pkg/analysis_server/lib/src/cider/fixes.dart @@ -58,7 +58,7 @@ class CiderFixesComputer { ); var fixes = await DartFixContributor().computeFixes(context); - fixes.sort(Fix.SORT_BY_RELEVANCE); + fixes.sort(Fix.compareFixes); result.add( CiderErrorFixes(error: error, fixes: fixes, lineInfo: lineInfo), diff --git a/pkg/analysis_server/lib/src/handler/legacy/edit_get_assists.dart b/pkg/analysis_server/lib/src/handler/legacy/edit_get_assists.dart index b66b6c5735e..c2350060592 100644 --- a/pkg/analysis_server/lib/src/handler/legacy/edit_get_assists.dart +++ b/pkg/analysis_server/lib/src/handler/legacy/edit_get_assists.dart @@ -101,7 +101,7 @@ class EditGetAssistsHandler extends LegacyHandler try { var processor = AssistProcessor(context); var assists = await processor.compute(); - assists.sort(Assist.SORT_BY_RELEVANCE); + assists.sort(Assist.compareAssists); for (var assist in assists) { changes.add(assist.change); } diff --git a/pkg/analysis_server/lib/src/handler/legacy/edit_get_fixes.dart b/pkg/analysis_server/lib/src/handler/legacy/edit_get_fixes.dart index b0bb054b901..09cfb6255aa 100644 --- a/pkg/analysis_server/lib/src/handler/legacy/edit_get_fixes.dart +++ b/pkg/analysis_server/lib/src/handler/legacy/edit_get_fixes.dart @@ -127,7 +127,7 @@ class EditGetFixesHandler extends LegacyHandler resourceProvider, error, content, options); var fixes = await generator.computeFixes(); if (fixes.isNotEmpty) { - fixes.sort(Fix.SORT_BY_RELEVANCE); + fixes.sort(Fix.compareFixes); var lineInfo = LineInfo.fromContent(content); var result = engine.ErrorsResultImpl( session, file, Uri.file(file), lineInfo, false, errors); @@ -179,7 +179,7 @@ error.errorCode: ${error.errorCode} } if (fixes.isNotEmpty) { - fixes.sort(Fix.SORT_BY_RELEVANCE); + fixes.sort(Fix.compareFixes); var serverError = newAnalysisError_fromEngine(result, error); var errorFixes = AnalysisErrorFixes(serverError); errorFixesList.add(errorFixes); @@ -227,7 +227,7 @@ error.errorCode: ${error.errorCode} PubspecFixGenerator(resourceProvider, error, content, document); var fixes = await generator.computeFixes(); if (fixes.isNotEmpty) { - fixes.sort(Fix.SORT_BY_RELEVANCE); + fixes.sort(Fix.compareFixes); var lineInfo = LineInfo.fromContent(content); var result = engine.ErrorsResultImpl( session, file, Uri.file(file), lineInfo, false, errors); diff --git a/pkg/analysis_server/lib/src/lsp/handlers/handler_code_actions.dart b/pkg/analysis_server/lib/src/lsp/handlers/handler_code_actions.dart index d37962bf42e..bd82a4a1eaa 100644 --- a/pkg/analysis_server/lib/src/lsp/handlers/handler_code_actions.dart +++ b/pkg/analysis_server/lib/src/lsp/handlers/handler_code_actions.dart @@ -35,22 +35,6 @@ class CodeActionHandler // CodeAction class). final codeActionPriorities = Expando(); - /// A comparator that can be used to sort [CodeActions]s using priorities - /// in [codeActionPriorities]. - /// - /// The highest number priority will be sorted before lower number priorities. - /// Items with the same priority are sorted alphabetically by their title. - late final Comparator _codeActionComparator = - (CodeAction a, CodeAction b) { - // We should never be sorting actions without priorities. - final aPriority = codeActionPriorities[a] ?? 0; - final bPriority = codeActionPriorities[b] ?? 0; - if (aPriority != bPriority) { - return bPriority - aPriority; - } - return a.title.compareTo(b.title); - }; - CodeActionHandler(super.server); @override @@ -200,6 +184,21 @@ class CodeActionHandler : Either2.t2(command); } + /// A function that can be used to sort [CodeActions]s using priorities + /// in [codeActionPriorities]. + /// + /// The highest number priority will be sorted before lower number priorities. + /// Items with the same priority are sorted alphabetically by their title. + int _compareCodeActions(CodeAction a, CodeAction b) { + // We should never be sorting actions without priorities. + final aPriority = codeActionPriorities[a] ?? 0; + final bPriority = codeActionPriorities[b] ?? 0; + if (aPriority != bPriority) { + return bPriority - aPriority; + } + return a.title.compareTo(b.title); + } + /// Creates a CodeAction to apply this assist. Note: This code will fetch the /// version of each document being modified so it's important to call this /// immediately after computing edits to ensure the document is not modified @@ -318,7 +317,7 @@ class CodeActionHandler })); final dedupedCodeActions = _dedupeActions(codeActions, range.start); - dedupedCodeActions.sort(_codeActionComparator); + dedupedCodeActions.sort(_compareCodeActions); return dedupedCodeActions .where((action) => shouldIncludeKind(action.kind)) @@ -450,7 +449,7 @@ class CodeActionHandler codeActions.addAll(pluginFixActions); final dedupedActions = _dedupeActions(codeActions, range.start); - dedupedActions.sort(_codeActionComparator); + dedupedActions.sort(_compareCodeActions); return dedupedActions .where((action) => shouldIncludeKind(action.kind)) diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart index 9e1f5f291d7..7e1be0354d5 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart @@ -1433,7 +1433,7 @@ class FixProcessor extends BaseProcessor { Future computeFix() async { await _addFromProducers(); - fixes.sort(Fix.SORT_BY_RELEVANCE); + fixes.sort(Fix.compareFixes); return fixes.isNotEmpty ? fixes.first : null; }