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 #5047 #5050
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -99,7 +100,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) | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought you gonna replace the object with the Like: hub: HubInterface;
this.hub = new Hub(this); |
||
} | ||
|
||
static helpers: Array<string>; | ||
|
@@ -119,7 +134,7 @@ export default class File extends Store { | |
ast: Object; | ||
scope: Scope; | ||
metadata: BabelFileMetadata; | ||
hub: Hub; | ||
hub: HubInterface; | ||
code: string; | ||
shebang: string; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @loganfsmyth what do you think about this approach? when |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you removed the
file
property from the interface, but we'll definitely need to keep it for the usage inbabel-core
at least, or else it will break people's plugins that read properties off ofhub
. For example some plugins dopath.hub.file.opts.filename
to get the name of the file being compiled.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch! fixed it :)