Unverified Commit 69d11874 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Fix constant inlining in shorthand properties (#98662)

### What?

Fixes constant replacement in object shorthand properties and adds
focused Turbopack execution coverage for the existing analyzer-aware
cross-module constants path.

### Why?

This is a pre-existing bug in `ConstantValueCodeGen`, which is shared by
the current analyzer-aware/opt-in constants implementation and the newer
codegen-only export-inlining work. When an imported constant is used as
`{ VALUE }`, the AST path ends at `Prop::Shorthand`; the old
expression-only visitor could replace the enclosing object literal,
collapsing `{ FIRST, SECOND }` to a single primitive.

This fix is intentionally independent of the export-inlining feature
stack so existing `turbopackCrossModuleConstants` users receive and
review the correction separately.

### How?

When the code-generation path ends at a shorthand property, it now
mirrors `EsmBinding`: the shorthand is expanded to a key/value property,
preserving the original key and generating the compile-time constant
only for the value. Other expression paths are unchanged, and generated
values retain the standard compile-time marker.

The execution fixture uses only analyzer-aware constants. Without the
fix it returns `"second"` instead of `{ FIRST: "first", SECOND: "second"
}`; with the fix it verifies the runtime object, both generated keys,
and both marked inlined values.

### Verification

- `NODE_PATH=/vercel/sandbox/test-deps/node_modules cargo test -p
turbopack-tests --test execution cross_module_constants_shorthand --
--nocapture`
- `cargo test -p turbopack-tests --test snapshot cross_module --
--nocapture`
- `cargo check -p turbopack-ecmascript -p turbopack-tests --tests`
- `cargo fmt --all -- --check`

<!-- NEXT_JS_LLM -->


<!-- fleet 29433e1c-e26e-435a-b516-45108f629d79 -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 9487264f
......@@ -7,6 +7,7 @@ use swc_core::{
ArrayLit, EsVersion, Expr, KeyValueProp, Lit, ObjectLit, Prop, PropName, Regex, Str,
},
parser::{Syntax, parse_file_as_expr},
visit::fields::PropField,
},
quote,
};
......@@ -37,12 +38,40 @@ impl ConstantValueCodeGen {
_chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<CodeGeneration> {
let value = self.value.clone();
let mut visitors = Vec::new();
let mut ast_path = self.path.0.clone();
let visitor = create_visitor!(self.path, visit_mut_expr, |expr: &mut Expr| {
*expr = value_to_expr(&value);
});
if matches!(
ast_path.last(),
Some(swc_core::ecma::visit::AstParentKind::Prop(
PropField::Shorthand
))
) {
ast_path.pop();
visitors.push(create_visitor!(
exact,
ast_path,
visit_mut_prop,
|prop: &mut Prop| {
if let Prop::Shorthand(ident) = prop {
*prop = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(ident.clone().into()),
value: value_to_expr(&value).into(),
});
}
}
));
} else {
visitors.push(create_visitor!(
self.path,
visit_mut_expr,
|expr: &mut Expr| {
*expr = value_to_expr(&value);
}
));
}
Ok(CodeGeneration::visitors(vec![visitor]))
Ok(CodeGeneration::visitors(visitors))
}
}
......
import { FIRST, SECOND } from 'shorthand-constants'
function readConstants() {
return { FIRST, SECOND }
}
it('inlines analyzer-aware constants in shorthand properties', () => {
expect(readConstants()).toEqual({ FIRST: 'first', SECOND: 'second' })
const source = readConstants.toString()
expect(source).toContain('FIRST:')
expect(source).toContain('SECOND:')
expect(source).toContain('TURBOPACK compile-time value')
})
{
"scopeHoisting": false,
"mangleExportNames": false,
"crossModuleConstants": true
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment