Skip to content

apex func_start false-positives on new ClassName( constructor calls #1963

Description

@squid-protocol

Summary

GitGalaxy's apex func_start regex (gitgalaxy/standards/language_standards.py, apex rule
block) misidentifies new ClassName(...) object-instantiation expressions as function/method
definitions when the new keyword sits at the start of a source line -- a common shape in
multi-line SObject-builder calls, e.g.:

Account acct = (Account) TestFactory.createSObject(
    new Account(name = 'Original Name'),
    true
);

Line 2 above (new Account(name = 'Original Name'),) gets matched as a function definition named
Account.

Root cause

The regex's optional return-type/modifier prefix group --
(?:[a-zA-Z_][\w.]*(?:generics)?(?:\[\])*\s+)? -- accepts any identifier-shaped token followed
by whitespace at the start of a line, with no exclusion list beyond the captured name itself
(class|interface|enum|if|for|while|switch|catch). new is a reserved instantiation keyword in
Apex, never a legitimate return type or modifier, but nothing excludes it from that prefix slot.
So new Account( parses as "return type = new, function name = Account" and passes the #1221
gating fix (which only requires the line start with some annotation/modifier/return-type-shaped
token, not that the token be a real one).

tree-sitter does not make this mistake: its Apex grammar structurally distinguishes
object_creation_expression from method_declaration.

Evidence

Discovered via the tri-comparison ledger
(apex/function/existence/agree[gitgalaxy]_vs[tree_sitter], 2 sampled occurrences,
AuraEnabledRecipes_Tests.cls:25 and :43). Confirmed by direct source read and by running the
regex standalone against the whole language-crucible/data/apex corpus, which surfaces the same
shape 6 times across 2 files:

  • apex-recipes/AuraEnabledRecipes_Tests.cls:6,25,43 -- new Account(...)
  • apex-recipes/SOQLRecipes_Tests.cls:226,235 -- new Account(...)
  • apex-recipes/SOQLRecipes_Tests.cls:504 -- new Opportunity(...)

All 6 are multi-line constructor-call arguments, none are real method/constructor definitions.

Suggested fix

Exclude new from the optional return-type/modifier prefix group in the apex func_start (and
likely args, which shares the identical prefix shape) regex -- e.g. a negative lookahead
(?!new\b) immediately before that group, so new can never be consumed as a pseudo return type.

Ledger

Tracked as validated in docs/self_scan/tri_comparison_ledger.json under
apex/function/existence/agree[gitgalaxy]_vs[tree_sitter] -- see that entry's verdict for the
full investigation writeup.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugUnintended behavior or logic failure in the engine

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions