Skip to content

Commit

Permalink
chore: add linting and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tib-tib authored and guilhermevrs committed Aug 9, 2020
1 parent ea851bf commit 275940c
Show file tree
Hide file tree
Showing 16 changed files with 678 additions and 128 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true # To avoid conflicts between create-react-app and eslint version (https://github.com/facebook/create-react-app/issues/5247)
27 changes: 27 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
extends: [
'airbnb-typescript',
'airbnb/hooks',
'plugin:react/recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/react',
'prettier/@typescript-eslint',
],
parserOptions: {
project: './tsconfig.json',
},
rules: {
'react/prop-types': 0,
'prettier/prettier': 'error',
'max-lines': ['error', 100],
},
settings: {
react: {
version: 'detect',
},
},
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'prettier'],
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
Expand Down
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 120,
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 2,
"semi": true
}
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
## Cards - UI
# Cards - UI

This repository contains the UI for the Cards project.
It was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
It was bootstrapped using [Create React App](https://github.com/facebook/create-react-app).

## Prerequisites

- Create a `.env` file by copying the `.env.example` file and setting the values you need.

## Available Scripts

Expand All @@ -12,7 +16,6 @@ In the project directory, you can run:
Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.


### `yarn test`

Launches the test runner in the interactive watch mode.<br />
Expand Down
3 changes: 3 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@commitlint/config-conventional']
};
27 changes: 24 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@
"react-scripts": "3.4.1",
"typescript": "~3.7.2"
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{ts,tsx}": ["yarn lint"]
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"version": "conventional-changelog -p angular -i CHANGELOG.md -t v -s"
"version": "conventional-changelog -p angular -i CHANGELOG.md -t v -s",
"lint": "eslint . --ext .ts --ext .tsx --fix"
},
"browserslist": {
"production": [
Expand All @@ -37,11 +47,22 @@
]
},
"devDependencies": {
"@types/react-router-dom": "^5.1.5",
"@commitlint/cli": "^9.1.1",
"@commitlint/config-conventional": "^9.1.1",
"@types/react-router-dom": "^5.1.5",
"@typescript-eslint/eslint-plugin": "^3.8.0",
"@typescript-eslint/parser": "^3.8.0",
"conventional-changelog-cli": "^2.0.34",
"husky": "^4.2.5"
"eslint": "^7.6.0",
"eslint-config-airbnb-typescript": "^9.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.5",
"eslint-plugin-react-hooks": "^4.0.8",
"husky": "^4.2.5",
"lint-staged": "^10.2.11",
"prettier": "^2.0.5"
}
}
5 changes: 1 addition & 4 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
Expand Down
32 changes: 16 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import React from 'react';
import { BrowserRouter as Router, Switch, Route, Redirect } from "react-router-dom";
import { HomePage } from "./components/pages/HomePage/HomePage";
import { BrowserRouter as Router, Switch, Route, Redirect } from 'react-router-dom';
import HomePage from './components/pages/HomePage/HomePage';

export const App = () => {
return (
<Router>
<Switch>
<Route exact path="/">
<HomePage />
</Route>
<Route path="*">
<Redirect to="/" />
</Route>
</Switch>
</Router>
);
};
const App: React.FC = () => (
<Router>
<Switch>
<Route exact path="/">
<HomePage />
</Route>
<Route path="*">
<Redirect to="/" />
</Route>
</Switch>
</Router>
);

export default App;
4 changes: 1 addition & 3 deletions src/components/pages/HomePage/HomePage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { render } from '@testing-library/react';
import { HomePage } from './HomePage';

import HomePage from './HomePage';

describe('HomePage', () => {
it('should render home page', () => {
Expand All @@ -10,4 +9,3 @@ describe('HomePage', () => {
expect(divElement).toBeInTheDocument();
});
});

4 changes: 3 additions & 1 deletion src/components/pages/HomePage/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ const useStyles = createUseStyles({
},
});

export const HomePage = () => {
const HomePage: React.FC = () => {
const classes = useStyles();

return <div className={classes.homePage}>I am on homepage</div>;
};

export default HomePage;
5 changes: 2 additions & 3 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ html, body, #root {

body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans',
'Droid Sans', 'Helvetica Neue', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import { App } from './App';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
Expand Down
57 changes: 20 additions & 37 deletions src/serviceWorker.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
// This optional code is used to register a service worker.
// register() is not called by default.

// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.

// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://bit.ly/CRA-PWA

/* eslint-disable no-console */
/* eslint-disable max-lines */
const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.0/8 are considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/)
);

type Config = {
onSuccess?: (registration: ServiceWorkerRegistration) => void;
onUpdate?: (registration: ServiceWorkerRegistration) => void;
};

export function register(config?: Config) {
export function register(config?: Config): void {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(
process.env.PUBLIC_URL,
window.location.href
);
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
Expand All @@ -44,6 +29,7 @@ export function register(config?: Config) {

if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
// eslint-disable-next-line @typescript-eslint/no-use-before-define
checkValidServiceWorker(swUrl, config);

// Add some additional logging to localhost, pointing developers to the
Expand All @@ -56,16 +42,18 @@ export function register(config?: Config) {
});
} else {
// Is not localhost. Just register service worker
// eslint-disable-next-line @typescript-eslint/no-use-before-define
registerValidSW(swUrl, config);
}
});
}
}

function registerValidSW(swUrl: string, config?: Config) {
function registerValidSW(swUrl: string, config?: Config): void {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
.then((registration) => {
// eslint-disable-next-line no-param-reassign
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
Expand Down Expand Up @@ -101,25 +89,22 @@ function registerValidSW(swUrl: string, config?: Config) {
};
};
})
.catch(error => {
.catch((error) => {
console.error('Error during service worker registration:', error);
});
}

function checkValidServiceWorker(swUrl: string, config?: Config) {
function checkValidServiceWorker(swUrl: string, config?: Config): void {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl, {
headers: { 'Service-Worker': 'script' }
headers: { 'Service-Worker': 'script' },
})
.then(response => {
.then((response) => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
if (response.status === 404 || (contentType != null && contentType.indexOf('javascript') === -1)) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
navigator.serviceWorker.ready.then((registration) => {
registration.unregister().then(() => {
window.location.reload();
});
Expand All @@ -130,19 +115,17 @@ function checkValidServiceWorker(swUrl: string, config?: Config) {
}
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
);
console.log('No internet connection found. App is running in offline mode.');
});
}

export function unregister() {
export function unregister(): void {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready
.then(registration => {
.then((registration) => {
registration.unregister();
})
.catch(error => {
.catch((error) => {
console.error(error.message);
});
}
Expand Down
10 changes: 2 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
Expand All @@ -19,7 +15,5 @@
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
]
"include": ["src"]
}
Loading

0 comments on commit 275940c

Please sign in to comment.