Skip to content

Commit 953ecbc

Browse files
authored
Correctly format imports with both as and if clauses. (#1550)
It would output the `as` clause after the `if` clauses, which isn't syntactically valid. Apparently no one has ever had a conditional import with as prefix because this was a bug in both the old and new formatters and has been a bug in the old formatter forever. Fix #1544.
1 parent 1101f65 commit 953ecbc

File tree

7 files changed

+219
-10
lines changed

7 files changed

+219
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
flag (#361).
1010
* Preserve type parameters on old-style function-typed formals that also use
1111
`this.` or `super.` (#1321).
12+
* Correctly format imports with both `as` and `if` clauses (#1544).
1213
* Remove temporary work around for analyzer 6.2.0 from dart_style 2.3.6.
1314
* Require `package:analyzer` `>=6.5.0 <7.0.0`.
1415

lib/src/front_end/piece_factory.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -812,13 +812,8 @@ mixin PieceFactory {
812812
pieces.visit(directive.uri);
813813
});
814814

815-
// Include any `if` clauses.
816-
var clauses = <Piece>[];
817-
for (var configuration in directive.configurations) {
818-
clauses.add(nodePiece(configuration));
819-
}
820-
821815
// Include the `as` clause.
816+
var clauses = <Piece>[];
822817
if (asKeyword != null) {
823818
clauses.add(pieces.build(() {
824819
pieces.token(deferredKeyword, spaceAfter: true);
@@ -828,6 +823,11 @@ mixin PieceFactory {
828823
}));
829824
}
830825

826+
// Include any `if` clauses.
827+
for (var configuration in directive.configurations) {
828+
clauses.add(nodePiece(configuration));
829+
}
830+
831831
// Include the `show` and `hide` clauses.
832832
for (var combinatorNode in directive.combinators) {
833833
switch (combinatorNode) {

lib/src/short/source_visitor.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,8 +1955,6 @@ class SourceVisitor extends ThrowingAstVisitor {
19551955
space();
19561956
visit(node.uri);
19571957

1958-
_visitConfigurations(node.configurations);
1959-
19601958
if (node.asKeyword != null) {
19611959
soloSplit();
19621960
token(node.deferredKeyword, after: space);
@@ -1965,6 +1963,7 @@ class SourceVisitor extends ThrowingAstVisitor {
19651963
visit(node.prefix);
19661964
}
19671965

1966+
_visitConfigurations(node.configurations);
19681967
_visitCombinators(node.combinators);
19691968
});
19701969
}

test/short/regression/1500/1544.unit

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
>>>
2+
import 'package:archive/archive.dart' as archive
3+
if (dart.library.io) 'package:archive/archive_io.dart';
4+
import 'package:flutter/services.dart';
5+
import 'package:fuzzy/web/e621/e621.dart';
6+
import 'package:fuzzy/web/e621/models/tag_d_b.dart';
7+
import 'package:http/http.dart' as http;
8+
import 'package:j_util/e621.dart' as e621;
9+
import 'package:fuzzy/log_management.dart' as lm;
10+
import 'package:j_util/j_util_full.dart';
11+
import 'package:flutter/foundation.dart';
12+
13+
// #region Logger
14+
lm.Printer get _print => lRecord.print;
15+
lm.FileLogger get _logger => lRecord.logger;
16+
// ignore: unnecessary_late
17+
late final lRecord = lm.generateLogger("TagDbImport");
18+
// #endregion Logger
19+
const bool DO_NOT_USE_TAG_DB = true;
20+
final tagDb = LateFinal<TagDB>();
21+
Future<TagDB> _core(String vf) {
22+
_print("Tag Database Decompressed!");
23+
return TagDB.makeFromCsvString(vf);
24+
}
25+
Future<TagDB> _androidCallback(http.StreamedResponse value) {
26+
return decompressGzPlainTextStream(value).then(_core);
27+
}
28+
Future<TagDB> _webCallback(ByteData data) {
29+
return http.ByteStream.fromBytes(
30+
archive.GZipDecoder().decodeBuffer(archive.InputStream(data)))
31+
.bytesToString()
32+
.then(_core);
33+
}
34+
final LazyInitializer<TagDB> tagDbLazy = LazyInitializer(() async {
35+
if (Platform.isWeb) {
36+
var data = await rootBundle.load("assets/tags-2024-06-05.csv.gz");
37+
_print("Tag Database Loaded!");
38+
return compute(_webCallback, data);
39+
} else {
40+
return
41+
E621.sendRequest(e621.Api.initDbExportRequest())
42+
.then((value) => compute(_androidCallback, value));
43+
// E621ApiEndpoints.dbExportTags
44+
// .getMoreData()
45+
// .sendRequest()
46+
// .then((value) => compute(_androidCallback, value));
47+
}
48+
});
49+
<<<
50+
import 'package:archive/archive.dart' as archive
51+
if (dart.library.io) 'package:archive/archive_io.dart';
52+
import 'package:flutter/services.dart';
53+
import 'package:fuzzy/web/e621/e621.dart';
54+
import 'package:fuzzy/web/e621/models/tag_d_b.dart';
55+
import 'package:http/http.dart' as http;
56+
import 'package:j_util/e621.dart' as e621;
57+
import 'package:fuzzy/log_management.dart' as lm;
58+
import 'package:j_util/j_util_full.dart';
59+
import 'package:flutter/foundation.dart';
60+
61+
// #region Logger
62+
lm.Printer get _print => lRecord.print;
63+
lm.FileLogger get _logger => lRecord.logger;
64+
// ignore: unnecessary_late
65+
late final lRecord = lm.generateLogger("TagDbImport");
66+
// #endregion Logger
67+
const bool DO_NOT_USE_TAG_DB = true;
68+
final tagDb = LateFinal<TagDB>();
69+
Future<TagDB> _core(String vf) {
70+
_print("Tag Database Decompressed!");
71+
return TagDB.makeFromCsvString(vf);
72+
}
73+
74+
Future<TagDB> _androidCallback(http.StreamedResponse value) {
75+
return decompressGzPlainTextStream(value).then(_core);
76+
}
77+
78+
Future<TagDB> _webCallback(ByteData data) {
79+
return http.ByteStream.fromBytes(
80+
archive.GZipDecoder().decodeBuffer(archive.InputStream(data)))
81+
.bytesToString()
82+
.then(_core);
83+
}
84+
85+
final LazyInitializer<TagDB> tagDbLazy = LazyInitializer(() async {
86+
if (Platform.isWeb) {
87+
var data = await rootBundle.load("assets/tags-2024-06-05.csv.gz");
88+
_print("Tag Database Loaded!");
89+
return compute(_webCallback, data);
90+
} else {
91+
return E621
92+
.sendRequest(e621.Api.initDbExportRequest())
93+
.then((value) => compute(_androidCallback, value));
94+
// E621ApiEndpoints.dbExportTags
95+
// .getMoreData()
96+
// .sendRequest()
97+
// .then((value) => compute(_androidCallback, value));
98+
}
99+
});

test/short/whitespace/directives.unit

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,9 @@ export 'a' if (b.c == 'd') 'e';
6565
>>> part-of with uri
6666
part of'uri.dart' ;
6767
<<<
68-
part of 'uri.dart';
68+
part of 'uri.dart';
69+
>>> Both configuration and prefix.
70+
import 'foo.dart' as foo if (config == 'value') 'other.dart';
71+
<<<
72+
import 'foo.dart' as foo
73+
if (config == 'value') 'other.dart';

test/tall/regression/1500/1544.unit

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
>>>
2+
import 'package:archive/archive.dart' as archive
3+
if (dart.library.io) 'package:archive/archive_io.dart';
4+
import 'package:flutter/services.dart';
5+
import 'package:fuzzy/web/e621/e621.dart';
6+
import 'package:fuzzy/web/e621/models/tag_d_b.dart';
7+
import 'package:http/http.dart' as http;
8+
import 'package:j_util/e621.dart' as e621;
9+
import 'package:fuzzy/log_management.dart' as lm;
10+
import 'package:j_util/j_util_full.dart';
11+
import 'package:flutter/foundation.dart';
12+
13+
// #region Logger
14+
lm.Printer get _print => lRecord.print;
15+
lm.FileLogger get _logger => lRecord.logger;
16+
// ignore: unnecessary_late
17+
late final lRecord = lm.generateLogger("TagDbImport");
18+
// #endregion Logger
19+
const bool DO_NOT_USE_TAG_DB = true;
20+
final tagDb = LateFinal<TagDB>();
21+
Future<TagDB> _core(String vf) {
22+
_print("Tag Database Decompressed!");
23+
return TagDB.makeFromCsvString(vf);
24+
}
25+
Future<TagDB> _androidCallback(http.StreamedResponse value) {
26+
return decompressGzPlainTextStream(value).then(_core);
27+
}
28+
Future<TagDB> _webCallback(ByteData data) {
29+
return http.ByteStream.fromBytes(
30+
archive.GZipDecoder().decodeBuffer(archive.InputStream(data)))
31+
.bytesToString()
32+
.then(_core);
33+
}
34+
final LazyInitializer<TagDB> tagDbLazy = LazyInitializer(() async {
35+
if (Platform.isWeb) {
36+
var data = await rootBundle.load("assets/tags-2024-06-05.csv.gz");
37+
_print("Tag Database Loaded!");
38+
return compute(_webCallback, data);
39+
} else {
40+
return
41+
E621.sendRequest(e621.Api.initDbExportRequest())
42+
.then((value) => compute(_androidCallback, value));
43+
// E621ApiEndpoints.dbExportTags
44+
// .getMoreData()
45+
// .sendRequest()
46+
// .then((value) => compute(_androidCallback, value));
47+
}
48+
});
49+
<<<
50+
import 'package:archive/archive.dart'
51+
as archive
52+
if (dart.library.io) 'package:archive/archive_io.dart';
53+
import 'package:flutter/services.dart';
54+
import 'package:fuzzy/web/e621/e621.dart';
55+
import 'package:fuzzy/web/e621/models/tag_d_b.dart';
56+
import 'package:http/http.dart' as http;
57+
import 'package:j_util/e621.dart' as e621;
58+
import 'package:fuzzy/log_management.dart' as lm;
59+
import 'package:j_util/j_util_full.dart';
60+
import 'package:flutter/foundation.dart';
61+
62+
// #region Logger
63+
lm.Printer get _print => lRecord.print;
64+
lm.FileLogger get _logger => lRecord.logger;
65+
// ignore: unnecessary_late
66+
late final lRecord = lm.generateLogger("TagDbImport");
67+
// #endregion Logger
68+
const bool DO_NOT_USE_TAG_DB = true;
69+
final tagDb = LateFinal<TagDB>();
70+
Future<TagDB> _core(String vf) {
71+
_print("Tag Database Decompressed!");
72+
return TagDB.makeFromCsvString(vf);
73+
}
74+
75+
Future<TagDB> _androidCallback(http.StreamedResponse value) {
76+
return decompressGzPlainTextStream(value).then(_core);
77+
}
78+
79+
Future<TagDB> _webCallback(ByteData data) {
80+
return http.ByteStream.fromBytes(
81+
archive.GZipDecoder().decodeBuffer(archive.InputStream(data)),
82+
).bytesToString().then(_core);
83+
}
84+
85+
final LazyInitializer<TagDB> tagDbLazy = LazyInitializer(() async {
86+
if (Platform.isWeb) {
87+
var data = await rootBundle.load("assets/tags-2024-06-05.csv.gz");
88+
_print("Tag Database Loaded!");
89+
return compute(_webCallback, data);
90+
} else {
91+
return E621
92+
.sendRequest(e621.Api.initDbExportRequest())
93+
.then((value) => compute(_androidCallback, value));
94+
// E621ApiEndpoints.dbExportTags
95+
// .getMoreData()
96+
// .sendRequest()
97+
// .then((value) => compute(_androidCallback, value));
98+
}
99+
});

test/tall/top_level/import.unit

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,10 @@ import 'some/uri.dart' if (config.name.debug == 'string') 'c';
6767
<<<
6868
import 'some/uri.dart'
6969
if (config.name.debug ==
70-
'string') 'c';
70+
'string') 'c';
71+
>>> Both configuration and prefix.
72+
import 'foo.dart' as foo if (config == 'value') 'other.dart';
73+
<<<
74+
import 'foo.dart'
75+
as foo
76+
if (config == 'value') 'other.dart';

0 commit comments

Comments
 (0)