Skip to content

Commit

Permalink
Pass down generator boolean in babel plugin (#5565)
Browse files Browse the repository at this point in the history
fixes #5564

---------

Co-authored-by: Tomasz Żelawski <tomasz.zelawski@swmansion.com>
  • Loading branch information
wcandillon and tjzel committed Jan 11, 2024
1 parent de9ef3d commit 2b6d51b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
42 changes: 42 additions & 0 deletions __tests__/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,48 @@ var animatedStyle = useAnimatedStyle(function () {
}());"
`;

exports[`babel plugin for generators makes a generator worklet factory 1`] = `
"var _worklet_4939499253486_init_data = {
code: "function*foo(){yield'hello';yield'world';}",
location: "/dev/null",
sourceMap: "\\"mock source map\\"",
version: "x.y.z"
};
var foo = function () {
var _e = [new global.Error(), 1, -27];
var foo = function* foo() {
yield 'hello';
yield 'world';
};
foo.__closure = {};
foo.__workletHash = 4939499253486;
foo.__initData = _worklet_4939499253486_init_data;
foo.__stackDetails = _e;
return foo;
}();"
`;

exports[`babel plugin for generators makes a generator worklet string 1`] = `
"var _worklet_4939499253486_init_data = {
code: "function*foo(){yield'hello';yield'world';}",
location: "/dev/null",
sourceMap: "\\"mock source map\\"",
version: "x.y.z"
};
var foo = function () {
var _e = [new global.Error(), 1, -27];
var foo = function* foo() {
yield 'hello';
yield 'world';
};
foo.__closure = {};
foo.__workletHash = 4939499253486;
foo.__initData = _worklet_4939499253486_init_data;
foo.__stackDetails = _e;
return foo;
}();"
`;

exports[`babel plugin for inline styles doesn't show a warning if user writes something like style={styles.value} 1`] = `
"function App() {
return React.createElement(Animated.View, {
Expand Down
32 changes: 32 additions & 0 deletions __tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1690,4 +1690,36 @@ describe('babel plugin', () => {
expect(code).toMatchSnapshot();
});
});

describe('for generators', () => {
it('makes a generator worklet factory', () => {
const input = html`<script>
function* foo() {
'worklet';
yield 'hello';
yield 'world';
}
</script>`;

const { code } = runPlugin(input);
expect(code).toContain('var foo = function* foo() {');
expect(code).toMatchSnapshot();
});

it('makes a generator worklet string', () => {
const input = html`<script>
function* foo() {
'worklet';
yield 'hello';
yield 'world';
}
</script>`;

const { code } = runPlugin(input);
expect(code).toContain(
`code: "function*foo(){yield'hello';yield'world';}"`
);
expect(code).toMatchSnapshot();
});
});
});
4 changes: 2 additions & 2 deletions plugin/build/plugin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion plugin/src/buildWorkletString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export function buildWorkletString(
const workletFunction = functionExpression(
identifier(name),
expression.params,
expression.body
expression.body,
expression.generator
);

const code = generate(workletFunction).code;
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/makeWorklet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function makeWorklet(

const clone = cloneNode(fun.node);
const funExpression = isBlockStatement(clone.body)
? functionExpression(null, clone.params, clone.body)
? functionExpression(null, clone.params, clone.body, clone.generator)
: clone;

let [funString, sourceMapString] = buildWorkletString(
Expand Down

0 comments on commit 2b6d51b

Please sign in to comment.