Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[eslint-config-expo] eslint config for expo projects #27915

Merged
merged 17 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/eslint-config-expo/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
root: true,
extends: ['./default.js', 'prettier'],
plugins: ['prettier'],
env: {
node: true,
},
rules: {
'prettier/prettier': 'warn',
},
};
12 changes: 12 additions & 0 deletions packages/eslint-config-expo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

## Unpublished

### 🛠 Breaking changes
Create a minimal ESLint config for Expo projects.

### 🎉 New features

### 🐛 Bug fixes

### 💡 Others
21 changes: 21 additions & 0 deletions packages/eslint-config-expo/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016-present Expo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions packages/eslint-config-expo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# eslint-config-expo

Base ESLint config for Expo projects. This is a minimal config that supports JSX and TypeScript, platform-specific global variables, and file extensions like `.android.js`, `.ios.js` and `.web.js`. You are intended to compose this base config with the linter rules of your choice in your own ESLint configuration.

## Installation

```sh
yarn add --dev eslint-config-expo
```

You will also need to install `eslint`:

```sh
yarn add --dev eslint
```

## Usage

Import this config into your own ESLint [configuration](https://eslint.org/docs/latest/use/configure/configuration-files) using the `extends` option. ESLint checks both `package.json` and `.eslintrc.*` files for its configuration:

### package.json
```js
{
"eslintConfig": {
"extends": ["expo", "eslint:recommended"]
}
}
```

### .eslintrc.js
```js
module.exports = {
extends: ["expo", "eslint:recommended"],
};
```
45 changes: 45 additions & 0 deletions packages/eslint-config-expo/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const {
jsExtensions,
tsExtensions,
platformSubextensions,
computeExpoExtensions,
} = require('./utils/extensions');

const allExtensions = computeExpoExtensions(
[...jsExtensions, ...tsExtensions],
platformSubextensions
);

module.exports = {
extends: ['./utils/core.js', './utils/typescript.js', './utils/react.js', './utils/expo.js'],
globals: {
__DEV__: 'readonly',
ErrorUtils: false,
FormData: false,
XMLHttpRequest: false,
alert: false,
cancelAnimationFrame: false,
cancelIdleCallback: false,
clearImmediate: false,
fetch: false,
navigator: false,
process: false,
requestAnimationFrame: false,
requestIdleCallback: false,
setImmediate: false,
window: false,
'shared-node-browser': true,
},
settings: {
'import/extensions': allExtensions,
'import/resolver': {
node: { extensions: allExtensions },
},
},
overrides: [
{
files: ['*.web.*'],
env: { browser: true },
},
],
};
46 changes: 46 additions & 0 deletions packages/eslint-config-expo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "eslint-config-expo",
"version": "7.0.0",
"description": "ESLint config for Expo apps",
"main": "default.js",
"scripts": {
"lint": "eslint ."
},
"repository": {
"type": "git",
"url": "git+https://github.com/expo/expo.git",
"directory": "packages/eslint-config-expo"
},
"keywords": [
"eslint-config",
"expo",
"react-native"
],
"files": [
"README.md",
"default.js",
"utils"
],
"author": "Expo",
"license": "MIT",
"bugs": {
"url": "https://github.com/expo/expo/issues"
},
"homepage": "https://github.com/expo/expo/tree/main/packages/eslint-config-expo",
"dependencies": {
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"eslint-plugin-expo": "^0.0.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-react": "^7.34.0",
"eslint-plugin-react-hooks": "^4.6.0"
},
"devDependencies": {
"eslint": "^8.57.0",
"prettier": "^3.2.5",
"eslint-plugin-prettier": "^5.1.3"
},
"peerDependencies": {
"eslint": ">=8.10"
}
}
71 changes: 71 additions & 0 deletions packages/eslint-config-expo/utils/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { jsExtensions } = require('./extensions');

