

app folder, and, after going to it, run the following command at the command prompt: npm init -y npm initializes a new project in the app folder by creating the package.json file in it.yarn in this material, but npm can also be used here. Prettier can be connected to an existing project.prettier package as a dependency on the development of our project: yarn add --dev prettier package.json , which looks like this: { "name": "react-boiler-plate", "version": "1.0.0", "description": "A react boiler plate", "main": "src/index.js", "author": "Adeel Imran", "license": "MIT", "scripts": {   "prettier": "prettier --write src/**/*.js" }, "devDependencies": {   "prettier": "^1.14.3" } } "prettier": "prettier — write src/**/*.js" line means "prettier": "prettier — write src/**/*.js" . In the meantime, let's create the src folder in the app folder. In this folder, create the file index.js , although you can name it as you like. let person =                     { name: "Yoda",               designation: 'Jedi Master '               };             function trainJedi (jediWarrion) { if (jediWarrion.name === 'Yoda') { console.log('No need! already trained'); } console.log(`Training ${jediWarrion.name} complete`)             } trainJedi(person)             trainJedi({ name: 'Adeel',             designation: 'padawan' }); src/app/index.js , in which there is a rather poorly designed code.scripts section of the package.json file there is an entry about Prettier. It is clear that we will use this tool to format the code. To do this, create a prettier.config.js file in the app folder and add the rules for Prettier: module.exports = { printWidth: 100, singleQuote: true, trailingComma: 'all', bracketSpacing: true, jsxBracketSameLine: false, tabWidth: 2, semi: true, }; printWidth: 100 - the length of the string should not exceed 100 characters.singleQuote: true - all double quotes will be converted to single quotes. Details on this can be found in the Airbnb Style Guide . I really like this tutorial, I use it to improve the quality of my code.trailingComma: 'all' - provides a comma after the last property of the object. Here is a good article on this topic.bracketSpacing: true - responsible for inserting spaces between the body of an object and curly brackets in object literals. If this property is set to true , then objects declared using object literals will look like this: { foo: bar } . If you set it to false , then such constructions will look like this: {foo: bar} .jsxBracketSameLine: false — thanks to this rule, the > character in multi-line JSX elements will be placed in the last line. This is how the code looks like if this rule is set to true : <button className="prettier-class" id="prettier-id" onClick={this.handleClick}> Click Here </button> false : <button className="prettier-class" id="prettier-id" onClick={this.handleClick} > Click Here </button> tabWidth: 2 - sets the number of spaces per alignment level.semi: true - if this rule is set to true , then a semicolon is added to the end of the expression. "prettier": "prettier  — write src/**/*.js" .js files in the src folder. The --write flag instructs him to save formatted files as they are processed and corrected for formatting errors found in them. yarn prettier 
package.json file, add some dependencies to it: { "name": "react-boiler-plate", "version": "1.0.0", "description": "A react boiler plate", "main": "src/index.js", "author": "Adeel Imran", "license": "MIT", "scripts": {   "lint": "eslint --debug src/",   "lint:write": "eslint --debug src/ --fix",   "prettier": "prettier --write src/**/*.js" }, "husky": {   "hooks": {     "pre-commit": "lint-staged"   } }, "lint-staged": {   "*.(js|jsx)": ["npm run lint:write", "git add"] }, "devDependencies": {   "babel-eslint": "^8.2.3",   "eslint": "^4.19.1",   "eslint-config-airbnb": "^17.0.0",   "eslint-config-jest-enzyme": "^6.0.2",   "eslint-plugin-babel": "^5.1.0",   "eslint-plugin-import": "^2.12.0",   "eslint-plugin-jest": "^21.18.0",   "eslint-plugin-jsx-a11y": "^6.0.3",   "eslint-plugin-prettier": "^2.6.0",   "eslint-plugin-react": "^7.9.1",   "husky": "^1.1.2",   "lint-staged": "^7.3.0",   "prettier": "^1.14.3" } } package.json . I believe that before using certain dependencies, it is worth knowing what role they play.babel-eslint - allows you to use linting as applied to everything Babel gives. You do not need this plugin if you are not using Flow or experimental features that ESLint does not yet support.eslint is the main tool used for linting code.eslint-config-airbnb — Provides Airbnb rules as a configuration that can be modified.eslint-plugin-babel is an ESLint plugin that complements the babel-eslint . It altered the rules that, when using babel-eslint , cause problems when processing experimental features.eslint-plugin-import - this package supports linting of fresh import/export syntaxes and allows you to prevent problems associated with incorrect spelling of paths to files and names of imported modules.eslint-plugin-jsx-a11y — Provides rules regarding the availability of JSX elements for people with disabilities. Web accessibility is very important.eslint-plugin-prettier — Helps ESLint and Prettier work together. It looks like this: when Prettier formats the code, it does so taking into account the rules of ESLint.eslint-plugin-react - contains ESLint-rules designed for React.package.json presented above has dependencies intended for unit testing using Jest / Enzyme . Here, if you decide to use these tools for testing, a description of the relevant packages.eslint-config-jest-enzyme - this package is intended for those cases when using the jest-environment-enzyme , which leads to the fact that the variables React and Enzyme are global. Thanks to him, ESLint will not issue warnings about such variables.eslint-plugin-jest — ESlint-plugin for eslint-plugin-jest — .husky and lint-staged ..eslintrc.js file in the app folder: module.exports = {   env: {       es6: true,       browser: true,       node: true,   },   extends: ['airbnb', 'plugin:jest/recommended', 'jest-enzyme'],   plugins: [       'babel',       'import',       'jsx-a11y',       'react',       'prettier',   ],   parser: 'babel-eslint',   parserOptions: {       ecmaVersion: 6,       sourceType: 'module',       ecmaFeatures: {           jsx: true       }   },   rules: {       'linebreak-style': 'off', //    Windows.       'arrow-parens': 'off', //   prettier       'object-curly-newline': 'off', //   prettier       'no-mixed-operators': 'off', //   prettier       'arrow-body-style': 'off', //  -   ?       'function-paren-newline': 'off', //   prettier       'no-plusplus': 'off',       'space-before-function-paren': 0, //   prettier       'max-len': ['error', 100, 2, { ignoreUrls: true, }], // airbnb           'no-console': 'error', // airbnb         'no-alert': 'error', // airbnb         'no-param-reassign': 'off', //  -   ?       "radix": "off", // parseInt, parseFloat  radix .    .       'react/require-default-props': 'off', // airbnb           'react/forbid-prop-types': 'off', // airbnb           'react/jsx-filename-extension': ['error', { extensions: ['.js'] }], // airbnb  .jsx       'prefer-destructuring': 'off',       'react/no-find-dom-node': 'off', //           'react/no-did-mount-set-state': 'off',       'react/no-unused-prop-types': 'off', //            'react/jsx-one-expression-per-line': 'off',       "jsx-a11y/anchor-is-valid": ["error", { "components": ["Link"], "specialLink": ["to"] }],       "jsx-a11y/label-has-for": [2, {           "required": {               "every": ["id"]           }       }], //     htmlFor  label       'prettier/prettier': ['error'],   }, }; .eslintignore file to the app folder: /.git /.vscode node_modules .eslintrc.js file is .eslintrc.js , and about the meaning of the constructions presented in it. module.exports = {  env:{},  extends: {},  plugin: {},  parser: {},  parserOptions: {},  rules: {}, }; env - allows you to specify a list of environments for which you plan to check the code. In our case, there are es6 , browser and node properties set to true . The es6 parameter es6 capabilities except modules (this option automatically sets the parserOptions parameter to 6 in the parserOptions block). The browser global variables, such as Windows . The node parameter adds global Node.js environment variables and scopes, for example, global . Details about the media can be found here .extends - is an array of strings with configurations, with each additional configuration extending the previous one. Here, airbnb airbnb rules are airbnb , which are extended to jest and then extended to jest-enzyme .plugins - here are the linting rules that we want to use. We apply the rules of babel , import , jsx-a11y , react , prettier , which we have already spoken about.parser - by default, ESLint uses the Espree parser, but since we are working with Babel, we need to use Babel-ESLint .parserOptions - since we changed the standard parser to babel-eslint , we need to set the properties in this block. The ecmaVersion property set to 6 indicates to ESLint that the ES6 code will be checked. Since we write code in EcmaScript modules, the sourceType property sourceType set to module . And finally, since we use React, which means using JSX, an object with the jsx key set to true written to the ecmaFeatures property.rules - this part of the .eslintrc.js file .eslintrc.js like the most, as it allows you to customize ESLint rules. All the rules that we have expanded or added using plug-ins can be changed or redefined, and this is done in the rules block. In the text of the file there are comments to the rules..eslintignore file. This file accepts a list of paths representing folders whose contents should not be processed using ESLint./.git - I do not need ESLint to check files related to Git./.vscode - the project has this folder due to the fact that I use VS Code. Here the editor stores configuration information that can be set for each project. This data also should not be processed by the linter.node-modules - dependency files also do not need to be checked with a linter.package.json . Here they are: "lint": "eslint --debug src/" "lint:write": "eslint --debug src/ --fix" yarn lint or npm run lint command, this will cause the linter to scan all the files in the src directory and display a detailed report on the files in which it found errors. Using this report, you can correct these errors.
yarn lint:write ), then ESLint will perform the same test that was performed before. The only difference is that in this mode the system will try to correct the detected errors, will try to bring the code into a more decent view.ctrl+shift+x ). Here, in the search field, you must enter eslint . A list of extensions appears. We are interested in that, in the information on the developer of which Dirk Baeumer is specified. After installing this extension, restart the editor.app ), create a .vscode folder (note the dot at the beginning of the name - this is important). In this folder, create a file settings.json following content: { "editor.formatOnSave": false, "eslint.autoFixOnSave": true, } editor.formatOnSave property, set to false , indicates that we do not need the standard configuration to be applied to the formatting of the file, as this may cause a conflict with ESLint and Prettier.eslint.autoFixOnSave property eslint.autoFixOnSave set to true , since it is necessary for the installed plugin to work every time the file is saved. Since ESLint and Prettier work together in a project, saving the file leads to both formatting and linting of the code.lint:write script is run, it will execute both the linting and formatting of the code. yarn add --dev husky package.json : "husky": {     "hooks": {         "pre-commit": "YOUR_COMMAND_HERE",    "pre-push": "YOUR_COMMAND_HERE"    } }, commit or push command, a certain script will be called, which, for example, performs testing of the code or its formatting. yarn add --dev lint-staged package.json file, add the following: "lint-staged": {     "*.(js|jsx)": ["npm run lint:write", "git add"] }, lint:write command will first be executed, checking the file contents and correcting errors, after which the files will be added to the index using the git add command. This command now targets .js and .jsx files, but you can do the same with other file types.lint-staged script, which, in turn, runs the lint:write script, which performs the linting and code formatting. After that, the files are added to the index, and then committed. It seems to me that it is very convenient. In fact, in the previously presented code of the package.json file, this is already implemented, we just did not talk about this before.package.json : { "name": "react-boiler-plate", "version": "1.0.0", "description": "A react boiler plate", "main": "src/index.js", "author": "Adeel Imran", "license": "MIT", "scripts": {   "lint": "eslint --debug src/",   "lint:write": "eslint --debug src/ --fix",   "prettier": "prettier --write src/**/*.js" }, "husky": {   "hooks": {     "pre-commit": "lint-staged"   } }, "lint-staged": {   "*.(js|jsx)": ["npm run lint:write", "git add"] }, "devDependencies": {   "babel-eslint": "^8.2.3",   "eslint": "^4.19.1",   "eslint-config-airbnb": "^17.0.0",   "eslint-config-jest-enzyme": "^6.0.2",   "eslint-plugin-babel": "^5.1.0",   "eslint-plugin-import": "^2.12.0",   "eslint-plugin-jest": "^21.18.0",   "eslint-plugin-jsx-a11y": "^6.0.3",   "eslint-plugin-prettier": "^2.6.0",   "eslint-plugin-react": "^7.9.1",   "husky": "^1.1.2",   "lint-staged": "^7.3.0",   "prettier": "^1.14.3" } }  $ git add . $ git commit -m "some descriptive message here" .eslintrc.js , and, if necessary, corrected. Thanks to this, the errors will never get to the working project repository..editorconfig file.editorconfig . It helps maintain a single set of rules in heterogeneous teams.app .editorconfig : # EditorConfig -  : http://EditorConfig.org #  EditorConfig   root = true [*.md] trim_trailing_whitespace = false [*.js] trim_trailing_whitespace = true #     Unix       [*] indent_style = space indent_size = 2 end_of_line = lf charset = utf-8 insert_final_newline = true max_line_length = 100 trim_trailing_whitespace = false — .md - . .js - false .indent_style = space — .indent_size = 2 — .end_of_line = lf — lf . , , . .insert_final_newline = true — .max_line_length = 100 — 100 .Source: https://habr.com/ru/post/428173/
All Articles