fix(pineToJS): allow the _ discard identifier to be declared multiple times - #302
Merged
Merged
Conversation
…imes
Pine Script treats `_` as a write-only discard identifier that may be
declared any number of times, in any scope, including several times in
the same scope (TradingView docs, "Using an underscore (_) as an
identifier"). JavaScript forbids re-declaring a let/var binding in one
scope, so scripts such as
[_, s1, _] = ta.macd(close, 12, 26, 9)
[_, s2, _] = ta.macd(close, 5, 10, 3)
_ = ta.sma(close, 10)
failed with `SyntaxError: Identifier '_' has already been declared`.
The codegen previously only de-duplicated `_` *within* a single tuple.
It now renames every `_` declaration target (plain declarations, tuple
declarations and `for [..] in` headers) to a fresh `_$N` placeholder.
`$` is not a legal Pine identifier character, so the placeholders cannot
collide with user variables.
Adds tests/transpiler/underscore-discard-identifier.test.ts as a
regression guard; expected values come from the same computations
written with uniquely-named variables.
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_46a2a17f-2b19-4949-8fb3-0a45881df53f) |
TradingView rejects any read of `_` (Undeclared identifier). PineTS surfaces every undeclared identifier as a runtime ReferenceError; these tests pin that `_` behaves the same and never silently resolves to a discarded value. 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_c0040e94-1cdf-4edd-922d-b58ff2f21b95) |
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 Script treats
_as a write-only discard identifier. Per the TradingView docs (Variable declarations → Using an underscore (_) as an identifier):PineTS only de-duplicated
_within a single tuple, so any script that declared_more than once in the same scope failed at the JS stage:Reproduced failures (before the fix):
_in the global scope_ = exprrepeated_followed by plain__in anifbranch followed by another_declaration after theifFix
src/transpiler/pineToJS/codegen.ts: every_declaration target — plain declarations, tuple declarations, andfor [..] inheaders — is renamed to a fresh_$Nplaceholder via a sharedrenameDiscardTargets/freshDiscardNamehelper.$is not a legal Pine identifier character, so the placeholders can never collide with user variables (the same_$Nscheme the codegen already uses for reserved-word renames).Generated JS for the script above:
The two previous copy-pasted intra-tuple dedup loops are replaced by the helper.
_stays write-onlyTradingView also says "A value assigned to such a variable cannot be accessed" — reading
_is a compile error there (Undeclared identifier "_"). Because every declaration target is renamed, a read of_in the generated JS is a genuinely undeclared identifier and fails withReferenceError: _ is not defined— the same way PineTS surfaces every other undeclared identifier. (Before this PR the first_in a tuple kept its name, soplot(_)silently returned the discarded value.) Tests pin this behavior.Tests
New
tests/transpiler/underscore-discard-identifier.test.ts(11 tests):ta.bbexample and a for-in loop body. Expected values come from the same computations written with uniquely-named variables (already-supported Pine), so the tests assert correctness of the resulting series, not just "no throw". All failed before the fix withIdentifier '_' has already been declared._(plot(_),_ + 1, inside a function body) throws._declaration survives and each placeholder is unique.Full suite:
npm test -- --run→ 175 files / 1863 tests passed, 0 failures.Notes
var [_, a, _] = ...is still rejected by the parser (Expected identifier after var). This matches TradingView, which does not allowvar/varipon tuple declarations._ := 5(reassigning_) is not caught. This is pre-existing general behavior — any:=to an undeclared name (e.g.foo := 5) currently becomes a sloppy-mode global rather than an error — and is not specific to_, so it is left out of this PR.