Skip to content

Commit d45f91b

Browse files
authored
Merge pull request #444 from Workiva/fixed_issue_with_organize_imports_and_dart_comment
FEDX-1952: Fixed bug with organizeDirectives and LanguageVersion comment
2 parents 18965fe + 27ed430 commit d45f91b

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

lib/src/utils/organize_directives/organize_directives.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,15 @@ void _assignCommentsBeforeTokenToNamespace(
133133
for (Token? comment = token.precedingComments;
134134
comment != null;
135135
comment = comment.next) {
136+
// the LanguageVersionToken (`// @dart=2.11`) must stay where it is at
137+
// the top of the file. Do not assign this to any namespace
138+
if (comment is LanguageVersionToken) continue;
139+
136140
if (_commentIsOnSameLineAsNamespace(
137-
comment, prevNamespace, sourceFileContents)) {
141+
comment,
142+
prevNamespace,
143+
sourceFileContents,
144+
)) {
138145
prevNamespace!.afterComments.add(comment);
139146
} else if (currNamespace != null) {
140147
currNamespace.beforeComments.add(comment);

test/tools/fixtures/organize_directives/directives.dart

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,11 @@ const unorganized19 = '''
569569
/* Multi-line comment 3 */
570570
import 'dart:html';
571571
/* Multi-line comment 4 */
572-
/*
573-
Multi-line comment 5
572+
/*
573+
Multi-line comment 5
574574
*/
575575
import 'dart:typed_data';
576-
/*
576+
/*
577577
Multi-line comment 1
578578
*/
579579
/* Multi-line comment 2 */
@@ -585,16 +585,16 @@ void main() {
585585
''';
586586

587587
const organized19 = '''
588-
/*
588+
/*
589589
Multi-line comment 1
590590
*/
591591
/* Multi-line comment 2 */
592592
import 'dart:async';
593593
/* Multi-line comment 3 */
594594
import 'dart:html';
595595
/* Multi-line comment 4 */
596-
/*
597-
Multi-line comment 5
596+
/*
597+
Multi-line comment 5
598598
*/
599599
import 'dart:typed_data';
600600
@@ -869,3 +869,18 @@ export 'package:dart_dev/dart_dev.dart'
869869
Type3, // comment
870870
Type4;
871871
''';
872+
873+
const unorganized32 = '''
874+
// @dart=2.11
875+
876+
import 'package:c_package/c_package.dart';
877+
878+
import 'package:a_package/a_package.dart';
879+
''';
880+
881+
const organized32 = '''
882+
// @dart=2.11
883+
884+
import 'package:a_package/a_package.dart';
885+
import 'package:c_package/c_package.dart';
886+
''';

test/utils/organize_imports/organize_directives_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ void main() {
108108
organized30,
109109
),
110110
_TestCase('31. comments with exports', unorganized31, organized31),
111+
_TestCase('32. with dart version comment', unorganized32, organized32),
111112
];
112113

113114
group('organizeDirectives', () {

0 commit comments

Comments
 (0)