diff --git a/CHANGELOG.md b/CHANGELOG.md index 211bba48..3ed7fb0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ flag (#361). * Preserve type parameters on old-style function-typed formals that also use `this.` or `super.` (#1321). +* Correctly format imports with both `as` and `if` clauses (#1544). * Remove temporary work around for analyzer 6.2.0 from dart_style 2.3.6. * Require `package:analyzer` `>=6.5.0 <7.0.0`. diff --git a/lib/src/front_end/piece_factory.dart b/lib/src/front_end/piece_factory.dart index 34143517..337f51b9 100644 --- a/lib/src/front_end/piece_factory.dart +++ b/lib/src/front_end/piece_factory.dart @@ -812,13 +812,8 @@ mixin PieceFactory { pieces.visit(directive.uri); }); - // Include any `if` clauses. - var clauses = []; - for (var configuration in directive.configurations) { - clauses.add(nodePiece(configuration)); - } - // Include the `as` clause. + var clauses = []; if (asKeyword != null) { clauses.add(pieces.build(() { pieces.token(deferredKeyword, spaceAfter: true); @@ -828,6 +823,11 @@ mixin PieceFactory { })); } + // Include any `if` clauses. + for (var configuration in directive.configurations) { + clauses.add(nodePiece(configuration)); + } + // Include the `show` and `hide` clauses. for (var combinatorNode in directive.combinators) { switch (combinatorNode) { diff --git a/lib/src/short/source_visitor.dart b/lib/src/short/source_visitor.dart index b9deb505..3102b613 100644 --- a/lib/src/short/source_visitor.dart +++ b/lib/src/short/source_visitor.dart @@ -1955,8 +1955,6 @@ class SourceVisitor extends ThrowingAstVisitor { space(); visit(node.uri); - _visitConfigurations(node.configurations); - if (node.asKeyword != null) { soloSplit(); token(node.deferredKeyword, after: space); @@ -1965,6 +1963,7 @@ class SourceVisitor extends ThrowingAstVisitor { visit(node.prefix); } + _visitConfigurations(node.configurations); _visitCombinators(node.combinators); }); } diff --git a/test/short/regression/1500/1544.unit b/test/short/regression/1500/1544.unit new file mode 100644 index 00000000..23d3cd90 --- /dev/null +++ b/test/short/regression/1500/1544.unit @@ -0,0 +1,99 @@ +>>> +import 'package:archive/archive.dart' as archive + if (dart.library.io) 'package:archive/archive_io.dart'; +import 'package:flutter/services.dart'; +import 'package:fuzzy/web/e621/e621.dart'; +import 'package:fuzzy/web/e621/models/tag_d_b.dart'; +import 'package:http/http.dart' as http; +import 'package:j_util/e621.dart' as e621; +import 'package:fuzzy/log_management.dart' as lm; +import 'package:j_util/j_util_full.dart'; +import 'package:flutter/foundation.dart'; + +// #region Logger +lm.Printer get _print => lRecord.print; +lm.FileLogger get _logger => lRecord.logger; +// ignore: unnecessary_late +late final lRecord = lm.generateLogger("TagDbImport"); +// #endregion Logger +const bool DO_NOT_USE_TAG_DB = true; +final tagDb = LateFinal(); +Future _core(String vf) { + _print("Tag Database Decompressed!"); + return TagDB.makeFromCsvString(vf); +} +Future _androidCallback(http.StreamedResponse value) { + return decompressGzPlainTextStream(value).then(_core); +} +Future _webCallback(ByteData data) { + return http.ByteStream.fromBytes( + archive.GZipDecoder().decodeBuffer(archive.InputStream(data))) + .bytesToString() + .then(_core); +} +final LazyInitializer tagDbLazy = LazyInitializer(() async { + if (Platform.isWeb) { + var data = await rootBundle.load("assets/tags-2024-06-05.csv.gz"); + _print("Tag Database Loaded!"); + return compute(_webCallback, data); + } else { + return + E621.sendRequest(e621.Api.initDbExportRequest()) + .then((value) => compute(_androidCallback, value)); + // E621ApiEndpoints.dbExportTags + // .getMoreData() + // .sendRequest() + // .then((value) => compute(_androidCallback, value)); + } +}); +<<< +import 'package:archive/archive.dart' as archive + if (dart.library.io) 'package:archive/archive_io.dart'; +import 'package:flutter/services.dart'; +import 'package:fuzzy/web/e621/e621.dart'; +import 'package:fuzzy/web/e621/models/tag_d_b.dart'; +import 'package:http/http.dart' as http; +import 'package:j_util/e621.dart' as e621; +import 'package:fuzzy/log_management.dart' as lm; +import 'package:j_util/j_util_full.dart'; +import 'package:flutter/foundation.dart'; + +// #region Logger +lm.Printer get _print => lRecord.print; +lm.FileLogger get _logger => lRecord.logger; +// ignore: unnecessary_late +late final lRecord = lm.generateLogger("TagDbImport"); +// #endregion Logger +const bool DO_NOT_USE_TAG_DB = true; +final tagDb = LateFinal(); +Future _core(String vf) { + _print("Tag Database Decompressed!"); + return TagDB.makeFromCsvString(vf); +} + +Future _androidCallback(http.StreamedResponse value) { + return decompressGzPlainTextStream(value).then(_core); +} + +Future _webCallback(ByteData data) { + return http.ByteStream.fromBytes( + archive.GZipDecoder().decodeBuffer(archive.InputStream(data))) + .bytesToString() + .then(_core); +} + +final LazyInitializer tagDbLazy = LazyInitializer(() async { + if (Platform.isWeb) { + var data = await rootBundle.load("assets/tags-2024-06-05.csv.gz"); + _print("Tag Database Loaded!"); + return compute(_webCallback, data); + } else { + return E621 + .sendRequest(e621.Api.initDbExportRequest()) + .then((value) => compute(_androidCallback, value)); + // E621ApiEndpoints.dbExportTags + // .getMoreData() + // .sendRequest() + // .then((value) => compute(_androidCallback, value)); + } +}); \ No newline at end of file diff --git a/test/short/whitespace/directives.unit b/test/short/whitespace/directives.unit index e284e65a..85116f33 100644 --- a/test/short/whitespace/directives.unit +++ b/test/short/whitespace/directives.unit @@ -65,4 +65,9 @@ export 'a' if (b.c == 'd') 'e'; >>> part-of with uri part of'uri.dart' ; <<< -part of 'uri.dart'; \ No newline at end of file +part of 'uri.dart'; +>>> Both configuration and prefix. +import 'foo.dart' as foo if (config == 'value') 'other.dart'; +<<< +import 'foo.dart' as foo + if (config == 'value') 'other.dart'; \ No newline at end of file diff --git a/test/tall/regression/1500/1544.unit b/test/tall/regression/1500/1544.unit new file mode 100644 index 00000000..ce44ffdc --- /dev/null +++ b/test/tall/regression/1500/1544.unit @@ -0,0 +1,99 @@ +>>> +import 'package:archive/archive.dart' as archive + if (dart.library.io) 'package:archive/archive_io.dart'; +import 'package:flutter/services.dart'; +import 'package:fuzzy/web/e621/e621.dart'; +import 'package:fuzzy/web/e621/models/tag_d_b.dart'; +import 'package:http/http.dart' as http; +import 'package:j_util/e621.dart' as e621; +import 'package:fuzzy/log_management.dart' as lm; +import 'package:j_util/j_util_full.dart'; +import 'package:flutter/foundation.dart'; + +// #region Logger +lm.Printer get _print => lRecord.print; +lm.FileLogger get _logger => lRecord.logger; +// ignore: unnecessary_late +late final lRecord = lm.generateLogger("TagDbImport"); +// #endregion Logger +const bool DO_NOT_USE_TAG_DB = true; +final tagDb = LateFinal(); +Future _core(String vf) { + _print("Tag Database Decompressed!"); + return TagDB.makeFromCsvString(vf); +} +Future _androidCallback(http.StreamedResponse value) { + return decompressGzPlainTextStream(value).then(_core); +} +Future _webCallback(ByteData data) { + return http.ByteStream.fromBytes( + archive.GZipDecoder().decodeBuffer(archive.InputStream(data))) + .bytesToString() + .then(_core); +} +final LazyInitializer tagDbLazy = LazyInitializer(() async { + if (Platform.isWeb) { + var data = await rootBundle.load("assets/tags-2024-06-05.csv.gz"); + _print("Tag Database Loaded!"); + return compute(_webCallback, data); + } else { + return + E621.sendRequest(e621.Api.initDbExportRequest()) + .then((value) => compute(_androidCallback, value)); + // E621ApiEndpoints.dbExportTags + // .getMoreData() + // .sendRequest() + // .then((value) => compute(_androidCallback, value)); + } +}); +<<< +import 'package:archive/archive.dart' + as archive + if (dart.library.io) 'package:archive/archive_io.dart'; +import 'package:flutter/services.dart'; +import 'package:fuzzy/web/e621/e621.dart'; +import 'package:fuzzy/web/e621/models/tag_d_b.dart'; +import 'package:http/http.dart' as http; +import 'package:j_util/e621.dart' as e621; +import 'package:fuzzy/log_management.dart' as lm; +import 'package:j_util/j_util_full.dart'; +import 'package:flutter/foundation.dart'; + +// #region Logger +lm.Printer get _print => lRecord.print; +lm.FileLogger get _logger => lRecord.logger; +// ignore: unnecessary_late +late final lRecord = lm.generateLogger("TagDbImport"); +// #endregion Logger +const bool DO_NOT_USE_TAG_DB = true; +final tagDb = LateFinal(); +Future _core(String vf) { + _print("Tag Database Decompressed!"); + return TagDB.makeFromCsvString(vf); +} + +Future _androidCallback(http.StreamedResponse value) { + return decompressGzPlainTextStream(value).then(_core); +} + +Future _webCallback(ByteData data) { + return http.ByteStream.fromBytes( + archive.GZipDecoder().decodeBuffer(archive.InputStream(data)), + ).bytesToString().then(_core); +} + +final LazyInitializer tagDbLazy = LazyInitializer(() async { + if (Platform.isWeb) { + var data = await rootBundle.load("assets/tags-2024-06-05.csv.gz"); + _print("Tag Database Loaded!"); + return compute(_webCallback, data); + } else { + return E621 + .sendRequest(e621.Api.initDbExportRequest()) + .then((value) => compute(_androidCallback, value)); + // E621ApiEndpoints.dbExportTags + // .getMoreData() + // .sendRequest() + // .then((value) => compute(_androidCallback, value)); + } +}); \ No newline at end of file diff --git a/test/tall/top_level/import.unit b/test/tall/top_level/import.unit index 80ad7866..ee1830e9 100644 --- a/test/tall/top_level/import.unit +++ b/test/tall/top_level/import.unit @@ -67,4 +67,10 @@ import 'some/uri.dart' if (config.name.debug == 'string') 'c'; <<< import 'some/uri.dart' if (config.name.debug == - 'string') 'c'; \ No newline at end of file + 'string') 'c'; +>>> Both configuration and prefix. +import 'foo.dart' as foo if (config == 'value') 'other.dart'; +<<< +import 'foo.dart' + as foo + if (config == 'value') 'other.dart'; \ No newline at end of file