Skip to content

Commit

Permalink
feat: detect react-compiler rules violations (#518)
Browse files Browse the repository at this point in the history
## 📜 Description

Added eslint plugin for `react-compiler` + plugin for detecting unused
`disable` directives.

## 💡 Motivation and Context

Since react-compiler was rolled out to public - it's crucial to
understand which part of the code in library can not be compiled
successfully and why. For that I added new eslint integration.

Also I know how to fix some of these violations, and don't want to have
outdated eslint comments in future, so I added a new plugin that detects
a usage of such unused directives and reports them.

I think this PR is a first step in `react-compiler` journey in this
library, so let's ship it :shipit:

## 📢 Changelog

<!-- High level overview of important changes -->
<!-- For example: fixed status bar manipulation; added new types
declarations; -->
<!-- If your changes don't affect one of platform/language below - then
remove this platform/language -->

### JS

- added 2 new eslint rules;
- ignored all new violations;

## 🤔 How Has This Been Tested?

Tested via CI and manually.

## 📝 Checklist

- [x] CI successfully passed
- [x] I added new mocks and corresponding unit-tests if library API was
changed
  • Loading branch information
kirillzyusko authored Jul 23, 2024
1 parent 8fc2303 commit dce5a57
Show file tree
Hide file tree
Showing 29 changed files with 301 additions and 2 deletions.
15 changes: 14 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
module.exports = {
root: true,
plugins: ["@typescript-eslint", "react", "react-native", "jest", "import"],
plugins: [
"@typescript-eslint",
"react",
"react-native",
"jest",
"import",
"react-compiler",
"eslint-comments",
],
extends: [
"@react-native",
"prettier",
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jest/recommended",
"plugin:import/typescript",
"plugin:eslint-comments/recommended",
],
settings: {
"import/parsers": {
Expand Down Expand Up @@ -80,6 +89,10 @@ module.exports = {
additionalTestBlockFunctions: [],
},
],
// react-compiler
"react-compiler/react-compiler": "error",
// eslint-comments
"eslint-comments/no-unused-disable": "error",
},
overrides: [
{
Expand Down
1 change: 1 addition & 0 deletions FabricExample/__tests__/keyboard-handler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function TestComponent() {
{
onStart: (e) => {
"worklet";
// eslint-disable-next-line react-compiler/react-compiler
height.value = e.height;
},
onMove: (e) => {
Expand Down
1 change: 1 addition & 0 deletions FabricExample/src/components/KeyboardAnimation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const useGradualKeyboardAnimation = () => {
onMove: (e) => {
"worklet";

// eslint-disable-next-line react-compiler/react-compiler
height.value = e.height;
},
onEnd: (e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const TextInputWithMicSelection = (props: TextInputProps) => {
const position = useSharedValue({ x: 0, y: 0 });

useEffect(() => {
// eslint-disable-next-line react-compiler/react-compiler
tag.value = findNodeHandle(ref.current) ?? -1;
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const useKeyboardAnimation = () => {
onMove: (e) => {
"worklet";

// eslint-disable-next-line react-compiler/react-compiler
progress.value = e.progress;
height.value = e.height;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const useKeyboardAnimation = () => {
// simply update `height` to destination and we need to listen to `onMove`
// handler to have a smooth animation
if (progress.value !== 1 && progress.value !== 0 && e.height !== 0) {
// eslint-disable-next-line react-compiler/react-compiler
shouldUseOnMoveHandler.value = true;
return;
}
Expand Down
1 change: 1 addition & 0 deletions FabricExample/src/screens/Examples/Lottie/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function LottieAnimation() {
onMove: (e) => {
"worklet";

// eslint-disable-next-line react-compiler/react-compiler
progress.value = e.progress;
},
},
Expand Down
1 change: 1 addition & 0 deletions FabricExample/src/screens/Examples/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default function ModalExample() {
onPress={() => setModalVisible(false)}
testID="close_button"
/>
{/* eslint-disable-next-line react-compiler/react-compiler */}
<KeyboardAnimationTemplate provider={useKeyboardAnimation} />
</View>
</Modal>
Expand Down
1 change: 1 addition & 0 deletions FabricExample/src/screens/Examples/NonUIProps/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function useGradualKeyboardAnimation() {
onMove: (e) => {
"worklet";

// eslint-disable-next-line react-compiler/react-compiler
height.value = e.height;
progress.value = e.progress;
},
Expand Down
1 change: 1 addition & 0 deletions FabricExample/src/screens/Examples/ReanimatedChat/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const useTelegramTransitions = () => {
// on Android Telegram is not using androidx.core values and uses custom interpolation
// duration is taken from here: https://github.com/DrKLO/Telegram/blob/e9a35cea54c06277c69d41b8e25d94b5d7ede065/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/AdjustPanLayoutHelper.java#L39
// and bezier is taken from: https://github.com/DrKLO/Telegram/blob/e9a35cea54c06277c69d41b8e25d94b5d7ede065/TMessagesProj/src/main/java/androidx/recyclerview/widget/ChatListItemAnimator.java#L40
// eslint-disable-next-line react-compiler/react-compiler
height.value = withTiming(-e.height, {
duration: 250,
easing: Easing.bezier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const useGradualAnimation = () => {
onMove: (e) => {
"worklet";

// eslint-disable-next-line react-compiler/react-compiler
height.value = e.height;
},
onEnd: (e) => {
Expand Down
1 change: 1 addition & 0 deletions example/__tests__/keyboard-handler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function TestComponent() {
{
onStart: (e) => {
"worklet";
// eslint-disable-next-line react-compiler/react-compiler
height.value = e.height;
},
onMove: (e) => {
Expand Down
1 change: 1 addition & 0 deletions example/src/components/KeyboardAnimation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const useGradualKeyboardAnimation = () => {
onMove: (e) => {
"worklet";

// eslint-disable-next-line react-compiler/react-compiler
height.value = e.height;
},
onEnd: (e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const TextInputWithMicSelection = (props: TextInputProps) => {
const position = useSharedValue({ x: 0, y: 0 });

useEffect(() => {
// eslint-disable-next-line react-compiler/react-compiler
tag.value = findNodeHandle(ref.current) ?? -1;
}, []);

Expand Down
1 change: 1 addition & 0 deletions example/src/screens/Examples/InteractiveKeyboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const useKeyboardAnimation = () => {
onMove: (e) => {
"worklet";

// eslint-disable-next-line react-compiler/react-compiler
progress.value = e.progress;
height.value = e.height;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const useKeyboardAnimation = () => {
// simply update `height` to destination and we need to listen to `onMove`
// handler to have a smooth animation
if (progress.value !== 1 && progress.value !== 0 && e.height !== 0) {
// eslint-disable-next-line react-compiler/react-compiler
shouldUseOnMoveHandler.value = true;
return;
}
Expand Down
1 change: 1 addition & 0 deletions example/src/screens/Examples/Lottie/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function LottieAnimation() {
onMove: (e) => {
"worklet";

// eslint-disable-next-line react-compiler/react-compiler
progress.value = e.progress;
},
},
Expand Down
1 change: 1 addition & 0 deletions example/src/screens/Examples/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default function ModalExample() {
onPress={() => setModalVisible(false)}
testID="close_button"
/>
{/* eslint-disable-next-line react-compiler/react-compiler */}
<KeyboardAnimationTemplate provider={useKeyboardAnimation} />
</View>
</Modal>
Expand Down
1 change: 1 addition & 0 deletions example/src/screens/Examples/NonUIProps/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function useGradualKeyboardAnimation() {
onMove: (e) => {
"worklet";

// eslint-disable-next-line react-compiler/react-compiler
height.value = e.height;
progress.value = e.progress;
},
Expand Down
1 change: 1 addition & 0 deletions example/src/screens/Examples/ReanimatedChat/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const useTelegramTransitions = () => {
// on Android Telegram is not using androidx.core values and uses custom interpolation
// duration is taken from here: https://github.com/DrKLO/Telegram/blob/e9a35cea54c06277c69d41b8e25d94b5d7ede065/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/AdjustPanLayoutHelper.java#L39
// and bezier is taken from: https://github.com/DrKLO/Telegram/blob/e9a35cea54c06277c69d41b8e25d94b5d7ede065/TMessagesProj/src/main/java/androidx/recyclerview/widget/ChatListItemAnimator.java#L40
// eslint-disable-next-line react-compiler/react-compiler
height.value = withTiming(-e.height, {
duration: 250,
easing: Easing.bezier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const useGradualAnimation = () => {
onMove: (e) => {
"worklet";

// eslint-disable-next-line react-compiler/react-compiler
height.value = e.height;
},
onEnd: (e) => {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jest": "^26.5.3",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react-compiler": "^0.0.0-experimental-0998c1e-20240625",
"husky": "^6.0.0",
"jest": "^29.6.3",
"pod-install": "^0.1.0",
Expand Down
1 change: 1 addition & 0 deletions src/animated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const KeyboardProvider = ({
"worklet";

if (platforms.includes(OS)) {
// eslint-disable-next-line react-compiler/react-compiler
progressSV.value = event.progress;
heightSV.value = -event.height;
}
Expand Down
1 change: 1 addition & 0 deletions src/components/KeyboardAvoidingView/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const useKeyboardAnimation = () => {
"worklet";

if (e.height > 0) {
// eslint-disable-next-line react-compiler/react-compiler
isClosed.value = false;
heightWhenOpened.value = e.height;
}
Expand Down
1 change: 1 addition & 0 deletions src/components/KeyboardAvoidingView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const KeyboardAvoidingView = forwardRef<
"worklet";

if (keyboard.isClosed.value || initialFrame.value === null) {
// eslint-disable-next-line react-compiler/react-compiler
initialFrame.value = layout;
}
}, []);
Expand Down
1 change: 1 addition & 0 deletions src/components/KeyboardAwareScrollView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ const KeyboardAwareScrollView = forwardRef<
return;
}

// eslint-disable-next-line react-compiler/react-compiler
layout.value = {
...input.value,
layout: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const useSmoothKeyboardHandler: typeof useKeyboardHandler = (
// dispatch `onEnd`
if (evt.height === height.value) {
handler.onEnd?.(evt);
// eslint-disable-next-line react-compiler/react-compiler
persistedHeight.value = height.value;
}
},
Expand Down
1 change: 1 addition & 0 deletions src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function useSharedHandlers<
// that it will have all of them) and then update them in worklet thread (calls are
// happening in FIFO order, so we will always have actual value).
const updateSharedHandlers = () => {
// eslint-disable-next-line react-compiler/react-compiler
handlers.value = jsHandlers.current;
};
const setHandlers = useCallback((handler: Handlers<T>) => {
Expand Down
Loading

0 comments on commit dce5a57

Please sign in to comment.