Skip to content

Conversation

Copy link

Copilot AI commented Oct 22, 2025

Problem

When Rooibos code coverage was enabled, the generated instrumented code changed the constructor execution order between base and derived classes. In the instrumented output, coverage tracking calls were inserted before super() calls, whereas super() should execute first to maintain standard class construction semantics.

Before this fix:

instance.new = function()
    RBS_CC_1_reportLine("9", 1)  ← Coverage call BEFORE super()
    m.super0_new()               ← Super() call
    RBS_CC_1_reportLine("10", 1)
    ? "DerivedClass constructor"
end function

This could potentially change initialization behavior and timing, violating the expected constructor call sequence.

Solution

Modified the CodeCoverageProcessor to detect super() calls and handle them specially:

  1. Added super() call detection: The processor now identifies when it's processing a super() call expression statement
  2. Special coverage insertion: For super() calls, coverage tracking is inserted after the call instead of before it
  3. Preserved execution order: Ensures super() executes first, followed by coverage tracking

After this fix:

instance.new = function()
    m.super0_new()               ← Super() call executes FIRST
    RBS_CC_1_reportLine("9", 1)  ← Coverage tracking comes AFTER
    RBS_CC_1_reportLine("10", 1)
    ? "DerivedClass constructor"
end function

Implementation Details

  • Added isSuperCallStatement() method to identify super() calls in the AST
  • Added convertSuperCallToCoverageStatement() method that uses arraySplice(owner, key + 1, 0, parsed) instead of arraySplice(owner, key, 0, parsed) to insert coverage tracking after the statement
  • Modified the ExpressionStatement handler in the AST walker to use special handling for super() calls

Testing

Added comprehensive tests covering:

  • Simple inheritance with single super() calls
  • Complex inheritance hierarchies with multiple levels
  • Verification that coverage tracking still works correctly
  • Regression testing to ensure existing functionality is preserved

All existing tests continue to pass, confirming no regressions were introduced.

Impact

This fix ensures that code instrumentation preserves the original constructor call sequence and semantics, addressing the issue where coverage mode could change the initialization order of class hierarchies.

Original prompt

This section details on the original issue you should resolve

<issue_title>Coverage instrumentation changes constructor execution order</issue_title>
<issue_description>### Description

When Rooibos/BrighterScript code coverage is enabled, the generated instrumented code changes the constructor execution order between base and derived classes.
In the instrumented dist/... output, the derived class’s constructor body runs before the super call, whereas in a normal (non-coverage) build, super executes first — as expected in standard class construction semantics.

Instrumentation should not alter constructor order; it should only add line-tracking markers between statements.


Observed behavior

Under coverage mode, the derived constructor executes before the super call, changing initialization order.


Expected behavior

Code instrumentation should preserve the original constructor call sequence and semantics.


Environment

  • Rooibos version: v6.0.0-alpha.48
    ("rooibos-roku": "https://github.com/rokucommunity/rooibos/releases/download/v6.0.0-alpha.48/rooibos-roku-6.0.0-alpha.48.tgz")
  • BrighterScript version: v1
  • Project type: Roku SceneGraph app (BrighterScript)
  • Setting: isRecordingCodeCoverage = true</issue_description>

<agent_instructions>Make sure you write good unit tests to prove there's an issue before you solve the problem. You should be able to compare the transpiled output, since the instrumentation is what's causing the issue. </agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #354


💡 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.

Copilot AI and others added 2 commits October 22, 2025 19:40
Co-authored-by: TwitchBronBron <2544493+TwitchBronBron@users.noreply.github.com>
Co-authored-by: TwitchBronBron <2544493+TwitchBronBron@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix coverage instrumentation constructor execution order Fix coverage instrumentation changing constructor execution order Oct 22, 2025
Copilot AI requested a review from TwitchBronBron October 22, 2025 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Coverage instrumentation changes constructor execution order

2 participants