Skip to content

Instantly share code, notes, and snippets.

@nakatanakatana
Created May 25, 2020 16:13
Show Gist options
  • Save nakatanakatana/26798721e24a573d7803648f8b477811 to your computer and use it in GitHub Desktop.
Save nakatanakatana/26798721e24a573d7803648f8b477811 to your computer and use it in GitHub Desktop.
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..3494874
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,10 @@
+root = true
+
+[*]
+end_of_line = lf
+insert_final_newline = true
+
+[*.{js,ts,jsx,tsx,json,md}]
+indent_style = space
+indent_size = 2
+
diff --git a/.eslintrc.js b/.eslintrc.js
new file mode 100644
index 0000000..081d167
--- /dev/null
+++ b/.eslintrc.js
@@ -0,0 +1,26 @@
+module.exports = {
+ env: {
+ browser: true,
+ es6: true,
+ node: true,
+ },
+ extends: [
+ "react-app",
+ "eslint:recommended",
+ "plugin:@typescript-eslint/eslint-recommended",
+ "plugin:@typescript-eslint/recommended",
+ ],
+ globals: {
+ Atomics: "readonly",
+ SharedArrayBuffer: "readonly",
+ },
+ parser: "@typescript-eslint/parser",
+ parserOptions: {
+ ecmaVersion: 11,
+ },
+ ignorePatterns: [],
+ plugins: ["@typescript-eslint", "prettier"],
+ rules: {
+ "prettier/prettier": "error",
+ },
+};
diff --git a/package.json b/package.json
index 6e34eaf..19d18b0 100644
--- a/package.json
+++ b/package.json
@@ -15,14 +15,21 @@
"react-scripts": "3.4.1",
"typescript": "^3.7.5"
},
+ "devDependencies": {
+ "eslint": "^6.6.0",
+ "eslint-plugin-import": "^2.20.2",
+ "eslint-plugin-node": "^11.1.0",
+ "eslint-plugin-prettier": "^3.1.3",
+ "eslint-plugin-promise": "^4.2.1",
+ "prettier": "^2.0.5"
+ },
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
- "eject": "react-scripts eject"
- },
- "eslintConfig": {
- "extends": "react-app"
+ "eject": "react-scripts eject",
+ "lint": "npm run lint:exec -- src/**/*.{ts,tsx,js,jsx}",
+ "lint:exec": "eslint"
},
"browserslist": {
"production": [
diff --git a/src/App.test.tsx b/src/App.test.tsx
index 4db7ebc..352d7b8 100644
--- a/src/App.test.tsx
+++ b/src/App.test.tsx
@@ -1,8 +1,8 @@
-import React from 'react';
-import { render } from '@testing-library/react';
-import App from './App';
+import React from "react";
+import { render } from "@testing-library/react";
+import App from "./App";
-test('renders learn react link', () => {
+test("renders learn react link", () => {
const { getByText } = render(<App />);
const linkElement = getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
diff --git a/src/App.tsx b/src/App.tsx
index a53698a..bc328b6 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,8 +1,8 @@
-import React from 'react';
-import logo from './logo.svg';
-import './App.css';
+import React from "react";
+import logo from "./logo.svg";
+import "./App.css";
-function App() {
+function App(): React.ReactElement {
return (
<div className="App">
<header className="App-header">
diff --git a/src/index.tsx b/src/index.tsx
index f5185c1..9710934 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -1,14 +1,14 @@
-import React from 'react';
-import ReactDOM from 'react-dom';
-import './index.css';
-import App from './App';
-import * as serviceWorker from './serviceWorker';
+import React from "react";
+import ReactDOM from "react-dom";
+import "./index.css";
+import App from "./App";
+import * as serviceWorker from "./serviceWorker";
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
- document.getElementById('root')
+ document.getElementById("root")
);
// If you want your app to work offline and load faster, you can change
diff --git a/src/serviceWorker.ts b/src/serviceWorker.ts
index b09523f..167f43a 100644
--- a/src/serviceWorker.ts
+++ b/src/serviceWorker.ts
@@ -11,9 +11,9 @@
// opt-in, read https://bit.ly/CRA-PWA
const isLocalhost = Boolean(
- window.location.hostname === 'localhost' ||
+ window.location.hostname === "localhost" ||
// [::1] is the IPv6 localhost address.
- window.location.hostname === '[::1]' ||
+ 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}$/
@@ -25,13 +25,10 @@ type Config = {
onUpdate?: (registration: ServiceWorkerRegistration) => void;
};
-export function register(config?: Config) {
- if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
+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
@@ -39,7 +36,7 @@ export function register(config?: Config) {
return;
}
- window.addEventListener('load', () => {
+ window.addEventListener("load", () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
if (isLocalhost) {
@@ -50,8 +47,8 @@ export function register(config?: Config) {
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
- 'This web app is being served cache-first by a service ' +
- 'worker. To learn more, visit https://bit.ly/CRA-PWA'
+ "This web app is being served cache-first by a service " +
+ "worker. To learn more, visit https://bit.ly/CRA-PWA"
);
});
} else {
@@ -62,24 +59,24 @@ export function register(config?: Config) {
}
}
-function registerValidSW(swUrl: string, config?: Config) {
+function registerValidSW(swUrl: string, config?: Config): void {
navigator.serviceWorker
.register(swUrl)
- .then(registration => {
- registration.onupdatefound = () => {
+ .then((registration) => {
+ registration.onupdatefound = (): void => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
- installingWorker.onstatechange = () => {
- if (installingWorker.state === 'installed') {
+ installingWorker.onstatechange = (): void => {
+ if (installingWorker.state === "installed") {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
- 'New content is available and will be used when all ' +
- 'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
+ "New content is available and will be used when all " +
+ "tabs for this page are closed. See https://bit.ly/CRA-PWA."
);
// Execute callback
@@ -90,7 +87,7 @@ function registerValidSW(swUrl: string, config?: Config) {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
- console.log('Content is cached for offline use.');
+ console.log("Content is cached for offline use.");
// Execute callback
if (config && config.onSuccess) {
@@ -101,25 +98,25 @@ function registerValidSW(swUrl: string, config?: Config) {
};
};
})
- .catch(error => {
- console.error('Error during service worker registration:', 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');
+ const contentType = response.headers.get("content-type");
if (
response.status === 404 ||
- (contentType != null && contentType.indexOf('javascript') === -1)
+ (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();
});
@@ -131,18 +128,18 @@ function checkValidServiceWorker(swUrl: string, config?: Config) {
})
.catch(() => {
console.log(
- 'No internet connection found. App is running in offline mode.'
+ "No internet connection found. App is running in offline mode."
);
});
}
-export function unregister() {
- if ('serviceWorker' in navigator) {
+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);
});
}
diff --git a/src/setupTests.ts b/src/setupTests.ts
index 74b1a27..5fdf001 100644
--- a/src/setupTests.ts
+++ b/src/setupTests.ts
@@ -2,4 +2,4 @@
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
-import '@testing-library/jest-dom/extend-expect';
+import "@testing-library/jest-dom/extend-expect";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment