Skip to content

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

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

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

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:

.eslintrc.js
const path = require('path');
module.exports = {
// Configuration for JavaScript files
extends: [
'expo',
'plugin:tailwindcss/recommended',
'prettier',
'eslint:recommended'
],
env: {
'jest/globals': true,
'node': true
},
plugins: [
'unicorn',
'@typescript-eslint',
'unused-imports',
'tailwindcss',
'simple-import-sort',
'sonarjs',
'jest'
],
parserOptions: {
project: './tsconfig.json',
},
rules: {
'import/no-duplicates': 'error',
'@typescript-eslint/no-explicit-any': 'error',
'unicorn/filename-case': [
'error',
{
case: 'kebabCase',
ignore: ['/android', '/ios'],
},
],
'max-params': ['error', 3], // Limit the number of parameters in a function to use object instead
'max-lines-per-function': ['error', 70],
'react/display-name': 'off',
'react/no-inline-styles': 'off',
'react/destructuring-assignment': 'off', // Vscode doesn't support automatically destructuring, it's a pain to add a new variable
'react/require-default-props': 'off', // Allow non-defined react props as undefined
'@typescript-eslint/comma-dangle': 'off', // Avoid conflict rule between Eslint and Prettier
'@typescript-eslint/consistent-type-imports': [
'warn',
{
prefer: 'type-imports',
fixStyle: 'inline-type-imports',
disallowTypeAnnotations: true,
},
], // Ensure `import type` is used when it's necessary
'import/prefer-default-export': 'off', // Named export is easier to refactor automatically
'import/no-cycle': ['error', { maxDepth: '∞' }],
'tailwindcss/classnames-order': [
'warn',
{
officialSorting: true,
},
], // Follow the same ordering as the official plugin `prettier-plugin-tailwindcss`
'simple-import-sort/imports': 'error', // Import configuration for `eslint-plugin-simple-import-sort`
'simple-import-sort/exports': 'error', // Export configuration for `eslint-plugin-simple-import-sort`
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/array-type': 'off',
'tailwindcss/no-custom-classname': 'off',
'unused-imports/no-unused-imports': 'error',
'unused-imports/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
}
],
curly: [2, 'all'],
'prefer-const': [
'error',
{
destructuring: 'any',
},
],
'object-shorthand': 'error',
'arrow-body-style': ["error", "as-needed"],
'no-console': ['error', {allow: ['error']}],
'guard-for-in': 'error'
},
overrides: [
// Configuration for translations files (i18next)
{
files: ['src/translations/*.json'],
extends: ['plugin:i18n-json/recommended'],
rules: {
'i18n-json/valid-message-syntax': [
2,
{
syntax: path.resolve('./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('./src/translations/en.json'),
},
],
'prettier/prettier': [
0,
{
singleQuote: true,
endOfLine: 'auto',
},
],
curly: [2, 'all'],
},
},
{
// Configuration for testing files
files: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
extends: ['plugin:testing-library/react'],
},
],
};

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

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.

.husky/pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
. "$(dirname "$0")/common.sh"
echo "===\n>> Checking branch name..."
# Check if branch protection is enabled
if [[ -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
fi
else
echo ">> Skipping branch protection."
fi
echo ">> Finish checking branch name"
echo ">> Linting your files and fixing them if needed..."
pnpm type-check
pnpm lint-staged

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.

.husky/post-merge
#!/usr/bin/env bash
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 install
fi
echo 'You are up to date :)'
echo 'If necessary, you can run pnpm prebuild to generate native code.'
exit 0

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.

.husky/commit-msg
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
pnpm commitlint --edit $1

We are using commitlint to check if the commit message is following the conventional commit format.

In general, your commit message should follow this format:

Terminal window
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 width
feat(ui): add button variants
feat(api): add usePost query hook

type should be one of the following: build, chore, ci ,docs,feat,fix, perf, refactor, revert, style or test.

πŸ”— Resources