convert @babel/helper-replace-supers to typescript (#12926)

This commit is contained in:
Bogdan Savluk 2021-03-16 13:05:15 +01:00 committed by GitHub
parent 281acd6448
commit befa46567f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,3 @@
// @flow
import type { HubInterface, NodePath } from "@babel/traverse"; 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";
@ -26,8 +25,11 @@ function getPrototypeOfExpression(objectRef, isStatic, file, isPrivateMethod) {
return t.callExpression(file.addHelper("getPrototypeOf"), [targetRef]); return t.callExpression(file.addHelper("getPrototypeOf"), [targetRef]);
} }
export function skipAllButComputedKey(path: NodePath) { export function skipAllButComputedKey(
path: NodePath<t.Method | t.ClassProperty | t.ClassPrivateProperty>,
) {
// If the path isn't computed, just skip everything. // If the path isn't computed, just skip everything.
// @ts-expect-error todo(flow->ts) check node type before cheking the property
if (!path.node.computed) { if (!path.node.computed) {
path.skip(); path.skip();
return; return;
@ -60,7 +62,7 @@ export const environmentVisitor = {
path.skip(); path.skip();
}, },
"Method|ClassProperty"(path: NodePath) { "Method|ClassProperty"(path: NodePath<t.Method | t.ClassProperty>) {
skipAllButComputedKey(path); skipAllButComputedKey(path);
}, },
}; };
@ -252,25 +254,25 @@ const looseHandlers = {
}, },
}; };
type ReplaceSupersOptionsBase = {| type ReplaceSupersOptionsBase = {
methodPath: NodePath, methodPath: NodePath<any>;
superRef: Object, superRef: any;
constantSuper: boolean, constantSuper?: boolean;
file: any, file: any;
// objectRef might have been shadowed in child scopes, // objectRef might have been shadowed in child scopes,
// in that case, we need to rename related variables. // in that case, we need to rename related variables.
refToPreserve?: BabelNodeIdentifier, refToPreserve?: t.Identifier;
|}; };
type ReplaceSupersOptions = type ReplaceSupersOptions =
| {| | ({
...ReplaceSupersOptionsBase, objectRef?: undefined;
getObjectRef: () => BabelNode, getObjectRef: () => t.Node;
|} } & ReplaceSupersOptionsBase)
| {| | ({
...ReplaceSupersOptionsBase, objectRef: t.Node;
objectRef: BabelNode, getObjectRef?: () => t.Node;
|}; } & ReplaceSupersOptionsBase);
export default class ReplaceSupers { export default class ReplaceSupers {
constructor(opts: ReplaceSupersOptions) { constructor(opts: ReplaceSupersOptions) {
@ -287,7 +289,7 @@ export default class ReplaceSupers {
this.constantSuper = process.env.BABEL_8_BREAKING this.constantSuper = process.env.BABEL_8_BREAKING
? opts.constantSuper ? opts.constantSuper
: // Fallback to isLoose for backward compatibility : // Fallback to isLoose for backward compatibility
opts.constantSuper ?? (opts: any).isLoose; opts.constantSuper ?? (opts as any).isLoose;
this.opts = opts; this.opts = opts;
} }
@ -298,7 +300,7 @@ export default class ReplaceSupers {
declare isStatic: boolean; declare isStatic: boolean;
declare methodPath: NodePath; declare methodPath: NodePath;
declare opts: ReplaceSupersOptions; declare opts: ReplaceSupersOptions;
declare superRef: Object; declare superRef: any;
getObjectRef() { getObjectRef() {
return t.cloneNode(this.opts.objectRef || this.opts.getObjectRef()); return t.cloneNode(this.opts.objectRef || this.opts.getObjectRef());