Skip to content

Commit

Permalink
Rewrite Hub as interface #5047 (#5050)
Browse files Browse the repository at this point in the history
* Rewrite Hub as interface #5047

* Update index.js
  • Loading branch information
yongxu authored and existentialism committed May 19, 2017
1 parent fdb8231 commit 8a7c954
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 27 deletions.
21 changes: 18 additions & 3 deletions packages/babel-core/src/transformation/file/index.js
Expand Up @@ -6,7 +6,8 @@ import convertSourceMap from "convert-source-map";
import OptionManager from "./options/option-manager";
import type Pipeline from "../pipeline";
import PluginPass from "../plugin-pass";
import { NodePath, Hub, Scope } from "babel-traverse";
import { NodePath, Scope } from "babel-traverse";
import type { HubInterface } from "babel-traverse";
import sourceMap from "source-map";
import generate from "babel-generator";
import codeFrame from "babel-code-frame";
Expand Down Expand Up @@ -98,7 +99,21 @@ export default class File extends Store {
this.code = "";
this.shebang = "";

this.hub = new Hub(this);
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)
};
}

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

Expand Down
14 changes: 11 additions & 3 deletions packages/babel-traverse/src/hub.js
@@ -1,6 +1,14 @@
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 {
constructor(file, options) {
this.file = file;
this.options = options;
buildError(node, msg, BuildError = TypeError): Error {
return new BuildError(msg);
}
}
16 changes: 9 additions & 7 deletions packages/babel-traverse/src/index.js
Expand Up @@ -5,10 +5,12 @@ import includes from "lodash/includes";
import * as t from "babel-types";
import * as cache from "./cache";

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

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

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

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

traverse.cheap = function (node, enter) {
return t.traverseFast(node, enter);
Expand Down
10 changes: 4 additions & 6 deletions packages/babel-traverse/src/path/index.js
Expand Up @@ -114,19 +114,17 @@ 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) {
traverse(this.node, visitor, this.scope, state, this);
}

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

set(key: string, node: Object) {
Expand Down
8 changes: 5 additions & 3 deletions packages/babel-traverse/src/path/introspection.js
Expand Up @@ -235,10 +235,12 @@ 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) {
Expand Down
9 changes: 4 additions & 5 deletions packages/babel-traverse/src/scope/index.js
Expand Up @@ -346,7 +346,8 @@ export default class Scope {
local.kind === "param" && (kind === "let" || kind === "const");

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

Expand Down Expand Up @@ -385,8 +386,6 @@ 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 @@ -419,9 +418,9 @@ export default class Scope {
} else if (i) {
args.push(t.numericLiteral(i));
helperName = "slicedToArray";
// TODO if (this.hub.file.isLoose("es6.forOf")) helperName += "-loose";
// TODO if (this.hub.isLoose("es6.forOf")) helperName += "-loose";
}
return t.callExpression(file.addHelper(helperName), args);
return t.callExpression(this.hub.addHelper(helperName), args);
}

hasLabel(name: string) {
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-traverse/test/hub.js
@@ -0,0 +1,13 @@
const assert = require("assert");
const Hub = require("../lib").default.Hub;

describe("hub", function () {
it("default buildError should return TypeError", function () {
const hub = new Hub();
const msg = "test_msg";
assert.deepEqual(
hub.buildError(null, msg),
new TypeError(msg)
);
});
});

0 comments on commit 8a7c954

Please sign in to comment.