Allow dart:_wasm to be imported in appropriate packages

Change-Id: I606caf26e387b445662e8af9362e992f228d52c2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/326727
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index 006f35c..3f6872f 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -3228,13 +3228,11 @@
 
   /// 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 @@
     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 a6bc606..0a1baf2 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 @@
   ],
 );
 
+final MockSdkLibrary _LIB_WASM = MockSdkLibrary('_wasm', [
+  MockSdkLibraryUnit('_wasm/wasm.dart', ''),
+]);
+
 final List<MockSdkLibrary> _LIBRARIES = [
   _LIB_CORE,
   _LIB_ASYNC,
@@ -1362,6 +1366,7 @@
   _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 44d02df..6b2b14e 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 @@
       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');
+  }
 }