Skip to content
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

[swift2objc] Support properties that throw #1939

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AmrAhmed119
Copy link

@AmrAhmed119 AmrAhmed119 commented Jan 23, 2025

Closes #1765

  • Adding support for throwing properties in swift by converting them into ordinary methods.

  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

Copy link

google-cla bot commented Jan 23, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@AmrAhmed119 AmrAhmed119 changed the title [swift2objc] Support properties that throw #1765 [swift2objc] Support properties that throw Jan 23, 2025
Copy link

PR Health

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

API leaks ✔️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbols
License Headers ✔️
// 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.
Files
no missing headers

All source files should start with a license header.

Unrelated files missing license headers
Files
pkgs/jni/lib/src/third_party/generated_bindings.dart
pkgs/objective_c/lib/src/ns_input_stream.dart

@coveralls
Copy link

Coverage Status

coverage: 87.109% (-1.2%) from 88.352%
when pulling f5d1118 on AmrAhmed119:throwing-properties
into 7a72868 on dart-lang:main.

@@ -73,11 +73,10 @@ bool _parseVariableThrows(Json json) {
final throws = json['declarationFragments']
.any((frag) => matchFragment(frag, 'keyword', 'throws'));
if (throws) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete this whole if statement and just return throws.

@@ -106,3 +109,23 @@ InitializerDeclaration _buildWrapperInitializer(
hasObjCAnnotation: wrappedClassInstance.hasObjCAnnotation,
);
}

List<MethodDeclaration> _convertPropertiesToMethods(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I understand correctly, you've added a isCallingProperty field to the method and function declarations, which changes the originalCallStatementGenerator param passed to _transformFunction to not pass args. That transformation happens in transformMethod, which is called immediately after you set this flag. In other words, you've added a flag to the AST node, that is only set here, and then is immediately consumed. The flag is only necessary because you're merging these methods with originalCompound.methods before passing them to transformMethod.

It would be cleaner to remove isCallingProperty, and remove _convertPropertiesToMethods, and do all that transformation logic in transformProperty. transformProperty would check the throws flag and decide whether to return a PropertyDeclaration or a MethodDeclaration (the declared return type would be AstNode). Then you would merge any MethodDeclarations it returned with the other methods after they've been transformed:

// transformedProperties is a List<AstNode?>
final transformedProperties = originalCompound.properties
    .map((property) => transformProperty(
          property,
          wrappedCompoundInstance,
          parentNamer,
          transformationMap,
        )).nonNulls.toList();

final transformedMethods = originalCompound.methods
    .map((method) => transformMethod(
          method,
          wrappedCompoundInstance,
          parentNamer,
          transformationMap,
        )).nonNulls.toList();

transformedCompound.properties =
  transformedProperties.whereType<PropertyDeclaration>()
  ..sort((Declaration a, Declaration b) => a.id.compareTo(b.id));

transformedCompound.methods = (transformedMethods +
  transformedProperties.whereType<MethodDeclaration>())
  ..sort((Declaration a, Declaration b) => a.id.compareTo(b.id));

The same goes for GlobalFunctionDeclaration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[swift2objc] Support properties that throw
3 participants