Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite Hub as interface #8469

Merged
merged 2 commits into from Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/babel-core/src/transformation/file/file.js
@@ -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";
Expand All @@ -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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loganfsmyth In the other PR you asked to keep file here, but I think that it can be removed? (people can already get it in visitors using this.file)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolo-ribaudo Yeah, my concern was avoiding breaking changes. I think my preference would still be to keep it at this point since we're already pretty close to release. I don't think we'd want it as part of the interface, but leaving it in the object is so easy. Can we leave it with a big comment saying that we should remove it in 8.x but it's being left for backward-compat with 6.x during 7.x's lifetime?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's avoid breaking things at this point.

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;
Expand Down
10 changes: 5 additions & 5 deletions packages/babel-helper-module-imports/src/import-builder.js
Expand Up @@ -10,11 +10,11 @@ export default class ImportBuilder {
_resultName = null;

_scope = null;
_file = null;
_hub = null;

constructor(importedSource, scope, file) {
constructor(importedSource, scope, hub) {
this._scope = scope;
this._file = file;
this._hub = hub;
this._importedSource = importedSource;
}

Expand Down Expand Up @@ -91,10 +91,10 @@ export default class ImportBuilder {
}

defaultInterop() {
return this._interop(this._file.addHelper("interopRequireDefault"));
return this._interop(this._hub.addHelper("interopRequireDefault"));
}
wildcardInterop() {
return this._interop(this._file.addHelper("interopRequireWildcard"));
return this._interop(this._hub.addHelper("interopRequireWildcard"));
}

_interop(callee) {
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-helper-module-imports/src/import-injector.js
Expand Up @@ -108,7 +108,7 @@ export default class ImportInjector {
/**
* The file used to inject helpers and resolve paths.
*/
_file;
_hub;

/**
* The default options to use with this instance when imports are added.
Expand All @@ -127,7 +127,7 @@ export default class ImportInjector {

this._programPath = programPath;
this._programScope = programPath.scope;
this._file = programPath.hub.file;
this._hub = programPath.hub;

this._defaultOpts = this._applyDefaults(importedSource, opts, true);
}
Expand Down Expand Up @@ -218,7 +218,7 @@ export default class ImportInjector {
const builder = new ImportBuilder(
importedSource,
this._programScope,
this._file,
this._hub,
);

if (importedType === "es6") {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-module-transforms/src/index.js
Expand Up @@ -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]);
}

/**
Expand Down
11 changes: 7 additions & 4 deletions packages/babel-plugin-transform-react-jsx-source/src/index.js
Expand Up @@ -59,10 +59,13 @@ export default declare(api => {
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
2 changes: 1 addition & 1 deletion packages/babel-plugin-transform-spread/src/index.js
Expand Up @@ -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,
]),
Expand Down
23 changes: 20 additions & 3 deletions packages/babel-traverse/src/hub.js
@@ -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);
}
}
1 change: 1 addition & 0 deletions packages/babel-traverse/src/index.js
Expand Up @@ -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 };

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-traverse/src/path/conversion.js
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-traverse/src/path/index.js
@@ -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";
Expand All @@ -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 = [];
Expand All @@ -49,7 +49,7 @@ export default class NodePath {
}

parent: Object;
hub: Hub;
hub: HubInterface;
contexts: Array<TraversalContext>;
data: Object;
shouldSkip: boolean;
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-traverse/src/path/introspection.js
Expand Up @@ -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) {
Expand Down
10 changes: 4 additions & 6 deletions packages/babel-traverse/src/scope/index.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 5 additions & 7 deletions packages/babel-traverse/test/arrow-transform.js
Expand Up @@ -13,13 +13,11 @@ function assertConversion(

const rootPath = NodePath.get({
hub: {
file: {
addHelper(helperName) {
return t.memberExpression(
t.identifier("babelHelpers"),
t.identifier(helperName),
);
},
addHelper(helperName) {
return t.memberExpression(
t.identifier("babelHelpers"),
t.identifier(helperName),
);
},
},
parentPath: null,
Expand Down
10 changes: 10 additions & 0 deletions packages/babel-traverse/test/hub.js
@@ -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));
});
});