From 93ac43e5b25cd2049e95d8f7a9ca940471a1a320 Mon Sep 17 00:00:00 2001 From: Manoj L Date: Fri, 13 Dec 2024 16:17:35 +0530 Subject: [PATCH] React Projects Styleguide - ESLint, Prettier configs (#27) * Task #221910 : React JS Coding Standards Conventions * Cleanup react style guide, update docs for ESLint and prettier --------- Co-authored-by: Rushi G --- docs/tekdi-style-guides/react-style-guide.md | 519 ++++++++++++++++++- docs/tools/common/_category_.json | 8 + docs/tools/common/prettier.md | 108 ++++ docs/tools/javascript/_category_.json | 8 + docs/tools/javascript/eslint.md | 220 ++++++++ docs/tools/python/_category_.json | 2 +- docusaurus.config.ts | 7 +- 7 files changed, 868 insertions(+), 4 deletions(-) create mode 100644 docs/tools/common/_category_.json create mode 100644 docs/tools/common/prettier.md create mode 100644 docs/tools/javascript/_category_.json create mode 100644 docs/tools/javascript/eslint.md diff --git a/docs/tekdi-style-guides/react-style-guide.md b/docs/tekdi-style-guides/react-style-guide.md index 38ff2a0..175009a 100644 --- a/docs/tekdi-style-guides/react-style-guide.md +++ b/docs/tekdi-style-guides/react-style-guide.md @@ -1,7 +1,524 @@ --- -sidebar_position: 4 +sidebar_position: 2 +tags: [react] --- # React Style Guide +## Style Guide @TODO + +## Linting, Formatting Tools +To help you assist in following style guide, we recommend **using tools suggested below, along with sample configs files suggested** to make most of these tools. + +### ESLint +Learn more about [ESLint tool](../tools/javascript/ESLint) + +#### Link to React specific docs for ESLint +- [ESLint, ESLint Plugins Installation](../tools/javascript/eslint#installation-for-react-projects-using-typescript/tools/javascript/eslint#recommended-eslint-configuration-for-react-projects-using-typescript) +- [ESLint Configuration](../tools/javascript/eslint#recommended-eslint-configuration-for-react-projects-using-typescript) + +### Prettier +Learn more about [Prettier tool](../tools/common/prettier) + +### Stylelint +Learn more about [Stylelint tool](../tools/css/stylelint) + + + diff --git a/docs/tools/common/_category_.json b/docs/tools/common/_category_.json new file mode 100644 index 0000000..20c815c --- /dev/null +++ b/docs/tools/common/_category_.json @@ -0,0 +1,8 @@ +{ + "label": "Common Tools", + "position": 99, + "link": { + "type": "generated-index", + "description": "Know more about recommended Common tools" + } +} diff --git a/docs/tools/common/prettier.md b/docs/tools/common/prettier.md new file mode 100644 index 0000000..973532f --- /dev/null +++ b/docs/tools/common/prettier.md @@ -0,0 +1,108 @@ +--- +sidebar_position: 2 +tags: [css, styleguide] +--- + +# Prettier + +@TODO + + \ No newline at end of file diff --git a/docs/tools/javascript/_category_.json b/docs/tools/javascript/_category_.json new file mode 100644 index 0000000..907562f --- /dev/null +++ b/docs/tools/javascript/_category_.json @@ -0,0 +1,8 @@ +{ + "label": "Javascript Tools", + "position": 2, + "link": { + "type": "generated-index", + "description": "Know more about recommended Javascript tools" + } +} diff --git a/docs/tools/javascript/eslint.md b/docs/tools/javascript/eslint.md new file mode 100644 index 0000000..9de7ffe --- /dev/null +++ b/docs/tools/javascript/eslint.md @@ -0,0 +1,220 @@ +--- +sidebar_position: 2 +tags: [javascript, linter, formatter, fixer, ESLint, styleguide] +--- + +# ESLint + +## About +- ESLint is a static code analyser for Javasvcript code. It lets you find and fix problems in your JavaScript code +- ESLint statically analyzes your code to quickly find problems. It is built into most text editors and you can run ESLint as part of your continuous integration pipeline. + +### Links +- [ESLint Homepage](https://ESLint.org/) +- [ESLint Playground](https://ESLint.org/play/) + +### ESLint Plugins +Depending upon the project need or whether you are using React or Angular or Node, you may use different ESLint plugins: + +#### For TypeScript based projects: +- [typescript-eslint](https://www.npmjs.com/package/typescript-eslint): Tooling which enables you to use TypeScript with ESLint + +#### For React based projects: +- [eslint-plugin-react](https://www.npmjs.com/package/eslint-plugin-react): Provides React specific linting rules for eslint +- [eslint-plugin-react-hooks](https://www.npmjs.com/package/eslint-plugin-react-hooks): This ESLint plugin enforces the Rules of Hooks. +- [eslint-plugin-react-refresh](https://www.npmjs.com/package/eslint-plugin-react-refresh): This ESLint plugin validate that your components can safely be updated with Fast Refresh. +- [eslint-plugin-jsx-a11y](https://www.npmjs.com/package/eslint-plugin-jsx-a11y): This plugin runs Static AST checker for accessibility rules on JSX elements. + +#### For projects using prettier as formatting tool: +##### Prettier +- [prettier](https://www.npmjs.com/package/prettier): Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary. + +##### ESLint plugins for Prettier +- [eslint-config-prettier](https://www.npmjs.com/package/eslint-config-prettier): Turns off all rules that are unnecessary or might conflict with Prettier. +- [eslint-plugin-prettier](https://www.npmjs.com/package/eslint-plugin-prettier): Runs Prettier as an ESLint rule and reports differences as individual ESLint issues. + +## Prerequisites +- Node.js (^18.18.0, ^20.9.0, or >=21.1.0) built with SSL support. (If you are using an official Node.js distribution, SSL is always built in.) + +## Installation +### Installation for Angular projects +```js +@TODO +``` + +### Installation for Nodejs - Expressjs projects +```js +@TODO +``` + +### Installation for Nodejs - Nestjs projects +```js +@TODO +``` + +### Installation for React projects using TypeScript +Navigate to your project directory and install npm packages required for ESLint, ESLint plugins and configurations. + +- If using npm +```sh +npm install globals \ +eslint @eslint/js typescript-eslint \ +eslint-plugin-react \ +eslint-plugin-react-hooks \ +eslint-plugin-react-refresh \ +eslint-plugin-jsx-a11y +prettier eslint-config-prettier eslint-plugin-prettier \ +--save-dev +``` + +- Or if using yarn + +```sh +yarn add globals \ +eslint @eslint/js typescript-eslint \ +eslint-plugin-react \ +eslint-plugin-react-hooks \ +eslint-plugin-react-refresh \ +eslint-plugin-jsx-a11y +prettier eslint-config-prettier eslint-plugin-prettier \ +--dev +``` + +:::warning +Remember to save these as `devDependencies` in your project. +::: + +## Config file for eslint.config.js +- From v8.21.0, eslint announced a new config system. In the new system, .eslintrc* is no longer used. eslint.config.js would be the default config file name. In eslint v8, the legacy system (.eslintrc*) would still be supported, while in eslint v9, only the new system would be supported. +- And from v8.23.0, eslint CLI starts to look up eslint.config.js. So, if your eslint is >=8.23.0, you're 100% ready to use the new config system. +- You might want to check out the official blog posts, + - https://eslint.org/blog/2022/08/new-config-system-part-1/ + - https://eslint.org/blog/2022/08/new-config-system-part-2/ + - https://eslint.org/blog/2022/08/new-config-system-part-3/ +- Create and use a file named `eslint.config.js` in your python project root folder + +### Recommended ESLint configuration for Angular projects +```js +@TODO +``` + +### Recommended ESLint configuration for Nodejs - Expressjs projects +```js +@TODO +``` + +### Recommended ESLint configuration for Nodejs - Nestjs projects +```js +@TODO +``` + +### Recommended ESLint configuration for React projects using TypeScript + +```js +import globals from 'globals'; +import pluginJs from '@eslint/js'; +import tseslint from 'typescript-eslint'; +import pluginReact from 'eslint-plugin-react'; +import pluginReactHooks from 'eslint-plugin-react-hooks'; +import pluginReactRefresh from 'eslint-plugin-react-refresh'; +import pluginJsxA11y from 'eslint-plugin-jsx-a11y'; +import pluginEslintPrettieRecommendedConfig from 'eslint-plugin-prettier/recommended'; + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + { + files: ['**/*.{js,mjs,cjs,ts,jsx,tsx}'], + }, + { + languageOptions: { + globals: { + ...globals.browser, + }, + }, + }, + pluginJs.configs.recommended, + ...tseslint.configs.recommended, + pluginReact.configs.flat.recommended, + pluginJsxA11y.flatConfigs.recommended, + pluginEslintPrettieRecommendedConfig, + { + plugins: { + 'react-hooks': pluginReactHooks, + 'react-refresh': pluginReactRefresh, + }, + settings: { + react: { + version: 'detect', + }, + }, + rules: { + 'react-hooks/rules-of-hooks': 'error', + 'react-hooks/exhaustive-deps': 'warn', + 'react-refresh/only-export-components': 'warn', + // Prettier and Indentation Rules + 'prettier/prettier': [ + 'error', + { + arrowParens: 'always', + endOfLine: 'lf', + semi: true, + singleQuote: true, + tabWidth: 4, + trailingComma: 'es5', + useTabs: true, + }, + ], + indent: ['error', 'tab'], + }, + }, + { + // Optional: ignore specific files or patterns + ignores: ['**/node_modules/**', 'dist/**', 'build/**'], + }, +]; +``` + + +### Recommended ESLint configuration for React Native projects +```js +@TODO +``` + + \ No newline at end of file diff --git a/docs/tools/python/_category_.json b/docs/tools/python/_category_.json index 20dd9e0..1fe8ff4 100644 --- a/docs/tools/python/_category_.json +++ b/docs/tools/python/_category_.json @@ -1,6 +1,6 @@ { "label": "Python Tools", - "position": 2, + "position": 3, "link": { "type": "generated-index", "description": "Know more about recommended Python tools" diff --git a/docusaurus.config.ts b/docusaurus.config.ts index e822cc2..73af812 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -28,8 +28,8 @@ const config: Config = { // useful metadata like html lang. For example, if your site is Chinese, you // may want to replace "en" with "zh-Hans". i18n: { - defaultLocale: "en", - locales: ["en"], + defaultLocale: "en-GB", + locales: ["en-GB"], }, presets: [ @@ -43,6 +43,9 @@ const config: Config = { // Please change this to your repo. // Remove this to remove the "edit this page" links. // editUrl: 'https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/', + + showLastUpdateTime: true, // ++ Tekdi + showLastUpdateAuthor: true, // ++ Tekdi }, /*blog: { showReadingTime: true,