diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index 006f35ca80c..3f6872ffb1f 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -3228,13 +3228,11 @@ class ErrorVerifier extends RecursiveAstVisitor /// Check that if the visiting library is not system, then any given library /// should not be SDK internal library. The [importElement] is the - /// [LibraryImportElement] retrieved from the node, if the element in the node was - /// `null`, then this method is not called - /// - /// See [CompileTimeErrorCode.IMPORT_INTERNAL_LIBRARY]. + /// [LibraryImportElement] retrieved from the node, if the element in the node + /// was `null`, then this method is not called. void _checkForImportInternalLibrary( ImportDirective directive, LibraryImportElement importElement) { - if (_isInSystemLibrary) { + if (_isInSystemLibrary || _isWasm(importElement)) { return; } @@ -5699,6 +5697,23 @@ class ErrorVerifier extends RecursiveAstVisitor return false; } + /// Return `true` if the [importElement] is the internal library `dart:_wasm` + /// and the current library is either `package:js/js.dart` or is in + /// `package:ui`. + bool _isWasm(LibraryImportElement importElement) { + var importedUri = importElement.importedLibrary?.source.uri.toString(); + if (importedUri != 'dart:_wasm') { + return false; + } + var importingUri = _currentLibrary.source.uri.toString(); + if (importingUri == 'package:js/js.dart') { + return true; + } else if (importingUri.startsWith('package:ui/')) { + return true; + } + return false; + } + /// Checks whether a `final`, `base` or `interface` modifier can be ignored. /// /// Checks whether a subclass in the current library diff --git a/pkg/analyzer/lib/src/test_utilities/mock_sdk.dart b/pkg/analyzer/lib/src/test_utilities/mock_sdk.dart index a6bc606e4aa..0a1baf2a0ce 100644 --- a/pkg/analyzer/lib/src/test_utilities/mock_sdk.dart +++ b/pkg/analyzer/lib/src/test_utilities/mock_sdk.dart @@ -1349,6 +1349,10 @@ class Point {} ], ); +final MockSdkLibrary _LIB_WASM = MockSdkLibrary('_wasm', [ + MockSdkLibraryUnit('_wasm/wasm.dart', ''), +]); + final List _LIBRARIES = [ _LIB_CORE, _LIB_ASYNC, @@ -1362,6 +1366,7 @@ final List _LIBRARIES = [ _LIB_HTML_DART2JS, _LIB_INTERCEPTORS, _LIB_INTERNAL, + _LIB_WASM, ]; /// Create a reduced approximation of Dart SDK in the [path]. diff --git a/pkg/analyzer/test/src/diagnostics/import_internal_library_test.dart b/pkg/analyzer/test/src/diagnostics/import_internal_library_test.dart index 44d02dfee05..6b2b14e1d68 100644 --- a/pkg/analyzer/test/src/diagnostics/import_internal_library_test.dart +++ b/pkg/analyzer/test/src/diagnostics/import_internal_library_test.dart @@ -28,4 +28,39 @@ import 'dart:_internal'; error(WarningCode.UNUSED_IMPORT, 7, 16), ]); } + + test_wasm_fromJs() async { + var filePath = _inPackage('js'); + newFile(filePath, ''' +import 'dart:_wasm'; +'''); + await resolveFile2(filePath); + assertErrorsInResolvedUnit(result, [ + error(WarningCode.UNUSED_IMPORT, 7, 12), + ]); + } + + test_wasm_fromUi() async { + var filePath = _inPackage('ui'); + newFile(filePath, ''' +import 'dart:_wasm'; +'''); + await resolveFile2(filePath); + assertErrorsInResolvedUnit(result, [ + error(WarningCode.UNUSED_IMPORT, 7, 12), + ]); + } + + String _inPackage(String packageName) { + var packageRoot = '$workspaceRootPath/$packageName'; + var builder = PackageConfigFileBuilder(); + builder.add( + name: packageName, + rootPath: packageRoot, + languageVersion: testPackageLanguageVersion, + ); + var path = '$packageRoot/.dart_tool/package_config.json'; + writePackageConfig(path, builder); + return convertPath('$packageRoot/lib/$packageName.dart'); + } }