Skip to content

Conversation

@oliverwoodings
Copy link

This fixes a weird stateful bug where a single comment-based JSX pragma declaration can change the pragma used for all subsequent transforms.

The issue is caused by the options object being mutated by the transform() function. In particular, it is done when a @jsx directive comment is found in a file. If you then re-use the same options object instance for your next transform call (which coincidentally is what is done by buble-loader), you will be potentially passing through an incorrect JSX pragma.

Example

const transform = require('buble')

const options = {}

transform(`
/* @jsx h */
const { h } = require('preact')
const foo = <div>Bar</div>
`, options)

/* output:
const { h } = require('preact')
const foo = h('div', {}, 'Bar')
*/

// At this point, options has been mutatated by calling `transform`, it now looks like this: { jsx: 'h' }

transform(`
const React = require('preact')
const foo = <div>Bar</div>
`, options)

/* output:
const React = require('preact')
const foo = h('div', {}, 'Bar')
*/

The solution I have implemented here is to shallow-clone the options object before mutating it.

@oliverwoodings oliverwoodings changed the title Clone options object before mutating it fix: Clone options object before mutating it Oct 1, 2019
@oliverwoodings
Copy link
Author

I have no idea why tests are failing in node 8, it works absolutely fine locally

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.

1 participant