Skip to content

Commit 38a3b21

Browse files
munificentCommit Queue
authored andcommitted
Don't pass a null language version to DartFormatter().
The DartFormatter() constructor call in edit_format.dart is passing in a language version, which is good. But it was passing a nullable Version because of the default case. That works in dart_style 2.3.7 where the Version is optional but fails in 3.0.0 because the Version is non-nullable there. This fixes that by defaulting to the latest version. I'm not sure under what conditions this can fail to find an effectiveLanguageVersion so I don't know if the latest version is the right default. Let me know if there's something better I should do here. Change-Id: I76477e5df9dcf2a78176207cff92c20c7f08a17b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391840 Reviewed-by: Paul Berry <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Auto-Submit: Bob Nystrom <[email protected]>
1 parent 3e71d18 commit 38a3b21

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:analyzer_plugin/protocol/protocol_common.dart';
1212
import 'package:dart_style/src/dart_formatter.dart';
1313
import 'package:dart_style/src/exceptions.dart';
1414
import 'package:dart_style/src/source_code.dart';
15+
import 'package:pub_semver/pub_semver.dart';
1516

1617
/// The handler for the `edit.format` request.
1718
class EditFormatHandler extends LegacyHandler {
@@ -52,13 +53,19 @@ class EditFormatHandler extends LegacyHandler {
5253

5354
var driver = server.getAnalysisDriver(file);
5455
var unit = await driver?.getResolvedUnit(file);
55-
var (pageWidth, effectiveLanguageVersion) = switch (unit) {
56-
ResolvedUnitResult() => (
57-
unit.analysisOptions.formatterOptions.pageWidth,
58-
unit.libraryElement2.effectiveLanguageVersion,
59-
),
60-
(_) => (null, null),
61-
};
56+
57+
int? pageWidth;
58+
Version effectiveLanguageVersion;
59+
if (unit is ResolvedUnitResult) {
60+
pageWidth = unit.analysisOptions.formatterOptions.pageWidth;
61+
effectiveLanguageVersion = unit.libraryElement2.effectiveLanguageVersion;
62+
} else {
63+
// If the unit doesn't resolve, don't try to format it since we don't
64+
// know what language version (and thus what formatting style) to apply.
65+
sendResponse(Response.formatWithErrors(request));
66+
return;
67+
}
68+
6269
var formatter = DartFormatter(
6370
pageWidth: pageWidth ?? params.lineLength,
6471
languageVersion: effectiveLanguageVersion);

0 commit comments

Comments
 (0)