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

Pass down generator boolean in babel plugin #5565

Merged
merged 6 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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