Skip to content

Commit

Permalink
analyzer: Report configurations in doc imports
Browse files Browse the repository at this point in the history
Work towards #50702

Change-Id: If9c3d702f16e609e0d1f2c2bf09ec02a73cd5a54
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/323427
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
srawlins authored and Commit Queue committed Aug 30, 2023
1 parent bbc3dcb commit 734e9c3
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3428,6 +3428,8 @@ WarningCode.DEPRECATED_NEW_IN_COMMENT_REFERENCE:
status: hasFix
WarningCode.DOC_IMPORT_CANNOT_BE_DEFERRED:
status: needsFix
WarningCode.DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS:
status: needsFix
WarningCode.DUPLICATE_EXPORT:
status: needsFix
notes: |-
Expand Down
7 changes: 7 additions & 0 deletions pkg/analyzer/lib/src/error/codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6049,6 +6049,13 @@ class WarningCode extends AnalyzerErrorCode {
static const WarningCode DOC_IMPORT_CANNOT_BE_DEFERRED = WarningCode(
'DOC_IMPORT_CANNOT_BE_DEFERRED',
"Doc imports can't be deferred.",
correctionMessage: "Try removing the 'deferred' keyword.",
);

static const WarningCode DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS = WarningCode(
'DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS',
"Doc imports can't have configurations.",
correctionMessage: "Try removing the configurations.",
);

/// Duplicate exports.
Expand Down
9 changes: 8 additions & 1 deletion pkg/analyzer/lib/src/error/doc_import_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class DocImportVerifier {
deferredKeyword,
);
}
// TODO(srawlins): Report warnings for configurations.
var configurations = node.import.configurations;
if (configurations.isNotEmpty) {
_errorReporter.reportErrorForOffset(
WarningCode.DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS,
configurations.first.offset,
configurations.last.end,
);
}
}
}
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/error/error_code_values.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ const List<ErrorCode> errorCodeValues = [
WarningCode.DEPRECATED_MIXIN_FUNCTION,
WarningCode.DEPRECATED_NEW_IN_COMMENT_REFERENCE,
WarningCode.DOC_IMPORT_CANNOT_BE_DEFERRED,
WarningCode.DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS,
WarningCode.DUPLICATE_EXPORT,
WarningCode.DUPLICATE_HIDDEN_NAME,
WarningCode.DUPLICATE_IGNORE,
Expand Down
11 changes: 8 additions & 3 deletions pkg/analyzer/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21545,9 +21545,6 @@ StaticWarningCode:
}
```
WarningCode:
DOC_IMPORT_CANNOT_BE_DEFERRED:
problemMessage: "Doc imports can't be deferred."
hasPublishedDocs: false
ARGUMENT_TYPE_NOT_ASSIGNABLE_TO_ERROR_HANDLER:
problemMessage: "The argument type '{0}' can't be assigned to the parameter type '{1} Function(Object)' or '{1} Function(Object, StackTrace)'."
hasPublishedDocs: true
Expand Down Expand Up @@ -22125,6 +22122,14 @@ WarningCode:
C.c();
}
```
DOC_IMPORT_CANNOT_BE_DEFERRED:
problemMessage: "Doc imports can't be deferred."
correctionMessage: Try removing the 'deferred' keyword.
hasPublishedDocs: false
DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS:
problemMessage: "Doc imports can't have configurations."
correctionMessage: Try removing the configurations.
hasPublishedDocs: false
DUPLICATE_EXPORT:
problemMessage: Duplicate export.
correctionMessage: Try removing all but one export of the library.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/src/error/codes.g.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../dart/resolution/context_collection_resolution.dart';

void main() {
defineReflectiveSuite(() {
defineReflectiveTests(DocImportCannotHaveConfigurationsTest);
});
}

@reflectiveTest
class DocImportCannotHaveConfigurationsTest extends PubPackageResolutionTest {
test_configurations() async {
await assertErrorsInCode('''
/// @docImport 'dart:math' if (dart.library.html) 'dart:html';
class C {}
''', [
error(WarningCode.DOC_IMPORT_CANNOT_HAVE_CONFIGURATIONS, 27, 61),
]);
}

test_noConfigurations() async {
await assertNoErrorsInCode('''
/// @docImport 'dart:math';
class C {}
''');
}
}
3 changes: 3 additions & 0 deletions pkg/analyzer/test/src/diagnostics/test_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ import 'deprecated_mixin_function_test.dart' as deprecated_mixin_function;
import 'division_optimization_test.dart' as division_optimization;
import 'doc_import_cannot_be_deferred_test.dart'
as doc_import_cannot_be_deferred;
import 'doc_import_cannot_have_configurations_test.dart'
as doc_import_cannot_have_configurations;
import 'duplicate_augmentation_import_test.dart'
as duplicate_augmentation_import;
import 'duplicate_constructor_default_test.dart'
Expand Down Expand Up @@ -1015,6 +1017,7 @@ main() {
deprecated_mixin_function.main();
division_optimization.main();
doc_import_cannot_be_deferred.main();
doc_import_cannot_have_configurations.main();
duplicate_augmentation_import.main();
duplicate_constructor_default.main();
duplicate_constructor_name.main();
Expand Down

0 comments on commit 734e9c3

Please sign in to comment.