Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle imports with "as" and "if" clauses in either order. #1555

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions lib/src/front_end/piece_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,26 @@ mixin PieceFactory {
pieces.visit(directive.uri);
});

// Include the `as` clause.
// Add all of the clauses and combinators.
var clauses = <Piece>[];

// The language specifies that configurations must appear after any `as`
// clause but the parser incorrectly accepts them before it and code in
// the wild relies on that. Instead of failing with an "unexpected output"
// error, just preserve the order of the clauses if they are out of order.
// See: https://github.com/dart-lang/sdk/issues/56641
var wroteConfigurations = false;
if (directive.configurations.isNotEmpty &&
asKeyword != null &&
directive.configurations.first.ifKeyword.offset < asKeyword.offset) {
for (var configuration in directive.configurations) {
clauses.add(nodePiece(configuration));
}

wroteConfigurations = true;
}

// Include the `as` clause.
if (asKeyword != null) {
clauses.add(pieces.build(() {
pieces.token(deferredKeyword, spaceAfter: true);
Expand All @@ -824,8 +842,10 @@ mixin PieceFactory {
}

// Include any `if` clauses.
for (var configuration in directive.configurations) {
clauses.add(nodePiece(configuration));
if (!wroteConfigurations) {
for (var configuration in directive.configurations) {
clauses.add(nodePiece(configuration));
}
}

// Include the `show` and `hide` clauses.
Expand Down
18 changes: 17 additions & 1 deletion lib/src/short/source_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,19 @@ class SourceVisitor extends ThrowingAstVisitor {
space();
visit(node.uri);

// The language specifies that configurations must appear after any `as`
// clause but the parser incorrectly accepts them before it and code in
// the wild relies on that. Instead of failing with an "unexpected output"
// error, just preserve the order of the clauses if they are out of order.
// See: https://github.com/dart-lang/sdk/issues/56641
var wroteConfigurations = false;
if (node.asKeyword case var asKeyword?
when node.configurations.isNotEmpty &&
node.configurations.first.ifKeyword.offset < asKeyword.offset) {
_visitConfigurations(node.configurations);
wroteConfigurations = true;
}

if (node.asKeyword != null) {
soloSplit();
token(node.deferredKeyword, after: space);
Expand All @@ -1963,7 +1976,10 @@ class SourceVisitor extends ThrowingAstVisitor {
visit(node.prefix);
}

_visitConfigurations(node.configurations);
if (!wroteConfigurations) {
_visitConfigurations(node.configurations);
}

_visitCombinators(node.combinators);
});
}
Expand Down
11 changes: 10 additions & 1 deletion test/short/whitespace/directives.unit
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,13 @@ part of 'uri.dart';
import 'foo.dart' as foo if (config == 'value') 'other.dart';
<<<
import 'foo.dart' as foo
if (config == 'value') 'other.dart';
if (config == 'value') 'other.dart';
>>> Configuration before prefix.
### This violates the language spec, but the parser currently allows it without
### reporting an error and code in the wild relies on that. So the formatter
### handles it gracefully. See: https://github.com/dart-lang/sdk/issues/56641
import 'foo.dart' if (config == 'value') 'other.dart' as foo;
<<<
import 'foo.dart'
if (config == 'value') 'other.dart'
as foo;
11 changes: 10 additions & 1 deletion test/tall/top_level/import.unit
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,13 @@ import 'foo.dart' as foo if (config == 'value') 'other.dart';
<<<
import 'foo.dart'
as foo
if (config == 'value') 'other.dart';
if (config == 'value') 'other.dart';
>>> Configuration before prefix.
### This violates the language spec, but the parser currently allows it without
### reporting an error and code in the wild relies on that. So the formatter
### handles it gracefully. See: https://github.com/dart-lang/sdk/issues/56641
import 'foo.dart' if (config == 'value') 'other.dart' as foo;
<<<
import 'foo.dart'
if (config == 'value') 'other.dart'
as foo;