Rewrite Hub as interface #5047

This commit is contained in:
Yongxu Ren
2016-12-27 11:52:51 -05:00
committed by Nicolò Ribaudo
parent 3a399d1eb9
commit 0fd3da110d
12 changed files with 67 additions and 32 deletions

View File

@@ -1,5 +1,22 @@
export default class Hub {
constructor(file) {
this.file = file;
import type Scope from "./scope";
export interface HubInterface {
getCode(): ?string;
getScope(): ?Scope;
addHelper(name: string): Object;
buildError(node: Object, msg: string, Error: Class<Error>): Error;
}
export default class Hub implements HubInterface {
getCode() {}
getScope() {}
addHelper() {
throw new Error("Helpers are not supported by the default hub.");
}
buildError(node, msg, Error = TypeError): Error {
return new Error(msg);
}
}

View File

@@ -7,6 +7,7 @@ import * as cache from "./cache";
export { default as NodePath } from "./path";
export { default as Scope } from "./scope";
export { default as Hub } from "./hub";
export type { HubInterface } from "./hub";
export { visitors };

View File

@@ -133,7 +133,7 @@ export function arrowFunctionToExpression({
this.get("body").unshiftContainer(
"body",
t.expressionStatement(
t.callExpression(this.hub.file.addHelper("newArrowCheck"), [
t.callExpression(this.hub.addHelper("newArrowCheck"), [
t.thisExpression(),
checkBinding
? t.identifier(checkBinding.name)

View File

@@ -1,4 +1,4 @@
import type Hub from "../hub";
import type { HubInterface } from "../hub";
import type TraversalContext from "../context";
import * as virtualTypes from "./lib/virtual-types";
import buildDebug from "debug";
@@ -24,7 +24,7 @@ import * as NodePath_comments from "./comments";
const debug = buildDebug("babel");
export default class NodePath {
constructor(hub: Hub, parent: Object) {
constructor(hub: HubInterface, parent: Object) {
this.parent = parent;
this.hub = hub;
this.contexts = [];
@@ -49,7 +49,7 @@ export default class NodePath {
}
parent: Object;
hub: Hub;
hub: HubInterface;
contexts: Array<TraversalContext>;
data: Object;
shouldSkip: boolean;
@@ -121,7 +121,7 @@ export default class NodePath {
}
buildCodeFrameError(msg: string, Error: typeof Error = SyntaxError): Error {
return this.hub.file.buildCodeFrameError(this.node, msg, Error);
return this.hub.buildError(this.node, msg, Error);
}
traverse(visitor: Object, state?: any) {

View File

@@ -196,10 +196,10 @@ export function referencesImport(moduleSource, importName) {
export function getSource() {
const node = this.node;
if (node.end) {
return this.hub.file.code.slice(node.start, node.end);
} else {
return "";
const code = this.hub.getCode();
if (code) return code.slice(node.start, node.end);
}
return "";
}
export function willIMaybeExecuteBefore(target) {

View File

@@ -362,7 +362,7 @@ export default class Scope {
(local.kind === "param" && (kind === "let" || kind === "const"));
if (duplicate) {
throw this.hub.file.buildCodeFrameError(
throw this.hub.buildError(
id,
`Duplicate declaration "${name}"`,
TypeError,
@@ -404,9 +404,7 @@ export default class Scope {
console.log(sep);
}
toArray(node: Object, i?: number | boolean) {
const file = this.hub.file;
toArray(node: Object, i?: number) {
if (t.isIdentifier(node)) {
const binding = this.getBinding(node.name);
if (binding && binding.constant && binding.path.isGenericType("Array")) {
@@ -444,12 +442,12 @@ export default class Scope {
// Used in array-rest to create an array from a subset of an iterable.
helperName = "slicedToArray";
// TODO if (this.hub.file.isLoose("es6.forOf")) helperName += "-loose";
// TODO if (this.hub.isLoose("es6.forOf")) helperName += "-loose";
} else {
// Used in array-rest to create an array
helperName = "toArray";
}
return t.callExpression(file.addHelper(helperName), args);
return t.callExpression(this.hub.addHelper(helperName), args);
}
hasLabel(name: string) {