Skip to content

Commit

Permalink
Version 3.5.0-307.0.dev
Browse files Browse the repository at this point in the history
Merge 1c534e5 into dev
  • Loading branch information
Dart CI committed Jun 27, 2024
2 parents d825219 + 1c534e5 commit 7d7fbb3
Show file tree
Hide file tree
Showing 25 changed files with 1,371 additions and 228 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@
`ExternalDartReferenceToObject` and `ObjectToExternalDartReference` are now
extensions on `T` and `ExternalDartReference<T>`, respectively, where `T
extends Object?`. See [#55342][] and [#55536][] for more details.
- Fixes some consistency issues with `Function.toJS` across all compilers.
Specifically, calling `Function.toJS` on the same function gives you a new JS
function (see issue [#55515][]), the max number of args that are passed to the
JS function is determined by the static type of the Dart function, and extra
args are dropped when passed to the JS function in all compilers (see
[#48186][]).

[#55508]: https://github.com/dart-lang/sdk/issues/55508
[#55267]: https://github.com/dart-lang/sdk/issues/55267
[#55342]: https://github.com/dart-lang/sdk/issues/55342
[#55536]: https://github.com/dart-lang/sdk/issues/55536
[#55515]: https://github.com/dart-lang/sdk/issues/55515
[#48186]: https://github.com/dart-lang/sdk/issues/48186

### Tools

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ class JsUtilOptimizer extends Transformer {
final List<Procedure> _callConstructorUncheckedTargets;
final CloneVisitorNotMembers _cloner = CloneVisitorWithMembers();
final Map<Member, _InvocationBuilder?> _externalInvocationBuilders = {};
final Procedure _functionToJSTarget;
final List<Procedure> _functionToJSTargets;
final Procedure _functionToJSNTarget;
final Procedure _getPropertyTarget;
final Procedure _getPropertyTrustTypeTarget;
final Procedure _globalContextTarget;
final Procedure _jsExportedDartFunctionToDartTarget;
final Procedure _jsFunctionToDart;
final InterfaceType _objectType;
final Procedure _setPropertyTarget;
final Procedure _setPropertyUncheckedTarget;
Expand Down Expand Up @@ -88,12 +93,25 @@ class JsUtilOptimizer extends Transformer {
5,
(i) => _coreTypes.index.getTopLevelProcedure(
'dart:js_util', '_callConstructorUnchecked$i')),
_functionToJSTarget = _coreTypes.index.getTopLevelProcedure(
'dart:js_interop', 'FunctionToJSExportedDartFunction|get#toJS'),
_functionToJSTargets = List<Procedure>.generate(
6,
(i) => _coreTypes.index
.getTopLevelProcedure('dart:js_util', '_functionToJS$i')),
_functionToJSNTarget = _coreTypes.index
.getTopLevelProcedure('dart:js_util', '_functionToJSN'),
_getPropertyTarget = _coreTypes.index
.getTopLevelProcedure('dart:js_util', 'getProperty'),
_getPropertyTrustTypeTarget = _coreTypes.index
.getTopLevelProcedure('dart:js_util', '_getPropertyTrustType'),
_globalContextTarget = _coreTypes.index.getTopLevelProcedure(
'dart:_js_helper', 'get:staticInteropGlobalContext'),
_jsExportedDartFunctionToDartTarget = _coreTypes.index
.getTopLevelProcedure('dart:js_interop',
'JSExportedDartFunctionToFunction|get#toDart'),
_jsFunctionToDart = _coreTypes.index
.getTopLevelProcedure('dart:js_util', '_jsFunctionToDart'),
_objectType = hierarchy.coreTypes.objectNonNullableRawType,
_setPropertyTarget = _coreTypes.index
.getTopLevelProcedure('dart:js_util', 'setProperty'),
Expand Down Expand Up @@ -480,6 +498,10 @@ class JsUtilOptimizer extends Transformer {
invocation = _lowerCallConstructor(node);
// TODO(srujzs): Delete the `isPatchedMember` check once
// https://github.com/dart-lang/sdk/issues/53367 is resolved.
} else if (target == _functionToJSTarget) {
invocation = _lowerFunctionToJS(node);
} else if (target == _jsExportedDartFunctionToDartTarget) {
invocation = _lowerJSExportedDartFunctionToDart(node);
} else if (target.isExternal && !JsInteropChecks.isPatchedMember(target)) {
final builder = _externalInvocationBuilders.putIfAbsent(
target, () => _getExternalInvocationBuilder(target));
Expand Down Expand Up @@ -662,6 +684,45 @@ class JsUtilOptimizer extends Transformer {
..parent = nodeParent;
}

/// For the given `dart:js_interop` `Function.toJS` invocation [node], returns
/// an invocation of `_functionToJSX` with the given `Function` argument,
/// where X is the number of the positional arguments.
///
/// If the number of the positional arguments is larger than 5, returns an
/// invocation of `_functionToJSN` instead.
StaticInvocation _lowerFunctionToJS(StaticInvocation node) {
// JS interop checks assert that the static type is available, and that
// there are no named arguments or type arguments.
final function = node.arguments.positional.single;
final functionType =
function.getStaticType(_staticTypeContext) as FunctionType;
final argumentsLength = functionType.positionalParameters.length;
Procedure target;
Arguments arguments;
if (argumentsLength < _functionToJSTargets.length) {
target = _functionToJSTargets[argumentsLength];
arguments = Arguments([function]);
} else {
target = _functionToJSNTarget;
arguments = Arguments([function, IntLiteral(argumentsLength)]);
}
return StaticInvocation(
target, arguments..fileOffset = node.arguments.fileOffset)
..fileOffset = node.fileOffset
..parent = node.parent;
}

/// For the given `dart:js_interop` `JSExportedDartFunction.toDart` invocation
/// [node], returns an invocation of `_jsFunctionToDart` with the given
/// `JSExportedDartFunction` argument.
StaticInvocation _lowerJSExportedDartFunctionToDart(StaticInvocation node) =>
StaticInvocation(
_jsFunctionToDart,
Arguments([node.arguments.positional[0]])
..fileOffset = node.arguments.fileOffset)
..fileOffset = node.fileOffset
..parent = node.parent;

/// Returns whether the given [node] is guaranteed to be allowed to interop
/// with JS.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2024, 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.

import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';

class InsertBody extends ResolvedCorrectionProducer {
InsertBody({required super.context});

@override
CorrectionApplicability get applicability =>
// TODO(applicability): comment on why.
CorrectionApplicability.singleLocation;

@override
FixKind get fixKind => DartFixKind.INSERT_BODY;

@override
Future<void> compute(ChangeBuilder builder) async {
var diagnostic = this.diagnostic;
if (diagnostic == null) return;

var message = diagnostic.problemMessage;
var insertOffset = message.offset + message.length;
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleInsertion(insertOffset, ' {}');
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
#
# Stats:
# - 42 "needsEvaluation"
# - 316 "needsFix"
# - 431 "hasFix"
# - 306 "needsFix"
# - 441 "hasFix"
# - 517 "noFix"

AnalysisOptionsErrorCode.INCLUDED_FILE_PARSE_ERROR:
Expand Down Expand Up @@ -2664,31 +2664,21 @@ ParserErrorCode.EQUALITY_CANNOT_BE_EQUALITY_OPERAND:
ParserErrorCode.EXPECTED_CASE_OR_DEFAULT:
status: noFix
ParserErrorCode.EXPECTED_CATCH_CLAUSE_BODY:
status: needsFix
notes: |-
Add an empty body.
status: hasFix
ParserErrorCode.EXPECTED_CLASS_BODY:
status: needsFix
notes: |-
Add an empty body.
status: hasFix
ParserErrorCode.EXPECTED_CLASS_MEMBER:
status: noFix
ParserErrorCode.EXPECTED_ELSE_OR_COMMA:
status: noFix
ParserErrorCode.EXPECTED_EXECUTABLE:
status: noFix
ParserErrorCode.EXPECTED_EXTENSION_BODY:
status: needsFix
notes: |-
Add an empty body.
status: hasFix
ParserErrorCode.EXPECTED_EXTENSION_TYPE_BODY:
status: needsFix
notes: |-
Add an empty body.
status: hasFix
ParserErrorCode.EXPECTED_FINALLY_CLAUSE_BODY:
status: needsFix
notes: |-
Add an empty body.
status: hasFix
ParserErrorCode.EXPECTED_IDENTIFIER_BUT_GOT_KEYWORD:
status: noFix
ParserErrorCode.EXPECTED_INSTEAD:
Expand All @@ -2698,9 +2688,7 @@ ParserErrorCode.EXPECTED_LIST_OR_MAP_LITERAL:
notes: |-
Add the braces or brackets for the literal.
ParserErrorCode.EXPECTED_MIXIN_BODY:
status: needsFix
notes: |-
Add an empty body.
status: hasFix
ParserErrorCode.EXPECTED_NAMED_TYPE_EXTENDS:
status: noFix
ParserErrorCode.EXPECTED_NAMED_TYPE_IMPLEMENTS:
Expand All @@ -2716,19 +2704,13 @@ ParserErrorCode.EXPECTED_REPRESENTATION_TYPE:
ParserErrorCode.EXPECTED_STRING_LITERAL:
status: noFix
ParserErrorCode.EXPECTED_SWITCH_EXPRESSION_BODY:
status: needsFix
notes: |-
Add an empty body.
status: hasFix
ParserErrorCode.EXPECTED_SWITCH_STATEMENT_BODY:
status: needsFix
notes: |-
Add an empty body.
status: hasFix
ParserErrorCode.EXPECTED_TOKEN:
status: hasFix
ParserErrorCode.EXPECTED_TRY_STATEMENT_BODY:
status: needsFix
notes: |-
Add an empty body.
status: hasFix
ParserErrorCode.EXPECTED_TYPE_NAME:
status: noFix
ParserErrorCode.EXPERIMENT_NOT_ENABLED:
Expand Down Expand Up @@ -3001,9 +2983,7 @@ ParserErrorCode.MISSING_CLOSING_PARENTHESIS:
ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE:
status: hasFix
ParserErrorCode.MISSING_ENUM_BODY:
status: needsFix
notes: |-
Add an empty body
status: hasFix
ParserErrorCode.MISSING_EXPRESSION_IN_INITIALIZER:
status: noFix
ParserErrorCode.MISSING_EXPRESSION_IN_THROW:
Expand Down
5 changes: 5 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,11 @@ abstract final class DartFixKind {
DartFixKindPriority.IN_FILE - 20,
'Inline type definitions everywhere in file',
);
static const INSERT_BODY = FixKind(
'dart.fix.insertBody',
DartFixKindPriority.DEFAULT,
'Insert body',
);
static const INSERT_SEMICOLON = FixKind(
'dart.fix.insertSemicolon',
DartFixKindPriority.DEFAULT,
Expand Down
31 changes: 31 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import 'package:analysis_server/src/services/correction/dart/flutter_remove_widg
import 'package:analysis_server/src/services/correction/dart/import_library.dart';
import 'package:analysis_server/src/services/correction/dart/inline_invocation.dart';
import 'package:analysis_server/src/services/correction/dart/inline_typedef.dart';
import 'package:analysis_server/src/services/correction/dart/insert_body.dart';
import 'package:analysis_server/src/services/correction/dart/insert_semicolon.dart';
import 'package:analysis_server/src/services/correction/dart/make_class_abstract.dart';
import 'package:analysis_server/src/services/correction/dart/make_conditional_on_debug_mode.dart';
Expand Down Expand Up @@ -1409,6 +1410,33 @@ final _builtInNonLintProducers = <ErrorCode, List<ProducerGenerator>>{
ParserErrorCode.EMPTY_RECORD_TYPE_WITH_COMMA: [
RemoveComma.emptyRecordType,
],
ParserErrorCode.EXPECTED_CATCH_CLAUSE_BODY: [
InsertBody.new,
],
ParserErrorCode.EXPECTED_CLASS_BODY: [
InsertBody.new,
],
ParserErrorCode.EXPECTED_EXTENSION_BODY: [
InsertBody.new,
],
ParserErrorCode.EXPECTED_EXTENSION_TYPE_BODY: [
InsertBody.new,
],
ParserErrorCode.EXPECTED_FINALLY_CLAUSE_BODY: [
InsertBody.new,
],
ParserErrorCode.EXPECTED_MIXIN_BODY: [
InsertBody.new,
],
ParserErrorCode.EXPECTED_SWITCH_EXPRESSION_BODY: [
InsertBody.new,
],
ParserErrorCode.EXPECTED_SWITCH_STATEMENT_BODY: [
InsertBody.new,
],
ParserErrorCode.EXPECTED_TRY_STATEMENT_BODY: [
InsertBody.new,
],
ParserErrorCode.EXPECTED_TOKEN: [
InsertSemicolon.new,
ReplaceWithArrow.new,
Expand Down Expand Up @@ -1485,6 +1513,9 @@ final _builtInNonLintProducers = <ErrorCode, List<ProducerGenerator>>{
ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE: [
AddTypeAnnotation.new,
],
ParserErrorCode.MISSING_ENUM_BODY: [
InsertBody.new,
],
ParserErrorCode.MISSING_FUNCTION_BODY: [
ConvertIntoBlockBody.missingBody,
],
Expand Down
Loading

0 comments on commit 7d7fbb3

Please sign in to comment.