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 30, 2016
1 parent b2e6926 commit 4e14889
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 27 deletions.
19 changes: 16 additions & 3 deletions packages/babel-core/src/transformation/file/index.js
Expand Up @@ -7,7 +7,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 @@ -99,7 +100,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 All @@ -119,7 +132,7 @@ export default class File extends Store {
ast: Object;
scope: Scope;
metadata: BabelFileMetadata;
hub: Hub;
hub: HubInterface;
code: string;
shebang: string;

Expand Down
Expand Up @@ -50,7 +50,10 @@ 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();
if (scope) {
scope.push({id: fileNameIdentifier, init: t.stringLiteral(fileName)});
}
state.fileNameIdentifier = fileNameIdentifier;
}

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, Error): Error {
return new TypeError(msg);
}
}
16 changes: 9 additions & 7 deletions packages/babel-traverse/src/index.js
Expand Up @@ -7,10 +7,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 @@ -37,9 +39,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 @@ -116,19 +116,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() {
let 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
8 changes: 4 additions & 4 deletions packages/babel-traverse/src/scope/index.js
Expand Up @@ -350,7 +350,8 @@ 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);
const errorMsg = messages.get("scopeDuplicateDeclaration", name);
throw this.hub.buildError ? this.hub.buildError(id, errorMsg, TypeError) : new TypeError(errorMsg);
}
}

Expand Down Expand Up @@ -389,7 +390,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 +423,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 4e14889

Please sign in to comment.