Skip to content

Commit

Permalink
Fix typing of ReplaceSupers options (babel#11121)
Browse files Browse the repository at this point in the history
* Fix type opts.getObjetRef to opts.getObjectRef

* Added // @flow to possibly catch for more errors.

* tweaks

Co-authored-by: InsignificantReasons <41794038+InsignificantReasons@users.noreply.github.com>
  • Loading branch information
2 people authored and rajasekarm committed Feb 17, 2020
1 parent d778427 commit 737fd3f
Showing 1 changed file with 30 additions and 16 deletions.
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

0 comments on commit 737fd3f

Please sign in to comment.