feat: @rnx-kit/eslint-plugin recommended ESLint rules for React devs (#777)

Extends:
- `eslint:recommended`
- `plugin:@typescript-eslint/recommended`
- `plugin:react/recommended`

And adds a rule for disallowing `export *`.
This commit is contained in:
Tommy Nguyen 2021-10-29 10:43:46 +02:00 коммит произвёл GitHub
Родитель eb6c6f3f9c
Коммит 04ebb242c7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 228 добавлений и 35 удалений

Просмотреть файл

@ -123,6 +123,7 @@ individually, as features are added and fixes are made.
| [@rnx-kit/console](https://github.com/microsoft/rnx-kit/tree/main/packages/console) | Simple console logger |
| [@rnx-kit/dep-check](https://github.com/microsoft/rnx-kit/tree/main/packages/dep-check) | Dependency checker for React Native apps |
| [@rnx-kit/esbuild-plugin-import-path-remapper](https://github.com/microsoft/rnx-kit/tree/main/packages/esbuild-plugin-import-path-remapper) | Esbuild plugin for remapping 'lib/' imports to 'src/' |
| [@rnx-kit/eslint-plugin](https://github.com/microsoft/rnx-kit/tree/main/packages/eslint-plugin) | A set of ESLint rules for React developers |
| [@rnx-kit/golang](https://github.com/microsoft/rnx-kit/tree/main/packages/golang) | Integrate Go into your monorepo and create native apps that accelerate development and CI builds |
| [@rnx-kit/jest-preset](https://github.com/microsoft/rnx-kit/tree/main/packages/jest-preset) | Jest preset for React Native code bases |
| [@rnx-kit/metro-config](https://github.com/microsoft/rnx-kit/tree/main/packages/metro-config) | Metro config for monorepos |

Просмотреть файл

@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "@rnx-kit/eslint-plugin recommended ESLint rules for React devs",
"packageName": "@rnx-kit/eslint-plugin",
"email": "4123478+tido64@users.noreply.github.com",
"dependentChangeType": "patch"
}

Просмотреть файл

@ -1,30 +1,3 @@
module.exports = {
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: "module",
},
plugins: ["@typescript-eslint"],
rules: {
"@typescript-eslint/no-unused-vars": "off", // too many false-positives with optional chaining
"@typescript-eslint/no-var-requires": "off",
"react/prop-types": "off",
},
settings: {
react: {
version: "detect",
},
},
extends: ["plugin:@rnx-kit/recommended"],
};

Просмотреть файл

@ -18,11 +18,7 @@
"format": "rnx-kit-scripts format"
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.0",
"eslint-plugin-jest": "^25.0.0",
"eslint-plugin-react": "^7.26.0",
"typescript": "^4.0.0"
"@rnx-kit/eslint-plugin": "^0.0.1",
"eslint": "^8.0.0"
}
}

Просмотреть файл

@ -0,0 +1,29 @@
# @rnx-kit/eslint-plugin
`@rnx-kit/eslint-plugin` is a set of rules that can be extended in your own
shareable ESLint config.
For more details on shareable configs, see
https://eslint.org/docs/developer-guide/shareable-configs.
## Install
```
yarn add @rnx-kit/eslint-plugin --dev
```
or if you're using npm:
```
npm add --save-dev @rnx-kit/eslint-plugin
```
## Usage
Add `@rnx-kit/eslint-plugin` to your ESLint configuration file:
```js
module.exports = {
extends: ["plugin:@rnx-kit/recommended"],
};
```

Просмотреть файл

@ -0,0 +1,2 @@
const { configureJust } = require("rnx-kit-scripts");
configureJust();

Просмотреть файл

@ -0,0 +1,56 @@
{
"name": "@rnx-kit/eslint-plugin",
"version": "0.0.1",
"description": "A set of ESLint rules for React developers",
"keywords": [
"eslint",
"eslintplugin",
"react",
"react-native",
"typescript"
],
"homepage": "https://github.com/microsoft/rnx-kit/tree/main/packages/eslint-plugin#readme",
"license": "MIT",
"files": [
"src/*"
],
"main": "src/index.js",
"repository": {
"type": "git",
"url": "https://github.com/microsoft/rnx-kit",
"directory": "packages/eslint-plugin"
},
"scripts": {
"build": "rnx-kit-scripts build",
"format": "rnx-kit-scripts format",
"lint": "rnx-kit-scripts lint",
"test": "rnx-kit-scripts test"
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint-plugin-jest": "^25.0.0",
"eslint-plugin-react": "^7.26.0"
},
"peerDependencies": {
"eslint": ">=6.0.0"
},
"devDependencies": {
"@types/eslint": "^7.28.0",
"eslint": "^8.0.0"
},
"depcheck": {
"ignoreMatches": [
"@typescript-eslint/eslint-plugin",
"@typescript-eslint/parser",
"eslint-plugin-jest",
"eslint-plugin-react"
]
},
"eslintConfig": {
"extends": "@rnx-kit/eslint-config"
},
"jest": {
"preset": "rnx-kit-scripts"
}
}

Просмотреть файл

@ -0,0 +1,34 @@
// @ts-check
"use strict";
module.exports = {
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: "module",
},
plugins: ["@rnx-kit", "@typescript-eslint"],
rules: {
"@rnx-kit/no-export-all": "warn",
"@typescript-eslint/no-unused-vars": "off", // too many false-positives with optional chaining
"@typescript-eslint/no-var-requires": "off",
"react/prop-types": "off",
},
settings: {
react: {
version: "detect",
},
},
};

Просмотреть файл

@ -0,0 +1,11 @@
// @ts-check
"use strict";
module.exports = {
configs: {
recommended: require("./configs/recommended"),
},
rules: {
"no-export-all": require("./rules/no-export-all"),
},
};

Просмотреть файл

@ -0,0 +1,28 @@
// @ts-check
"use strict";
/** @type {import("eslint").Rule.RuleModule} */
module.exports = {
meta: {
type: "problem",
docs: {
description: "disallow `export *`",
category: "Possible Errors",
recommended: true,
url: require("../../package.json").homepage,
},
fixable: "code",
schema: [], // no options
},
create: (context) => {
return {
ExportAllDeclaration: (node) => {
context.report({
node,
message:
"Prefer explicit exports over `export *` to avoid name clashes, and improve tree-shakeability.",
});
},
};
},
};

Просмотреть файл

@ -0,0 +1,36 @@
import { RuleTester } from "eslint";
import rule from "../src/rules/no-export-all";
const config = {
env: {
es6: true,
node: true,
},
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: "module",
},
};
describe("disallows `export *`", () => {
const ruleTester = new RuleTester(config);
ruleTester.run("no-export-all", rule, {
valid: [
"export const name = 'Arnold';",
"const name = 'Arnold'; export { name };",
"export default 'Arnold';",
"const name = 'Arnold'; export { name as default };",
"export { escape } from 'chopper';",
],
invalid: [
{
code: "export * from 'chopper';",
errors: 1,
},
],
});
});

Просмотреть файл

@ -0,0 +1,7 @@
{
"extends": "rnx-kit-scripts/tsconfig-shared.json",
"compilerOptions": {
"noEmit": true
},
"include": ["src/*"]
}

Просмотреть файл

@ -1917,6 +1917,19 @@
"@types/node" "*"
"@types/responselike" "*"
"@types/eslint@^7.28.0":
version "7.28.2"
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-7.28.2.tgz#0ff2947cdd305897c52d5372294e8c76f351db68"
integrity sha512-KubbADPkfoU75KgKeKLsFHXnU4ipH7wYg0TRT33NK3N3yiu7jlFAAoygIWBV+KbuHx/G+AvuGX6DllnK35gfJA==
dependencies:
"@types/estree" "*"
"@types/json-schema" "*"
"@types/estree@*":
version "0.0.50"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.50.tgz#1e0caa9364d3fccd2931c3ed96fdbeaa5d4cca83"
integrity sha512-C6N5s2ZFtuZRj54k2/zyRhNDjJwwcViAM3Nbm8zjBpbqAdZ00mr0CFxvSKeO8Y/e03WVFLpQMdHYVfUd6SB+Hw==
"@types/glob@^7.0.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.2.0.tgz#bc1b5bf3aa92f25bd5dd39f35c57361bdce5b2eb"
@ -1972,7 +1985,7 @@
jest-diff "^27.0.0"
pretty-format "^27.0.0"
"@types/json-schema@^7.0.9":
"@types/json-schema@*", "@types/json-schema@^7.0.9":
version "7.0.9"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.9.tgz#97edc9037ea0c38585320b28964dde3b39e4660d"
integrity sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==