Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing of ReplaceSupers options #11121

Merged
merged 3 commits into from Feb 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 30 additions & 16 deletions packages/babel-helper-replace-supers/src/index.js
@@ -1,4 +1,5 @@
import type { NodePath } from "@babel/traverse";
// @flow
import type { HubInterface, NodePath } from "@babel/traverse";
import traverse from "@babel/traverse";
import memberExpressionToFunctions from "@babel/helper-member-expression-to-functions";
import optimiseCall from "@babel/helper-optimise-call-expression";
Expand All @@ -25,7 +26,7 @@ function getPrototypeOfExpression(objectRef, isStatic, file, isPrivateMethod) {
return t.callExpression(file.addHelper("getPrototypeOf"), [targetRef]);
}

function skipAllButComputedKey(path) {
function skipAllButComputedKey(path: NodePath) {
// If the path isn't computed, just skip everything.
if (!path.node.computed) {
path.skip();
Expand All @@ -41,18 +42,19 @@ function skipAllButComputedKey(path) {
}

export const environmentVisitor = {
TypeAnnotation(path) {
TypeAnnotation(path: NodePath) {
path.skip();
},
Function(path) {

Function(path: NodePath) {
// Methods will be handled by the Method visit
if (path.isMethod()) return;
// Arrow functions inherit their parent's environment
if (path.isArrowFunctionExpression()) return;
path.skip();
},

"Method|ClassProperty|ClassPrivateProperty"(path) {
"Method|ClassProperty|ClassPrivateProperty"(path: NodePath) {
skipAllButComputedKey(path);
},
};
Expand Down Expand Up @@ -189,8 +191,25 @@ const looseHandlers = {
},
};

type ReplaceSupersOptionsBase = {|
methodPath: NodePath,
superRef: Object,
isLoose: boolean,
file: any,
|};

type ReplaceSupersOptions =
| {|
...ReplaceSupersOptionsBase,
getObjectRef: () => BabelNode,
|}
| {|
...ReplaceSupersOptionsBase,
objectRef: BabelNode,
|};

export default class ReplaceSupers {
constructor(opts: Object) {
constructor(opts: ReplaceSupersOptions) {
const path = opts.methodPath;

this.methodPath = path;
Expand All @@ -203,18 +222,13 @@ export default class ReplaceSupers {
this.opts = opts;
}

file: HubInterface;
isLoose: boolean;
isPrivateMethod: boolean;
isStatic: boolean;
methodPath: NodePath;
opts: ReplaceSupersOptions;
superRef: Object;
isStatic: boolean;
isLoose: boolean;
file;
opts: {
getObjetRef: Function,
methodPath: NodePath,
superRef: Object,
isLoose: boolean,
file: any,
};

getObjectRef() {
return t.cloneNode(this.opts.objectRef || this.opts.getObjectRef());
Expand Down