Skip to content

Commit

Permalink
Fix: Throw when super in arrow with default / rest
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSodaSea committed Nov 8, 2022
1 parent f6336da commit 073b3b0
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-parameters/package.json
Expand Up @@ -14,6 +14,7 @@
},
"main": "./lib/index.js",
"dependencies": {
"@babel/helper-environment-visitor": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^"
},
"keywords": [
Expand Down
19 changes: 19 additions & 0 deletions packages/babel-plugin-transform-parameters/src/index.ts
@@ -1,8 +1,25 @@
import { traverse } from "@babel/core";
import environmentVisitor from "@babel/helper-environment-visitor";
import { declare } from "@babel/helper-plugin-utils";
import convertFunctionParams from "./params";
import convertFunctionRest from "./rest";
export { convertFunctionParams };

const checkSuperCallsVisitor = traverse.visitors.merge<{}>([
{
CallExpression(child) {
if (child.get("callee").isSuper()) {
// NOTE: this may happen in `@babel/preset-env` with target `since 2017`, or in the default setting of `create-react-app`.
throw child.buildCodeFrameError(
"It's not possible to compile `super()` in an arrow function with default or rest parameters without compiling classes.\n" +
"Please add '@babel/plugin-transform-classes' to your Babel configuration.",
);
}
},
},
environmentVisitor,
]);

export interface Options {
loose?: boolean;
}
Expand All @@ -26,6 +43,8 @@ export default declare((api, options: Options) => {
.get("params")
.some(param => param.isRestElement() || param.isAssignmentPattern())
) {
path.traverse(checkSuperCallsVisitor);

// default/rest visitors require access to `arguments`, so it cannot be an arrow
path.arrowFunctionToExpression({ noNewArrows });

Expand Down
@@ -0,0 +1,6 @@
class Bar extends Foo {
constructor() {
let f = (x = super()) => x;
f();
}
}
@@ -0,0 +1,4 @@
{
"plugins": ["transform-parameters"],
"throws": "It's not possible to compile `super()` in an arrow function with default or rest parameters without compiling classes.\nPlease add '@babel/plugin-transform-classes' to your Babel configuration."
}
@@ -0,0 +1,6 @@
class Bar extends Foo {
constructor() {
let f = (...args) => super(...args);
f();
}
}
@@ -0,0 +1,4 @@
{
"plugins": ["transform-parameters"],
"throws": "It's not possible to compile `super()` in an arrow function with default or rest parameters without compiling classes.\nPlease add '@babel/plugin-transform-classes' to your Babel configuration."
}
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -2862,6 +2862,7 @@ __metadata:
resolution: "@babel/plugin-transform-parameters@workspace:packages/babel-plugin-transform-parameters"
dependencies:
"@babel/core": "workspace:^"
"@babel/helper-environment-visitor": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^"
peerDependencies:
Expand Down

0 comments on commit 073b3b0

Please sign in to comment.