Skip to content

Commit

Permalink
feat(eslint): add eslint for react and typescript support
Browse files Browse the repository at this point in the history
* Since eslint supports TS: https://eslint.org/blog/2019/01/future-typescript-eslint
* If you are curious about the why part: microsoft/TypeScript#29288,
see `Linting` section
  • Loading branch information
jozsefDevs committed Apr 1, 2020
1 parent ecdd4a7 commit 619fb51
Show file tree
Hide file tree
Showing 9 changed files with 1,059 additions and 41 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# http://editorconfig.org

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
54 changes: 54 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* React rules: https://github.com/yannickcr/eslint-plugin-react/tree/master/docs/rules
*/
module.exports = {
ignorePatterns: ["*.config.js", "node_modules/", "dist"],
parserOptions: {
project: `./tsconfig.json`
},
plugins: [
"@typescript-eslint",
"eslint-comments",
"promise",
],
extends: [
"airbnb-typescript",
"plugin:@typescript-eslint/recommended",
"plugin:eslint-comments/recommended",
"plugin:promise/recommended",
"prettier",
"prettier/react",
"prettier/@typescript-eslint",
],
env: {
browser: true,
},
rules: {
// Too restrictive, writing ugly code to defend against a very unlikely scenario: https://eslint.org/docs/rules/no-prototype-builtins
"no-prototype-builtins": "off",
// https://basarat.gitbooks.io/typescript/docs/tips/defaultIsBad.html
"import/prefer-default-export": "off",
"import/no-default-export": "error",
// Too restrictive: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/destructuring-assignment.md
"react/destructuring-assignment": "off",
// No jsx extension: https://github.com/facebook/create-react-app/issues/87#issuecomment-234627904
"react/jsx-filename-extension": "off",
// Since we're using TS, no need for propTypes
"react/prop-types": "off",
"react/state-in-constructor": "off",
// Use function hoisting to improve code readability
"no-use-before-define": [
"error",
{ functions: false, classes: true, variables: true },
],
// Makes no sense to allow type inferrence for expression parameters, but require typing the response
"@typescript-eslint/explicit-function-return-type": [
"error",
{ allowExpressions: true, allowTypedFunctionExpressions: true },
],
"@typescript-eslint/no-use-before-define": [
"error",
{ functions: false, classes: true, variables: true, typedefs: true },
]
}
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
.env.test.local
.env.production.local
.idea
.eslintcache

npm-debug.log*
yarn-debug.log*
Expand Down
3 changes: 2 additions & 1 deletion .huskyrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
hooks: {
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
'pre-commit': 'yarn lint && lint-staged',
},
};
};
5 changes: 5 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
"prettier --write"
]
}
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
"react-dom": "^16.13.1"
},
"scripts": {
"test": "yarn type-check",
"test": "yarn type-check && yarn lint",
"type-check": "tsc --noEmit",
"type-check:watch": "yarn type-check -- --watch",
"build:types": "tsc --emitDeclarationOnly",
"build:js": "webpack --progress --mode=production",
"build": "yarn build:types && yarn build:js"
"build": "yarn build:types && yarn build:js",
"lint": "eslint src/**/*{.ts,.tsx} --format=pretty",
"format": "prettier --write src/**/*.{js,jsx,ts,tsx,json,css,scss,md}"
},
"eslintConfig": {
"extends": "react-app"
Expand Down Expand Up @@ -51,8 +53,20 @@
"@semantic-release/git": "^9.0.0",
"@types/react": "^16.9.29",
"@types/react-dom": "^16.9.5",
"@typescript-eslint/eslint-plugin": "^2.26.0",
"babel-loader": "^8.1.0",
"eslint": "^6.8.0",
"eslint-config-airbnb-typescript": "^7.2.0",
"eslint-config-prettier": "^6.10.1",
"eslint-formatter-pretty": "^3.0.1",
"eslint-plugin-eslint-comments": "^3.1.2",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-react": "^7.19.0",
"husky": "^4.2.3",
"lint-staged": "^10.1.1",
"prettier": "^2.0.2",
"semantic-release": "^17.0.4",
"surge": "^0.21.3",
"typescript": "^3.8.3",
Expand Down
6 changes: 3 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import React, { ReactElement } from 'react';
import { Counter } from "./Counter";
import { Hello } from "./Hello";

export function App() {
export function App(): ReactElement {
return (
<div className="App">
<Hello name={ 'react-webpack-typescript-babel' } />
<Hello name="react-webpack-typescript-babel" />
<Counter />
</div>
);
Expand Down
15 changes: 8 additions & 7 deletions src/Counter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react';
import React, {Component, ReactElement} from 'react';

export interface CounterState {
value: number;
Expand All @@ -7,16 +7,17 @@ export interface CounterState {
export class Counter extends Component<{}, CounterState> {
readonly state: CounterState = { value: 0 };

public render() {
private handleIncrement = (): void => this.setState(prevState => ({ value: prevState.value + 1 }));

private handleDecrement = (): void => this.setState(prevState => ({ value: prevState.value - 1 }));

public render(): ReactElement {
return (
<>
<div>{ this.state.value }</div>
<button onClick={ this.handleIncrement }>+</button>
<button onClick={ this.handleDecrement }>-</button>
<button type="button" onClick={ this.handleIncrement }>+</button>
<button type="button" onClick={ this.handleDecrement }>-</button>
</>
);
}

private handleIncrement = () => this.setState({ value: this.state.value + 1 });
private handleDecrement = () => this.setState({ value: this.state.value - 1 });
}
Loading

0 comments on commit 619fb51

Please sign in to comment.