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

Allow worklet referencing in plugin #5911

Merged
merged 11 commits into from
Jun 10, 2024
Prev Previous commit
Next Next commit
allow for objects and refactor the pipeline for less redundacne
  • Loading branch information
tjzel committed Apr 18, 2024
commit 0b7821d24d70297386979de229948be5ee4b5b05
72 changes: 72 additions & 0 deletions __tests__/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,78 @@ style = function () {
animatedStyle = useAnimatedStyle(style);"
`;

exports[`babel plugin for referenced worklets workletizes ObjectExpression on its AssignmentExpression 1`] = `
"var handler;
var _worklet_16988157363598_init_data = {
code: "function onScroll(){}",
location: "/dev/null",
sourceMap: "\\"mock source map\\"",
version: "x.y.z"
};
handler = {
onScroll: function () {
const _e = [new global.Error(), 1, -27];
const onScroll = function () {};
onScroll.__closure = {};
onScroll.__workletHash = 16988157363598;
onScroll.__initData = _worklet_16988157363598_init_data;
onScroll.__stackDetails = _e;
return onScroll;
}()
};
var scrollHandler = useAnimatedScrollHandler(handler);"
`;

exports[`babel plugin for referenced worklets workletizes ObjectExpression on its VariableDeclarator 1`] = `
"var _worklet_16988157363598_init_data = {
code: "function onScroll(){}",
location: "/dev/null",
sourceMap: "\\"mock source map\\"",
version: "x.y.z"
};
var handler = {
onScroll: function () {
const _e = [new global.Error(), 1, -27];
const onScroll = function () {};
onScroll.__closure = {};
onScroll.__workletHash = 16988157363598;
onScroll.__initData = _worklet_16988157363598_init_data;
onScroll.__stackDetails = _e;
return onScroll;
}()
};
var scrollHandler = useAnimatedScrollHandler(handler);"
`;

exports[`babel plugin for referenced worklets workletizes ObjectExpression only on last AssignmentExpression 1`] = `
"var handler;
handler = {
onScroll: function onScroll() {
return 1;
}
};
var _worklet_9384619123806_init_data = {
code: "function onScroll(){return'AssignmentExpression';}",
location: "/dev/null",
sourceMap: "\\"mock source map\\"",
version: "x.y.z"
};
handler = {
onScroll: function () {
const _e = [new global.Error(), 1, -27];
const onScroll = function () {
return 'AssignmentExpression';
};
onScroll.__closure = {};
onScroll.__workletHash = 9384619123806;
onScroll.__initData = _worklet_9384619123806_init_data;
onScroll.__stackDetails = _e;
return onScroll;
}()
};
var scrollHandler = useAnimatedScrollHandler(handler);"
`;

exports[`babel plugin for sequence expressions supports SequenceExpression 1`] = `
"function App() {
(0, fun)({
Expand Down
45 changes: 45 additions & 0 deletions __tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,51 @@ describe('babel plugin', () => {
expect(code).toMatchSnapshot();
});

it('workletizes ObjectExpression on its VariableDeclarator', () => {
const input = html`<script>
let handler = {
onScroll: () => {},
};
const scrollHandler = useAnimatedScrollHandler(handler);
</script>`;

const { code } = runPlugin(input);
expect(code).toHaveWorkletData(1);
expect(code).toMatchSnapshot();
});

it('workletizes ObjectExpression on its AssignmentExpression', () => {
const input = html`<script>
let handler;
handler = {
onScroll: () => {},
};
const scrollHandler = useAnimatedScrollHandler(handler);
</script>`;

const { code } = runPlugin(input);
expect(code).toHaveWorkletData(1);
expect(code).toMatchSnapshot();
});

it('workletizes ObjectExpression only on last AssignmentExpression', () => {
const input = html`<script>
let handler;
handler = {
onScroll: () => 1,
};
handler = {
onScroll: () => 'AssignmentExpression',
};
const scrollHandler = useAnimatedScrollHandler(handler);
</script>`;

const { code } = runPlugin(input);
expect(code).toHaveWorkletData(1);
expect(code).toIncludeInWorkletString('AssignmentExpression');
expect(code).toMatchSnapshot();
});

it('prefers FunctionDeclaration over AssignmentExpression', () => {
const input = html`<script>
function style() {
Expand Down
1 change: 1 addition & 0 deletions plugin/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ module.exports = {
],
rules: {
'curly': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
},
ignorePatterns: ['**/*.d.ts','jestUtils.ts']};
183 changes: 112 additions & 71 deletions plugin/build/plugin.js

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

Loading
Loading