Fix recursive const and enum resolution during transpilation#1578
Fix recursive const and enum resolution during transpilation#1578TwitchBronBron merged 5 commits intomasterfrom
Conversation
Co-authored-by: TwitchBronBron <[email protected]>
Co-authored-by: TwitchBronBron <[email protected]>
TwitchBronBron
left a comment
There was a problem hiding this comment.
This looks great! Can you please add unit tests that prove cyclical const references are mitigated? (something like
const a = b
const b = c
const c = a
also consts inside array literal consts.
|
Hey there! I just built a new temporary npm package based on 167b20b. You can download it here or install it by running the following command: npm install https://github.com/rokucommunity/brighterscript/releases/download/v0.0.0-packages/brighterscript-0.70.2-copilot-fix-nested-consts-transpilation.20251028160636.tgz |
TwitchBronBron
left a comment
There was a problem hiding this comment.
Please fix the situation where enums are not resolved when used in consts. Make a few tests where the items are all spread across multiple files, and prove it still recursively resolves them all. Like this:
'source/theming1.bs
namespace Themeing
const BACKGROUND_COLOR = Theming.Color.BLACK
end namespace'source/theming2.bs
namespace Theming
enum Color
BLACK = "#000000"
end enum
end namespace'source/test.bs
sub test()
aa = {
backgroundOverlay: {
color: Themeing.BACKGROUND_COLOR
}
}
end subAlso handle a const using an enum using a const using an enum. (like const a, enum b, const c, enum d)
|
@copilot can you please implement my latest review comment with code changes to fix the issue? |
…ains - Enhanced resolveConstValue to recursively resolve both consts and enums - Added circular reference detection with isCircular flag to prevent infinite loops - Added tests for cyclical const references - Added tests for consts inside array literals - Added tests for enum used in const (same file and cross-file) - Added tests for const->enum->const->enum chains across multiple files - All 26 ConstStatement tests passing Co-authored-by: TwitchBronBron <[email protected]>
Added comprehensive tests for cyclical const references and consts inside array literals. The cyclical reference test verifies that infinite loops are prevented - when circular references are detected, the transpiler leaves the reference as-is rather than entering an infinite resolution loop. Commit: 5e1e9f8
Enhanced the recursive resolution to handle enums in addition to consts. Added comprehensive multi-file tests covering:
All scenarios now correctly resolve to their final literal values, including when definitions are spread across multiple files. Commit: 5e1e9f8 |
|
Hey there! I just built a new temporary npm package based on 5e1e9f8. You can download it here or install it by running the following command: npm install https://github.com/rokucommunity/brighterscript/releases/download/v0.0.0-packages/brighterscript-0.70.2-copilot-fix-nested-consts-transpilation.20251029143045.tgz |
|
Hey there! I just built a new temporary npm package based on 5e1e9f8. You can download it here or install it by running the following command: npm install https://github.com/rokucommunity/brighterscript/releases/download/v0.0.0-packages/brighterscript-0.70.2-copilot-fix-nested-consts-transpilation.20251029143046.tgz |
|
Hey there! I just built a new temporary npm package based on 1150bec. You can download it here or install it by running the following command: npm install https://github.com/rokucommunity/brighterscript/releases/download/v0.0.0-packages/brighterscript-0.70.2-copilot-fix-nested-consts-transpilation.20251031132538.tgz |
Fixes an issue where nested const and enum references were not being fully resolved during transpilation, resulting in incorrect output with extra parentheses or unresolved namespace references instead of the final literal values.
Problem
When a const references another const or enum (either directly or through namespace paths), the transpiler was only performing single-level resolution. This caused issues like:
Would transpile to:
Additionally, enums used in consts were not being resolved at all, especially across files. As shown in the provided screenshots,
Theming_Color_BLUEandTheming_PRIMARY_COLORwere not being resolved to their final literal values when definitions were spread across multiple files.Solution
Implemented recursive const and enum resolution in
BrsFilePreTranspileProcessor.ts:resolveConstValue()method to recursively follow both const and enum references until reaching a final literal valueisCircularflag to prevent infinite loopsprocessExpression()to use recursive resolution for both consts and enumsThe fix ensures that:
const FLAG_B = FLAG_Aresolves to the final value ofFLAG_Aaa.bb.FLAG_Aare fully resolvedTest Coverage
Added comprehensive test cases covering:
aa.bb.FLAG_A)All existing const functionality continues to work unchanged, ensuring backward compatibility.
Fixes #1506
Original prompt
Fixes #1506
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.