Skip to content

Commit

Permalink
Revert "Rewrite Hub as interface #5047" (#5306)
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfsmyth committed Feb 14, 2017
1 parent 48709e9 commit 454933e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 64 deletions.
21 changes: 3 additions & 18 deletions packages/babel-core/src/transformation/file/index.js
Expand Up @@ -6,8 +6,7 @@ import convertSourceMap from "convert-source-map";
import OptionManager from "./options/option-manager";
import type Pipeline from "../pipeline";
import PluginPass from "../plugin-pass";
import { NodePath, Scope } from "babel-traverse";
import type { HubInterface } from "babel-traverse";
import { NodePath, Hub, Scope } from "babel-traverse";
import sourceMap from "source-map";
import generate from "babel-generator";
import codeFrame from "babel-code-frame";
Expand Down Expand Up @@ -99,21 +98,7 @@ export default class File extends Store {
this.code = "";
this.shebang = "";

this.hub = {
// keep it for the usage in babel-core, ex: path.hub.file.opts.filename
file: this,
mark: (type: string, message: string, loc: Object) => {
this.metadata.marked.push({
type,
message,
loc
});
},
addHelper: this.addHelper.bind(this),
getCode: () => this.code,
getScope: () => this.scope,
buildError: this.buildCodeFrameError.bind(this)
};
this.hub = new Hub(this);
}

static helpers: Array<string>;
Expand All @@ -133,7 +118,7 @@ export default class File extends Store {
ast: Object;
scope: Scope;
metadata: BabelFileMetadata;
hub: HubInterface;
hub: Hub;
code: string;
shebang: string;

Expand Down
14 changes: 3 additions & 11 deletions packages/babel-traverse/src/hub.js
@@ -1,14 +1,6 @@
import Scope from "./scope";
export interface HubInterface {
mark?: (type: string, message: string) => void;
addHelper?: (name: string) => Object;
getScope?: () => Scope;
getCode?: () => string;
buildError:(node: Object, msg: string, Error: Error) => Error;
}

export default class Hub {
buildError(node, msg, BuildError = TypeError): Error {
return new BuildError(msg);
constructor(file, options) {
this.file = file;
this.options = options;
}
}
16 changes: 7 additions & 9 deletions packages/babel-traverse/src/index.js
Expand Up @@ -5,12 +5,10 @@ import includes from "lodash/includes";
import * as t from "babel-types";
import * as cache from "./cache";

import NodePath from "./path";
import Scope from "./scope";
import Hub from "./hub";

export { visitors, NodePath, Scope, Hub };
export type { HubInterface } from "./hub";
export { default as NodePath } from "./path";
export { default as Scope } from "./scope";
export { default as Hub } from "./hub";
export { visitors };

export default function traverse(
parent: Object | Array<Object>,
Expand All @@ -37,9 +35,9 @@ traverse.visitors = visitors;
traverse.verify = visitors.verify;
traverse.explode = visitors.explode;

traverse.NodePath = NodePath;
traverse.Scope = Scope;
traverse.Hub = Hub;
traverse.NodePath = require("./path");
traverse.Scope = require("./scope");
traverse.Hub = require("./hub");

traverse.cheap = function (node, enter) {
return t.traverseFast(node, enter);
Expand Down
10 changes: 6 additions & 4 deletions packages/babel-traverse/src/path/index.js
Expand Up @@ -114,17 +114,19 @@ export default class NodePath {
}

buildCodeFrameError(msg: string, Error: typeof Error = SyntaxError): Error {
return this.hub.buildError(this.node, msg, Error);
return this.hub.file.buildCodeFrameError(this.node, msg, Error);
}

traverse(visitor: Object, state?: any) {
traverse(this.node, visitor, this.scope, state, this);
}

mark(type: string, message: string) {
if (this.hub.mark) {
this.hub.mark(type, message, this.node.loc);
}
this.hub.file.metadata.marked.push({
type,
message,
loc: this.node.loc
});
}

set(key: string, node: Object) {
Expand Down
8 changes: 3 additions & 5 deletions packages/babel-traverse/src/path/introspection.js
Expand Up @@ -235,12 +235,10 @@ export function referencesImport(moduleSource, importName) {
export function getSource() {
const node = this.node;
if (node.end) {
const code = this.hub.getCode();
if (code) {
return code.slice(node.start, node.end);
}
return this.hub.file.code.slice(node.start, node.end);
} else {
return "";
}
return "";
}

export function willIMaybeExecuteBefore(target) {
Expand Down
9 changes: 5 additions & 4 deletions packages/babel-traverse/src/scope/index.js
Expand Up @@ -346,8 +346,7 @@ export default class Scope {
local.kind === "param" && (kind === "let" || kind === "const");

if (duplicate) {
const errorMsg = messages.get("scopeDuplicateDeclaration", name);
throw this.hub.buildError ? this.hub.buildError(id, errorMsg, TypeError) : new TypeError(errorMsg);
throw this.hub.file.buildCodeFrameError(id, messages.get("scopeDuplicateDeclaration", name), TypeError);
}
}

Expand Down Expand Up @@ -386,6 +385,8 @@ export default class Scope {
}

toArray(node: Object, i?: number) {
const file = this.hub.file;

if (t.isIdentifier(node)) {
const binding = this.getBinding(node.name);
if (binding && binding.constant && binding.path.isGenericType("Array")) return node;
Expand Down Expand Up @@ -418,9 +419,9 @@ export default class Scope {
} else if (i) {
args.push(t.numericLiteral(i));
helperName = "slicedToArray";
// TODO if (this.hub.isLoose("es6.forOf")) helperName += "-loose";
// TODO if (this.hub.file.isLoose("es6.forOf")) helperName += "-loose";
}
return t.callExpression(this.hub.addHelper(helperName), args);
return t.callExpression(file.addHelper(helperName), args);
}

hasLabel(name: string) {
Expand Down
13 changes: 0 additions & 13 deletions packages/babel-traverse/test/hub.js

This file was deleted.

0 comments on commit 454933e

Please sign in to comment.