Skip to content

Commit cecd7ec

Browse files
aroogledart-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[dart:js_interop] Warning if @js Annotations used for Extension Type Constructors
Fixes #61274 Annotating a JS interop extension type constructor with an @js annotation has no effect. This change warns the user if such an annotation is present. Change-Id: Ic324ea2c57d6ecc5241a0983859afb2aa74c2dcc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/527740 Reviewed-by: Mark Zhou <[email protected]> Commit-Queue: Arnav Arora <[email protected]> Reviewed-by: Srujan Gaddam <[email protected]> Reviewed-by: Nate Biggs <[email protected]>
1 parent 98900c5 commit cecd7ec

5 files changed

Lines changed: 43 additions & 7 deletions

File tree

‎pkg/_js_interop_checks/lib/js_interop_checks.dart‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,23 @@ class JsInteropChecks extends RecursiveVisitor {
335335
} else {
336336
_checkJsInteropOperator(node);
337337

338-
// Check JS Interop positional and named parameters. Literal constructors
339-
// can only have named parameters, and every other interop member can only
340-
// have positional parameters.
341-
final isObjectLiteralConstructor =
338+
final isConstructor =
342339
node.isExtensionTypeMember &&
343340
(extensionIndex.getExtensionTypeDescriptor(node)!.kind ==
344341
ExtensionTypeMemberKind.Constructor ||
345342
extensionIndex.getExtensionTypeDescriptor(node)!.kind ==
346-
ExtensionTypeMemberKind.Factory) &&
347-
node.function.namedParameters.isNotEmpty;
343+
ExtensionTypeMemberKind.Factory);
344+
345+
// `@JS` annotations on extension type constructors have no effect.
346+
if (isConstructor && hasJSInteropAnnotation(node)) {
347+
report(diag.jsInteropExtensionConstructorJsAnnotationHasNoEffect);
348+
}
349+
350+
// Check JS Interop positional and named parameters. Literal constructors
351+
// can only have named parameters, and every other interop member can only
352+
// have positional parameters.
353+
final isObjectLiteralConstructor =
354+
isConstructor && node.function.namedParameters.isNotEmpty;
348355
final isAnonymousFactory = _classHasAnonymousAnnotation && node.isFactory;
349356
if (isObjectLiteralConstructor || isAnonymousFactory) {
350357
_checkLiteralConstructorHasNoPositionalParams(

‎pkg/front_end/lib/src/codes/diagnostic.g.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7613,6 +7613,14 @@ Message _withArgumentsJsInteropExportNoExportableMembers({
76137613
);
76147614
}
76157615

7616+
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
7617+
const MessageCode jsInteropExtensionConstructorJsAnnotationHasNoEffect =
7618+
const MessageCode(
7619+
"JsInteropExtensionConstructorJsAnnotationHasNoEffect",
7620+
problemMessage: """The '@JS' annotation on an extension type constructor has no effect.""",
7621+
correctionMessage: """Remove the '@JS' annotation from the extension type constructor and put it on the extension type to rename non-object literal constructors.""",
7622+
);
7623+
76167624
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
76177625
const MessageCode jsInteropExtensionTypeMemberNotInterop = const MessageCode(
76187626
"JsInteropExtensionTypeMemberNotInterop",

‎pkg/front_end/messages.status‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ front_end/JsInteropExportInvalidInteropTypeArgument/example: missingExample # We
7575
front_end/JsInteropExportInvalidTypeArgument/example: missingExample # Web compiler specific
7676
front_end/JsInteropExportMemberCollision/example: missingExample # Web compiler specific
7777
front_end/JsInteropExportNoExportableMembers/example: missingExample # Web compiler specific
78+
front_end/JsInteropExtensionConstructorJsAnnotationHasNoEffect/example: Fail # Web compiler specific
7879
front_end/JsInteropExtensionTypeMemberNotInterop/example: missingExample # Web compiler specific
7980
front_end/JsInteropExtensionTypeNotInterop/example: missingExample # Web compiler specific
8081
front_end/JsInteropExtensionTypeUsedWithWrongJsAnnotation/example: missingExample # Web compiler specific

‎pkg/front_end/messages.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6073,6 +6073,11 @@ jsInteropIsATearoff:
60736073
problemMessage: "'isA' can't be torn off."
60746074
correctionMessage: "Use a method that calls 'isA' and tear off that method instead."
60756075

6076+
jsInteropExtensionConstructorJsAnnotationHasNoEffect:
6077+
parameters: none
6078+
problemMessage: "The '@JS' annotation on an extension type constructor has no effect."
6079+
correctionMessage: "Remove the '@JS' annotation from the extension type constructor and put it on the extension type to rename non-object literal constructors."
6080+
60766081
jsInteropExtensionTypeNotInterop:
60776082
parameters:
60786083
Name extensionTypeName: The name of the extension type that is marked with a `@JS` annotation.

‎tests/lib/js/static_interop_test/extension_type/incorrect_js_annotation_static_test.dart‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,22 @@ external jsi.JSVoid jsiTopLevel();
1818
external jsi.JSVoid pkgJsTopLevel();
1919

2020
@jsi.JS()
21-
extension type JsiExtensionType._(jsi.JSObject _) {}
21+
extension type JsiExtensionType._(jsi.JSObject _) {
22+
@jsi.JS()
23+
external JsiExtensionType();
24+
// ^
25+
// [web] The '@JS' annotation on an extension type constructor has no effect.
26+
27+
@jsi.JS()
28+
external JsiExtensionType.literal({int foo});
29+
// ^
30+
// [web] The '@JS' annotation on an extension type constructor has no effect.
31+
32+
@jsi.JS()
33+
external factory JsiExtensionType.factory();
34+
// ^
35+
// [web] The '@JS' annotation on an extension type constructor has no effect.
36+
}
2237

2338
@pkgJs.JS()
2439
extension type PkgJsExtensionType._(jsi.JSObject _) {}

0 commit comments

Comments
 (0)