module.exports = {
parserOptions: {
sourceType: 'module',
ecmaVersion: 2022,
ecmaFeatures: { impliedStrict: true, jsx: true },
},
env: { es2022: true },
globals: {
console: 'readonly',
exports: false,
global: false,
module: false,
require: false,
},
extends: ['plugin:import/errors'],
plugins: ['import', 'expo'],
rules: {
eqeqeq: ['warn', 'smart'],
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'no-dupe-args': 'error',
'no-dupe-class-members': 'error',
'no-dupe-keys': 'error',
'no-duplicate-case': 'error',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'no-empty-character-class': 'warn',
'no-empty-pattern': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'no-extend-native': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'no-extra-bind': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'no-redeclare': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'no-undef': 'error',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'no-unreachable': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'no-unsafe-negation': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'no-unused-expressions': ['warn', { allowShortCircuit: true, enforceForJSX: true }],
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'no-unused-labels': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'no-unused-vars': [
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'warn',
{
vars: 'all',
args: 'none',
ignoreRestSiblings: true,
caughtErrors: 'all',
caughtErrorsIgnorePattern: '^_',
},
],
'no-with': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'unicode-bom': ['warn', 'never'],
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'use-isnan': 'error',
'valid-typeof': 'error',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'import/first': 'warn', //keep
'import/default': 'off',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
},
settings: {
'import/extensions': jsExtensions,
'import/ignore': [
// react-native's main module is Flow, not JavaScript, and raises parse errors. Additionally,
// several other react-native-related packages still publish Flow code as their main source.
'node_modules[\\\\/]+@?react-native',
],
'import/resolver': {
node: { extensions: jsExtensions },
},
},
overrides: [
{
files: ['*.d.ts'],
rules: {
'import/order': 'off',
},
},
],
};
7 changes: 7 additions & 0 deletions packages/eslint-config-expo/utils/expo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
plugins: ['expo'],
rules: {
'expo/no-env-var-destructuring': ['error'],
'expo/no-dynamic-env-var': ['error'],
},
};
21 changes: 21 additions & 0 deletions packages/eslint-config-expo/utils/extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const jsExtensions = ['.js', '.jsx'];
const tsExtensions = ['.ts', '.tsx', '.d.ts'];

const platformSubextensions = ['.android', '.ios', '.web', '.native'];

function computeExpoExtensions(baseExtensions, platformSubextensions) {
const expoExtensions = [];
for (const platform of [...platformSubextensions, '']) {
for (const base of baseExtensions) {
expoExtensions.push(`${platform}${base}`);
}
}
return expoExtensions;
}

module.exports = {
jsExtensions,
tsExtensions,
platformSubextensions,
computeExpoExtensions,
};
18 changes: 18 additions & 0 deletions packages/eslint-config-expo/utils/react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
parserOptions: { ecmaFeatures: { jsx: true } },
extends: ['plugin:react/recommended', 'plugin:react-hooks/recommended'],
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
rules: {
'react/jsx-no-duplicate-props': 'error',
'react/jsx-no-undef': 'error',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'react/jsx-uses-react': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'react/jsx-uses-vars': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'react/no-direct-mutation-state': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'react/no-this-in-sfc': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'react/no-unknown-property': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'react/require-render-return': 'warn',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
'react/react-in-jsx-scope': 'off',
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
},
settings: {
react: { version: 'detect' },
},
};
92 changes: 92 additions & 0 deletions packages/eslint-config-expo/utils/typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const { jsExtensions, tsExtensions } = require('./extensions');

const allExtensions = [...jsExtensions, ...tsExtensions];

module.exports = {
overrides: [
{
files: ['*.js', '*.jsx'],
settings: {
'import/parsers': {
'@typescript-eslint/parser': tsExtensions,
},
},
},
{
files: ['*.ts', '*.tsx', '*.d.ts'],
extends: ['plugin:import/typescript'],
parser: '@typescript-eslint/parser',
parserOptions: {
// eslint-plugin-react@7.32.2 accesses superTypeParameters, which is deprecated by
kadikraman marked this conversation as resolved.
Show resolved Hide resolved
// typescript-estree and prints a warning by default
suppressDeprecatedPropertyWarnings: true,
},
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/array-type': ['warn', { default: 'array' }],
'@typescript-eslint/ban-types': [
'error',
{
types: {
Number: {
message: 'Use `number` instead.',
fixWith: 'number',
},
Boolean: {
message: 'Use `boolean` instead.',
fixWith: 'boolean',
},
Symbol: {
message: 'Use `symbol` instead.',
fixWith: 'symbol',
},
Object: {
message: 'Use `object` instead.',
fixWith: 'object',
},
String: {
message: 'Use `string` instead.',
fixWith: 'string',
},
},
extendDefaults: false,
},
],
'@typescript-eslint/consistent-type-assertions': [
'warn',
{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allow' },
],
'@typescript-eslint/no-extra-non-null-assertion': 'warn',

// Overrides
'no-dupe-class-members': 'off',
'@typescript-eslint/no-dupe-class-members': 'error',

'no-redeclare': 'off',
'@typescript-eslint/no-redeclare': 'warn',

'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': [
'warn',
{ vars: 'all', args: 'none', ignoreRestSiblings: true, caughtErrors: 'all' },
],

'no-useless-constructor': 'off',
'@typescript-eslint/no-useless-constructor': 'warn',

// The typescript-eslint FAQ recommends turning off "no-undef" in favor of letting tsc check for
// undefined variables, including types
'no-undef': 'off',
},
settings: {
'import/extensions': allExtensions,
'import/parsers': {
'@typescript-eslint/parser': tsExtensions,
},
'import/resolver': {
node: { extensions: allExtensions },
},
},
},
],
};
Loading
Loading