Repository navigation
fix(transpiler): user-defined functions named after a constant namespace - #310
Merged
Merged
Conversation
Pine allows a script to declare a function whose name matches a constant namespace (`position(x) => close + x`) and to keep using that namespace's members in the same script. Two transpiler passes broke this: - The pineToJS codegen collision pass renamed the declaration to `position_$N` but left bare call sites alone (it assumed a bare callee is always the built-in, which is only true for VARIABLE collisions such as `fill = 3` alongside `fill(p1, p2)`), so `plot(position(14))` resolved to the constants object -> `TypeError: position is not a function`. The pass now records which collision names were declared as functions and renames their bare callees too. - The parser's `name -> name_var` rewrite (for a variable sharing a user function's name) fired on the namespace base of `position.top_right` once a `position()` function existed -> `ReferenceError: position_var is not defined`. It now skips a collision-name identifier followed by `.`. Affects every entry of NAMESPACE_COLLISION_NAMES and makes adding names to that list (e.g. `scale`, #305) safe for scripts that use them as functions. Tests: tests/transpiler/namespace-identifier-collision.test.ts (7 new cases: bare call, member-access coexistence, argument position, nested/indirect calls, variable-collision guard, `scale` forward guard). Co-authored-by: Cursor <cursoragent@cursor.com>
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_78b89a92-4aa8-4ca1-908e-844b50faa144) |
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_8d502aea-c463-4d6c-9bd9-cd78f2c1e669) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Pine allows a user-defined function to share its name with a constant namespace, and to keep using that namespace's members in the same script. This valid script fails on
devtoday:→
TypeError: position is not a functionTwo transpiler passes are involved:
Codegen collision pass (
codegen.ts,renameVariableRefsInAST): the declaration is renamed toposition_$0, but bare call sites are deliberately left alone. That assumption is right for variable collisions (fill = 3next to the built-infill(p1, p2)) but wrong for function collisions — a constants namespace is not callable, soposition(14)can only mean the user function.Parser
name → name_varrewrite (parser.ts, ~L1749): once a functionposition()exists, any non-call reference topositionis treated as a variable sharing the function's name. That fires on the namespace base ofposition.top_right→position_var.top_right→ReferenceError: position_var is not defined.This affects every entry of
NAMESPACE_COLLISION_NAMES(position,font,order,currency,size,format, …) and was introduced with the 0.9.32 "user variables named after a constant namespace" fix, which only considered variables. It also means adding a name to that list can turn a working script into a broken one — which is exactly what happens toscale(x)=>close+xwith #305 as-is (works ondev,TypeError: scale is not a functionon the PR branch).Fix
codegen.ts: track collision names that were declared as aFunctionDeclaration(userFunctionCollisions) and rename their bare callees, alongside the existingJS_RESERVED_WORDScallee rename. Variable collisions are unchanged (guarded by a test).parser.ts: the_varrewrite skips an identifier that is a collision name and is immediately followed by.(namespace member access).Both changes are in the pineToJS stage only; the Phase-2 transpiler is untouched.
Tests
tests/transpiler/namespace-identifier-collision.test.ts, newdescribeblock, 7 cases:position(x)=>…bare call resolves to the user functionposition(x)=>…coexisting withtable.new(position.top_right, …)font(x)=>…coexisting withfont.family_monospaceas a named argumentmath.max(...)and in arithmeticfillis still renamed while the built-infill(...)keeps reaching the namespacescale(x)=>…works (must keep working oncescalebecomes a collision name)5 of these fail on
devfor the stated reasons (is not a function×4,position_var is not defined×1); all pass with the fix.Regression check
devbaseline is 1838, +7 new tests, 0 failures.tsc --emitDeclarationOnly -p tsconfig.dts.json: clean.scale(x)=>close+xandindicator(scale = scale.right)in the same script.Relation to #305
#305 is correct in what it adds, but on its own it regresses
scale(x)=>…. With this PR merged first, #305 becomes safe to merge as-is.Note
Medium Risk
Changes pineToJS identifier renaming for every
NAMESPACE_COLLISION_NAMESentry when used as UDFs; mistakes could mis-route calls vs namespace access, but behavior is narrowly scoped and heavily regression-tested.Overview
Fixes valid Pine scripts that declare a user function with the same name as a constant namespace (
position(x) => …,font(x) => …) while still using namespace members likeposition.top_right.The pineToJS codegen collision pass now tracks collision names declared as functions (
userFunctionCollisions) and renames bare call sitesname(...)toname_$N, not only the declaration. Variable collisions stay unchanged:fill = 3plus built-infill(p1, p2)still targets the namespace at the call.The parser no longer applies the
name → name_varrewrite when a collision-name identifier is immediately followed by., soposition.top_rightstays a namespace access instead of becomingposition_var.top_right.Regression coverage is in
tests/transpiler/namespace-identifier-collision.test.ts(UDF calls, mixed namespace use, nested/indirect calls, and the variable-collision guard). This also unblocks safely extendingNAMESPACE_COLLISION_NAMES(e.g.scalein #305) for scripts that use those names as functions.Reviewed by Cursor Bugbot for commit 3b451e6. Bugbot is set up for automated code reviews on this repo. Configure here.