Skip to content

javascript/typescript func_start: ternary's cond ? name : function(){} branch misattributes the other-branch identifier as the function's name #1632

Closed

Description

@squid-protocol

Summary

func_start's object-literal-method branch (^[ \t]*identifier(?=[ \t\n]*:[ \t\n]*(?:async\s*)?(?:function...|...=>))) exists to catch foo: function() {} object-literal shorthand and foo: () => {} arrow assignments. It cannot distinguish that shape from a ternary expression whose true-branch is a bare identifier and whose false-branch is an anonymous function:

process = special ?
    mightThrow :
    function() {
        ...
    };

Here mightThrow is a plain identifier reference (the ternary's true branch), not an object key. mightThrow : followed (across the vertical-assignment-shield's [ \t\n]*) by function() { on the next line satisfies the branch anyway, so GitGalaxy reports a phantom function named mightThrow starting at the function() { — misattributing the ternary's other branch identifier as this anonymous function's name.

Repro

language-crucible/data/javascript/jquery/deferred.js:182-184:

process = special ?
    mightThrow :
    function() {
        try {
            mightThrow();

Confirmed via direct DB query on a galaxyscope --db-only scan of the file: function_data records mightThrow|184|0 in addition to the real mightThrow|108|0 (the actual named function-expression definition earlier in the file, itself real and correctly detected). Confirmed via tree_sitter_language_pack: the real, tree-sitter-parsed function() { at line 184 is a bare anonymous function_expression whose parent is a ternary_expression, not a pair/variable_declarator/assignment_expression — there is no real name to attribute to it at all.

This is the same regex text in typescript's func_start (gitgalaxy/standards/language_standards.py, ^[ \t]*[a-zA-Z_$][\w$]*(?=[ \t\n]*:[ \t\n]*(?:async\s*)?(?:<...>\s*)?(?:function...|...=>))), so the identical false-positive shape (cond ? name : function(){}/cond ? name : () => {}) is expected to reproduce there too — not independently confirmed with a TS corpus repro in this pass.

Suggested fix shape

The branch needs to rule out "identifier is a ternary true-branch, not an object/namespace key" the same way #1221's Invocation Shield ruled out bare call statements: a real object-literal-method/namespace-assignment key: is never preceded on the same logical statement by a ? — so a negative lookbehind for a preceding ? (skipping intervening whitespace/newlines, bounded the same way the rest of this regex bounds its whitespace runs) before the anchor would exclude the ternary shape while leaving genuine foo: function(){} object keys (which are never themselves inside a ternary's true-branch position) unaffected. Needs verification against a batch of real ? ... : function and ? ... : () => corpus samples to confirm no real object-key case gets false-negatived by the lookbehind.

Out of scope

  • Whether addHelper: function () { ... }() (an IIFE that immediately invokes and returns a different anonymous function) in threejs/Editor.js:392 should still be named addHelper — investigated during the same pass, found genuinely ambiguous (GitGalaxy names the outer factory; the literal addHelper binding is actually whatever the IIFE returns) rather than a clear bug, not pursued further here.
  • General ground-truth measurement artifacts in Flow-typed react/*.js corpus files (tree-sitter's plain JS grammar can't parse Flow syntax) — filed separately, see the sibling issue from this same investigation pass.

Scope note

This only affects func_start name-extraction accuracy (epic #813's extraction pillars), not risk-scoring signatures — a false function name doesn't change any risk/threat metric, just which name a real function record is attributed to.

Activity

  1. added
    bug
    testing
    core-engine
    threat: false-positive
    on Aug 14, 2026
  2. uuzzrm commented on Aug 15, 2026

    @uuzzrm
    Contributor

    Opened #1657 for this. The fix rules out the ternary true-branch shape with a bounded backward scan for a preceding ? - a real object/namespace key is never preceded by ? (skipping whitespace), so that position is exclusively the ternary case. Repro yields zero satellites now; javascript extra_functions 11 -> 10 with no recall loss. Pairs with #1654 and #1655 - same _slice_by_braces region, independent fixes.

  3. added this to the v2.4.8 milestone on Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-enginepriority: mediumtestingthreat: false-positive

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions