Enforce React Native Architecture with ESLint
Summary
Pixel shares Part 3 of Building Bareship, showing how ESLint enforces five-layer import rules and prevents features from depending on each other. Dependency-cruiser rechecks the graph in CI, while additional rules catch untranslated text, non-RTL-safe styles, and React Compiler cache issues. Clear error messages help developers fix violations quickly.
DEV Community
Code review shouldn't catch import rules: a React Native architecture ESLint enforces
Short answer: Bareship writes its architecture down as lint errors instead of a wiki page. ESLint checks that imports go one way through five layers and that features never import each other, and dependency-cruiser checks the whole graph again in CI. Other rules each came from a real bug: hard-coded text, left/right styles that don't mirror in right-to-left languages, and calls the React Compiler caches. Every error message says how to fix it and links the decision behind it, so a teammate or an AI agent can fix the code without asking anyone. This is Part 3 of Building Bareship. Part 2 covered the offline queue and ended with a promise: an architecture the linter enforces. Every React Native project starts with folders and good intentions. Someone draws the layers on a whiteboard, everyone agrees, and it holds for a few weeks. Then a settings screen borrows a hook from the feed because it's quicker, a string skips translation because it's "temporary", and the person who knew the rules is on leave. Code review is supposed to catch this, but reviewers are looking at whether the feature works, not at import paths. So in Bareship the rules are errors. They show up in the editor while you type, block the commit, and fail CI. One file, three errors Here's a small screen I wrote to break the rules on purpose. It sits in the settings feature, shows how many pages of posts the feed has loaded, and adds a little margin: src/features/settings/screens/ProfileCard.tsx import { View } from 'react-native';
import { StyleSheet } from 'react-native-unistyles';
import { useFeed } from '@/features/feed/api';
import { Text } from '@/shared/ui/primitives/Text';
export function ProfileCard() {
const feed = useFeed();
return (
Your posts: {feed.data?.pages.length}
);
}
const styles = StyleSheet.create({ card: { marginLeft: 16 } });
It type-checks and it runs. ESLint says no, three times: src/features/settings/screens/ProfileCard.tsx
4:25 error Layer rule broken. Imports go app → features → entities → data → shared,
and features never import each other (docs/decisions/0005) boundaries/dependencies
11:13 error disallow literal string: Your posts: {feed.data?.pages.length}
i18next/no-literal-string
16:44 error Use start/end (marginStart, paddingEnd, start, borderStartWidth…) so the layout
mirrors in right-to-left languages (ADR 0017) no-restricted-syntax
✖ 3 problems (3 errors, 0 warnings)
And dependency-cruiser, which CI runs over the whole import graph, catches the first one again: error features-are-isolated: src/features/settings/screens/ProfileCard.tsx → src/features/feed/api.ts
x 1 dependency violations (1 errors, 0 warnings). 226 modules, 722 dependencies cruised.
None of these would have broken a test. Each one costs real time later: the settings feature now breaks when the feed changes, the text can't be translated, and the margin is on the wrong side in Arabic or Urdu. Layers, and features that don't know each other The code in src/ is split into five layers, and imports only go down: Layer Holds May import app Navigation, boot, providers everything below features One folder per feature: screens, hooks, API calls entities, data, shared, and only itself entities Domain models more than one feature needs, such as the session and the user data, shared data The query client, the offline queue, storage shared shared UI primitives, the HTTP client, i18n, config shared The rule that matters most is the one in the middle row: a feature never imports another feature. If the settings screen needs the feed's data, that's a sign the shared part belongs lower down, in entities or shared. Then you can delete or rewrite the feed without touching settings, which is also how pnpm strip-examples can remove the whole demo with ...
Powered by Readybot.io