Rules and Conventions
In order to enforce a consistent code style and avoid common issues in the codebase, we have a set of rules and conventions that we follow and enforce through the starter.
Typescript
Section titled “Typescript”This starter uses TypeScript to provide type safety and avoid common bugs in the codebase. The project configuration is based on Expo config with some updates to support absolute imports.
If you are not familiar with Typescript, you can check this article to learn more about it : Typescript for React Developers
You can find more information about it here.
Naming
Section titled “Naming”We follow kabab-case for naming files and folders as we think it’s the most readable and consistent way to name files and folders in large projects and it’s the most common way to name files and folders in the react native community.
Example of kabab-case naming: my-component.tsx
For naming variables, functions, classes, interfaces, and enums, we follow camelCase as it’s the most common way to name variables in the React community. It is enforced by the linter, as you cannot create a function component without using camelCase.
Linting
Section titled “Linting”Using a linter is a must in any JavaScript project. For starters, we are using ESLint with the React Native community config and some custom rules to ensure that we are following the rules and conventions related to file naming, Tailwind CSS classes, TypeScript types, import order, internationalization files, and more.
Here is the complete ESLint configuration file:
import path from 'node:path';import { fileURLToPath } from 'node:url';
import { defineConfig, globalIgnores } from 'eslint/config';import expoConfig from 'eslint-config-expo/flat.js';import i18nJsonPlugin from 'eslint-plugin-i18n-json';import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';import reactCompiler from 'eslint-plugin-react-compiler';import simpleImportSort from 'eslint-plugin-simple-import-sort';import testingLibrary from 'eslint-plugin-testing-library';// eslint-disable-next-line import/no-named-as-default, import/no-named-as-default-member, import/namespaceimport eslintPluginUnicorn from 'eslint-plugin-unicorn';import unusedImports from 'eslint-plugin-unused-imports';import { configs, parser } from 'typescript-eslint';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export default defineConfig([ globalIgnores([ 'dist/*', 'node_modules', '__tests__/', 'coverage', '.expo', '.expo-shared', 'android', 'ios', '.vscode', 'docs/', 'cli/', 'expo-env.d.ts', ]), expoConfig, eslintPluginPrettierRecommended, reactCompiler.configs.recommended, { plugins: { 'simple-import-sort': simpleImportSort, unicorn: eslintPluginUnicorn, 'unused-imports': unusedImports, }, rules: { 'max-params': ['error', 3], 'max-lines-per-function': ['error', 70], 'react/display-name': 'off', 'react/no-inline-styles': 'off', 'react/destructuring-assignment': 'off', 'react/require-default-props': 'off', 'unicorn/filename-case': [ 'error', { case: 'kebabCase', ignore: ['/android', '/ios'], }, ], 'simple-import-sort/imports': 'error', 'simple-import-sort/exports': 'error', 'unused-imports/no-unused-imports': 'error', 'unused-imports/no-unused-vars': [ 'error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_', caughtErrorsIgnorePattern: '^_', }, ], 'import/prefer-default-export': 'off', 'import/no-cycle': ['error', { maxDepth: '∞' }], 'prettier/prettier': ['error', { ignores: ['expo-env.d.ts'] }], }, }, { files: ['**/*.ts', '**/*.tsx'], languageOptions: { parser: parser, parserOptions: { project: './tsconfig.json', sourceType: 'module', }, }, rules: { ...configs.recommended.rules, '@typescript-eslint/comma-dangle': 'off', '@typescript-eslint/consistent-type-imports': [ 'warn', { prefer: 'type-imports', fixStyle: 'inline-type-imports', disallowTypeAnnotations: true, }, ], }, }, { files: ['src/translations/*.json'], plugins: { 'i18n-json': i18nJsonPlugin }, processor: { meta: { name: '.json' }, ...i18nJsonPlugin.processors['.json'], }, rules: { ...i18nJsonPlugin.configs.recommended.rules, 'i18n-json/valid-message-syntax': [ 2, { syntax: path.resolve( __dirname, './scripts/i18next-syntax-validation.js' ), }, ], 'i18n-json/valid-json': 2, 'i18n-json/sorted-keys': [ 2, { order: 'asc', indentSpaces: 2, }, ], 'i18n-json/identical-keys': [ 2, { filePath: path.resolve(__dirname, './src/translations/en.json'), }, ], 'prettier/prettier': [ 0, { singleQuote: true, endOfLine: 'auto', }, ], }, }, { files: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'], plugins: { 'testing-library': testingLibrary }, rules: { ...testingLibrary.configs.react.rules, }, },]);Git Hooks with Husky
Section titled “Git Hooks with Husky”The starter comes with a set of git hooks that help us to enforce rules and conventions. Those hooks are configured using Husky. and here is the complete list of the hooks:
pre-commit
Section titled “pre-commit”As the name suggest, this hook will run before each commit and it will make sure you are not committing directly on the main branch and it will run the linter and typescript checking on the staged files.
. "$(dirname "$0")/common.sh"
echo "===\n>> Checking branch name..."
# Check if branch protection is enabledif [[ -z $SKIP_BRANCH_PROTECTION ]]; then BRANCH=$(git rev-parse --abbrev-ref HEAD) PROTECTED_BRANCHES="^(main|master)"
if [[ $BRANCH =~ $PROTECTED_BRANCHES ]]; then echo ">> Direct commits to the $BRANCH branch are not allowed. Please choose a new branch name." exit 1 fielse echo ">> Skipping branch protection."fi
echo ">> Finish checking branch name"echo ">> Linting your files and fixing them if needed..."
pnpm type-checkpnpm lint-stagedpost-merge
Section titled “post-merge”As the name suggest, this hook will run after each merge and it will check if there is any changed in pnpm-lock.yaml and if there is any, it will run pnpm install to make sure the dependencies are up to date.
function changed { git diff --name-only HEAD@{1} HEAD | grep "^$1" >/dev/null 2>&1}
echo 'Checking for changes in pnpm-lock.yml...'
if changed 'pnpm-lock.yml'; then echo "📦 pnpm-lock.yml changed. Run pnpm install to bring your dependencies up to date." pnpm installfi
echo 'You are up to date :)'
echo 'If necessary, you can run pnpm prebuild to generate native code.'
exit 0commit-msg
Section titled “commit-msg”This hook will check if the commit message is following the conventional commit format. If it’s not, the commit will be aborted and will show you what going wrong with your commit message.
pnpm commitlint --edit $1We are using commitlint to check if the commit message is following the conventional commit format.
In general, your commit message should follow this format:
type(scope?): subject #scope is optional; multiple scopes are supported (current delimiter options: "/", "\" and ",")Real world examples can look like this:
fix(ui): fix input widthfeat(ui): add button variantsfeat(api): add usePost query hooktype should be one of the following: build, chore, ci ,docs,feat,fix, perf, refactor, revert, style or test.