Fix semantic analyzer incorrectly flagging named parameters as undefined variables #62
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.
The semantic analyzer treated named parameters in function calls (e.g.,
title="Length",shorttitle="ADR") as variable references, causing false "undefined variable" errors.Changes
Modified
SemanticAnalyzer.ts: When visiting function call arguments, detectAssignmentnodes representing named parameters and validate only their right-hand side (value), not the left-hand side (parameter name)Added test coverage: 6 tests covering named parameters in
indicator(),input.*(),plot(), mixed positional/named parameters, and validation that undefined variables are still caughtExample
This now transpiles successfully:
Previously failed with:
Original prompt
Fix: Semantic analyzer incorrectly flags named parameters as undefined variables
Problem
The semantic analyzer is incorrectly reporting errors for named parameters in function calls. When parsing PineScript like:
The analyzer treats the left-hand side of named arguments (
shorttitle,timeframe,timeframe_gaps,title) as variable references and reports:Root Cause
In
SemanticAnalyzer.ts, when visiting function call arguments, the analyzer visits each argument expression. When an argument is a named parameter (anAssignmentnode liketitle="Length"), thevisitExpressionmethod visits the left-hand sideIdentifiernode and checks if it's defined in the symbol table - which it isn't, because it's a parameter name, not a variable reference.The current code at lines 100-105 correctly skips
IndicatorDeclarationitself:But the issue occurs in
visitExpressionwhen processingAssignmentnodes that are function arguments (named parameters).Solution
Modify the semantic analyzer to recognize when an
Assignmentnode is being used as a named parameter (inside a function call) vs. a variable assignment (at statement level).When visiting function call arguments:
Assignmentnode, it's a named parameterImplementation
In
packages/pine2ts/src/transpiler/semantic/SemanticAnalyzer.ts:FunctionCallcase invisitExpressionto handle named arguments specially:Also handle
MethodCallnodes the same way, as they can also have named parameters.Add test cases to verify:
indicator()with named parameters worksinput.int(),input.float(), etc. withtitle=parameter worksplot()withtitle=,color=parameters worksAffected Files
packages/pine2ts/src/transpiler/semantic/SemanticAnalyzer.tsTest Cases
Add tests in
packages/pine2ts/tests/semantic.test.ts:Acceptance Criteria
indicator()with named parameters (shorttitle=,overlay=,timeframe=, etc.) transpiles without errorsinput.*()functions with named parameters (title=,minval=,maxval=, etc.) transpile without errorsplot()with named parameters (title=,color=,linewidth=, etc.) transpiles without errorsThis pull request was created as a result of the following prompt from Copilot chat.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.