Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 31, 2025

  • Explore repository structure and understand the issue
  • Identify the bug in enum.rs (lines 430-434) - missing number-to-string conversion in template literals
  • Create test case for enum template literal with numbers
  • Fix the bug by converting numbers to strings in template literal evaluation
  • Verify the fix with manual testing (regular numbers, mixed types, negative numbers, floats, Infinity, NaN)
  • Run unit tests - all passing
  • Run code review - reviewed, minor comment about exhaustiveness (already handled)
  • Run security check - no issues found
  • Update transform conformance snapshots per CI feedback
  • Fix template literal quasi bug (use enumerate instead of zip)
  • Add test case for trailing quasi edge case
  • Task completed

Summary

Fixed a bug where numeric enum values in TypeScript were not converted to strings when used in template literals. The fix converts ConstantValue::Number to strings using to_js_string() method.

Fixed an additional bug where the last quasi (suffix) in template literals was being dropped due to using zip instead of properly iterating over all quasis with conditional expression access.

Updated the transform conformance snapshots to include both test cases.

Security Summary

No security vulnerabilities were found in the changes.

Original prompt

This section details on the original issue you should resolve

<issue_title>transformer: computed enum with number in string template is incorrect</issue_title>
<issue_description>playground
Original report: vitejs/rolldown-vite#472

Input

export enum NumberEnum {
  NUM_1 = 1000,
  NUM_2 = 2000,
  NUM_3 = 3000,
  NUM_4 = 4000,
}

export enum ComputedEnum {
  COMPUTED_1 = `${NumberEnum.NUM_1}-${NumberEnum.NUM_2}`,
  COMPUTED_2 = `${NumberEnum.NUM_3}-${NumberEnum.NUM_4}`,
}

console.log(Object.values(ComputedEnum));

Actual

export let NumberEnum = /* @__PURE__ */ function(NumberEnum) {
	NumberEnum[NumberEnum["NUM_1"] = 1e3] = "NUM_1";
	NumberEnum[NumberEnum["NUM_2"] = 2e3] = "NUM_2";
	NumberEnum[NumberEnum["NUM_3"] = 3e3] = "NUM_3";
	NumberEnum[NumberEnum["NUM_4"] = 4e3] = "NUM_4";
	return NumberEnum;
}({});
export let ComputedEnum = /* @__PURE__ */ function(ComputedEnum) {
	ComputedEnum["COMPUTED_1"] = "-";
	ComputedEnum["COMPUTED_2"] = "-";
	return ComputedEnum;
}({});
console.log(Object.values(ComputedEnum));

Expected

export let NumberEnum = /* @__PURE__ */ function(NumberEnum) {
	NumberEnum[NumberEnum["NUM_1"] = 1e3] = "NUM_1";
	NumberEnum[NumberEnum["NUM_2"] = 2e3] = "NUM_2";
	NumberEnum[NumberEnum["NUM_3"] = 3e3] = "NUM_3";
	NumberEnum[NumberEnum["NUM_4"] = 4e3] = "NUM_4";
	return NumberEnum;
}({});
export let ComputedEnum = /* @__PURE__ */ function(ComputedEnum) {
	ComputedEnum["COMPUTED_1"] = "1000-2000";
	ComputedEnum["COMPUTED_2"] = "3000-4000";
	return ComputedEnum;
}({});
console.log(Object.values(ComputedEnum));

</issue_description>

<agent_instructions>We should take a look at ./crates/oxc_transformer/src/typescript/enum.rs to check if there is a bug</agent_instructions>

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


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

@graphite-app
Copy link
Contributor

graphite-app bot commented Oct 31, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

Copilot AI changed the title [WIP] Fix computed enum with number in string template fix(transformer): convert enum numbers to strings in template literals Oct 31, 2025
Copilot AI requested a review from Dunqing October 31, 2025 06:39
Copilot finished work on behalf of Dunqing October 31, 2025 06:39
@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-bug Category - Bug labels Nov 2, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 2, 2025

CodSpeed Performance Report

Merging #15183 will not alter performance

Comparing copilot/fix-computed-enum-issue (b4c7d79) with main (ec6721c)

Summary

✅ 33 untouched
⏩ 4 skipped1

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Dunqing
Copy link
Member

Dunqing commented Nov 3, 2025

@copilot CI job fails, the reason is that you have to update the coverage snapshots

Copy link
Contributor Author

Copilot AI commented Nov 3, 2025

@copilot CI job fails, the reason is that you have to update the coverage snapshots

Updated the transform conformance snapshot in commit 6f83696.

Copilot AI requested a review from Dunqing November 3, 2025 01:50
Copilot finished work on behalf of Dunqing November 3, 2025 01:50
@Dunqing Dunqing marked this pull request as ready for review November 3, 2025 06:03
Copilot AI review requested due to automatic review settings November 3, 2025 06:03
@Dunqing Dunqing force-pushed the copilot/fix-computed-enum-issue branch from 6f83696 to 08626e2 Compare November 3, 2025 06:03
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for converting numeric enum values to strings within template literals during TypeScript enum transformation. The key change enables template literals in enum declarations to correctly interpolate numeric enum members (e.g., `${NumberEnum.NUM_1}-${NumberEnum.NUM_2}`).

  • Extended enum evaluation to handle ConstantValue::Number in template literals by converting them to strings
  • Added test case demonstrating template literals with numeric enum references

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
crates/oxc_transformer/src/typescript/enum.rs Modified template literal evaluation to handle both string and number constant values by matching on the enum type and converting numbers to JS strings
tasks/transform_conformance/tests/babel-plugin-transform-typescript/test/fixtures/enum-template-literal-number/input.ts Added test input with numeric enum and computed enum using template literals
tasks/transform_conformance/tests/babel-plugin-transform-typescript/test/fixtures/enum-template-literal-number/output.js Added expected transformation output
tasks/transform_conformance/snapshots/oxc.snap.md Updated test pass count and added semantic mismatch details for the new test case

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot finished work on behalf of Dunqing November 4, 2025 00:50
@Dunqing Dunqing requested a review from sapphi-red November 4, 2025 02:26
@Dunqing Dunqing merged commit 7a5c011 into main Nov 4, 2025
36 of 47 checks passed
@Dunqing Dunqing deleted the copilot/fix-computed-enum-issue branch November 4, 2025 03:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transformer: computed enum with number in string template is incorrect

3 participants