[dart:js_interop_unsafe] Add has helper

Shorthand of hasProperty for the common case. Reworded some docs
to be less strict about not using this library (since has and
hasProperty have no good alternatives).

CoreLibraryReviewExempt: Backend-specific library.
Change-Id: I7e82385fd18ff4fbc83266e9b08c9d854ca62a78
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/343681
Reviewed-by: Sigmund Cherem <sigmund@google.com>
This commit is contained in:
Srujan Gaddam 2024-01-03 01:00:19 +00:00 committed by Commit Queue
parent acbbfd60fb
commit 3f275d858f
2 changed files with 21 additions and 16 deletions

View file

@ -4,21 +4,23 @@
/// Utility methods to manipulate JS objects dynamically.
///
/// This library is meant to be used when the names of properties or methods are
/// not known statically. This library is similar to 'dart:js_util', except
/// the methods here are extension methods that use JS types. This enables
/// support with dart2wasm.
/// This library is typically meant to be used when the names of properties or
/// methods are not known statically. This library is similar to 'dart:js_util',
/// except the methods here are extension methods that use JS types. This
/// enables support with dart2wasm.
///
/// In general, we expect people to use 'dart:js_interop' and that this library
/// will be rarely used. Prefer to write JS interop interfaces and external
/// static interop members using 'dart:js_interop'. The APIs in this library are
/// meant to work around issues and help with migration from older JS interop
/// libraries like 'dart:js'.
/// will be less commonly used. Prefer to write JS interop interfaces and
/// external static interop members using 'dart:js_interop'. The APIs in this
/// library are generally meant to work around issues and help with migration
/// from older JS interop libraries like 'dart:js'.
///
/// As the name suggests, usage of this library is considered unsafe. This means
/// that safe usage of these methods cannot necessarily be verified statically.
/// Therefore, they should be used cautiously and only when the same effect
/// cannot be achieved with static interop.
/// As the name suggests, usage of this library *can* be unsafe. This means that
/// safe usage of these methods cannot necessarily be verified statically.
/// Prefer using statically analyzable values like constants or literals for
/// property or method names so that usage can be verified. This library should
/// be used cautiously and only when the same effect cannot be achieved with
/// static interop.
///
/// {@category Web}
library dart.js_interop_unsafe;
@ -26,6 +28,9 @@ library dart.js_interop_unsafe;
import 'dart:js_interop';
extension JSObjectUnsafeUtilExtension on JSObject {
/// Shorthand helper to check for String properties.
bool has(String property) => hasProperty(property.toJS).toDart;
/// Whether or not this [JSObject] has a given property.
external JSBoolean hasProperty(JSAny property);

View file

@ -14,17 +14,17 @@ external void eval(String code);
void createObjectTest() {
JSObject o = JSObject();
void testHasGetSet(String property, String? value) {
// []/[]=
Expect.isFalse(o.hasProperty(property.toJS).toDart);
// has/[]/[]=
Expect.isFalse(o.has(property));
o[property] = value?.toJS;
Expect.isTrue(o.hasProperty(property.toJS).toDart);
Expect.isTrue(o.has(property));
Expect.equals(value, (o[property] as JSString?)?.toDart);
Expect.isTrue(o.delete(property.toJS).toDart);
// Weirdly enough, delete almost always returns true.
Expect.isTrue(o.delete(property.toJS).toDart);
// getProperty/setProperty
// hasProperty/getProperty/setProperty
Expect.isFalse(o.hasProperty(property.toJS).toDart);
o.setProperty(property.toJS, value?.toJS);
Expect.isTrue(o.hasProperty(property.toJS).toDart);