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

Make babel-relay-plugin run before other transforms #714

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions scripts/babel-relay-plugin/lib/babelAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,37 @@ var path = require('path');

function babelAdapter(Plugin, t, name, visitorsBuilder) {
if (Plugin == null) {
// Babel 6.
return visitorsBuilder(t);
var _ret = (function () {
// Babel 6.

var _visitorsBuilder = visitorsBuilder(t);

var _visitorsBuilder$visitor = _visitorsBuilder.visitor;
var _Program = _visitorsBuilder$visitor.Program;
var _TaggedTemplateExpression = _visitorsBuilder$visitor.TaggedTemplateExpression;

var taggedTemplateExpressionVisitor = {
TaggedTemplateExpression: function TaggedTemplateExpression(path) {
return _TaggedTemplateExpression(path, this);
}
};

/**
* Run both transforms on Program, to make sure that they run before other plugins
*/
return {
v: {
visitor: {
Program: function Program(path, state) {
_Program(path, state);
path.traverse(taggedTemplateExpressionVisitor, state);
}
}
}
};
})();

if (typeof _ret === 'object') return _ret.v;
}
// Babel 5.
var legacyT = _extends({}, t, {
Expand Down
20 changes: 19 additions & 1 deletion scripts/babel-relay-plugin/src/babelAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,25 @@ function babelAdapter(
): mixed {
if (Plugin == null) {
// Babel 6.
return visitorsBuilder(t);
const { visitor: { Program, TaggedTemplateExpression } } = visitorsBuilder(t);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Omit space inside curly braces.

const {visitor: {Program, …


const taggedTemplateExpressionVisitor = {
TaggedTemplateExpression(path) {
return TaggedTemplateExpression(path, this);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this required?

In other words, why can't we just use:

path.traverse({TaggedTemplateExpression}, state);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, the way I understand it is that TaggedTemplateExpression, when in it's normal plugin form, takes in path and state as arguments. state doesn't get passed in, I don't think, when using path.traverse. Instead, state is passed in as this.

Honestly, I'm not a super Babel expert either -- I followed what @thejameskyle did here:

https://phabricator.babeljs.io/rBC0b264aa38af5ba7fb121b216be2fb69eece7217c

}
}

/**
* Run both transforms on Program, to make sure that they run before other plugins
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Omit comma and add a period.

*/
return {
visitor: {
Program(path, state) {
Program(path, state);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not a super Babel expert, and better safe than sorry. Does the return value never matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, after a second look, it doesn't matter. Will fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in skevy@f748816

Copy link
Contributor

Choose a reason for hiding this comment

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

@skevy is the (void) return value here intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@josephsavona I'm not sure why it didn't invalidate the commit...but I removed the return. It's not needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

awesome, thanks for confirming.

path.traverse(taggedTemplateExpressionVisitor, state);
}
}
}
}
// Babel 5.
const legacyT = {
Expand Down