ESLint plugin providing custom rules for React Native development including platform-colors and no-deep-imports rules
npx @tessl/cli install tessl/npm-react-native--eslint-plugin@0.81.0@react-native/eslint-plugin is an ESLint plugin that provides custom rules specifically designed for React Native development. It includes two main rules: platform-colors for enforcing statically analyzable PlatformColor/DynamicColorIOS calls, and no-deep-imports for preventing deep imports from React Native internal modules.
npm install --save-dev @react-native/eslint-plugin// ESLint plugin structure
const plugin = require("@react-native/eslint-plugin");
// plugin.rules contains: { "platform-colors": ..., "no-deep-imports": ... }
// Direct rule access
const platformColorsRule = require("@react-native/eslint-plugin/platform-colors");
const noDeepImportsRule = require("@react-native/eslint-plugin/no-deep-imports");
// Utility mapping for deep imports fix
const { publicAPIMapping } = require("@react-native/eslint-plugin/utils");For ESLint configuration (.eslintrc.json):
{
"plugins": ["@react-native"]
}This plugin is designed to be used as part of @react-native/eslint-config, but can be configured independently:
{
"plugins": ["@react-native"],
"rules": {
"@react-native/platform-colors": "error",
"@react-native/no-deep-imports": "error"
}
}Accessing the plugin programmatically:
const plugin = require("@react-native/eslint-plugin");
// Get available rules
console.log(Object.keys(plugin.rules)); // ["platform-colors", "no-deep-imports"]
// Access public API mapping for programmatic use
const { publicAPIMapping } = require("@react-native/eslint-plugin/utils");
console.log(publicAPIMapping["Libraries/Components/Button"]);
// { default: "Button", types: ["ButtonProps"] }The main plugin export containing all ESLint rule definitions.
/**
* Main plugin export with ESLint rules
*/
exports.rules = {
"platform-colors": require('./platform-colors'),
"no-deep-imports": require('./no-deep-imports')
};Enforces that calls to PlatformColor() and DynamicColorIOS() are statically analyzable to enable performance optimizations.
/**
* ESLint rule for validating PlatformColor and DynamicColorIOS calls
*/
const platformColorsRule = {
meta: {
type: "problem",
docs: {
description: "Ensure that PlatformColor() and DynamicColorIOS() are passed literals of the expected shape."
},
messages: {
platformColorArgsLength: "PlatformColor() must have at least one argument that is a literal.",
platformColorArgTypes: "PlatformColor() every argument must be a literal.",
dynamicColorIOSArg: "DynamicColorIOS() must take a single argument of type Object",
dynamicColorIOSValue: "DynamicColorIOS() value must be either a literal or a PlatformColor() call."
},
schema: []
},
create: function(context) {
return {
CallExpression: function(node) {
// Rule implementation
}
};
}
};Rule Behavior:
PlatformColor() receives at least one literal argumentPlatformColor() arguments are literals (not variables or expressions)DynamicColorIOS() receives exactly one object expression argumentDynamicColorIOS() object properties are either literals or PlatformColor() callsUsage Example:
// ✅ Valid - static literals
const color1 = PlatformColor("systemBlue", "blue");
const color2 = DynamicColorIOS({
light: "white",
dark: PlatformColor("systemBlack")
});
// ❌ Invalid - dynamic values
const colorName = "systemBlue";
const color3 = PlatformColor(colorName); // Error: arguments must be literalsPrevents deep imports from React Native internal modules and provides automatic fixes to use top-level imports instead.
/**
* ESLint rule for preventing deep imports from React Native
*/
const noDeepImportsRule = {
meta: {
type: "problem",
docs: {
description: "Disallow deep imports from react native"
},
messages: {
deepImport: "'{{importPath}}' React Native deep imports are deprecated. Please use the top level import instead."
},
schema: [],
fixable: "code"
},
create: function(context) {
return {
ImportDeclaration: function(node) {
// Handle ES6 imports
},
CallExpression: function(node) {
// Handle CommonJS require() calls
}
};
}
};Rule Behavior:
react-native/* pathsreact-native/Libraries/Core/InitializeCoreUsage Example:
// ❌ Invalid - deep import
import Button from "react-native/Libraries/Components/Button";
// ✅ Valid - top-level import (auto-fixed)
import { Button } from "react-native";
// ❌ Invalid - deep require
const Button = require("react-native/Libraries/Components/Button");
// ✅ Valid - top-level require (auto-fixed)
const { Button } = require("react-native");Utility object that maps React Native internal module paths to their public API equivalents.
/**
* Mapping of React Native internal paths to public API equivalents
* @type {Object.<string, {default: string|null, types: string[]|null}>}
*/
const publicAPIMapping = {
// Internal path as key, mapping info as value
"Libraries/Components/Button": {
default: "Button", // Public default export name
types: ["ButtonProps"] // Available type names for TypeScript
}
// ... 100+ more mappings
};
module.exports = {
publicAPIMapping
};Mapping Structure:
"Libraries/Components/Button")default (export name) and types (TypeScript types) propertiesExample Mappings:
const publicAPIMapping = {
"Libraries/Components/Button": {
default: "Button",
types: ["ButtonProps"]
},
"Libraries/Components/ActivityIndicator/ActivityIndicator": {
default: "ActivityIndicator",
types: ["ActivityIndicatorProps"]
},
"Libraries/Utilities/Platform": {
default: "Platform",
types: null
}
// ... 100+ more mappings
};/**
* ESLint rule metadata structure
* @typedef {Object} RuleMeta
* @property {"problem"|"suggestion"|"layout"} type - Rule type
* @property {Object} docs - Documentation object
* @property {string} docs.description - Rule description
* @property {Object.<string, string>} messages - Error message templates
* @property {Array} schema - JSON schema for rule options
* @property {"code"|"whitespace"} [fixable] - Whether rule provides fixes
*/
/**
* ESLint rule context object
* @typedef {Object} RuleContext
* @property {function} report - Report rule violations
*/
/**
* ESLint rule definition
* @typedef {Object} ESLintRule
* @property {RuleMeta} meta - Rule metadata
* @property {function} create - Creates rule visitor functions
*/
/**
* Public API mapping entry
* @typedef {Object.<string, {default: string|null, types: string[]|null}>} PublicAPIMapping
*/
/**
* Plugin export structure
* @typedef {Object} ESLintPlugin
* @property {Object.<string, ESLintRule>} rules - Available rules
*/