diff --git a/LibTest/js_interop/staticInterop_A01_t01.dart b/LibTest/js_interop/staticInterop_A01_t01.dart new file mode 100644 index 0000000000..cbf0bcd10c --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t01.dart @@ -0,0 +1,27 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks it is a compile-time error if a class is annotated with +/// `@staticInterop` but has no `@JS()` annotation. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; + +@staticInterop +class C { +// ^ +// [analyzer] unspecified +// [web] unspecified +} + +main() { + print(C); +} diff --git a/LibTest/js_interop/staticInterop_A01_t02.dart b/LibTest/js_interop/staticInterop_A01_t02.dart new file mode 100644 index 0000000000..e1a93c5b98 --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t02.dart @@ -0,0 +1,38 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks it is a compile-time error if an enum is annotated with +/// `@staticInterop`. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; + +@staticInterop +enum E1 { +// ^^ +// [analyzer] unspecified +// [web] unspecified + e0; +} + +@staticInterop +@JS() +enum E2 { +// ^^ +// [analyzer] unspecified +// [web] unspecified + e0; +} + +main() { + print(E1); + print(E2); +} diff --git a/LibTest/js_interop/staticInterop_A01_t03.dart b/LibTest/js_interop/staticInterop_A01_t03.dart new file mode 100644 index 0000000000..43f9d2dd1b --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t03.dart @@ -0,0 +1,34 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a compile-time error if an extension is +/// annotated with `@staticInterop`. +/// @author sgrekhov22@gmail.com +/// @issue 61119, 61124 + +import 'dart:js_interop'; + +class C {} + +@staticInterop +@JS() +extension Ext on C { +// ^^^ +// [analyzer] unspecified +// [web] unspecified + external String getString(); + external int getNumber(); + external bool getBool(); +} + +main() { + print(C); +} diff --git a/LibTest/js_interop/staticInterop_A01_t04.dart b/LibTest/js_interop/staticInterop_A01_t04.dart new file mode 100644 index 0000000000..cc8efeb452 --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t04.dart @@ -0,0 +1,37 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a warning if a mixin is annotated with +/// `@staticInterop`. +/// @author sgrekhov22@gmail.com +/// @issue 61124 + +import 'dart:js_interop'; + +@staticInterop +mixin M1 { +// ^^ +// [analyzer] unspecified +// [web] unspecified +} + +@staticInterop +@JS() +mixin M2 { +// ^^ +// [analyzer] unspecified +// [web] unspecified +} + +main() { + print(M1); + print(M2); +} diff --git a/LibTest/js_interop/staticInterop_A01_t05.dart b/LibTest/js_interop/staticInterop_A01_t05.dart new file mode 100644 index 0000000000..bffdd6e1cb --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t05.dart @@ -0,0 +1,50 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a warning if an extension type is annotated +/// with `@staticInterop`. +/// @author sgrekhov22@gmail.com +/// @issue 61124 + +import 'dart:js_interop'; + +@staticInterop +extension type ET1(JSObject o) {} +// ^^^ +// [analyzer] unspecified +// [web] unspecified + +@staticInterop +@JS() +extension type ET2(JSObject o) {} +// ^^^ +// [analyzer] unspecified +// [web] unspecified + +@staticInterop +extension type ET3(JSObject o) implements JSObject {} +// ^^^ +// [analyzer] unspecified +// [web] unspecified + +@staticInterop +@JS() +extension type ET4(JSObject o) implements JSObject {} +// ^^^ +// [analyzer] unspecified +// [web] unspecified + +main() { + print(ET1); + print(ET2); + print(ET3); + print(ET4); +} diff --git a/LibTest/js_interop/staticInterop_A01_t06.dart b/LibTest/js_interop/staticInterop_A01_t06.dart new file mode 100644 index 0000000000..403abb0c1d --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t06.dart @@ -0,0 +1,48 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a warning if a declaration that isn't a JS +/// interop class is annotated with `@staticInterop`. +/// @author sgrekhov22@gmail.com +/// @issue 61124 + +import 'dart:js_interop'; + +@staticInterop +int variable = 42; +// ^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + +@staticInterop +String get getter => "getter"; +// ^^^^^^ +// [analyzer] unspecified +// [web] unspecified + +@staticInterop +String func() => "function"; +// ^^^^ +// [analyzer] unspecified +// [web] unspecified + +@staticInterop +void set setter(String _) {} +// ^^^^^^ +// [analyzer] unspecified +// [web] unspecified + +main() { + print(variable); + print(getter); + print(func); + setter = ""; +} diff --git a/LibTest/js_interop/staticInterop_A01_t07.dart b/LibTest/js_interop/staticInterop_A01_t07.dart new file mode 100644 index 0000000000..949c901d84 --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t07.dart @@ -0,0 +1,31 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a warning if a local declaration is annotated +/// with `@staticInterop`. +/// @author sgrekhov22@gmail.com +/// @issue 61124 + +import 'dart:js_interop'; + +main() { + @staticInterop + int variable = 42; +// ^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + String func() => "function"; +// ^^^^ +// [analyzer] unspecified +// [web] unspecified +} diff --git a/LibTest/js_interop/staticInterop_A01_t08.dart b/LibTest/js_interop/staticInterop_A01_t08.dart new file mode 100644 index 0000000000..380e43a247 --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t08.dart @@ -0,0 +1,83 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a warning if a class member is annotated with +/// `@staticInterop`. +/// @author sgrekhov22@gmail.com +/// @issue 61124 + +import 'dart:js_interop'; + +class C { + @staticInterop + C(); +//^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + factory C.f() = C; +// ^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + int instanceVariable = 42; +// ^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + String get instanceGetter => "instanceGetter"; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + String instanceMethod() => "instanceMethod"; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + void set instanceSetter(String _) {} +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static int staticVariable = 42; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static String get staticGetter => "staticGetter"; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static String staticMethod() => "staticMethod"; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static void set staticSetter(String _) {} +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified +} + +main() { + print(C); +} diff --git a/LibTest/js_interop/staticInterop_A01_t09.dart b/LibTest/js_interop/staticInterop_A01_t09.dart new file mode 100644 index 0000000000..17de035f15 --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t09.dart @@ -0,0 +1,71 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a warning if a mixin member is annotated with +/// `@staticInterop`. +/// @author sgrekhov22@gmail.com +/// @issue 61124 + +import 'dart:js_interop'; + +mixin M { + @staticInterop + int instanceVariable = 42; +// ^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + String get instanceGetter => "instanceGetter"; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + String instanceMethod() => "instanceMethod"; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + void set instanceSetter(String _) {} +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static int staticVariable = 42; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static String get staticGetter => "staticGetter"; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static String staticMethod() => "staticMethod"; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static void set staticSetter(String _) {} +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified +} + +main() { + print(M); +} diff --git a/LibTest/js_interop/staticInterop_A01_t10.dart b/LibTest/js_interop/staticInterop_A01_t10.dart new file mode 100644 index 0000000000..72414ff58b --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t10.dart @@ -0,0 +1,79 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a warning if an enum member is annotated with +/// `@staticInterop`. +/// @author sgrekhov22@gmail.com +/// @issue 61124 + +import 'dart:js_interop'; + +enum E { + e0; + + @staticInterop + const E(); +//^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + final int instanceVariable = 42; +// ^^^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + String get instanceGetter => "instanceGetter"; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + String instanceMethod() => "instanceMethod"; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + void set instanceSetter(String _) {} +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static int staticVariable = 42; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static String get staticGetter => "staticGetter"; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static String staticMethod() => "staticMethod"; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static void set staticSetter(String _) {} +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified +} + +main() { + print(E); +} diff --git a/LibTest/js_interop/staticInterop_A01_t11.dart b/LibTest/js_interop/staticInterop_A01_t11.dart new file mode 100644 index 0000000000..ea8a90bada --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t11.dart @@ -0,0 +1,77 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a warning if an extension type member is +/// annotated with `@staticInterop`. +/// @author sgrekhov22@gmail.com +/// @issue 61124 + +import 'dart:js_interop'; + +extension type ET(JSObject e) { + @staticInterop + ET.n(this.e); +//^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + factory ET.f(JSObject e) = ET; +// ^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + String get instanceGetter => "instanceGetter"; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + String instanceMethod() => "instanceMethod"; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + void set instanceSetter(String _) {} +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static int staticVariable = 42; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static String get staticGetter => "staticGetter"; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static String staticMethod() => "staticMethod"; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static void set staticSetter(String _) {} +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified +} + +main() { + print(ET); +} diff --git a/LibTest/js_interop/staticInterop_A01_t12.dart b/LibTest/js_interop/staticInterop_A01_t12.dart new file mode 100644 index 0000000000..bcdc8d2d8d --- /dev/null +++ b/LibTest/js_interop/staticInterop_A01_t12.dart @@ -0,0 +1,69 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a warning if an extension member is annotated +/// with `@staticInterop`. +/// @author sgrekhov22@gmail.com +/// @issue 61124 + +import 'dart:js_interop'; + +@staticInterop +@JS() +class C {} + +extension Ext on C { + @staticInterop + String get instanceGetter => "instanceGetter"; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + String instanceMethod() => "instanceMethod"; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + void set instanceSetter(String _) {} +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static int staticVariable = 42; +// ^^^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static String get staticGetter => "staticGetter"; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static String staticMethod() => "staticMethod"; +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified + + @staticInterop + static void set staticSetter(String _) {} +// ^^^^^^^^^^^^ +// [analyzer] unspecified +// [web] unspecified +} + +main() { + print(C); +} diff --git a/LibTest/js_interop/staticInterop_A02_t01.dart b/LibTest/js_interop/staticInterop_A02_t01.dart new file mode 100644 index 0000000000..d754ffa4ff --- /dev/null +++ b/LibTest/js_interop/staticInterop_A02_t01.dart @@ -0,0 +1,59 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a compile-time error if a class annotated +/// with `@staticInterop` declares any instance members. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; + +@staticInterop +@JS() +class C1 { + int x = 0; +// ^ +// [analyzer] unspecified +// [web] unspecified +} + +@staticInterop +@JS() +class C2 { + int get x => 0; +// ^ +// [analyzer] unspecified +// [web] unspecified +} + +@staticInterop +@JS() +class C3 { + int x() => 0; +// ^ +// [analyzer] unspecified +// [web] unspecified +} + +@staticInterop +@JS() +class C4 { + void set x(int _) {} +// ^ +// [analyzer] unspecified +// [web] unspecified +} + +main() { + print(C1); + print(C2); + print(C3); + print(C4); +} diff --git a/LibTest/js_interop/staticInterop_A02_t02.dart b/LibTest/js_interop/staticInterop_A02_t02.dart new file mode 100644 index 0000000000..0a9ac8cd50 --- /dev/null +++ b/LibTest/js_interop/staticInterop_A02_t02.dart @@ -0,0 +1,29 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is not an error if a class annotated with +/// `@staticInterop` declares static members. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; + +@staticInterop +@JS() +class C { + static String v = "v"; + static String get g => "g"; + static String m() => "m"; + static void set s(String s) {} +} + +main() { + print(C); +} diff --git a/LibTest/js_interop/staticInterop_A02_t03.dart b/LibTest/js_interop/staticInterop_A02_t03.dart new file mode 100644 index 0000000000..68f5b87bd2 --- /dev/null +++ b/LibTest/js_interop/staticInterop_A02_t03.dart @@ -0,0 +1,36 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a compile-time error if a class annotated +/// with `@staticInterop` declares any generative constructors. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; + +@staticInterop +@JS() +class C { + C(); +//^ +// [analyzer] unspecified +// [web] unspecified + + C.n(int x); +//^^^ +// [analyzer] unspecified +// [web] unspecified + + factory C.f() = C; // Factory constructors are allowed +} + +main() { + print(C); +} diff --git a/LibTest/js_interop/staticInterop_A02_t04.dart b/LibTest/js_interop/staticInterop_A02_t04.dart new file mode 100644 index 0000000000..08f09132de --- /dev/null +++ b/LibTest/js_interop/staticInterop_A02_t04.dart @@ -0,0 +1,36 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that it is a compile-time error if a class annotated +/// with `@staticInterop` declares any external generative constructors. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; + +@staticInterop +@JS() +class C { + external C(); +// ^ +// [analyzer] unspecified +// [web] unspecified + + external C.n(int x); +// ^^^ +// [analyzer] unspecified +// [web] unspecified + + external factory C.f(); // Factory constructors are allowed +} + +main() { + print(C); +} diff --git a/LibTest/js_interop/staticInterop_A03_t01.dart b/LibTest/js_interop/staticInterop_A03_t01.dart new file mode 100644 index 0000000000..8991bc2ee1 --- /dev/null +++ b/LibTest/js_interop/staticInterop_A03_t01.dart @@ -0,0 +1,50 @@ +// Copyright (c) 2025, 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. + +/// @assertion `staticInterop` enables the JS annotated class to be treated as +/// a "static" interop class. +/// +/// These classes allow interop with native types, like the ones in `dart:html`. +/// These classes should not contain any instance members, inherited or +/// otherwise, and should instead use static extension members. +/// +/// @description Checks that a class annotated with `@staticInterop` can interop +/// with JS objects. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../../Utils/expect.dart'; +import 'js_utils.dart'; + +@staticInterop +@JS() +class C { +} + +extension Ext on C { + external String getString(); + external int getNumber(); + external bool getBool(); +} + +main() { + eval(r''' + globalThis.obj = { + getString: function () { + return "I'm JS String"; + }, + getNumber: function () { + return 42; + }, + getBool: function () { + return true; + } + }; + '''); + C c = globalContext["obj"] as C; + Expect.equals("I'm JS String", c.getString()); + Expect.equals(42, c.getNumber()); + Expect.equals(true, c.getBool()); +}