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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #5050 +/- ##
=========================================
Coverage ? 89.22%
=========================================
Files ? 203
Lines ? 9848
Branches ? 2628
=========================================
Hits ? 8787
Misses ? 1061
Partials ? 0
Continue to review full report at Codecov.
|
78dd051
to
e034c4f
Compare
packages/babel-traverse/src/index.js
Outdated
@@ -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 }; |
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 guess there isn't an export shortcut as in ES6 ? Like:
export type Hub from "./hub";
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.
no it isn't, this is how flow export types. this has no influence to the compiled code.
message, | ||
loc: this.node.loc | ||
}); | ||
this.hub.mark && this.hub.mark(type, message, this.node.loc); |
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.
To be consistent in coding style, could you use an explicit if there?
packages/babel-traverse/src/hub.js
Outdated
addHelper: (name: string) => Object; | ||
getScope: () => Object; | ||
getCode: () => string; | ||
buildError:(node: Object, msg: string, Error: Error) => Error; |
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 don't know if some users rely on this class.
I would like to keep the class there, what do you think? I'm not sure because the class is empty anyway.
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.
yeah, totally! I think using export default class Hub
instead export type Hub
would probably be good here.
Though Hub
itself wasn't been referred in repo, it is possible that someone used Hub
to store states. We can totally keep it.
@@ -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)}); |
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.
Could you use an explicit if there?
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.
absolutely! Thanks for reviewing. Working on it :)
packages/babel-traverse/src/hub.js
Outdated
export type Hub = { | ||
mark: (type: string, message: string) => void; | ||
addHelper: (name: string) => Object; | ||
getScope: () => Object; |
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 think getScope
returns a Scope
object.
de50db4
to
4e14889
Compare
@xtuc what do you think about this new version? I made some changes based on your suggestion. |
getCode: () => this.code, | ||
getScope: () => this.scope, | ||
buildError: this.buildCodeFrameError.bind(this) | ||
}; |
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 thought you gonna replace the object with the Hub
class in packages/babel-traverse/src/hub.js
. Can we replace it?
Like:
hub: HubInterface;
this.hub = new Hub(this);
packages/babel-traverse/src/hub.js
Outdated
constructor(file, options) { | ||
this.file = file; | ||
this.options = options; | ||
buildError(node, msg, Error): Error { |
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.
The coverage fails there because the method buildError
isn't tested.
If you move the content of the object (as described above) there, we need to add tests for it.
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 added a test for this, though the test doesn't seem very necessary.
It did pass the coverage test now! Thanks for reviewing :)
@xtuc any progress or request on this PR? Thanks |
packages/babel-traverse/src/hub.js
Outdated
this.file = file; | ||
this.options = options; | ||
buildError(node, msg, Error): Error { | ||
return 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.
This should be new Error
since this function should return a type based on the third parameter of the function.
@@ -99,7 +100,19 @@ export default class File extends Store { | |||
this.code = ""; | |||
this.shebang = ""; | |||
|
|||
this.hub = new Hub(this); | |||
this.hub = { |
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 in babel-core
at least, or else it will break people's plugins that read properties off of hub
. For example some plugins do path.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 :)
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 comment
The reason will be displayed to describe this comment to others. Learn more.
@loganfsmyth what do you think about this approach? when Error
is not specified, use TypeError
as defaiult.
also changed its name to BuildError
in the implementation, which is more descriptive IMO.
Hey @yongxu! It looks like one or more of your builds have failed. I've copied the relevant info below to save you some time. |
Whoo thanks @yongxu 🎉, sorry for the delay!! 🙏 |
Nice work 👍 |
Thanks guys! Appreciate it, will contribute more :) |
I am really sorry about this breaking issue @loganfsmyth , I am looking into it. |
* Add new flow preset (#5288) * Fix PathHoister hoisting JSX member expressions on "this". (#5143) The PathHoister ignored member references on "this", causing it to potentially hoist an expression above its function scope. This patch tells the hoister to watch for "this", and if seen, mark the nearest non-arrow function scope as the upper limit for hoistng. This fixes #4397 and is an alternative to #4787. * Fix PathHoister hoisting before bindings. (#5153) Fixes #5149 and enables a few additional safe hoists. * Fix linting error * feature: Support pure expressions in transform-react-constant-elements (#4812) * Fix loose for-of with label (#5298) * Rewrite Hub as interface #5047 (#5050) * Rewrite Hub as interface #5047 * Update index.js * Avoid adding unnecessary closure for block scoping (#5246) When you write ``` for (const x of l) { setTimeout(() => x); } ``` we need to add a closure because the variable is meant to be block-scoped and recreated each time the block runs. We do this. However, we also add the closure when no loop is present. This isn't necessary, because if no loop is present then each piece of code runs at most once. I changed the transform to only add a closure if a variable is referenced from within a loop. * Add greenkeeperio-bot to mention-bot blacklist (#5301) [skip ci] * Upgrade lerna to current beta. (#5300) * Revert "Upgrade lerna to current beta." (#5303) * Add charset so tests work with convert-source-map@>1.4 (#5302) * Add CHANGELOG for 6.23.0 [skip ci] (#5304) * Update babel-types README from script. * v6.23.0 * Revert change that lerna force-committed. * Revert "Rewrite Hub as interface #5047" (#5306) * v6.23.1 * Revert lerna again
@yongxu Let's pull this conversation back over here, since the other thread is about the revert. The issue that this case is because
|
Yeah that is very true. Now I totally agree with you to leave this out in the current master. |
Yup! If you'd like to resubmit this for 7.0, I think that would be fine, as long as you are cool with waiting a bit longer for it to be released. |
So I'm trying to use babel-traverse as a standalone library (just traverses an ast built from babylon). I found this pr and merged it into a local clone of babel branch 7.0. The below file links all point to yongxu's fork. When trying to use TypeError: Cannot read property 'buildError' of undefined
at NodePath.buildCodeFrameError (/.../babel/packages/babel-traverse/lib/path/index.js:174:20) where that line in source can be found here buildCodeFrameError(msg: string, Error: typeof Error = SyntaxError): Error {
return this.hub.buildError(this.node, msg, Error);
} Looking into it, I don't see where a hub object is created besides in babel-core, which isn't included in babel-traverse. When I debug, sure enough the What am I missing? I just don't see how this is a standalone package as-is. |
@olsonpm If you are using babel-traverse as a standalone and call I think it would be great to have a definition for the This PR was reverted due to minor version resolving issue. Now since we are progressing to v7, should we try to merge it back? @loganfsmyth Sorry for the delayed response, had been too busy recently. :) |
This PR implements Rewrite Hub as interface #5047 <--check out more details in this issue
will make
babel-traverse
a standalone package