Skip to content
This repository was archived by the owner on Sep 10, 2023. It is now read-only.

Commit 9a6b3be

Browse files
committed
fix: fix class extends without super
1 parent 60da1e2 commit 9a6b3be

File tree

3 files changed

+233
-190
lines changed

3 files changed

+233
-190
lines changed

src/error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ export class ErrInvalidIterable extends ErrIsNot {
3333
super(name, "iterable");
3434
}
3535
}
36+
37+
export const ErrNoSuper = new ReferenceError(
38+
`Uncaught ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor`
39+
);

src/evaluate.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
ErrNotSupport,
55
ErrDuplicateDeclard,
66
ErrUnexpectedToken,
7-
ErrInvalidIterable
7+
ErrInvalidIterable,
8+
ErrNoSuper
89
} from "./error";
910
import {Scope, ScopeVar, Kind} from "./scope";
1011
import {
@@ -832,7 +833,24 @@ const evaluate_map = {
832833
}
833834
});
834835

835-
evaluate(constructor, newScope, {SuperClass});
836+
if (node.superClass) {
837+
// make sure super exist in first line
838+
const superCallExpression: types.ExpressionStatement = <types.ExpressionStatement>(<any>constructor).body.body.shift();
839+
840+
if (
841+
!types.isExpressionStatement(superCallExpression) ||
842+
!types.isCallExpression(superCallExpression.expression) ||
843+
!types.isSuper(superCallExpression.expression.callee)
844+
) {
845+
throw ErrNoSuper;
846+
} else {
847+
// TODO: run super
848+
}
849+
}
850+
851+
constructor.body.body.forEach(n =>
852+
evaluate(n, newScope, {SuperClass})
853+
);
836854
}
837855

838856
return __this;

0 commit comments

Comments
 (0)