diff --git a/packages/babel-helper-replace-supers/src/index.js b/packages/babel-helper-replace-supers/src/index.js index 3412d321caf2..72d1e9849b07 100644 --- a/packages/babel-helper-replace-supers/src/index.js +++ b/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"; @@ -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(); @@ -41,10 +42,11 @@ 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 @@ -52,7 +54,7 @@ export const environmentVisitor = { path.skip(); }, - "Method|ClassProperty|ClassPrivateProperty"(path) { + "Method|ClassProperty|ClassPrivateProperty"(path: NodePath) { skipAllButComputedKey(path); }, }; @@ -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; @@ -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());