File tree Expand file tree Collapse file tree 6 files changed +99
-7
lines changed Expand file tree Collapse file tree 6 files changed +99
-7
lines changed Original file line number Diff line number Diff line change @@ -103,13 +103,14 @@ This plugin exports some custom rules that you can optionally use in your projec
103103<!-- begin auto-generated rules list -->
104104
105105💼 Configurations enabled in.\
106- ✅ Set in the ` recommended ` configuration .\
106+ ⚠️ Configurations set to warn in .\
107107🧪 Set in the ` tests ` configuration.\
108108🔧 Automatically fixable by the [ ` --fix ` CLI option] ( https://eslint.org/docs/user-guide/command-line-interface#--fix ) .
109109
110110| Name | Description | 💼 | 🔧 |
111111| :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------- | :--------------------- | :-- |
112112| [ avoid-intl-number-format] ( https://github.com/bamlab/react-native-project-config/blob/main/packages/eslint-plugin/docs/rules/avoid-intl-number-format.md ) | Disallow the use of ` Intl.NumberFormat ` due to potential performance issues. | ![ badge-performance] [ ] | |
113+ | [ avoid-react-native-svg] ( https://github.com/bamlab/react-native-project-config/blob/main/packages/eslint-plugin/docs/rules/avoid-react-native-svg.md ) | Disallow importing the ` react-native-svg ` package. | | ![ badge-performance] [ ] | |
113114| [ await-user-event] ( https://github.com/bamlab/react-native-project-config/blob/main/packages/eslint-plugin/docs/rules/await-user-event.md ) | Enforces awaiting userEvent calls | 🧪 | 🔧 |
114115| [ no-different-displayname] ( https://github.com/bamlab/react-native-project-config/blob/main/packages/eslint-plugin/docs/rules/no-different-displayname.md ) | Enforce component displayName to match with component name | ✅ | 🔧 |
115116| [ no-animated-without-native-driver] ( https://github.com/bamlab/react-native-project-config/blob/main/packages/eslint-plugin/docs/rules/no-animated-without-native-driver.md ) | Disallow the use of ` Animated ` with ` useNativeDriver: false ` | ![ badge-performance] [ ] | |
Original file line number Diff line number Diff line change 1+ # Disallow importing the ` react-native-svg ` package (` @bam.tech/avoid-react-native-svg ` )
2+
3+ ⚠️ This rule _ warns_ in the ` performance ` config.
4+
5+ <!-- end auto-generated rule header -->
6+
7+ Prevents from using "react-native-svg" import to avoid performance issues.
8+
9+ ## Rule details
10+
11+ Examples of ** incorrect** code for this rule:
12+
13+ ``` jsx
14+ import Svg from " react-native-svg" ;
15+ ```
16+
17+ ``` jsx
18+ const Svg = require (" react-native-svg" );
19+ ```
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import { defineConfig } from "eslint-define-config";
33export const performanceConfig = defineConfig ( {
44 rules : {
55 "no-restricted-imports" : [
6- "warn " ,
6+ "error " ,
77 {
88 paths : [
99 {
@@ -23,16 +23,12 @@ export const performanceConfig = defineConfig({
2323 message :
2424 "Please use useFocusEffect instead of useIsFocused to avoid excessive rerenders." ,
2525 } ,
26- {
27- name : "react-native-svg" ,
28- message :
29- "Usage of react-native-svg is discouraged. Consider alternatives if applicable." ,
30- } ,
3126 ] ,
3227 } ,
3328 ] ,
3429 "@bam.tech/no-animated-without-native-driver" : "error" ,
3530 "@bam.tech/avoid-intl-number-format" : "error" ,
31+ "@bam.tech/avoid-react-native-svg" : "warn" ,
3632 } ,
3733 overrides : [
3834 {
Original file line number Diff line number Diff line change 1+ import type { Rule } from "eslint" ;
2+
3+ // Custom Rule: No react-native-svg Import
4+ export const avoidReactNativeSvgImportRule : Rule . RuleModule = {
5+ meta : {
6+ type : "problem" ,
7+ docs : {
8+ description : "Disallow importing the `react-native-svg` package." ,
9+ category : "Possible Errors" ,
10+ recommended : true ,
11+ url : "https://github.com/bamlab/react-native-project-config/tree/main/packages/eslint-plugin/docs/rules/no-react-native-svg-import.md" ,
12+ } ,
13+ messages : {
14+ noReactNativeSvgImport :
15+ "Do not import `react-native-svg`. Consider using an alternative method for SVG handling or ensure it's necessary for your use case." ,
16+ } ,
17+ schema : [ ] ,
18+ } ,
19+
20+ create ( context ) {
21+ return {
22+ ImportDeclaration ( node ) {
23+ if ( node . source . value === "react-native-svg" ) {
24+ context . report ( {
25+ node,
26+ messageId : "noReactNativeSvgImport" ,
27+ } ) ;
28+ }
29+ } ,
30+ CallExpression ( node ) {
31+ if (
32+ node . callee . type === "Identifier" &&
33+ node . callee . name === "require" &&
34+ node . arguments . length > 0 &&
35+ node . arguments [ 0 ] . type === "Literal" &&
36+ node . arguments [ 0 ] . value === "react-native-svg"
37+ ) {
38+ context . report ( {
39+ node,
40+ messageId : "noReactNativeSvgImport" ,
41+ } ) ;
42+ }
43+ } ,
44+ } ;
45+ } ,
46+ } ;
Original file line number Diff line number Diff line change 11import { avoidIntlNumberFormatRule } from "./avoid-intl-number-format" ;
2+ import { avoidReactNativeSvgImportRule } from "./avoid-react-native-svg" ;
23import { awaitUserEventRule } from "./await-user-event" ;
34import { noDifferentDisplaynameRule } from "./no-different-displayname" ;
45import { noAnimatedWithoutNativeDriverRule } from "./no-animated-without-native-driver" ;
@@ -12,4 +13,5 @@ export default {
1213 "no-different-displayname" : noDifferentDisplaynameRule ,
1314 "no-animated-without-native-driver" : noAnimatedWithoutNativeDriverRule ,
1415 "avoid-intl-number-format" : avoidIntlNumberFormatRule ,
16+ "avoid-react-native-svg" : avoidReactNativeSvgImportRule ,
1517} ;
Original file line number Diff line number Diff line change 1+ // Save without formatting: [⌘ + K] > [S]
2+
3+ // This should trigger an error breaking eslint-plugin-bam-custom-rules:
4+ // bam-custom-rules/avoid-react-native-svg
5+
6+ import { avoidReactNativeSvgImportRule } from "../../../lib/rules/avoid-react-native-svg" ;
7+ import { RuleTester } from "eslint" ;
8+
9+ const ruleTester = new RuleTester ( {
10+ parser : require . resolve ( "@typescript-eslint/parser" ) ,
11+ } ) ;
12+
13+ const valid = [ `` ] ;
14+
15+ const invalid = [
16+ `import Svg from 'react-native-svg';` ,
17+ `const Svg = require('react-native-svg');` ,
18+ ] ;
19+
20+ ruleTester . run ( "avoid-react-native-svg" , avoidReactNativeSvgImportRule , {
21+ valid,
22+ invalid : invalid . map ( ( code ) => ( {
23+ code,
24+ errors : [
25+ "Do not import `react-native-svg`. Consider using an alternative method for SVG handling or ensure it's necessary for your use case." ,
26+ ] ,
27+ } ) ) ,
28+ } ) ;
You can’t perform that action at this time.
0 commit comments