Rewrite Hub as interface #5047
This commit is contained in:
parent
3a399d1eb9
commit
0fd3da110d
@ -1,7 +1,7 @@
|
||||
// @flow
|
||||
|
||||
import * as helpers from "@babel/helpers";
|
||||
import { NodePath, Hub, Scope } from "@babel/traverse";
|
||||
import { NodePath, Scope, type HubInterface } from "@babel/traverse";
|
||||
import { codeFrameColumns } from "@babel/code-frame";
|
||||
import traverse from "@babel/traverse";
|
||||
import * as t from "@babel/types";
|
||||
@ -27,10 +27,18 @@ export default class File {
|
||||
ast: Object = {};
|
||||
scope: Scope;
|
||||
metadata: {} = {};
|
||||
hub: Hub = new Hub(this);
|
||||
code: string = "";
|
||||
inputMap: Object | null = null;
|
||||
|
||||
hub: HubInterface = {
|
||||
// keep it for the usage in babel-core, ex: path.hub.file.opts.filename
|
||||
file: this,
|
||||
getCode: () => this.code,
|
||||
getScope: () => this.scope,
|
||||
addHelper: this.addHelper.bind(this),
|
||||
buildError: this.buildCodeFrameError.bind(this),
|
||||
};
|
||||
|
||||
constructor(options: {}, { code, ast, inputMap }: NormalizedFile) {
|
||||
this.opts = options;
|
||||
this.code = code;
|
||||
|
||||
@ -112,7 +112,7 @@ export function wrapInterop(
|
||||
throw new Error(`Unknown interop: ${type}`);
|
||||
}
|
||||
|
||||
return t.callExpression(programPath.hub.file.addHelper(helper), [expr]);
|
||||
return t.callExpression(programPath.hub.addHelper(helper), [expr]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -59,10 +59,13 @@ export default declare(api => {
|
||||
const fileNameIdentifier = path.scope.generateUidIdentifier(
|
||||
FILE_NAME_VAR,
|
||||
);
|
||||
path.hub.file.scope.push({
|
||||
const scope = path.hub.getScope();
|
||||
if (scope) {
|
||||
scope.push({
|
||||
id: fileNameIdentifier,
|
||||
init: t.stringLiteral(fileName),
|
||||
});
|
||||
}
|
||||
state.fileNameIdentifier = fileNameIdentifier;
|
||||
}
|
||||
|
||||
|
||||
@ -143,7 +143,7 @@ export default declare((api, options) => {
|
||||
}
|
||||
|
||||
path.replaceWith(
|
||||
t.callExpression(path.hub.file.addHelper("construct"), [
|
||||
t.callExpression(path.hub.addHelper("construct"), [
|
||||
node.callee,
|
||||
args,
|
||||
]),
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 };
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -13,7 +13,6 @@ function assertConversion(
|
||||
|
||||
const rootPath = NodePath.get({
|
||||
hub: {
|
||||
file: {
|
||||
addHelper(helperName) {
|
||||
return t.memberExpression(
|
||||
t.identifier("babelHelpers"),
|
||||
@ -21,7 +20,6 @@ function assertConversion(
|
||||
);
|
||||
},
|
||||
},
|
||||
},
|
||||
parentPath: null,
|
||||
parent: inputAst,
|
||||
container: inputAst,
|
||||
|
||||
10
packages/babel-traverse/test/hub.js
Normal file
10
packages/babel-traverse/test/hub.js
Normal file
@ -0,0 +1,10 @@
|
||||
import assert from "assert";
|
||||
import { Hub } from "../lib";
|
||||
|
||||
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));
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user