Skip to content

Conversation

@cuyl
Copy link
Contributor

@cuyl cuyl commented Dec 19, 2025

A huge refactor to improve variable evaluate for defaultMessage and description.

Fixes #564, Fixes #565, Fixes #566

@changeset-bot
Copy link

changeset-bot bot commented Dec 19, 2025

⚠️ No Changeset found

Latest commit: e326be8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

See https://rustdoc.swc.rs/swc_ecma_minifier/eval/struct.Evaluator.html
Please use it, instead of implemengint it manually.

@cuyl
Copy link
Contributor Author

cuyl commented Dec 20, 2025

@kdy1 Thanks

@cuyl
Copy link
Contributor Author

cuyl commented Dec 21, 2025

I’ve attempted to refactor the evaluation logic by replacing evaluate_template_literal_string_with_visitor with Evaluator::eval_tpl and evaluate_expression_with_visitor with Evaluator::eval.

However, it appears that Evaluator::eval does not currently support BinaryExpression, which is causing the current build/test failures.

So I leave evaluate_expression_with_visitor here, and I found some code in crates/swc_ecma_minifier/src/compress/pure/strings.rs can concat strings but it's private that I cannot use.

@kdy1

@kdy1
Copy link
Member

kdy1 commented Dec 21, 2025

Can you send a PR towards https://github.com/swc-project/swc?

@kdy1
Copy link
Member

kdy1 commented Dec 21, 2025

@cuyl
Copy link
Contributor Author

cuyl commented Dec 21, 2025

OK, I will open a PR to swc.
@kdy1

@cuyl
Copy link
Contributor Author

cuyl commented Dec 21, 2025

Waiting for swc-project/swc#11390

I found that Evaluator::data does similar job with variable_bindings here, so I move the resolve_identifier to Evaluator.
In order to keep React Compiler work #467 , I have to explode the store function.
I'm not sure if we can move this part of logic to swc.

fn visit_mut_assign_expr(&mut self, assign_expr: &mut AssignExpr) {
assign_expr.visit_mut_children_with(self);
// Track assignment expressions for React Compiler optimizations
// Handle patterns like: t1 = { ... }
if let AssignTarget::Simple(SimpleAssignTarget::Ident(ident)) = &assign_expr.left {
let variable_id = ident.id.to_id();
// Check if we already have a binding for this variable
let should_update = match self.resolve_identifier(ident) {
Some(existing_expr) => {
// Only overwrite if the new expression is an object literal
// and the existing one is not, or if both are object literals
match (existing_expr, &*assign_expr.right) {
(Expr::Object(_), Expr::Object(_)) => true, // Both objects, update
(_, Expr::Object(_)) => true, /* New is object, existing */
// is not, update
(Expr::Object(_), _) => false, /* Existing is object, new is not, don't */
// update
_ => true, // Neither is object, update
}
}
None => true, // No existing binding, always update
};
if should_update {
self.evaluator.store(variable_id, &assign_expr.right);
}
}
}

@kdy1

@kdy1
Copy link
Member

kdy1 commented Dec 21, 2025

I think we should move it to the main SWC repository. Nice catch! It seems like I didn't review the PR for fixing the React Compielr support carefully enough.

kdy1 pushed a commit to swc-project/swc that referenced this pull request Dec 22, 2025
**Description:**

Improve Evaluator for `@swc/plugin-formatjs` usage.

 - Support evaluate BinaryExpression.
 - Add public functions store and resolve_identifier.
 - Make `eval_as_expr` a public function.

**Related issue:**

ref: swc-project/plugins#567
@kdy1
Copy link
Member

kdy1 commented Dec 24, 2025

Do you want me to update swc_core of this repository to reflect Evaluator changes?

@kdy1
Copy link
Member

kdy1 commented Dec 24, 2025

Oh… I realized that I forgot to publish the changes. My bad, sorry.

@cuyl
Copy link
Contributor Author

cuyl commented Dec 24, 2025

No worries, you can publish it and then upgrade swc_core in this branch.
@kdy1

@cuyl cuyl requested a review from kdy1 December 25, 2025 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants