├── .gitignore ├── index.js ├── typescript.js ├── package.json ├── .eslintrc.js ├── tsconfig.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | haters/ 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const eslintrc = require('./.eslintrc'); 2 | 3 | module.exports = eslintrc; 4 | -------------------------------------------------------------------------------- /typescript.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | globals: { 3 | React: true, 4 | JSX: true, 5 | }, 6 | extends: [ 7 | 'plugin:@typescript-eslint/recommended', // Uses rules from `@typescript-eslint/eslint-plugin`, 8 | 'airbnb-typescript', 9 | 'plugin:@typescript-eslint/recommended-requiring-type-checking', 10 | // Layer in all the JS Rules 11 | './.eslintrc.js', 12 | ], 13 | // then add some extra good stuff for Typescript 14 | parser: '@typescript-eslint/parser', 15 | plugins: ['@typescript-eslint'], 16 | // Then we add our own custom typescript rules 17 | rules: { 18 | // This allows us to use async function on addEventListener(). Discussion: https://twitter.com/wesbos/status/1337074242161172486 19 | '@typescript-eslint/no-misused-promises': [ 20 | 'error', 21 | { 22 | checksVoidReturn: false, 23 | }, 24 | ], 25 | '@typescript-eslint/no-explicit-any': 'off', 26 | 'no-unused-vars': 0, 27 | '@typescript-eslint/no-unused-vars': [1, { ignoreRestSiblings: true }], 28 | 'no-redeclare': 'off', 29 | '@typescript-eslint/no-redeclare': [ 30 | 'warn', 31 | { 32 | ignoreDeclarationMerge: true, 33 | }, 34 | ], 35 | '@typescript-eslint/no-floating-promises': 'off', 36 | '@typescript-eslint/no-use-before-define': 'off', 37 | // this is covered by the typescript compiler, so we don't need it 38 | 'no-undef': 'off', 39 | 'no-shadow': 'off', // TS does it 40 | }, 41 | parserOptions: { 42 | project: './tsconfig.json', 43 | }, 44 | }; 45 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eslint-config-wesbos", 3 | "version": "3.1.4", 4 | "description": "ESLint and Prettier Config from Wes Bos", 5 | "keywords": [ 6 | "javascript", 7 | "typescript", 8 | "ecmascript", 9 | "eslint", 10 | "lint", 11 | "config", 12 | "prettier" 13 | ], 14 | "main": "index.js", 15 | "repository": "git@github.com:wesbos/eslint-config-wesbos.git", 16 | "author": "Wes bos ", 17 | "license": "MIT", 18 | "peerDependencies": { 19 | "@babel/core": "^7.19.3", 20 | "@types/node": "^18.8.3", 21 | "@typescript-eslint/eslint-plugin": "^5.39.0", 22 | "@typescript-eslint/parser": "^5.39.0", 23 | "eslint": "^8.24.0", 24 | "eslint-config-airbnb": "^19.0.4", 25 | "eslint-config-airbnb-typescript": "^17.0.0", 26 | "eslint-config-prettier": "^8.5.0", 27 | "eslint-plugin-html": "^7.1.0", 28 | "eslint-plugin-import": "^2.26.0", 29 | "eslint-plugin-jsx-a11y": "^6.6.1", 30 | "eslint-plugin-prettier": "^4.2.1", 31 | "eslint-plugin-react": "^7.31.8", 32 | "eslint-plugin-react-hooks": "^4.6.0", 33 | "prettier": "^2.7.1", 34 | "typescript": "^4.8.4", 35 | "@babel/eslint-parser": "^7.19.1", 36 | "@babel/preset-react": "^7.18.6" 37 | }, 38 | "devDependencies": { 39 | "@babel/core": "^7.19.3", 40 | "@types/node": "^18.8.3", 41 | "@typescript-eslint/eslint-plugin": "^5.39.0", 42 | "@typescript-eslint/parser": "^5.39.0", 43 | "eslint": "^8.24.0", 44 | "eslint-config-airbnb": "^19.0.4", 45 | "eslint-config-airbnb-typescript": "^17.0.0", 46 | "eslint-config-prettier": "^8.5.0", 47 | "eslint-plugin-html": "^7.1.0", 48 | "eslint-plugin-import": "^2.26.0", 49 | "eslint-plugin-jsx-a11y": "^6.6.1", 50 | "eslint-plugin-prettier": "^4.2.1", 51 | "eslint-plugin-react": "^7.31.8", 52 | "eslint-plugin-react-hooks": "^4.6.0", 53 | "prettier": "^2.7.1", 54 | "typescript": "^4.8.4", 55 | "@babel/eslint-parser": "^7.19.1", 56 | "@babel/preset-react": "^7.18.6" 57 | }, 58 | "scripts": { 59 | "lint": "eslint .", 60 | "lint:fix": "eslint --fix" 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: ['airbnb', 'prettier'], 3 | parser: '@babel/eslint-parser', 4 | parserOptions: { 5 | requireConfigFile: false, 6 | babelOptions: { 7 | presets: ['@babel/preset-react'], 8 | }, 9 | }, 10 | env: { 11 | browser: true, 12 | node: true, 13 | jquery: true, 14 | jest: true, 15 | }, 16 | rules: { 17 | 'no-debugger': 0, 18 | 'no-use-before-define': 'off', 19 | 'import/no-cycle': 'off', 20 | 'no-alert': 0, 21 | 'no-await-in-loop': 0, 22 | 'no-return-assign': ['error', 'except-parens'], 23 | 'no-restricted-syntax': [ 24 | 2, 25 | 'ForInStatement', 26 | 'LabeledStatement', 27 | 'WithStatement', 28 | ], 29 | 'no-unused-vars': [ 30 | 1, 31 | { 32 | ignoreRestSiblings: true, 33 | argsIgnorePattern: 'res|next|^err', 34 | }, 35 | ], 36 | 'prefer-const': [ 37 | 'error', 38 | { 39 | destructuring: 'all', 40 | }, 41 | ], 42 | 'arrow-body-style': [2, 'as-needed'], 43 | 'no-unused-expressions': [ 44 | 2, 45 | { 46 | allowTaggedTemplates: true, 47 | }, 48 | ], 49 | 'no-param-reassign': [ 50 | 2, 51 | { 52 | props: false, 53 | }, 54 | ], 55 | 'no-console': 0, 56 | 'import/prefer-default-export': 0, 57 | import: 0, 58 | 'func-names': 0, 59 | 'space-before-function-paren': 0, 60 | 'comma-dangle': 0, 61 | 'max-len': 0, 62 | 'import/extensions': 0, 63 | 'no-underscore-dangle': 0, 64 | 'consistent-return': 0, 65 | 'react/display-name': 1, 66 | 'react/no-array-index-key': 0, 67 | 'react/react-in-jsx-scope': 0, 68 | 'react/prefer-stateless-function': 0, 69 | 'react/forbid-prop-types': 0, 70 | 'react/no-unescaped-entities': 0, 71 | 'react/function-component-definition': 0, 72 | 'jsx-a11y/accessible-emoji': 0, 73 | 'jsx-a11y/label-has-associated-control': [ 74 | 'error', 75 | { 76 | assert: 'either', 77 | }, 78 | ], 79 | 'react/require-default-props': 0, 80 | 'react/jsx-filename-extension': [ 81 | 1, 82 | { 83 | extensions: ['.js', '.jsx', '.ts', '.tsx', '.mdx'], 84 | }, 85 | ], 86 | radix: 0, 87 | 'no-shadow': [ 88 | 2, 89 | { 90 | hoist: 'all', 91 | allow: ['resolve', 'reject', 'done', 'next', 'err', 'error'], 92 | }, 93 | ], 94 | quotes: [ 95 | 2, 96 | 'single', 97 | { 98 | avoidEscape: true, 99 | allowTemplateLiterals: true, 100 | }, 101 | ], 102 | 'prettier/prettier': [ 103 | 'error', 104 | { 105 | trailingComma: 'es5', 106 | singleQuote: true, 107 | printWidth: 80, 108 | // below line only for windows users facing CLRF and eslint/prettier error 109 | // non windows users feel free to delete it 110 | endOfLine: 'auto', 111 | }, 112 | ], 113 | 'jsx-a11y/href-no-hash': 'off', 114 | 'jsx-a11y/anchor-is-valid': [ 115 | 'warn', 116 | { 117 | aspects: ['invalidHref'], 118 | }, 119 | ], 120 | 'react-hooks/rules-of-hooks': 'error', 121 | 'react-hooks/exhaustive-deps': 'warn', 122 | '@typescript-eslint/comma-dangle': ['off'], 123 | 'react/jsx-props-no-spreading': 'off', 124 | }, 125 | plugins: ['html', 'prettier', 'react-hooks'], 126 | }; 127 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "include": [ 3 | "**/*", 4 | "**/.*" 5 | ], 6 | "compilerOptions": { 7 | "types": [ 8 | "node" 9 | ], 10 | // What should we export the code as? 11 | "target": "ES5", 12 | // What types of modules should we use? 13 | "module": "commonjs", 14 | // What libs should we include? This is different than target because we could be using Polyfills 15 | // "lib": [], /* Specify library files to be included in the compilation. */ 16 | // "allowJs": true, /* Allow javascript files to be compiled. */ 17 | // "checkJs": true, /* Report errors in .js files. */ 18 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 19 | // "declaration": true, /* Generates corresponding '.d.ts' file. */ 20 | // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ 21 | // "sourceMap": true, /* Generates corresponding '.map' file. */ 22 | // "outFile": "./", /* Concatenate and emit output to single file. */ 23 | // "outDir": "./", /* Redirect output structure to the directory. */ 24 | // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 25 | // "composite": true, /* Enable project compilation */ 26 | // "removeComments": true, /* Do not emit comments to output. */ 27 | // "noEmit": true, /* Do not emit outputs. */ 28 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 29 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 30 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 31 | /* Strict Type-Checking Options */ 32 | "strict": true /* Enable all strict type-checking options. */, 33 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 34 | // "strictNullChecks": true, /* Enable strict null checks. */ 35 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 36 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ 37 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 38 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 39 | /* Additional Checks */ 40 | // "noUnusedLocals": true, /* Report errors on unused locals. */ 41 | // "noUnusedParameters": true, /* Report errors on unused parameters. */ 42 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 43 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 44 | /* Module Resolution Options */ 45 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 46 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ 47 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 48 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 49 | // "typeRoots": [], /* List of folders to include type definitions from. */ 50 | // "types": [], /* Type declaration files to be included in compilation. */ 51 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 52 | "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 53 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 54 | /* Source Map Options */ 55 | // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 56 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 57 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 58 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 59 | /* Experimental Options */ 60 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 61 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # No-Sweat™ Eslint and Prettier Setup 2 | These are my settings for ESLint and Prettier 3 | 4 | You might like them - or you might not. Don't worry you can always change them. 5 | 6 | ## What it does 7 | * Lints JavaScript based on the latest standards 8 | * Fixes issues and formatting errors with Prettier 9 | * Lints + Fixes inside of html script tags 10 | * Lints + Fixes React via eslint-config-airbnb 11 | * You can see all the [rules here](https://github.com/wesbos/eslint-config-wesbos/blob/master/.eslintrc.js) - these generally abide by the code written in my courses. You are very welcome to overwrite any of these settings, or just fork the entire thing to create your own. 12 | 13 | ## Installing 14 | 15 | You can use eslint globally and/or locally per project. 16 | 17 | It's usually best to install this locally once per project, that way you can have project specific settings as well as sync those settings with others working on your project via git. 18 | 19 | I also install globally so that any project or rogue JS file I write will have linting and formatting applied without having to go through the setup. You might disagree and that is okay, just don't do it then 😃. 20 | 21 | 22 | ## Local / Per Project Install 23 | 24 | 1. If you don't already have a `package.json` file, create one with `npm init`. 25 | 26 | 2. Then we need to install everything needed by the config: 27 | 28 | ``` 29 | npx install-peerdeps --dev eslint-config-wesbos 30 | ``` 31 | 32 | 3. You can see in your package.json there are now a big list of devDependencies. 33 | 34 | 4. Create a `.eslintrc` file in the root of your project's directory (it should live where package.json does). Your `.eslintrc` file should look like this: 35 | 36 | ```json 37 | { 38 | "extends": [ "wesbos" ] 39 | } 40 | ``` 41 | 42 | For TypeScript projects, use `wesbos/typescript`. 43 | 44 | ```json 45 | { 46 | "extends": [ "wesbos/typescript" ] 47 | } 48 | ``` 49 | 50 | TypeScript users will also need a `tsconfig.json` file in their project. An empty object (`{}`) will do if this is a new project. 51 | 52 | 53 | Tip: You can alternatively put this object in your `package.json` under the property `"eslintConfig":`. This makes one less file in your project. 54 | 55 | 5. You can add two scripts to your package.json to lint and/or fix: 56 | 57 | ```json 58 | "scripts": { 59 | "lint": "eslint .", 60 | "lint:fix": "eslint . --fix" 61 | }, 62 | ``` 63 | 64 | 6. Now you can manually lint your code by running `npm run lint` and fix all fixable issues with `npm run lint:fix`. You probably want your editor to do this though. 65 | 66 | ## Settings 67 | 68 | If you'd like to overwrite eslint or prettier settings, you can add the rules in your `.eslintrc` file. The [ESLint rules](https://eslint.org/docs/rules/) go directly under `"rules"` while [prettier options](https://prettier.io/docs/en/options.html) go under `"prettier/prettier"`. Note that prettier rules overwrite anything in my config (trailing comma, and single quote), so you'll need to include those as well. 69 | 70 | ```js 71 | { 72 | "extends": [ 73 | "wesbos" 74 | ], 75 | "rules": { 76 | "no-console": 2, 77 | "prettier/prettier": [ 78 | "error", 79 | { 80 | "trailingComma": "es5", 81 | "singleQuote": true, 82 | "printWidth": 120, 83 | "tabWidth": 8, 84 | } 85 | ] 86 | } 87 | } 88 | ``` 89 | 90 | ## With VS Code 91 | 92 | You should read this entire thing. Serious! 93 | 94 | Once you have done one, or both, of the above installs. You probably want your editor to lint and fix for you. Here are the instructions for VS Code: 95 | 96 | 1. Install the [ESLint package](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) 97 | 2. Now we need to setup some VS Code settings via `Code/File` → `Preferences` → `Settings`. It's easier to enter these settings while editing the `settings.json` file, so click the Open (Open Settings) icon in the top right corner: 98 | ```js 99 | // These are all my auto-save configs 100 | "editor.formatOnSave": true, 101 | // turn it off for JS and JSX, we will do this via eslint 102 | "[javascript]": { 103 | "editor.formatOnSave": false 104 | }, 105 | "[javascriptreact]": { 106 | "editor.formatOnSave": false 107 | }, 108 | // show eslint icon at bottom toolbar 109 | "eslint.alwaysShowStatus": true, 110 | // tell the ESLint plugin to run on save 111 | "editor.codeActionsOnSave": { 112 | "source.fixAll": true 113 | } 114 | ``` 115 | 116 | After attempting to lint your file for the first time, you may need to click on 'ESLint' in the bottom right and select 'Allow Everywhere' in the alert window. 117 | 118 | Finally you'll usually need to restart VS code. They say you don't need to, but it's never worked for me until I restart. 119 | 120 | ## With Create React App 121 | 122 | 1. Run `npx install-peerdeps --dev eslint-config-wesbos` 123 | 1. Crack open your `package.json` and replace `"extends": "react-app"` with `"extends": "wesbos"` 124 | 125 | ## With Gatsby 126 | 127 | 1. Run `npx install-peerdeps --dev eslint-config-wesbos` 128 | 1. If you have an existing `.prettierrc` file, delete it. 129 | 1. follow the `Local / Per Project Install` steps above 130 | 131 | ## With WSL 132 | 133 | Can someone add this? 134 | 135 | ## With JetBrains Products (IntelliJ IDEA, WebStorm, RubyMine, PyCharm, PhpStorm, etc) 136 | 137 | If you have previously configured ESLint to run via a File Watcher, [turn that off.](https://www.jetbrains.com/help/idea/using-file-watchers.html#enableFileWatcher) 138 | 139 | ### If you choose Local / Per Project Install Above 140 | 1. Open ESLint configuration by going to File > Settings (Edit > Preferences on Mac) > Languages & Frameworks > Code Quality Tools > ESLint (optionally just search settings for "eslint") 141 | 1. Select **Automatic ESLint Configuration** 142 | 1. Check **Run eslint --fix on save** 143 | 144 | ### If you choose Global Install 145 | 146 | The following steps are for a typical Node / ESLint global installtion. If you have a customized setup, refer to JetBrains docs for more [ESLint Configuration Options](https://www.jetbrains.com/help/webstorm/eslint.html#ws_js_eslint_manual_configuration). 147 | 148 | 1. Open ESLint configuration by going to File > Settings (Edit > Preferences on Mac) > Languages & Frameworks > Code Quality Tools > ESLint (optionally just search settings for "eslint") 149 | 1. Select **Manual ESLint configuration** 150 | 1. Choose your **Node interpreter** from the detected installations 151 | 1. Select the global **ESLint package** from the dropdown 152 | 1. Leave Configuration File as **Automatic Search** 153 | 1. Check **Run eslint --fix on save** 154 | 155 | ### Ensure the Prettier plugin is disabled if installed. 156 | 157 | 1. Open Prettier configuration by going to File > Settings (Edit > Preferences on Mac) > Languages & Frameworks > Code Quality Tools > Prettier (optionally just search settings for "prettier") 158 | 1. Uncheck both **On code reformat** and **On save** 159 | 1. *Optional BUT IMPORTANT:* If you have the Prettier extension enabled for other languages like CSS and HTML, turn it off for JS since we are doing it through Eslint already. 160 | 1. Make sure the **Run for files** glob does not include `js,ts,jsx,tsx`. 161 | 2. An example glob for styles, config, and markdown. `{**/*,*}.{yml,css,sass,md}` 162 | 163 | ## With Typescript 164 | 165 | Same instructions as above, just make sure you extend `wesbos/typescript` instead of just `wesbos`. 166 | 167 | ## With Yarn 168 | 169 | It should just work, but if they aren't showing up in your package.json, try `npx install-peerdeps --dev eslint-config-wesbos -Y` 170 | 171 | ## Issues with ESLint not formatting code 172 | 173 | If you experience issues with ESLint not formatting the code or you receive a `Parsing error: Cannot find module '@babel/preset-react` error message then you need to check that you opened the folder where you installed and configured ESLint directly in VS Code. The correct folder to open will be the one where you installed the `eslint-config-wesbos` npm package and where you created the `.eslintrc` file. 174 | 175 | Opening a parent folder or child folder in your code editor will cause ESLint to fail in finding the ESLint npm packages and the formatting won't work. 176 | 177 | ```sh 178 | your-username 179 | | 180 | projects 181 | | 182 | beginner-javascript # <- Open this folder directly in your code editor 183 | .eslintrc 184 | package.json 185 | node_modules/ 186 | exercises/ 187 | playground/ 188 | ``` 189 | --------------------------------------------------------------------------------