Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
tjzel committed Jul 2, 2024
1 parent 4115e83 commit ecacb53
Show file tree
Hide file tree
Showing 8 changed files with 700 additions and 3,578 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"packages": [
"packages/react-native-reanimated",
"packages/eslint-plugin-reanimated",
"packages/react-native-reanimated/plugin",
"apps/*"
]
},
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native-reanimated/plugin/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const reanimatedPlugin = require('./index.js');

module.exports = {
plugins: [reanimatedPlugin],
};
82 changes: 79 additions & 3 deletions packages/react-native-reanimated/plugin/build/plugin.js

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

5 changes: 5 additions & 0 deletions packages/react-native-reanimated/plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"devDependencies": {
"@babel/cli": "^7.20.0",
"@babel/core": "^7.20.0",
"@babel/plugin-transform-unicode-regex": "^7.24.7",
"@babel/traverse": "^7.20.0",
"@babel/types": "^7.20.0",
"@react-native/eslint-config": "^0.72.1",
"@types/node": "^18.15.11",
"@typescript-eslint/eslint-plugin": "^7.0.2",
Expand Down
81 changes: 81 additions & 0 deletions packages/react-native-reanimated/plugin/src/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {
blockStatement,
directive,
directiveLiteral,
returnStatement,
} from '@babel/types';

import type {
Expression,
Program,
BlockStatement,
VariableDeclaration,
} from '@babel/types';
import type { NodePath } from '@babel/core';
import type { ReanimatedPluginPass } from './types';

export function processIfWorkletFile(
path: NodePath<Program>,
state: ReanimatedPluginPass
) {
if (
path.node.directives.some(
(functionDirective) => functionDirective.value.value === 'worklet'
)
) {
processWorkletFile(path, state);
// Remove 'worklet' directive from the file afterwards.
path.node.directives = path.node.directives.filter(
(functionDirective) => functionDirective.value.value !== 'worklet'
);
}
}

function processWorkletFile(
path: NodePath<Program>,
_state: ReanimatedPluginPass
) {
path.get('body').forEach((bodyPath) => {
if (bodyPath.isVariableDeclaration()) {
processVariableDeclaration(bodyPath);
}
if (bodyPath.isFunctionDeclaration()) {
appendWorkletDirective(bodyPath.node.body);
}
});
}

function processVariableDeclaration(path: NodePath<VariableDeclaration>) {
path.get('declarations').forEach((declaration) => {
const initPath = declaration.get('init');
if (initPath.isFunctionExpression()) {
appendWorkletDirective(initPath.node.body);
} else if (initPath.isArrowFunctionExpression()) {
const bodyPath = initPath.get('body');

// In case of arrow function with no body, i.e. () => 1.
if (!bodyPath.isBlockStatement()) {
bodyPath.replaceWith(
blockStatement([returnStatement(bodyPath.node as Expression)])
);
}
appendWorkletDirective(bodyPath.node as BlockStatement);
} else if (initPath.isObjectExpression()) {
initPath.node.properties.forEach((property) => {
if (property.type === 'ObjectMethod') {
appendWorkletDirective(property.body);
}
});
}
});
}

function appendWorkletDirective(node: BlockStatement) {
if (
!node.directives.some(
(functionDirective) => functionDirective.value.value === 'worklet'
)
) {
node.directives.push(directive(directiveLiteral('worklet')));
}
}
12 changes: 10 additions & 2 deletions packages/react-native-reanimated/plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PluginItem, NodePath } from '@babel/core';
import type { CallExpression } from '@babel/types';
import type { CallExpression, JSXAttribute, Program } from '@babel/types';
import {
processIfAutoworkletizableCallback,
processCalleesAutoworkletizableCallbacks,
Expand All @@ -11,6 +11,7 @@ import { processInlineStylesWarning } from './inlineStylesWarning';
import { addCustomGlobals } from './utils';
import { initializeGlobals } from './globals';
import { substituteWebCallExpression } from './webOptimization';
import { processIfWorkletFile } from './file';

module.exports = function (): PluginItem {
function runWithTaggedExceptions(fun: () => void) {
Expand Down Expand Up @@ -50,8 +51,15 @@ module.exports = function (): PluginItem {
});
},
},
Program: {
enter(path: NodePath<Program>, state: ReanimatedPluginPass) {
runWithTaggedExceptions(() => {
processIfWorkletFile(path, state);
});
},
},
JSXAttribute: {
enter(path, state) {
enter(path: NodePath<JSXAttribute>, state: ReanimatedPluginPass) {
runWithTaggedExceptions(() =>
processInlineStylesWarning(path, state)
);
Expand Down
Loading

0 comments on commit ecacb53

Please sign in to comment.