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

Commit

Permalink
fix: fix class extends without super
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 7, 2018
1 parent 60da1e2 commit 9a6b3be
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 190 deletions.
4 changes: 4 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ export class ErrInvalidIterable extends ErrIsNot {
super(name, "iterable");
}
}

export const ErrNoSuper = new ReferenceError(
`Uncaught ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor`
);
22 changes: 20 additions & 2 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
ErrNotSupport,
ErrDuplicateDeclard,
ErrUnexpectedToken,
ErrInvalidIterable
ErrInvalidIterable,
ErrNoSuper
} from "./error";
import {Scope, ScopeVar, Kind} from "./scope";
import {
Expand Down Expand Up @@ -832,7 +833,24 @@ const evaluate_map = {
}
});

evaluate(constructor, newScope, {SuperClass});
if (node.superClass) {
// make sure super exist in first line
const superCallExpression: types.ExpressionStatement = <types.ExpressionStatement>(<any>constructor).body.body.shift();

if (
!types.isExpressionStatement(superCallExpression) ||
!types.isCallExpression(superCallExpression.expression) ||
!types.isSuper(superCallExpression.expression.callee)
) {
throw ErrNoSuper;
} else {
// TODO: run super
}
}

constructor.body.body.forEach(n =>
evaluate(n, newScope, {SuperClass})
);
}

return __this;
Expand Down
Loading

0 comments on commit 9a6b3be

Please sign in to comment.