Skip to content

Commit

Permalink
Rewrite Hub as interface #5047
Browse files Browse the repository at this point in the history
  • Loading branch information
yongxu committed Dec 28, 2016
1 parent b2e6926 commit 78dd051
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 20 deletions.
14 changes: 13 additions & 1 deletion packages/babel-core/src/transformation/file/index.js
Expand Up @@ -99,7 +99,19 @@ export default class File extends Store {
this.code = "";
this.shebang = "";

this.hub = new Hub(this);
this.hub = {
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 Down
Expand Up @@ -50,7 +50,8 @@ export default function ({ types: t }) {
: null;

const fileNameIdentifier = path.scope.generateUidIdentifier(FILE_NAME_VAR);
path.hub.file.scope.push({id: fileNameIdentifier, init: t.stringLiteral(fileName)});
const scope = path.hub.getScope();
scope && scope.push({id: fileNameIdentifier, init: t.stringLiteral(fileName)});
state.fileNameIdentifier = fileNameIdentifier;
}

Expand Down
11 changes: 6 additions & 5 deletions packages/babel-traverse/src/hub.js
@@ -1,6 +1,7 @@
export default class Hub {
constructor(file, options) {
this.file = file;
this.options = options;
}
export type Hub = {
mark: (type: string, message: string) => void;
addHelper: (name: string) => Object;
getScope: () => Object;
getCode: () => string;
buildError:(node: Object, msg: string, Error: Error) => Error;
}
4 changes: 2 additions & 2 deletions packages/babel-traverse/src/index.js
Expand Up @@ -9,7 +9,8 @@ import * as cache from "./cache";

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

export default function traverse(
Expand Down Expand Up @@ -39,7 +40,6 @@ traverse.explode = visitors.explode;

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

traverse.cheap = function (node, enter) {
return t.traverseFast(node, enter);
Expand Down
8 changes: 2 additions & 6 deletions packages/babel-traverse/src/path/index.js
Expand Up @@ -116,19 +116,15 @@ 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
});
this.hub.mark && this.hub.mark(type, message, this.node.loc);
}

set(key: string, node: Object) {
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-traverse/src/path/introspection.js
Expand Up @@ -235,7 +235,12 @@ export function referencesImport(moduleSource, importName) {
export function getSource() {
let node = this.node;
if (node.end) {
return this.hub.file.code.slice(node.start, node.end);
const code = this.hub.getCode();
if (code) {
return code.slice(node.start, node.end);
} else {
return "";
}
} else {
return "";
}
Expand Down
11 changes: 7 additions & 4 deletions packages/babel-traverse/src/scope/index.js
Expand Up @@ -350,7 +350,11 @@ export default class Scope {
if (!duplicate) duplicate = local.kind === "param" && (kind === "let" || kind === "const");

if (duplicate) {
throw this.hub.file.buildCodeFrameError(id, messages.get("scopeDuplicateDeclaration", name), TypeError);
if (this.hub.buildError) {
throw this.hub.buildError(id, messages.get("scopeDuplicateDeclaration", name), TypeError);
} else {
throw new TypeError(messages.get("scopeDuplicateDeclaration", name));
}
}
}

Expand Down Expand Up @@ -389,7 +393,6 @@ export default class Scope {
}

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

if (t.isIdentifier(node)) {
let binding = this.getBinding(node.name);
Expand Down Expand Up @@ -423,9 +426,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

0 comments on commit 78dd051

Please sign in to comment.