Fix typing of ReplaceSupers options (#11121)

* 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>
This commit is contained in:
Brian Ng 2020-02-11 03:55:46 -06:00 committed by GitHub
parent bc308a1b15
commit 157bb6e831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
import type { NodePath } from "@babel/traverse"; // @flow
import type { HubInterface, NodePath } from "@babel/traverse";
import traverse from "@babel/traverse"; import traverse from "@babel/traverse";
import memberExpressionToFunctions from "@babel/helper-member-expression-to-functions"; import memberExpressionToFunctions from "@babel/helper-member-expression-to-functions";
import optimiseCall from "@babel/helper-optimise-call-expression"; 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]); return t.callExpression(file.addHelper("getPrototypeOf"), [targetRef]);
} }
function skipAllButComputedKey(path) { function skipAllButComputedKey(path: NodePath) {
// If the path isn't computed, just skip everything. // If the path isn't computed, just skip everything.
if (!path.node.computed) { if (!path.node.computed) {
path.skip(); path.skip();
@ -41,10 +42,11 @@ function skipAllButComputedKey(path) {
} }
export const environmentVisitor = { export const environmentVisitor = {
TypeAnnotation(path) { TypeAnnotation(path: NodePath) {
path.skip(); path.skip();
}, },
Function(path) {
Function(path: NodePath) {
// Methods will be handled by the Method visit // Methods will be handled by the Method visit
if (path.isMethod()) return; if (path.isMethod()) return;
// Arrow functions inherit their parent's environment // Arrow functions inherit their parent's environment
@ -52,7 +54,7 @@ export const environmentVisitor = {
path.skip(); path.skip();
}, },
"Method|ClassProperty|ClassPrivateProperty"(path) { "Method|ClassProperty|ClassPrivateProperty"(path: NodePath) {
skipAllButComputedKey(path); 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 { export default class ReplaceSupers {
constructor(opts: Object) { constructor(opts: ReplaceSupersOptions) {
const path = opts.methodPath; const path = opts.methodPath;
this.methodPath = path; this.methodPath = path;
@ -203,18 +222,13 @@ export default class ReplaceSupers {
this.opts = opts; this.opts = opts;
} }
methodPath: NodePath; file: HubInterface;
superRef: Object;
isStatic: boolean;
isLoose: boolean; isLoose: boolean;
file; isPrivateMethod: boolean;
opts: { isStatic: boolean;
getObjetRef: Function, methodPath: NodePath;
methodPath: NodePath, opts: ReplaceSupersOptions;
superRef: Object, superRef: Object;
isLoose: boolean,
file: any,
};
getObjectRef() { getObjectRef() {
return t.cloneNode(this.opts.objectRef || this.opts.getObjectRef()); return t.cloneNode(this.opts.objectRef || this.opts.getObjectRef());