Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(apex): parse Apex method parameters as positional type-first signatures (#4427) (#4429)

Rishabh Mishraad0b794a2de5
diff --git a/packages/core/src/repowise/core/analysis/signature_effect.py b/packages/core/src/repowise/core/analysis/signature_effect.py
index 2cfedc661..1cdd0cf53 100644
--- a/packages/core/src/repowise/core/analysis/signature_effect.py
+++ b/packages/core/src/repowise/core/analysis/signature_effect.py
@@ -7,9 +7,9 @@ parameter lists and asks the narrower question: could a call site written
against the old signature stop working?
The reading depends on the language. Where callers can name arguments (Python,
-C#, Kotlin), a rename breaks them; where they cannot (JavaScript, Go, Java),
-it does not. Where types are checked at the call (TypeScript, Rust, Go, Java),
-a retyped parameter or return breaks callers; where they are not (Python,
+C#, Kotlin), a rename breaks them; where they cannot (JavaScript, Go, Java,
+Apex), it does not. Where types are checked at the call (TypeScript, Rust, Go, Java,
+Apex), a retyped parameter or return breaks callers; where they are not (Python,
JavaScript), it is only an annotation.
It is conservative in one direction only. A form it cannot parse is reported as
@@ -33,13 +33,13 @@ EFFECT_UNKNOWN = "unknown" # unparseable, or a change this module cannot judge
# Callers can pass arguments by name, so a parameter's name is contract.
# Unlisted languages (and ``None``) are treated as named: fail closed.
_POSITIONAL_ONLY_LANGUAGES = frozenset(
- {"javascript", "typescript", "go", "java", "c", "cpp", "rust"}
+ {"javascript", "typescript", "go", "java", "c", "cpp", "rust", "apex"}
)
# Annotations are not checked at the call, so a retype is not a break.
# Unlisted languages (and ``None``) are treated as typed: fail closed.
_LENIENT_TYPE_LANGUAGES = frozenset({"python", "javascript", "ruby", "php"})
# ``Type name`` rather than ``name: Type``.
-_TYPE_FIRST_LANGUAGES = frozenset({"java", "csharp", "c", "cpp", "dart", "php"})
+_TYPE_FIRST_LANGUAGES = frozenset({"java", "csharp", "c", "cpp", "dart", "php", "apex"})
_HASH_COMMENT_LANGUAGES = frozenset({"python", "ruby", "php"})
_PAIRS = {")": "(", "]": "[", "}": "{"}
diff --git a/tests/unit/analysis/test_change_contracts.py b/tests/unit/analysis/test_change_contracts.py
index 0f5ff7a67..0ffe44dc2 100644
--- a/tests/unit/analysis/test_change_contracts.py
+++ b/tests/unit/analysis/test_change_contracts.py
@@ -505,3 +505,43 @@ def test_snapshot_base_is_carried_on_the_result():
head = [_file("app/a.py", [_sym("run", path="app/a.py", sig="run(x, y)")])]
assert _compute(base, head).base_is_snapshot is False
assert _compute(base, head, base_is_snapshot=True).base_is_snapshot is True
+
+
+def test_apex_parameter_rename_is_compatible_not_breaking():
+ base = [
+ _file(
+ "force-app/AccountService.cls",
+ [
+ _sym(
+ "findByName", path="force-app/AccountService.cls", sig="findByName(String name)"
+ )
+ ],
+ )
+ ]
+ head = [
+ _file(
+ "force-app/AccountService.cls",
+ [
+ _sym(
+ "findByName",
+ path="force-app/AccountService.cls",
+ sig="findByName(String query)",
+ )
+ ],
+ )
+ ]
+ impact = _compute(
+ base,
+ head,
+ changed=("force-app/AccountService.cls",),
+ calls=[
+ (
+ "force-app/AccountTrigger.trigger::run",
+ "force-app/AccountService.cls::findByName",
+ )
+ ],
+ )
+ assert impact.breaking == []
+ assert len(impact.changes) == 1
+ assert impact.changes[0].is_breaking is False
+ assert impact.changes[0].signature_effect == "compatible"
diff --git a/tests/unit/analysis/test_signature_effect.py b/tests/unit/analysis/test_signature_effect.py
index b8f6a74e5..ff4760799 100644
--- a/tests/unit/analysis/test_signature_effect.py
+++ b/tests/unit/analysis/test_signature_effect.py
@@ -13,7 +13,9 @@ from repowise.core.analysis.signature_effect import (
EFFECT_COMPATIBLE,
EFFECT_NONE,
EFFECT_UNKNOWN,
+ bound_names,
classify_signature_change,
+ parameter_names,
)
BREAKS = {EFFECT_BREAKING, EFFECT_UNKNOWN}
@@ -37,6 +39,7 @@ def _effect(before: str, after: str, lang: str | None, kind: str | None = "funct
("function F(a, b int)", "function F(a int, b int)", "go"),
# Reordering keyword-only parameters moves nothing a caller passes by position.
("def f(a, *, b=1, c=2)", "def f(a, *, c=2, b=1)", "python"),
+ ("void f(String a, Integer b)", "void f(\n String a,\n Integer b\n)", "apex"),
],
)
def test_text_only_changes_have_no_effect(before, after, lang):
@@ -83,6 +86,12 @@ def test_text_only_changes_have_no_effect(before, after, lang):
"java",
"renamed `a` to `b`, which callers never name",
),
+ (
+ "void f(String a) -> void",
+ "void f(String b) -> void",
+ "apex",
+ "renamed `a` to `b`, which callers never name",
+ ),
("function F(a int)", "function F(a int, opts ...Opt)", "go", "added `...opts`"),
("f(String[] a) -> void", "f(String... a) -> void", "java", "now takes `a` as varargs"),
(
@@ -199,6 +208,18 @@ def test_changes_every_existing_call_survives_are_compatible(before, after, lang
"csharp",
"changed its return type from `int` to `string`",
),
+ (
+ "foo(String a) -> void",
+ "foo(String a) -> Integer",
+ "apex",
+ "changed its return type from `void` to `Integer`",
+ ),
+ (
+ "foo(String[] a) -> void",
+ "foo(String a) -> void",
+ "apex",
+ "changed the type of `a` from `String[]` to `String`",
+ ),
(
"function C({a}: {a: string})",
"function C({a, b}: {a: string; b: number})",
@@ -286,3 +307,9 @@ def test_a_widened_parameter_or_narrowed_return_type_is_compatible():
"function f() -> string", "function f() -> string | null", "typescript"
)
assert widened_return.effect == EFFECT_BREAKING
+
+
+def test_apex_parameter_and_bound_names():
+ sig = "findByName(String name, List<Id> ids) -> List<Account>"
+ assert parameter_names(sig, "apex") == (None, ["name", "ids"])
+ assert bound_names(sig, "apex") == {"name", "ids"}