From 3f275d858fa9d3fe0ffa75cc9b2df945377e57b8 Mon Sep 17 00:00:00 2001 From: Srujan Gaddam Date: Wed, 3 Jan 2024 01:00:19 +0000 Subject: [PATCH] [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 --- .../js_interop_unsafe/js_interop_unsafe.dart | 29 +++++++++++-------- tests/lib/js_interop_unsafe/basic_test.dart | 8 ++--- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/sdk/lib/js_interop_unsafe/js_interop_unsafe.dart b/sdk/lib/js_interop_unsafe/js_interop_unsafe.dart index 1e14491fe1f..6a4df132137 100644 --- a/sdk/lib/js_interop_unsafe/js_interop_unsafe.dart +++ b/sdk/lib/js_interop_unsafe/js_interop_unsafe.dart @@ -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); diff --git a/tests/lib/js_interop_unsafe/basic_test.dart b/tests/lib/js_interop_unsafe/basic_test.dart index 8391fa199d4..adf341d8e03 100644 --- a/tests/lib/js_interop_unsafe/basic_test.dart +++ b/tests/lib/js_interop_unsafe/basic_test.dart @@ -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);