-
Notifications
You must be signed in to change notification settings - Fork 29
#3180. Add @JS() annotation tests. Part 2.
#3268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
190f38f
#3180. Add `@JS()` annotation tests. Part 2.
sgrekhov 2ff35e4
Implement review recommendations
sgrekhov 5c4e999
Update js_A01_t13.dart
eernstg 6136a1b
Update js_A01_t14.dart
eernstg 8244de8
Update js_A01_t15.dart
eernstg 2989a13
Implement review recommendations
sgrekhov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // 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 An annotation on a JavaScript interop declaration. | ||
| /// | ||
| /// This annotation defines a given library, top-level external declaration, or | ||
| /// extension type as a JavaScript interop declaration. | ||
| /// | ||
| /// @description Check that it is a compile-time error if an external member of | ||
| /// a JS interop extension type uses non-valid JS interop type. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
|
|
||
| class C {} | ||
|
|
||
| @JS() | ||
| external void f1(C v); | ||
| // ^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| @JS() | ||
| external C f2(); | ||
| // ^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| @JS() | ||
| external C var1; | ||
| // ^^^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| @JS() | ||
| external final C var2; | ||
| // ^^^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| @JS() | ||
| external C get getter; | ||
| // ^^^^^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| @JS() | ||
| external void set setter(C v); | ||
| // ^^^^^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| main() { | ||
| print(var1); | ||
| print(var2); | ||
| print(getter); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 An annotation on a JavaScript interop declaration. | ||
| /// | ||
| /// This annotation defines a given library, top-level external declaration, or | ||
| /// extension type as a JavaScript interop declaration. | ||
| /// | ||
| /// @description Check that it is a compile-time error if an external member of | ||
| /// a JS interop extension type uses a non-valid JS interop type. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
|
|
||
| @JS() | ||
| external int f1<T extends num>(T _); | ||
| // ^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| @JS() | ||
| external T f2<T extends Object>(); | ||
| // ^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| main() { | ||
| f1(0); | ||
| f2(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 An annotation on a JavaScript interop declaration. | ||
| /// | ||
| /// This annotation defines a given library, top-level external declaration, or | ||
| /// extension type as a JavaScript interop declaration. | ||
| /// | ||
| /// @description Check that it is not an error if an external JS interop | ||
| /// function has a type parameter. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| @JS() | ||
| external T f1<T extends JSAny>(); | ||
|
|
||
| @JS() | ||
| external T f2<T extends JSObject>(T _); | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| globalThis.f1 = function() {return 1;}; | ||
| globalThis.f2 = function(v) {return v;}; | ||
| '''); | ||
| Expect.equals(1, f1<JSNumber>().toDartInt); | ||
| Expect.mapEquals( | ||
| {"a": "b"}, | ||
| f2<JSObject>({"a": "b"}.jsify() as JSObject).dartify() | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // 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 An annotation on a JavaScript interop declaration. | ||
| /// | ||
| /// This annotation defines a given library, top-level external declaration, or | ||
| /// extension type as a JavaScript interop declaration. | ||
| /// | ||
| /// @description Check that JS interop extension type may have not-external | ||
| /// members. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| @JS() | ||
| extension type ET1(JSObject _) implements JSObject { | ||
| external int f1(); | ||
| String f2(int v) => "f2($v)"; | ||
| static String f3() => "f3"; | ||
| external static String f4(); | ||
| } | ||
|
|
||
| extension type ET2(JSObject _) implements JSObject { | ||
| external int g1(); | ||
| String g2(int v) => "g2($v)"; | ||
| static String g3() => "g3"; | ||
| external static String g4(); | ||
| } | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| class ET1 { | ||
| f1() { | ||
| return 1; | ||
| } | ||
| static f4() { | ||
| return "f4"; | ||
| } | ||
| } | ||
| class ET2 { | ||
| g1() { | ||
| return 2; | ||
| } | ||
| static g4() { | ||
| return "g4"; | ||
| } | ||
| } | ||
| globalThis.ET1 = ET1; | ||
| globalThis.ET2 = ET2; | ||
| globalThis.et1 = new ET1(); | ||
| globalThis.et2 = new ET2(); | ||
| '''); | ||
| ET1 et1 = ET1(globalContext["et1"] as JSObject); | ||
| ET2 et2 = ET2(globalContext["et2"] as JSObject); | ||
| Expect.equals(1, et1.f1()); | ||
| Expect.equals("f2(2)", et1.f2(2)); | ||
| Expect.equals("f3", ET1.f3()); | ||
| Expect.equals("f4", ET1.f4()); | ||
| Expect.equals(2, et2.g1()); | ||
| Expect.equals("g2(2)", et2.g2(2)); | ||
| Expect.equals("g3", ET2.g3()); | ||
| Expect.equals("g4", ET2.g4()); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // 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 An annotation on a JavaScript interop declaration. | ||
| /// | ||
| /// This annotation defines a given library, top-level external declaration, or | ||
| /// extension type as a JavaScript interop declaration. | ||
| /// | ||
| /// @description Check that JS interop extension type may have not-external | ||
| /// members, which are then unrelated to JavaScript members with the same name. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| @JS() | ||
| extension type ET1(JSObject _) implements JSObject { | ||
| external int f1(); | ||
| String f2(int v) => "f2($v)"; | ||
| static String f3() => "f3"; | ||
| } | ||
|
|
||
| extension type ET2(JSObject _) implements JSObject { | ||
| external int g1(); | ||
| String g2(int v) => "g2($v)"; | ||
| static String g3() => "g3"; | ||
| } | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| class ET1 { | ||
| f1() { | ||
| return 1; | ||
| } | ||
| f2() { | ||
| return "JS f2"; | ||
| } | ||
| static f3() { | ||
| return "JS f3"; | ||
| } | ||
| } | ||
| class ET2 { | ||
| g1() { | ||
| return 2; | ||
| } | ||
| g2() { | ||
| return "JS g2"; | ||
| } | ||
| static g3() { | ||
| return "JS g3"; | ||
| } | ||
| } | ||
| globalThis.et1 = new ET1(); | ||
| globalThis.et2 = new ET2(); | ||
| '''); | ||
| ET1 et1 = ET1(globalContext["et1"] as JSObject); | ||
| ET2 et2 = ET2(globalContext["et2"] as JSObject); | ||
| Expect.equals(1, et1.f1()); | ||
| Expect.equals("f2(2)", et1.f2(2)); | ||
| Expect.equals("f3", ET1.f3()); | ||
| Expect.equals(2, et2.g1()); | ||
| Expect.equals("g2(2)", et2.g2(2)); | ||
| Expect.equals("g3", ET2.g3()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 An annotation on a JavaScript interop declaration. | ||
| /// | ||
| /// This annotation defines a given library, top-level external declaration, or | ||
| /// extension type as a JavaScript interop declaration. | ||
| /// | ||
| /// @description Check that it is a compile-time error if an extension type is | ||
| /// annotated with `@JS()` but its representation type is not a valid JS interop | ||
| /// type. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
|
|
||
| @JS() | ||
| extension type ET1<T extends JSAny>(T _) implements JSAny { | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // ^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
| } | ||
|
|
||
| @JS() | ||
| extension type ET2(int _) implements num { | ||
| // ^^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
| } | ||
|
|
||
| main() { | ||
| print(ET1); | ||
| print(ET2); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 An annotation on a JavaScript interop declaration. | ||
| /// | ||
| /// This annotation defines a given library, top-level external declaration, or | ||
| /// extension type as a JavaScript interop declaration. | ||
| /// | ||
| /// @description Check that JS interop extension type may have a type parameter. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| @JS() | ||
| extension type ET1<T extends JSAny>(JSObject _) implements JSObject { | ||
| external T f1(); | ||
| } | ||
|
|
||
| @JS() | ||
| extension type ET2<T extends JSObject>(JSObject _) implements JSObject { | ||
| external T g1(); | ||
| } | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| class ET1 { | ||
| f1() { | ||
| return 1; | ||
| } | ||
| } | ||
| class ET2 { | ||
| g1() { | ||
| return ["two"]; | ||
| } | ||
| } | ||
| globalThis.et1 = new ET1(); | ||
| globalThis.et2 = new ET2(); | ||
| '''); | ||
| ET1<JSNumber> et1 = globalContext["et1"] as ET1<JSNumber>; | ||
| Expect.equals(1, et1.f1().toDartInt); | ||
|
|
||
| ET2<JSArray> et2 = globalContext["et2"] as ET2<JSArray>; | ||
| Expect.listEquals(["two"], et2.g1().dartify()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 An annotation on a JavaScript interop declaration. | ||
| /// | ||
| /// This annotation defines a given library, top-level external declaration, or | ||
| /// extension type as a JavaScript interop declaration. | ||
| /// | ||
| /// @description Check that it is a compile-time error if an external member of | ||
| /// a JS interop extension type uses a non-valid JS interop type. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
|
|
||
| @JS() | ||
| extension type ET1<T extends num>(JSObject _) implements JSObject { | ||
| external void f1(T _); | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // ^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
| } | ||
|
|
||
| extension type ET2<T extends String>(JSObject _) implements JSObject { | ||
| external T g1(); | ||
| // ^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
| } | ||
|
|
||
| main() { | ||
| print(ET1); | ||
| print(ET2); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.