Skip to content

Commit 961aa05

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Warn about doc-import combinators and prefixes
Change-Id: I1104cc41198ce5e6db7cf01b089fc562380e8636 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/410445 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent f057e13 commit 961aa05

File tree

9 files changed

+122
-1
lines changed

9 files changed

+122
-1
lines changed

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,8 +3498,12 @@ WarningCode.DOC_DIRECTIVE_UNKNOWN:
34983498
status: noFix
34993499
WarningCode.DOC_IMPORT_CANNOT_BE_DEFERRED:
35003500
status: needsFix
3501+
WarningCode.DOC_IMPORT_CANNOT_HAVE_COMBINATORS:
3502+
status: needsFix
35013503
WarningCode.DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS:
35023504
status: needsFix
3505+
WarningCode.DOC_IMPORT_CANNOT_HAVE_PREFIX:
3506+
status: needsFix
35033507
WarningCode.DUPLICATE_EXPORT:
35043508
status: needsFix
35053509
notes: |-

pkg/analyzer/lib/src/error/codes.g.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6462,12 +6462,24 @@ class WarningCode extends ErrorCode {
64626462
correctionMessage: "Try removing the 'deferred' keyword.",
64636463
);
64646464

6465+
static const WarningCode DOC_IMPORT_CANNOT_HAVE_COMBINATORS = WarningCode(
6466+
'DOC_IMPORT_CANNOT_HAVE_COMBINATORS',
6467+
"Doc imports can't have show or hide combinators.",
6468+
correctionMessage: "Try removing the combinator.",
6469+
);
6470+
64656471
static const WarningCode DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS = WarningCode(
64666472
'DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS',
64676473
"Doc imports can't have configurations.",
64686474
correctionMessage: "Try removing the configurations.",
64696475
);
64706476

6477+
static const WarningCode DOC_IMPORT_CANNOT_HAVE_PREFIX = WarningCode(
6478+
'DOC_IMPORT_CANNOT_HAVE_PREFIX',
6479+
"Doc imports can't have prefixes.",
6480+
correctionMessage: "Try removing the prefix.",
6481+
);
6482+
64716483
/// Duplicate exports.
64726484
///
64736485
/// No parameters.

pkg/analyzer/lib/src/error/doc_comment_verifier.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ class DocCommentVerifier {
4848
errorCode: WarningCode.DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS,
4949
);
5050
}
51+
52+
// TODO(srawlins): Support combinators.
53+
var combinators = docImport.import.combinators;
54+
if (combinators.isNotEmpty) {
55+
_errorReporter.atOffset(
56+
offset: combinators.first.offset,
57+
length: combinators.last.end - combinators.first.offset,
58+
errorCode: WarningCode.DOC_IMPORT_CANNOT_HAVE_COMBINATORS,
59+
);
60+
}
61+
62+
// TODO(srawlins): Support prefixes. This was done temporarily with
63+
// https://dart-review.googlesource.com/c/sdk/+/387861, but this was
64+
// reverted as it increased memory usage.
65+
var prefix = docImport.import.prefix;
66+
if (prefix != null) {
67+
_errorReporter.atOffset(
68+
offset: prefix.offset,
69+
length: prefix.end - prefix.offset,
70+
errorCode: WarningCode.DOC_IMPORT_CANNOT_HAVE_PREFIX,
71+
);
72+
}
5173
}
5274

