Skip to content

Commit

Permalink
fix: rewrite object shorthand property reference (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin authored Dec 4, 2023
1 parent cd52449 commit 9f3332b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
19 changes: 17 additions & 2 deletions crates/rolldown/src/bundler/renderer/impl_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'ast, 'r> Visit<'ast> for AstRenderer<'r> {

// `IdentifierReference` in callee position need to be handled specially
if let oxc::ast::ast::Expression::Identifier(s) = &expr.callee {
self.render_identifier_reference(s, true);
self.render_identifier_reference(s, true, false);
} else {
self.visit_expression(&expr.callee);
}
Expand All @@ -77,7 +77,7 @@ impl<'ast, 'r> Visit<'ast> for AstRenderer<'r> {
}

fn visit_identifier_reference(&mut self, ident: &oxc::ast::ast::IdentifierReference) {
self.render_identifier_reference(ident, false);
self.render_identifier_reference(ident, false, false);
}

fn visit_import_declaration(&mut self, decl: &oxc::ast::ast::ImportDeclaration<'ast>) {
Expand Down Expand Up @@ -248,4 +248,19 @@ impl<'ast, 'r> Visit<'ast> for AstRenderer<'r> {
self.visit_binding_pattern(&pat.left);
self.visit_expression(&pat.right);
}

fn visit_object_property(&mut self, prop: &ast::ObjectProperty) {
// rewrite `const val = { a };` to `const val = { a: a.xxx }`
if let (ast::PropertyKey::Identifier(ident), ast::Expression::Identifier(reference)) =
(&prop.key, &prop.value)
{
// Here compare name and span of ident and reference to distinguish shorthand property
if ident.name == reference.name && ident.span == reference.span {
self.render_identifier_reference(reference, false, true);
return;
}
}
self.visit_property_key(&prop.key);
self.visit_expression(&prop.value);
}
}
10 changes: 9 additions & 1 deletion crates/rolldown/src/bundler/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,28 @@ impl<'r> AstRenderer<'r> {
&ident.name,
ident.span,
false,
false,
);
}

fn render_identifier_reference(
&mut self,
ident: &'_ oxc::ast::ast::IdentifierReference,
is_callee: bool,
shorthand: bool,
) {
let Some(symbol_id) = self.ctx.module.scope.symbol_id_for(ident.reference_id.get().unwrap())
else {
// This is global identifier references, eg `console.log`. We don't need to rewrite it.
return;
};
self.rewrite_symbol((self.ctx.module.id, symbol_id).into(), &ident.name, ident.span, is_callee);
self.rewrite_symbol(
(self.ctx.module.id, symbol_id).into(),
&ident.name,
ident.span,
is_callee,
shorthand,
);
}
}

Expand Down
15 changes: 10 additions & 5 deletions crates/rolldown/src/bundler/renderer/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl<'r> AstRenderer<'r> {
original_name: &Atom,
pos: Span,
is_callee: bool,
shorthand: bool,
) {
let canonical_ref = self.ctx.graph.symbols.par_canonical_ref_for(symbol_ref);
let symbol = self.ctx.graph.symbols.get(canonical_ref);
Expand All @@ -138,11 +139,15 @@ impl<'r> AstRenderer<'r> {
};

if original_name != &rendered_symbol {
self.ctx.source.update(
pos.start,
pos.end,
if is_callee { format!("(0, {rendered_symbol})",) } else { rendered_symbol.into_owned() },
);
if shorthand {
self.ctx.source.update(pos.start, pos.end, format!("{original_name}: {rendered_symbol}"));
} else {
self.ctx.source.update(
pos.start,
pos.end,
if is_callee { format!("(0, {rendered_symbol})",) } else { rendered_symbol.into_owned() },
);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: crates/rolldown/tests/common/case.rs
expression: content
input_file: crates/rolldown/tests/fixtures/deconflict/object_shorthand_property
---
# Assets

## main.mjs

```js
import { default as assert } from "assert";
// foo.js
var foo_default = 'foo'
// main.js
const value = { foo: foo_default }
assert.strictEqual(value.foo, 'foo')
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'foo'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import foo from './foo'
import assert from 'assert'

const value = { foo }

assert.strictEqual(value.foo, 'foo')
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"input": {
"external": [
"assert"
]
}
}

0 comments on commit 9f3332b

Please sign in to comment.