Skip to content

Commit

Permalink
Fix: Handle no super() in class constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Nov 21, 2024
1 parent e785020 commit 73f077a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 71 deletions.
6 changes: 4 additions & 2 deletions crates/oxc_transformer/src/es2022/class_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2123,10 +2123,12 @@ impl<'a, 'c> ConstructorInitsInserter<'a, 'c> {
// TODO: Re-parent child scopes of `init`s
let stmts = &mut body.statements;
let mut stmts_iter = stmts.iter_mut();
let mut index = 0;
let mut index = 1; // 1 because index is index to insert after `super()`, not before
loop {
// Constructor must contain at least one `super()`, so loop must exit before get to end of `stmts`
let stmt = stmts_iter.next().unwrap();
// TODO: Should be able to use `unwrap` here. Only not because `super()` could be in arguments.
// Change this to `unwrap()` once that's dealt with.
let Some(stmt) = stmts_iter.next() else { return };
if let Statement::ExpressionStatement(expr_stmt) = &*stmt {
if let Expression::CallExpression(call_expr) = &expr_stmt.expression {
if let Expression::Super(_) = &call_expr.callee {
Expand Down
11 changes: 8 additions & 3 deletions tasks/coverage/snapshots/semantic_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ commit: df9d1650

semantic_typescript Summary:
AST Parsed : 6490/6490 (100.00%)
Positive Passed: 2588/6490 (39.88%)
Positive Passed: 2587/6490 (39.86%)
tasks/coverage/typescript/tests/cases/compiler/2dArrays.ts
semantic error: Symbol reference IDs mismatch for "Cell":
after transform: SymbolId(0): [ReferenceId(1)]
Expand Down Expand Up @@ -42114,7 +42114,7 @@ rebuilt : []
tasks/coverage/typescript/tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedClassOverridesProtectedMembers.ts
semantic error: Symbol reference IDs mismatch for "x":
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(1), ReferenceId(2), ReferenceId(3), ReferenceId(4), ReferenceId(5), ReferenceId(6), ReferenceId(7), ReferenceId(8), ReferenceId(9), ReferenceId(10), ReferenceId(23)]
rebuilt : SymbolId(1): [ReferenceId(1), ReferenceId(2), ReferenceId(14)]
rebuilt : SymbolId(1): [ReferenceId(1), ReferenceId(2), ReferenceId(12)]
Symbol reference IDs mismatch for "y":
after transform: SymbolId(1): [ReferenceId(12), ReferenceId(13), ReferenceId(14), ReferenceId(15), ReferenceId(16), ReferenceId(17), ReferenceId(18), ReferenceId(19), ReferenceId(20), ReferenceId(21), ReferenceId(22)]
rebuilt : SymbolId(2): [ReferenceId(10), ReferenceId(11)]
Expand All @@ -42139,7 +42139,7 @@ rebuilt : ["require"]
tasks/coverage/typescript/tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedClassOverridesPublicMembers.ts
semantic error: Symbol reference IDs mismatch for "x":
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(1), ReferenceId(2), ReferenceId(3), ReferenceId(4), ReferenceId(5), ReferenceId(6), ReferenceId(7), ReferenceId(8), ReferenceId(9), ReferenceId(10), ReferenceId(23), ReferenceId(42), ReferenceId(44)]
rebuilt : SymbolId(1): [ReferenceId(1), ReferenceId(2), ReferenceId(14)]
rebuilt : SymbolId(1): [ReferenceId(1), ReferenceId(2), ReferenceId(12)]
Symbol reference IDs mismatch for "y":
after transform: SymbolId(1): [ReferenceId(12), ReferenceId(13), ReferenceId(14), ReferenceId(15), ReferenceId(16), ReferenceId(17), ReferenceId(18), ReferenceId(19), ReferenceId(20), ReferenceId(21), ReferenceId(22), ReferenceId(26), ReferenceId(29), ReferenceId(33), ReferenceId(36), ReferenceId(40), ReferenceId(45)]
rebuilt : SymbolId(2): [ReferenceId(10), ReferenceId(11), ReferenceId(20), ReferenceId(23), ReferenceId(27), ReferenceId(30), ReferenceId(34)]
Expand Down Expand Up @@ -45567,6 +45567,11 @@ Unresolved references mismatch:
after transform: ["require"]
rebuilt : ["Factory", "require"]

tasks/coverage/typescript/tests/cases/conformance/es6/classDeclaration/superCallBeforeThisAccessing5.ts
semantic error: Symbol reference IDs mismatch for "_defineProperty":
after transform: SymbolId(1): [ReferenceId(0)]
rebuilt : SymbolId(0): []

tasks/coverage/typescript/tests/cases/conformance/es6/classExpressions/typeArgumentInferenceWithClassExpression1.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["_Class2", "_defineProperty", "foo"]
Expand Down
46 changes: 29 additions & 17 deletions tasks/transform_conformance/snapshots/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: d20b314c

Passed: 401/839
Passed: 405/839

# All Passed:
* babel-plugin-transform-class-static-block
Expand Down Expand Up @@ -273,7 +273,7 @@ x Output mismatch
x Output mismatch


# babel-plugin-transform-class-properties (74/264)
# babel-plugin-transform-class-properties (78/264)
* assumption-constantSuper/complex-super-class/input.js
x Output mismatch

Expand All @@ -296,7 +296,15 @@ x Output mismatch
x Output mismatch

* assumption-setPublicClassFields/foobar/input.js
x Output mismatch
Scope children mismatch:
after transform: ScopeId(1): [ScopeId(2), ScopeId(3)]
rebuilt : ScopeId(1): [ScopeId(2)]
Scope children mismatch:
after transform: ScopeId(2): []
rebuilt : ScopeId(2): [ScopeId(3)]
Scope parent mismatch:
after transform: ScopeId(3): Some(ScopeId(1))
rebuilt : ScopeId(3): Some(ScopeId(2))

* assumption-setPublicClassFields/regression-T7364/input.mjs
Scope children mismatch:
Expand Down Expand Up @@ -345,9 +353,6 @@ x Output mismatch
* assumption-setPublicClassFields/super-expression/input.js
x Output mismatch

* assumption-setPublicClassFields/super-statement/input.js
x Output mismatch

* assumption-setPublicClassFields/super-with-collision/input.js
x Output mismatch

Expand Down Expand Up @@ -439,7 +444,15 @@ x Output mismatch
x Output mismatch

* private/foobar/input.js
x Output mismatch
Scope children mismatch:
after transform: ScopeId(1): [ScopeId(2), ScopeId(3)]
rebuilt : ScopeId(1): [ScopeId(2)]
Scope children mismatch:
after transform: ScopeId(2): []
rebuilt : ScopeId(2): [ScopeId(3)]
Scope parent mismatch:
after transform: ScopeId(3): Some(ScopeId(1))
rebuilt : ScopeId(3): Some(ScopeId(2))

* private/nested-class-computed-redeclared/input.js
x Output mismatch
Expand Down Expand Up @@ -542,9 +555,6 @@ x Output mismatch
* private/super-expression/input.js
x Output mismatch

* private/super-statement/input.js
x Output mismatch

* private/tagged-template/input.js
x Output mismatch

Expand Down Expand Up @@ -797,7 +807,15 @@ x Output mismatch
x Output mismatch

* public/foobar/input.js
x Output mismatch
Scope children mismatch:
after transform: ScopeId(1): [ScopeId(2), ScopeId(3)]
rebuilt : ScopeId(1): [ScopeId(2)]
Scope children mismatch:
after transform: ScopeId(2): []
rebuilt : ScopeId(2): [ScopeId(3)]
Scope parent mismatch:
after transform: ScopeId(3): Some(ScopeId(1))
rebuilt : ScopeId(3): Some(ScopeId(2))

* public/non-block-arrow-func/input.mjs
x Output mismatch
Expand Down Expand Up @@ -852,9 +870,6 @@ x Output mismatch
* public/super-expression/input.js
x Output mismatch

* public/super-statement/input.js
x Output mismatch

* public/super-with-collision/input.js
x Output mismatch

Expand Down Expand Up @@ -926,9 +941,6 @@ x Output mismatch
* public-loose/super-expression/input.js
x Output mismatch

* public-loose/super-statement/input.js
x Output mismatch

* public-loose/super-with-collision/input.js
x Output mismatch

Expand Down
Loading

0 comments on commit 73f077a

Please sign in to comment.