1
0
mirror of https://github.com/dart-lang/sdk synced 2024-07-03 00:08:46 +00:00

[pkg:js] Require users to use @JS when using @staticInterop

Change-Id: Iea0a54ff03b32bc910ef2c6eb633bffd09cf0671
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/268108
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Reviewed-by: Riley Porter <rileyporter@google.com>
This commit is contained in:
Srujan Gaddam 2022-11-21 20:19:06 +00:00 committed by Commit Queue
parent aab6ab8b84
commit 4c34e780cf
7 changed files with 89 additions and 2 deletions

View File

@ -148,6 +148,9 @@
from using type parameters e.g. `external void method<T>(T t)`. Use a
non-`external` extension method for type parameters instead. See [#49350][]
for more details.
- Classes with this annotation should also have the `@JS` annotation. You can
also have the `@anonymous` annotation with these two annotations for an object
literal constructor, but it isn't required.
[#48730]: https://github.com/dart-lang/sdk/issues/48730
[#49941]: https://github.com/dart-lang/sdk/issues/49941

View File

@ -7502,6 +7502,35 @@ Message _withArgumentsJsInteropStaticInteropMockMissingImplements(
arguments: {'name': name, 'name2': name2, 'string': string});
}
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Template<
Message Function(
String
name)> templateJsInteropStaticInteropNoJSAnnotation = const Template<
Message Function(String name)>(
problemMessageTemplate:
r"""`@staticInterop` classes should also have the `@JS` annotation.""",
correctionMessageTemplate: r"""Add `@JS` to class '#name'.""",
withArguments: _withArgumentsJsInteropStaticInteropNoJSAnnotation);
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Code<Message Function(String name)>
codeJsInteropStaticInteropNoJSAnnotation =
const Code<Message Function(String name)>(
"JsInteropStaticInteropNoJSAnnotation",
);
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
Message _withArgumentsJsInteropStaticInteropNoJSAnnotation(String name) {
if (name.isEmpty) throw 'No name provided';
name = demangleMixinApplicationName(name);
return new Message(codeJsInteropStaticInteropNoJSAnnotation,
problemMessage:
"""`@staticInterop` classes should also have the `@JS` annotation.""",
correctionMessage: """Add `@JS` to class '${name}'.""",
arguments: {'name': name});
}
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Code<Null> codeJsInteropStaticInteropSyntheticConstructor =
messageJsInteropStaticInteropSyntheticConstructor;

View File

@ -25,6 +25,7 @@ import 'package:_fe_analyzer_shared/src/messages/codes.dart'
messageJsInteropStaticInteropGenerativeConstructor,
messageJsInteropStaticInteropSyntheticConstructor,
templateJsInteropDartClassExtendsJSClass,
templateJsInteropStaticInteropNoJSAnnotation,
templateJsInteropStaticInteropWithInstanceMembers,
templateJsInteropStaticInteropWithNonStaticSupertype,
templateJsInteropJSClassExtendsDartClass,
@ -177,9 +178,17 @@ class JsInteropChecks extends RecursiveVisitor {
}
}
}
// Validate that superinterfaces are all annotated as static as well. Note
// that mixins are already disallowed and therefore are not checked here.
if (_classHasStaticInteropAnnotation) {
if (!_classHasJSAnnotation) {
_diagnosticsReporter.report(
templateJsInteropStaticInteropNoJSAnnotation
.withArguments(cls.name),
cls.fileOffset,
cls.name.length,
cls.fileUri);
}
// Validate that superinterfaces are all annotated as static as well. Note
// that mixins are already disallowed and therefore are not checked here.
for (var supertype in cls.implementedTypes) {
if (!hasStaticInteropAnnotation(supertype.classNode)) {
_diagnosticsReporter.report(

View File

@ -604,6 +604,8 @@ JsInteropStaticInteropMockMissingImplements/analyzerCode: Fail # Web compiler sp
JsInteropStaticInteropMockMissingImplements/example: Fail # Web compiler specific
JsInteropStaticInteropMockNotStaticInteropType/analyzerCode: Fail # Web compiler specific
JsInteropStaticInteropMockNotStaticInteropType/example: Fail # Web compiler specific
JsInteropStaticInteropNoJSAnnotation/analyzerCode: Fail # Web compiler specific
JsInteropStaticInteropNoJSAnnotation/example: Fail # Web compiler specific
JsInteropStaticInteropSyntheticConstructor/analyzerCode: Fail # Web compiler specific
JsInteropStaticInteropSyntheticConstructor/example: Fail # Web compiler specific
JsInteropStaticInteropTrustTypesUsageNotAllowed/analyzerCode: Fail # Web compiler specific

View File

@ -5308,6 +5308,10 @@ JsInteropStaticInteropMockNotStaticInteropType:
problemMessage: "Type argument '#type' needs to be a `@staticInterop` type."
correctionMessage: "Use a `@staticInterop` class instead."
JsInteropStaticInteropNoJSAnnotation:
problemMessage: "`@staticInterop` classes should also have the `@JS` annotation."
correctionMessage: "Add `@JS` to class '#name'."
JsInteropStaticInteropSyntheticConstructor:
problemMessage: "Synthetic constructors on `@staticInterop` classes can not be used."
correctionMessage: "Declare an external factory constructor for this `@staticInterop` class and use that instead."

View File

@ -0,0 +1,19 @@
// Copyright (c) 2022, 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.
@JS()
library js_and_staticinterop_annotation_static_test;
import 'package:js/js.dart';
@staticInterop
class NoJSAnnotation {}
// ^
// [web] `@staticInterop` classes should also have the `@JS` annotation.
@anonymous
@staticInterop
class AnonymousNoJSAnnotation {}
// ^
// [web] `@staticInterop` classes should also have the `@JS` annotation.

View File

@ -0,0 +1,21 @@
// Copyright (c) 2022, 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.
// @dart = 2.9
@JS()
library js_and_staticinterop_annotation_static_test;
import 'package:js/js.dart';
@staticInterop
class NoJSAnnotation {}
// ^
// [web] `@staticInterop` classes should also have the `@JS` annotation.
@anonymous
@staticInterop
class AnonymousNoJSAnnotation {}
// ^
// [web] `@staticInterop` classes should also have the `@JS` annotation.