-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Enforce the use of ReanimatedError instead of Error (#6454)
## Summary This PR adds eslint rule that will enforce the use of the new `ReanimatedError` in favor of `Error` in the library source files. ## Example image ![Screenshot 2024-08-28 at 17 25 11](https://github.com/user-attachments/assets/c11043ba-cfb4-43da-93d5-ea79611d1421) ## Test plan Just try to use `throw new Error` inside the `packages/react-native-reanimated` instead of `throw new ReanimatedError` and see that eslint complains about incorrect usage.
- Loading branch information
Showing
11 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/eslint-plugin-reanimated/__tests__/useReanimatedError.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* eslint-disable */ | ||
// @ts-nocheck | ||
// TODO: FIX THESE | ||
// eslint-disable-next-line import/no-unresolved | ||
import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
import { rules } from '../src'; | ||
|
||
// For reasons unknown the following line causes Jest to hang indefinitely | ||
// hence we disable this test suite until it's resolved | ||
// const ruleTester = new RuleTester({ | ||
// parserOptions: { | ||
// ecmaFeatures: { | ||
// jsx: true, | ||
// }, | ||
// sourceType: 'module', | ||
// }, | ||
// parser: '@typescript-eslint/parser', | ||
// }); | ||
|
||
// const testCases = { | ||
// withReanimatedError: { | ||
// /** Correct code correctly classified as satisfying the rule */ | ||
// trueNegative: [ | ||
// `const err = new ReanimatedError('Something went wrong');`, | ||
// `function createError() { return new ReanimatedError('Error message'); }`, | ||
// `throw new ReanimatedError('Custom error');`, | ||
// ], | ||
// /** Incorrect code correctly classified as not satisfying the rule */ | ||
// truePositive: [ | ||
// { | ||
// code: `const err = new Error('Something went wrong');`, | ||
// errors: [{ messageId: 'useReanimatedError' }], | ||
// output: `const err = new ReanimatedError('Something went wrong');`, | ||
// }, | ||
// { | ||
// code: `function createError() { return new Error('Error message'); }`, | ||
// errors: [{ messageId: 'useReanimatedError' }], | ||
// output: `function createError() { return new ReanimatedError('Error message'); }`, | ||
// }, | ||
// { | ||
// code: `throw new Error('Custom error');`, | ||
// errors: [{ messageId: 'useReanimatedError' }], | ||
// output: `throw new ReanimatedError('Custom error');`, | ||
// }, | ||
// ], | ||
// /** Incorrect code incorrectly classified as satisfying the rule */ | ||
// falseNegative: [], | ||
// /** Incorrect code incorrectly classified as not satisfying the rule */ | ||
// falsePositive: [], | ||
// }, | ||
// }; | ||
|
||
// const { withReanimatedError } = testCases; | ||
|
||
// const ruleName = 'use-reanimated-error'; | ||
// ruleTester.run(`Test rule ${ruleName}`, rules[ruleName], { | ||
// valid: [...withReanimatedError.trueNegative], | ||
// invalid: [...withReanimatedError.truePositive], | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import type { TSESLint } from '@typescript-eslint/utils'; | ||
import noAnimatedStyleToNonAnimatedComponent from './noAnimatedStyleToNonAnimatedComponent'; | ||
import useReanimatedError from './useReanimatedError'; | ||
|
||
export const rules = { | ||
'animated-style-non-animated-component': | ||
noAnimatedStyleToNonAnimatedComponent, | ||
'use-reanimated-error': useReanimatedError, | ||
} satisfies Record<string, TSESLint.RuleModule<string, Array<unknown>>>; |
42 changes: 42 additions & 0 deletions
42
packages/eslint-plugin-reanimated/src/useReanimatedError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'; | ||
// eslint-disable-next-line import/no-unresolved | ||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
|
||
const rule: TSESLint.RuleModule<'useReanimatedError', []> = { | ||
create: function (context) { | ||
return { | ||
NewExpression(node: TSESTree.NewExpression) { | ||
// Check if the expression is `new Error` | ||
if ( | ||
node.callee.type === AST_NODE_TYPES.Identifier && | ||
node.callee.name === 'Error' | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'useReanimatedError', | ||
fix: function (fixer) { | ||
// Replace `Error` with `ReanimatedError` | ||
return fixer.replaceText(node.callee, 'ReanimatedError'); | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
recommended: 'recommended', | ||
description: | ||
'Warns when `new Error` is used instead of `new ReanimatedError`.', | ||
}, | ||
messages: { | ||
useReanimatedError: 'Use `new ReanimatedError` instead of `new Error`.', | ||
}, | ||
type: 'suggestion', | ||
schema: [], | ||
fixable: 'code', | ||
}, | ||
defaultOptions: [], | ||
}; | ||
|
||
export default rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/eslint-plugin-reanimated/types/useReanimatedError.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { TSESLint } from '@typescript-eslint/utils'; | ||
declare const rule: TSESLint.RuleModule<'useReanimatedError', []>; | ||
export default rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
module.exports = { | ||
extends: ['../../.eslintrc.js'], | ||
plugins: ['eslint-plugin-reanimated'], | ||
ignorePatterns: ['lib'], | ||
rules: { | ||
'reanimated/use-reanimated-error': 'error', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters