eslint-plugin-react-native

repository·master·Indexed 20 days ago

https://github.com/intellicode/eslint-plugin-react-native

An ESLint plugin providing React Native specific linting rules focusing on styles, platform-specific components, and common pitfalls. Version 5.0.0 includes rules such as no-unused-styles, sort-styles, split-platform-components, no-inline-styles, no-color-literals, no-raw-text, and no-single-element-style-arrays.

Tokens
6.4K
Snippets
25
Records
28
Agent score
70%

What's inside eslint-plugin-react-native

  1. Install eslint-plugin-react-native

    master

    To use this plugin, you must install eslint and eslint-plugin-react-native. It is highly recommended to also install eslint-plugin-react for full functionality.

    If you are installing ESLint globally, you must also install the plugins globally. Otherwise, install them locally as dev dependencies.

    $ npm install --save-dev eslint
    $ npm install --save-dev eslint-plugin-react
    $ npm install --save-dev eslint-plugin-react-native
  2. Use the `split-platform-components` rule to enforce platform-specific filenames

    master

    The react-native/split-platform-components rule ensures that when you use platform-specific components (like ProgressBarAndroid or ActivityIndicatorIOS), you use the correct platform-specific filename extensions (e.g., .android.js or .ios.js). This prevents platform-specific code from being included in the wrong platform's bundle.

    Warning Patterns

    • Using a platform-specific component in a generic filename (e.g., Hello.js).
    • Using both Android and iOS specific components in the same generic file.

    Correct Patterns

    • Use .ios.js for files containing iOS-only components (e.g., Hello.ios.js).
    • Use .android.js for files containing Android-only components (e.g., Hello.android.js).
    // Warning: Generic filename with platform-specific component
    // filename: Hello.js
    import { ActivityIndicatorIOS } from 'react-native';
    
    // Correct: Platform-specific filename
    // filename: Hello.ios.js
    import { ActivityIndicatorIOS } from 'react-native';
  3. Use the no-raw-text rule to prevent un-wrapped strings

    master

    The no-raw-text rule ensures that all strings in React Native are wrapped within a <Text> component. In React Native, rendering raw text directly inside a <View> or other non-text components will cause a runtime error. This rule detects patterns where text is placed directly in children of components that are not <Text> components.

    // ❌ Incorrect: This will trigger a warning
    <View>some text</View>
    
    const text = 'some text';
    <View>{`${text}`}</View>
    
    // ✅ Correct: Text is wrapped in a Text component
    <View><Text>some text</Text></View>
    
    const text = 'some text';
    <View><Text>{`${text}`}</Text></View>
  4. Configure eslint-plugin-react-native

    master

    To enable the plugin, add react-native to your ESLint plugins array. You must also ensure your parser is configured to support JSX.

    To whitelist browser-like globals common in React Native, add react-native/react-native to your env configuration.

    {
      "plugins": ["react", "react-native"],
      "parserOptions": {
        "ecmaFeatures": {
          "jsx": true
        }
      },
      "env": {
        "react-native/react-native": true
      }
    }
  5. Configure no-raw-text options

    master

    The no-raw-text rule accepts an options object to customize its behavior:

    • skip: An array of custom component names that the rule should ignore when checking for raw text. Use this if you have custom components that are designed to handle text children safely.
    // Example configuration to skip specific components
    {
      "rules": {
        "react-native/no-raw-text": ["error", { "skip": ["MyCustomTextContainer"] }]
      }
    }
  6. Configure `split-platform-components` rule options

    master

    You can customize the regex patterns used to identify Android and iOS platform files via the androidPathRegex and iosPathRegex options. This is useful if you use custom file extensions or non-standard naming conventions.

    Option Schema

    "react-native/split-platform-components": [ <enabled>, { 
      androidPathRegex: <string>, 
      iosPathRegex: <string> 
    }]
  7. Configure stylesheet providers and rules

    master

    You can customize which stylesheet providers the plugin recognizes using the react-native/style-sheet-object-names setting.

    Individual rules can be enabled or disabled in the rules section. Common rules include no-unused-styles, split-platform-components, no-inline-styles, no-color-literals, no-raw-text, and no-single-element-style-arrays.

    {
      "settings": {
        "react-native/style-sheet-object-names": ["EStyleSheet", "OtherStyleSheet", "PStyleSheet"]
      },
      "rules": {
        "react-native/no-unused-styles": 2,
        "react-native/split-platform-components": 2,
        "react-native/no-inline-styles": 2,
        "react-native/no-color-literals": 2,
        "react-native/no-raw-text": 2,
        "react-native/no-single-element-style-arrays": 2
      }
    }
  8. Use ignoreStyleProperties to skip sorting style object properties

    master

    If you want to enforce sorting of the top-level class names in a StyleSheet but do not care about the order of properties within those styles, set ignoreStyleProperties to true.

    /* eslint react-native/sort-styles: ["error", "asc", { "ignoreStyleProperties": true }] */
    
    // This is NOT a warning because properties (width, color) are not enforced to be sorted,
    // even though the class names (anchor, button) are correctly sorted in 'asc' mode.
    const styles = StyleSheet.create({
      anchor: {},
      button: {
        width: 100,
        color: 'green',
      },
    });
  9. Use ignoreClassNames to skip sorting top-level StyleSheet keys

    master

    If you want to enforce sorting of properties inside style objects but do not care about the order of the class names themselves, set ignoreClassNames to true.

    /* eslint react-native/sort-styles: ["error", "asc", { "ignoreClassNames": true }] */
    
    // This is NOT a warning because class names (button, anchor) are not enforced to be sorted,
    // even though the rule is in 'asc' mode.
    const styles = StyleSheet.create({
      button: {
        color: 'green',
        width: 100,
      },
      anchor: {},
    });
  10. Enforce sorted StyleSheet keys with react-native/sort-styles

    master

    The react-native/sort-styles rule enforces alphabetical ordering of keys within StyleSheet.create definitions. This helps maintain readability and consistency in React Native style objects. By default, it enforces ascending order for both the class names (the top-level keys in the StyleSheet) and the style properties (the keys within each style object).

    // Example of a violation (unsorted keys in ascending mode):
    const styles = StyleSheet.create({
      button: {
        width: 100,
        color: 'green',
      },
    });
    
    // Example of valid code (sorted keys in ascending mode):
    const styles = StyleSheet.create({
      button: {
        color: 'green',
        width: 100,
      },
    });