From 9d86369877728314cb6b429b7d825290aedb8e01 Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 10 Feb 2022 22:02:17 +0100 Subject: [PATCH 01/10] Remove Airbnb config in favour of the default one --- .eslintignore | 3 +-- .eslintrc.json | 8 +++----- package.json | 2 -- 3 files changed, 4 insertions(+), 9 deletions(-) mode change 100644 => 120000 .eslintignore diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index 9f089b5..0000000 --- a/.eslintignore +++ /dev/null @@ -1,2 +0,0 @@ -dev -dist diff --git a/.eslintignore b/.eslintignore new file mode 120000 index 0000000..6200b3a --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +./.gitignore \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json index 1dcf5f8..3a67d20 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,13 +1,11 @@ { "root": true, "env": { - "browser": true, - "es6": true, - "node": true + "browser": true }, - "extends": ["airbnb-base", "prettier"], + "extends": ["eslint:recommended", "prettier"], "parserOptions": { - "ecmaVersion": 2020, + "ecmaVersion": "latest", "sourceType": "module" } } diff --git a/package.json b/package.json index 60f021f..ced7a4b 100644 --- a/package.json +++ b/package.json @@ -49,9 +49,7 @@ "autoprefixer": "^10.4.2", "cssnano": "^5.0.16", "eslint": "^8.8.0", - "eslint-config-airbnb-base": "^15.0.0", "eslint-config-prettier": "^8.3.0", - "eslint-plugin-import": "^2.25.4", "postcss": "^8.4.6", "postcss-banner": "^4.0.1", "postcss-calc": "^8.2.3", From db7c10048ffcc7cebc5018c08d04c634add6878a Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 10 Feb 2022 22:06:50 +0100 Subject: [PATCH 02/10] Add eslint-plugin-unicorn --- .eslintrc.json | 2 +- package.json | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index 3a67d20..dd0b2f8 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -3,7 +3,7 @@ "env": { "browser": true }, - "extends": ["eslint:recommended", "prettier"], + "extends": ["eslint:recommended", "plugin:unicorn/recommended", "prettier"], "parserOptions": { "ecmaVersion": "latest", "sourceType": "module" diff --git a/package.json b/package.json index ced7a4b..fc1ace4 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "cssnano": "^5.0.16", "eslint": "^8.8.0", "eslint-config-prettier": "^8.3.0", + "eslint-plugin-unicorn": "^40.1.0", "postcss": "^8.4.6", "postcss-banner": "^4.0.1", "postcss-calc": "^8.2.3", From bea94e545d0cd671193bf69a0b4678df87912ba9 Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 10 Feb 2022 22:16:26 +0100 Subject: [PATCH 03/10] Re-export shareon with `export ... from` --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 7439056..ef7818d 100644 --- a/src/index.js +++ b/src/index.js @@ -6,4 +6,4 @@ if (s && s.hasAttribute("init")) { initializeShareon(); } -export default initializeShareon; +export { default } from "./shareon"; From 9a6f39af4ad18bee7774e241a56cffa7505f8c7a Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 10 Feb 2022 22:19:03 +0100 Subject: [PATCH 04/10] Move button listener outside the loop --- src/shareon.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/shareon.js b/src/shareon.js index 668b0df..ef23e67 100644 --- a/src/shareon.js +++ b/src/shareon.js @@ -30,6 +30,10 @@ const urlBuilderMap = { whatsapp: (d) => `https://wa.me/?text=${d.title}%0D%0A${d.url}${d.text ? `%0D%0A%0D%0A${d.text}` : ''}`, }; +const openUrl = (buttonUrl) => () => { + window.open(buttonUrl, "_blank", "noopener,noreferrer"); +}; + const initializeShareon = () => { const shareonContainers = document.getElementsByClassName("shareon"); @@ -81,11 +85,7 @@ const initializeShareon = () => { child.setAttribute("rel", "noopener noreferrer"); child.setAttribute("target", "_blank"); } else { - const getButtonListener = (buttonUrl) => () => { - window.open(buttonUrl, "_blank", "noopener,noreferrer"); - }; - - child.addEventListener("click", getButtonListener(url)); + child.addEventListener("click", openUrl(url)); } break; // once a network is detected we don't want to check further From 51a75972350cbe3e8672d69f0fa50d9e909c2c2a Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 10 Feb 2022 22:19:54 +0100 Subject: [PATCH 05/10] Use querySelectorAll instead of getElementsByClassName --- src/shareon.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shareon.js b/src/shareon.js index ef23e67..f8b06fa 100644 --- a/src/shareon.js +++ b/src/shareon.js @@ -35,7 +35,7 @@ const openUrl = (buttonUrl) => () => { }; const initializeShareon = () => { - const shareonContainers = document.getElementsByClassName("shareon"); + const shareonContainers = document.querySelectorAll(".shareon"); // iterate over
for (let i = 0; i < shareonContainers.length; i += 1) { From 95cb4ce15853951281d41fbd81e1cc5d66d273da Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 10 Feb 2022 22:20:06 +0100 Subject: [PATCH 06/10] Rename index variables in loops --- src/shareon.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/shareon.js b/src/shareon.js index f8b06fa..4164053 100644 --- a/src/shareon.js +++ b/src/shareon.js @@ -38,14 +38,22 @@ const initializeShareon = () => { const shareonContainers = document.querySelectorAll(".shareon"); // iterate over
- for (let i = 0; i < shareonContainers.length; i += 1) { + for ( + let containerIndex = 0; + containerIndex < shareonContainers.length; + containerIndex += 1 + ) { /** @type Element */ - const container = shareonContainers[i]; + const container = shareonContainers[containerIndex]; // iterate over children of
- for (let j = 0; j < container.children.length; j += 1) { + for ( + let childIndex = 0; + childIndex < container.children.length; + childIndex += 1 + ) { /** @type Element */ - const child = container.children[j]; + const child = container.children[childIndex]; if (child) { const classListLength = child.classList.length; From 4bd28fa8aa0ed193629778c8ce89c965f0639b25 Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 10 Feb 2022 22:20:18 +0100 Subject: [PATCH 07/10] Lint all files --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fc1ace4..6aa828e 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "build:js": "vite build", "build:css": "postcss src/shareon.css -o dist/shareon.min.css --map", "dev": "vite", - "lint": "prettier --check . && eslint src/*.js", + "lint": "prettier --check . && eslint .", "size": "size-limit", "test": "pnpm run lint && pnpm run build && pnpm run size", "postversion": "pnpm run build" From dd3afd72b3be33d8f834d3109bb5a68476ec41b7 Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 10 Feb 2022 22:20:53 +0100 Subject: [PATCH 08/10] Use clearer name for package.json variable --- postcss.config.js | 4 ++-- vite.config.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/postcss.config.js b/postcss.config.js index 0b0d275..e53f920 100644 --- a/postcss.config.js +++ b/postcss.config.js @@ -1,5 +1,5 @@ -const pkg = require("./package.json"); -const bannerText = `${pkg.name} v${pkg.version}`; +const package_ = require("./package.json"); +const bannerText = `${package_.name} v${package_.version}`; module.exports = { map: { diff --git a/vite.config.js b/vite.config.js index da82255..9bdb10c 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,6 +1,6 @@ import { defineConfig } from "vite"; import * as path from "path"; -import pkg from "./package.json"; +import package_ from "./package.json"; export default defineConfig({ esbuild: { @@ -17,7 +17,7 @@ export default defineConfig({ }, rollupOptions: { output: { - banner: `/*! ${pkg.name} v${pkg.version} */`, + banner: `/*! ${package_.name} v${package_.version} */`, }, }, }, From e0e0f9bf63a7dfbf15caf63931a2e53556185a72 Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 10 Feb 2022 22:21:09 +0100 Subject: [PATCH 09/10] Use "node" protocol for import --- vite.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vite.config.js b/vite.config.js index 9bdb10c..e608fd4 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,5 +1,5 @@ import { defineConfig } from "vite"; -import * as path from "path"; +import * as path from "node:path"; import package_ from "./package.json"; export default defineConfig({ From 15ce773189cb2252457b5fa40bf5a8eaa6ffc008 Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Thu, 10 Feb 2022 22:21:17 +0100 Subject: [PATCH 10/10] Tweak ESLint config --- .eslintrc.json | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index dd0b2f8..3fd5a09 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,8 +4,26 @@ "browser": true }, "extends": ["eslint:recommended", "plugin:unicorn/recommended", "prettier"], + "rules": { + "unicorn/no-for-loop": 0 + }, "parserOptions": { "ecmaVersion": "latest", "sourceType": "module" - } + }, + "overrides": [ + { + "files": ["postcss.config.js", "vite.config.js"], + "env": { + "node": true, + "browser": false + } + }, + { + "files": ["postcss.config.js"], + "rules": { + "unicorn/prefer-module": 0 + } + } + ] }