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

Fix dynamic interpolations #486 #536

Merged
merged 2 commits into from
Jan 15, 2019

Conversation

ijjk
Copy link
Member

@ijjk ijjk commented Jan 6, 2019

After digging into this issue it looks like a change from babel-plugin-transform-es2015-arrow-functions to @babel/plugin-transform-arrow-functions in babel 7's @babel/preset-env broke the dynamic interpolation. If you had the spec option set to true on the @babel/plugin-transform-arrow-functions you wouldn't have encountered this error, but since the default is false most people using babel 7 and dynamic interpolation in this way would encounter an error.

After talking in Babel's slack it seems one of the ways to fix this is to make sure to traverse the JSX elements before @babel/plugin-transform-arrow-functions has a chance to transform preventing this from being changed before the className attribute is added to the elements.

This is based off my upgrade-babel branch so that should probably be finished first. I wasn't sure if you wanted this fix on a separate pull request or not so if you want me to just push it on the other branch and merge it on that pull request let me know. Fixes #486

@ijjk ijjk requested a review from giuseppeg as a code owner January 6, 2019 00:34
@giuseppeg
Copy link
Collaborator

Can you rebase with master?

not being added in time for other transformers to edit them
@ijjk ijjk force-pushed the fix-dynamic-interpolations branch from 1ac902f to 396e2a0 Compare January 6, 2019 18:29
@ijjk
Copy link
Member Author

ijjk commented Jan 6, 2019

It should be rebased to master now

Copy link
Collaborator

@giuseppeg giuseppeg left a comment

Choose a reason for hiding this comment

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

thanks for looking into this! I will need to run a few tests to see if such a change would slow down builds

src/babel.js Outdated
JSXOpeningElement(path, state) {
const el = path.node
const { name } = el.name || {}
let state = {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is not "cool" we should call traverse with the state and opts we get from the plugin https://github.com/babel/babel/blob/aaec2cd51dd24a1ecf52b82e0bbad0a5ccd60b6f/packages/babel-traverse/src/index.js#L14-L20

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah that was a bit hacky sorry, it's updated to pass the state the correct way now

Copy link
Collaborator

Choose a reason for hiding this comment

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

No worries and no need to be sorry! Thank you

test/index.js Outdated
const { code } = await transform(
'./fixtures/dynamic-this-value-in-arrow.js',
{
presets: ['@babel/preset-env']
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe install @babel/plugin-transform-arrow-functions and run it like this:

const { code } = await transform(
  './fixtures/dynamic-this-value-in-arrow.js',
  {
    plugins: [
      '@babel/plugin-transform-arrow-functions',
      plugin,
    ]
  }
)

Copy link
Member Author

Choose a reason for hiding this comment

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

That works, I updated it to just use the @babel/plugin-transform-arrow-functions plugin instead of the @babel/preset-env preset

@giuseppeg
Copy link
Collaborator

I will need to run a few tests to see if such a change would slow down builds

Probably this is not a big deal after all. I ran zeit.co and in dev it is built in 87ms. @timneutkens FYI we are switching to visiting the Program and then start a traversal manually to solve conflicts with other Babel plugins.

transform-arrow-functions in dynamic `this` test
@giuseppeg giuseppeg merged commit 9708877 into vercel:master Jan 15, 2019
@giuseppeg
Copy link
Collaborator

thanks again @ijjk I'll cut a release tomorrow morning (EU time)

@ijjk
Copy link
Member Author

ijjk commented Jan 15, 2019

Ok sounds good. Thanks for getting this merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dynamic interpolations that contain "this" fail under some circumstances
2 participants