5375
void validateArgumentCount(DocDirectiveTag tag) {

pkg/analyzer/lib/src/error/error_code_values.g.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,9 @@ const List<ErrorCode> errorCodeValues = [
999999
WarningCode.DOC_DIRECTIVE_MISSING_TWO_ARGUMENTS,
10001000
WarningCode.DOC_DIRECTIVE_UNKNOWN,
10011001
WarningCode.DOC_IMPORT_CANNOT_BE_DEFERRED,
1002+
WarningCode.DOC_IMPORT_CANNOT_HAVE_COMBINATORS,
10021003
WarningCode.DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS,
1004+
WarningCode.DOC_IMPORT_CANNOT_HAVE_PREFIX,
10031005
WarningCode.DUPLICATE_EXPORT,
10041006
WarningCode.DUPLICATE_HIDDEN_NAME,
10051007
WarningCode.DUPLICATE_IGNORE,

pkg/analyzer/messages.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23749,10 +23749,18 @@ WarningCode:
2374923749
problemMessage: "Doc imports can't be deferred."
2375023750
correctionMessage: Try removing the 'deferred' keyword.
2375123751
hasPublishedDocs: false
23752+
DOC_IMPORT_CANNOT_HAVE_COMBINATORS:
23753+
problemMessage: "Doc imports can't have show or hide combinators."
23754+
correctionMessage: Try removing the combinator.
23755+
hasPublishedDocs: false
2375223756
DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS:
2375323757
problemMessage: "Doc imports can't have configurations."
2375423758
correctionMessage: Try removing the configurations.
2375523759
hasPublishedDocs: false
23760+
DOC_IMPORT_CANNOT_HAVE_PREFIX:
23761+
problemMessage: "Doc imports can't have prefixes."
23762+
correctionMessage: Try removing the prefix.
23763+
hasPublishedDocs: false
2375623764
DUPLICATE_EXPORT:
2375723765
problemMessage: Duplicate export.
2375823766
correctionMessage: Try removing all but one export of the library.

pkg/analyzer/test/src/diagnostics/doc_import_cannot_be_deferred_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ class DocImportCannotBeDeferredTest extends PubPackageResolutionTest {
2121
class C {}
2222
''', [
2323
error(WarningCode.DOC_IMPORT_CANNOT_BE_DEFERRED, 27, 8),
24+
error(WarningCode.DOC_IMPORT_CANNOT_HAVE_PREFIX, 39, 4),
2425
]);
2526
}
2627

2728
test_notDeferred() async {
2829
await assertNoErrorsInCode('''
29-
/// @docImport 'dart:math' as math;
30+
/// @docImport 'dart:math';
3031
class C {}
3132
''');
3233
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/src/error/codes.g.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/context_collection_resolution.dart';
9+
10+
void main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(DocImportCannotHaveCombinatorsTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class DocImportCannotHaveCombinatorsTest extends PubPackageResolutionTest {
18+
test_configurations() async {
19+
await assertErrorsInCode('''
20+
/// @docImport 'dart:math' show max;
21+
class C {}
22+
''', [
23+
error(WarningCode.DOC_IMPORT_CANNOT_HAVE_COMBINATORS, 27, 8),
24+
]);
25+
}
26+
27+
test_noConfigurations() async {
28+
await assertNoErrorsInCode('''
29+
/// @docImport 'dart:math';
30+
class C {}
31+
''');
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/src/error/codes.g.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/context_collection_resolution.dart';
9+
10+
void main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(DocImportCannotHavePrefixTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class DocImportCannotHavePrefixTest extends PubPackageResolutionTest {
18+
test_configurations() async {
19+
await assertErrorsInCode('''
20+
/// @docImport 'dart:math' as math;
21+
class C {}
22+
''', [
23+
error(WarningCode.DOC_IMPORT_CANNOT_HAVE_PREFIX, 30, 4),
24+
]);
25+
}
26+
27+
test_noConfigurations() async {
28+
await assertNoErrorsInCode('''
29+
/// @docImport 'dart:math';
30+
class C {}
31+
''');
32+
}
33+
}

pkg/analyzer/test/src/diagnostics/test_all.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,12 @@ import 'doc_directive_missing_two_arguments_test.dart'
201201
as doc_directive_missing_two_arguments;
202202
import 'doc_import_cannot_be_deferred_test.dart'
203203
as doc_import_cannot_be_deferred;
204+
import 'doc_import_cannot_have_combinators_test.dart'
205+
as doc_import_cannot_have_combinators;
204206
import 'doc_import_cannot_have_configurations_test.dart'
205207
as doc_import_cannot_have_configurations;
208+
import 'doc_import_cannot_have_prefix_test.dart'
209+
as doc_import_cannot_have_prefix;
206210
import 'duplicate_constructor_default_test.dart'
207211
as duplicate_constructor_default;
208212
import 'duplicate_constructor_name_test.dart' as duplicate_constructor_name;
@@ -1060,7 +1064,9 @@ main() {
10601064
doc_directive_missing_three_arguments.main();
10611065
doc_directive_missing_two_arguments.main();
10621066
doc_import_cannot_be_deferred.main();
1067+
doc_import_cannot_have_combinators.main();
10631068
doc_import_cannot_have_configurations.main();
1069+
doc_import_cannot_have_prefix.main();
10641070
duplicate_constructor_default.main();
10651071
duplicate_constructor_name.main();
10661072
duplicate_definition.main();

0 commit comments

Comments
 (0)