Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 15, 2024
1 parent 98dbbe1 commit 96c9853
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 15 deletions.
114 changes: 110 additions & 4 deletions crates/oxc_transformer/src/es2018/object_rest_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,120 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
let Expression::AssignmentExpression(assign_expr) = expr else {
return;
};
dbg!(&assign_expr.left);
let AssignmentTarget::ObjectAssignmentTarget(target) = &mut assign_expr.left else {
if !Self::has_nested_target_rest(&assign_expr.left) {
return;
};
let Some(rest) = target.rest.take() else { return };
}

let mut builder =
DeclsBuilder::new(VariableDeclarationKind::Var, &mut assign_expr.right, ctx);

let data = Self::walk_assignment_target(&mut assign_expr.left, &mut builder, ctx);

if !builder.decls.is_empty() {
for node in ctx.ancestors() {
if let Ancestor::ExpressionStatementExpression(decl) = node {
let declaration = ctx.ast.alloc_variable_declaration(
SPAN,
VariableDeclarationKind::Var,
builder.decls,
false,
);
let statement = Statement::VariableDeclaration(declaration);
self.ctx.statement_injector.insert_before(&decl.address(), statement);
break;
}
}
}

let mut expressions = ctx.ast.vec();

expressions.push(ctx.ast.expression_assignment(
assign_expr.span,
assign_expr.operator,
ctx.ast.move_assignment_target(&mut assign_expr.left),
builder.maybe_bound_identifier.create_read_expression(ctx),
));

for datum in data {
let (lhs, rhs) = datum.get_lhs_rhs(
&builder.maybe_bound_identifier,
&mut self.excluded_variabled_declarators,

Check warning on line 207 in crates/oxc_transformer/src/es2018/object_rest_spread.rs

View workflow job for this annotation

GitHub Actions / Spell Check

"variabled" should be "variables" or "variable".
self.ctx,
ctx,
);
if let BindingPatternOrAssignmentTarget::AssignmentTarget(lhs) = lhs {
let op = AssignmentOperator::Assign;
expressions.push(ctx.ast.expression_assignment(SPAN, op, lhs, rhs));
}
}

*expr = ctx.ast.expression_sequence(assign_expr.span, expressions);
}

fn walk_assignment_target(
target: &mut AssignmentTarget<'a>,
builder: &mut DeclsBuilder<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Vec<Datum<'a>> {
match target {
AssignmentTarget::ObjectAssignmentTarget(t) => {
Self::transform_object_assignment_target(t, builder, ctx)
.map_or_else(|| vec![], |d| vec![d])
}
_ => vec![],
}
}

fn transform_object_assignment_target(
object_assignment_target: &mut ObjectAssignmentTarget,
builder: &mut DeclsBuilder<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Datum<'a>> {
let rest = object_assignment_target.rest.take();
None
// let datum = Datum {
// lhs: BindingPatternOrAssignmentTarget::AssignmentTarget(rest.target),
// path: vec![],
// keys: ctx.ast.vec(),
// has_no_properties: true,
// all_primitives: true,
// };
// let data = t.properties.iter_mut().flag_map(|p| {
// match p {
// AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(_) => None
// AssignmentTargetProperty::AssignmentTargetPropertyProperty(p) => {
// match &p.binding {
// }
// }
// }
}

fn has_nested_target_rest(target: &AssignmentTarget<'a>) -> bool {
match target {
AssignmentTarget::ObjectAssignmentTarget(t) => {
t.rest.is_some()
|| t.properties.iter().any(|p| match p {
AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(_) => false,
AssignmentTargetProperty::AssignmentTargetPropertyProperty(p) => {
match &p.binding {
AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(t) => {
Self::has_nested_target_rest(&t.binding)
}
_ => Self::has_nested_target_rest(p.binding.to_assignment_target()),
}
}
})
}
AssignmentTarget::ArrayAssignmentTarget(t) => {
t.elements.iter().flatten().any(|e| match e {
AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(t) => {
Self::has_nested_target_rest(&t.binding)
}
_ => Self::has_nested_target_rest(e.to_assignment_target()),
}) || t.rest.as_ref().is_some_and(|r| Self::has_nested_target_rest(&r.target))
}
_ => false,
}
}
}

Expand Down
44 changes: 33 additions & 11 deletions tasks/transform_conformance/snapshots/babel_exec.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Error: 'eval' and 'arguments' cannot be used as a binding identifier in strict m
❯ ssrTransformScript ../../node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:52319:11
❯ loadAndTransform ../../node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:51917:72

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/10]
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/11]

