|
| 1 | +# eslint-plugin-pressto |
| 2 | + |
| 3 | +ESLint plugin for [pressto](https://github.com/enzomanuelmangano/pressto) - enforces best practices when using pressto's animated components. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install --save-dev eslint-plugin-pressto |
| 9 | +# or |
| 10 | +yarn add -D eslint-plugin-pressto |
| 11 | +# or |
| 12 | +bun add -D eslint-plugin-pressto |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +### With ESLint Flat Config (`eslint.config.js`) |
| 18 | + |
| 19 | +```javascript |
| 20 | +const presstoPlugin = require('eslint-plugin-pressto'); |
| 21 | + |
| 22 | +module.exports = [ |
| 23 | + { |
| 24 | + plugins: { |
| 25 | + pressto: presstoPlugin, |
| 26 | + }, |
| 27 | + rules: { |
| 28 | + 'pressto/require-worklet-directive': 'error', |
| 29 | + }, |
| 30 | + }, |
| 31 | +]; |
| 32 | +``` |
| 33 | + |
| 34 | +### With Legacy Config (`.eslintrc.js`) |
| 35 | + |
| 36 | +```javascript |
| 37 | +module.exports = { |
| 38 | + plugins: ['pressto'], |
| 39 | + rules: { |
| 40 | + 'pressto/require-worklet-directive': 'error', |
| 41 | + }, |
| 42 | +}; |
| 43 | +``` |
| 44 | + |
| 45 | +## Rules |
| 46 | + |
| 47 | +### `pressto/require-worklet-directive` |
| 48 | + |
| 49 | +Enforces the use of `'worklet'` directive in functions passed to `createAnimatedPressable`. |
| 50 | + |
| 51 | +When using `createAnimatedPressable`, the animation function must run on the UI thread using React Native Reanimated. This requires a `'worklet'` directive as the first statement in the function body. |
| 52 | + |
| 53 | +#### Rule Details |
| 54 | + |
| 55 | +❌ Examples of **incorrect** code: |
| 56 | + |
| 57 | +```javascript |
| 58 | +const AnimatedButton = createAnimatedPressable(() => { |
| 59 | + // Missing 'worklet' directive |
| 60 | + return { |
| 61 | + opacity: withSpring(1), |
| 62 | + }; |
| 63 | +}); |
| 64 | + |
| 65 | +// Arrow function with implicit return (not supported) |
| 66 | +const AnimatedButton = createAnimatedPressable(() => ({ |
| 67 | + opacity: withSpring(1), |
| 68 | +})); |
| 69 | +``` |
| 70 | + |
| 71 | +✅ Examples of **correct** code: |
| 72 | + |
| 73 | +```javascript |
| 74 | +const AnimatedButton = createAnimatedPressable(() => { |
| 75 | + 'worklet'; |
| 76 | + return { |
| 77 | + opacity: withSpring(1), |
| 78 | + }; |
| 79 | +}); |
| 80 | + |
| 81 | +const AnimatedButton = createAnimatedPressable(function() { |
| 82 | + 'worklet'; |
| 83 | + return { |
| 84 | + opacity: withSpring(1), |
| 85 | + }; |
| 86 | +}); |
| 87 | +``` |
| 88 | + |
| 89 | +## Why This Plugin? |
| 90 | + |
| 91 | +The `'worklet'` directive is required for functions that need to run on the UI thread in React Native Reanimated. Forgetting to add it can lead to runtime errors or unexpected behavior. This plugin helps catch these issues during development. |
| 92 | + |
| 93 | +## Related |
| 94 | + |
| 95 | +- [pressto](https://github.com/enzomanuelmangano/pressto) - Custom React Native touchables with animations |
| 96 | +- [React Native Reanimated](https://docs.swmansion.com/react-native-reanimated/) - React Native's Animated library reimplemented |
| 97 | + |
| 98 | +## License |
| 99 | + |
| 100 | +MIT |
0 commit comments