Skip to content

Commit 9d3557a

Browse files
authored
#3180. Add more tests for JS interop. Part 2. (#3274)
Add more tests for JS interop. Part 2.
1 parent f589cfa commit 9d3557a

12 files changed

+652
-0
lines changed

LibTest/js_interop/JS/js_A01_t02.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,21 @@ external int f2({required int v});
2525
// [analyzer] unspecified
2626
// [web] unspecified
2727

28+
@JS()
29+
external int f3(int id, {int v});
30+
// ^
31+
// [analyzer] unspecified
32+
// [web] unspecified
33+
34+
@JS("foo")
35+
external int f4(int id, {required int v});
36+
// ^
37+
// [analyzer] unspecified
38+
// [web] unspecified
39+
2840
main() {
2941
print(f1());
3042
print(f2(v: 1));
43+
print(f3(3, v: 1));
44+
print(f4(4, v: 1));
3145
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Within an interop type, you can declare several different types
6+
/// of external interop members:
7+
/// - Constructors. When called, constructors with only positional parameters
8+
/// create a new JS object whose constructor is defined by the name of the
9+
/// extension type using `new`.
10+
///
11+
/// @description Check that it is a compile-time error to tear-off a constructor
12+
/// of a JS interop type.
13+
/// @author [email protected]
14+
15+
import 'dart:js_interop';
16+
17+
extension type ET._(JSObject v) implements JSObject {
18+
external ET(int id, String? name);
19+
external ET.fromId(int id);
20+
ET.create(this.v);
21+
}
22+
23+
@JS("ET")
24+
extension type ET2.p(JSObject v) implements JSObject {
25+
external factory ET2(int id, String? name);
26+
external factory ET2.fromId(int id);
27+
factory ET2.create(JSObject v) = ET2.p;
28+
}
29+
30+
main() {
31+
var etNew = ET.new;
32+
// ^^^^^^
33+
// [analyzer] unspecified
34+
// [web] unspecified
35+
36+
var etFromId = ET.fromId;
37+
// ^^^^^^^^^
38+
// [analyzer] unspecified
39+
// [web] unspecified
40+
41+
var etPrimary1 = ET._; // Ok, not external constructor
42+
var etPrimary2 = ET.create;
43+
44+
var et2New = ET2.new;
45+
// ^^^^^^^
46+
// [analyzer] unspecified
47+
// [web] unspecified
48+
49+
var et2fromId = ET2.fromId;
50+
// ^^^^^^^^^^
51+
// [analyzer] unspecified
52+
// [web] unspecified
53+
54+
var et2Primary1 = ET2.p; // Ok, not external constructor
55+
var et2Primary2 = ET2.create;
56+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Within an interop type, you can declare several different types
6+
/// of external interop members:
7+
/// - Constructors. When called, constructors with only positional parameters
8+
/// create a new JS object whose constructor is defined by the name of the
9+
/// extension type using `new`.
10+
///
11+
/// @description Check that a constructor of a JS interop type with positional
12+
/// parameters when called creates a new JS object whose constructor is defined
13+
/// by the name of the extension type using `new`. Test the case when JS type
14+
/// contains a static member with the same name.
15+
/// @author [email protected]
16+
17+
import 'dart:js_interop';
18+
import '../../Utils/expect.dart';
19+
import 'js_utils.dart';
20+
21+
extension type ET._(JSObject _) implements JSObject {
22+
external ET.create(int id, String? name);
23+
24+
external int? get id;
25+
external String? get name;
26+
}
27+
28+
@JS("ET")
29+
extension type ET2._(JSObject _) implements JSObject {
30+
external factory ET2.create(int id, String? name);
31+
32+
external int? get id;
33+
external String? get name;
34+
}
35+
36+
main() {
37+
eval(r'''
38+
class ET {
39+
constructor(id, name) {
40+
this._id = id;
41+
this._name = name;
42+
}
43+
static create(id, name) {
44+
return 42;
45+
}
46+
get id() {
47+
return this._id;
48+
}
49+
get name() {
50+
return this._name;
51+
}
52+
}
53+
globalThis.ET = ET;
54+
''');
55+
ET et1 = ET.create(1, "one");
56+
Expect.equals(1, et1.id);
57+
Expect.equals("one", et1.name);
58+
59+
ET2 et2 = ET2.create(2, "two");
60+
Expect.equals(2, et2.id);
61+
Expect.equals("two", et2.name);
62+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Within an interop type, you can declare several different types
6+
/// of external interop members:
7+
/// - Constructors. When called, constructors with only positional parameters
8+
/// create a new JS object whose constructor is defined by the name of the
9+
/// extension type using `new`.
10+
///
11+
/// @description Check that it is a warning if an external constructor of JS
12+
/// interop type is annotated with `@JS()` annotation.
13+
/// @author [email protected]
14+
/// @issue 61274
15+
16+
import 'dart:js_interop';
17+
import '../../Utils/expect.dart';
18+
import 'js_utils.dart';
19+
20+
extension type ET._(JSObject _) implements JSObject {
21+
@JS("create")
22+
external ET(int id, String? name);
23+
// ^^
24+
// [analyzer] unspecified
25+
// [web] unspecified
26+
27+
@JS("fromId")
28+
external ET.f(int id);
29+
// ^^^^
30+
// [analyzer] unspecified
31+
// [web] unspecified
32+
33+
}
34+
35+
@JS("ET")
36+
extension type ET2._(JSObject _) implements JSObject {
37+
@JS("")
38+
external ET2(int id, String? name);
39+
// ^^^
40+
// [analyzer] unspecified
41+
// [web] unspecified
42+
43+
@JS(null)
44+
external ET2.f(int id);
45+
// ^^^^^
46+
// [analyzer] unspecified
47+
// [web] unspecified
48+
}
49+
50+
main() {
51+
print(ET);
52+
print(ET2);
53+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Within an interop type, you can declare several different types
6+
/// of external interop members:
7+
/// - Constructors. When called, constructors with only positional parameters
8+
/// create a new JS object whose constructor is defined by the name of the
9+
/// extension type using `new`.
10+
///
11+
/// @description Check that default values of an external constructor of a JS
12+
/// interop type are ignored.
13+
/// @author [email protected]
14+
15+
import 'dart:js_interop';
16+
import '../../Utils/expect.dart';
17+
import 'js_utils.dart';
18+
19+
extension type ET._(JSObject _) implements JSObject {
20+
external ET([int? id = 0, String? name = "default"]);
21+
22+
external int? get id;
23+
external String? get name;
24+
}
25+
26+
@JS("ET")
27+
extension type ET2._(JSObject _) implements JSObject {
28+
external factory ET2([int? id = 0, String? name = "default"]);
29+
30+
external int? get id;
31+
external String? get name;
32+
}
33+
34+
main() {
35+
eval(r'''
36+
class ET {
37+
constructor(id, name) {
38+
this._id = id;
39+
this._name = name;
40+
}
41+
get id() {
42+
return this._id;
43+
}
44+
get name() {
45+
return this._name;
46+
}
47+
}
48+
globalThis.ET = ET;
49+
''');
50+
ET et1 = ET();
51+
Expect.isNull(et1.id);
52+
Expect.isNull(et1.name);
53+
54+
ET et2 = ET(2);
55+
Expect.equals(2, et2.id);
56+
Expect.isNull(et2.name);
57+
58+
ET2 et3 = ET2();
59+
Expect.isNull(et3.id);
60+
Expect.isNull(et3.name);
61+
62+
ET2 et4 = ET2(4);
63+
Expect.equals(4, et4.id);
64+
Expect.isNull(et4.name);
65+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion You can also write external members in extensions of interop
6+
/// types.
7+
///
8+
/// @description Check that external members in extensions of interop types are
9+
/// allowed and work as expected.
10+
/// @author [email protected]
11+
12+
import 'dart:js_interop';
13+
import '../../Utils/expect.dart';
14+
import 'js_utils.dart';
15+
16+
extension type ET._(JSObject _) implements JSObject {
17+
external ET(int id, String? name);
18+
19+
external int get id;
20+
}
21+
22+
extension on ET {
23+
external String? get name;
24+
external void set name(String? value);
25+
external int answer();
26+
}
27+
28+
@JS("ET")
29+
extension type ET2._(JSObject _) implements JSObject {
30+
external factory ET2(int id, String? name);
31+
32+
external int get id;
33+
}
34+
35+
extension Ext on ET2 {
36+
@JS("name")
37+
external String? get getName;
38+
@JS("name")
39+
external void set setName(String? value);
40+
@JS("answer")
41+
external int getAnswer();
42+
}
43+
44+
main() {
45+
eval(r'''
46+
class ET {
47+
constructor(id, name) {
48+
this._id = id;
49+
this._name = name;
50+
}
51+
get id() {
52+
return this._id;
53+
}
54+
get name() {
55+
return this._name;
56+
}
57+
set name(v) {
58+
return this._name = v;
59+
}
60+
answer() {
61+
return 42;
62+
}
63+
}
64+
globalThis.ET = ET;
65+
''');
66+
ET et1 = ET(1, "one");
67+
Expect.equals(1, et1.id);
68+
Expect.equals("one", et1.name);
69+
et1.name = "1";
70+
Expect.equals("1", et1.name);
71+
Expect.equals(42, et1.answer());
72+
73+
ET2 et2 = ET2(2, "two");
74+
Expect.equals(2, et2.id);
75+
Expect.equals("two", et2.getName);
76+
et2.setName = "2";
77+
Expect.equals("2", et2.getName);
78+
Expect.equals(42, et2.getAnswer());
79+
}

0 commit comments

Comments
 (0)