⎯⎯⎯⎯⎯⎯ Failed Tests 9 ⎯⎯⎯⎯⎯⎯⎯
⎯⎯⎯⎯⎯⎯ Failed Tests 10 ⎯⎯⎯⎯⎯⎯⎯

FAIL fixtures/babel-plugin-transform-object-rest-spread-test-fixtures-assumption-pureGetters-rest-remove-unused-excluded-keys-exec.test.js > exec
AssertionError: expected true to be false // Object.is equality
Expand All @@ -31,7 +31,7 @@ AssertionError: expected true to be false // Object.is equality
| ^
11| })

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/10]
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/11]

FAIL fixtures/babel-plugin-transform-object-rest-spread-test-fixtures-assumption-pureGetters-spread-single-call-exec.test.js > exec
AssertionError: expected { foo: +0, middle: 1, bar: 1 } to deeply equal { foo: +0, middle: +0, bar: 1 }
Expand All @@ -54,7 +54,7 @@ AssertionError: expected { foo: +0, middle: 1, bar: 1 } to deeply equal { foo: +
13| foo: 0,
14| middle: 0,

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/10]
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/11]

FAIL fixtures/babel-plugin-transform-object-rest-spread-test-fixtures-assumption-setSpreadProperties-expression-exec.test.js > exec
AssertionError: expected [ 1 ] to deeply equal [ 1, 2 ]
Expand All @@ -74,7 +74,7 @@ AssertionError: expected [ 1 ] to deeply equal [ 1, 2 ]
| ^
11| })

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/10]
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/11]

FAIL fixtures/babel-plugin-transform-object-rest-spread-test-fixtures-assumption-setSpreadProperties-no-object-assign-exec-exec.test.js > exec
AssertionError: expected [Function] to throw an error
Expand All @@ -86,7 +86,29 @@ AssertionError: expected [Function] to throw an error
14| const obj2 = { "NOWRITE": 456 };
15| expect(() => {

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/10]
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/11]

FAIL fixtures/babel-plugin-transform-object-rest-spread-test-fixtures-object-rest-impure-computed-exec-exec.test.js > exec
AssertionError: expected { '2': 'two', z: 'zee' } to deeply equal {}

- Expected
+ Received

- Object {}
+ Object {
+ "2": "two",
+ "z": "zee",
+ }

❯ fixtures/babel-plugin-transform-object-rest-spread-test-fixtures-object-rest-impure-computed-exec-exec.test.js:30:12
28| ({[key]: y, z} = _$z), x = _extends({}, (_objectDestructuringEmpty(_$…
29| expect(y).toBe("two");
30| expect(x).toEqual({});
| ^
31| expect(z).toBe("zee");
32| var order = [];

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/11]

FAIL fixtures/babel-plugin-transform-react-jsx-source-test-fixtures-react-source-basic-sample-exec.test.js > exec
ReferenceError: transformAsync is not defined
Expand All @@ -98,7 +120,7 @@ ReferenceError: transformAsync is not defined
5| var expected = `
6| var _jsxFileName = "/fake/path/mock.js";

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/10]
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[7/11]

FAIL fixtures/babel-plugin-transform-react-jsx-source-test-fixtures-react-source-with-source-exec.test.js > exec
ReferenceError: transformAsync is not defined
Expand All @@ -110,7 +132,7 @@ ReferenceError: transformAsync is not defined
5| var expected = "var x = <sometag __source=\"custom\" />;";
6| return actualP.then((actual) => {

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[7/10]
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[8/11]

FAIL fixtures/babel-preset-env-test-fixtures-plugins-integration-issue-15170-exec.test.js > exec
AssertionError: expected [Function] to not throw an error but 'ReferenceError: x is not defined' was thrown
Expand All @@ -128,7 +150,7 @@ undefined
| ^
7| })

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[8/10]
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[9/11]

FAIL fixtures/babel-preset-env-test-fixtures-sanity-check-es2015-constants-exec.test.js > exec
TypeError: Assignment to constant variable.
Expand All @@ -139,7 +161,7 @@ TypeError: Assignment to constant variable.
| ^
6| })

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[9/10]
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[10/11]

FAIL fixtures/babel-preset-env-test-fixtures-sanity-regex-dot-all-exec.test.js > exec
AssertionError: expected false to be true // Object.is equality
Expand All @@ -158,5 +180,5 @@ AssertionError: expected false to be true // Object.is equality
11| expect(/hello.world/su.test(input)).toBe(true);
12| })

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[10/10]
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[11/11]

0 comments on commit 96c9853

Please sign in to comment.