Skip to content

Commit

Permalink
Updated ESLint to v9.12.0 & migrated to a flat config file (#4162)
Browse files Browse the repository at this point in the history
In some of the translation features I was using optional chaining &
eslint would toss errors saying it was invalid despite it working. This
led me to exploring the config to find the [yaml config we used was
depreciated](https://eslint.org/docs/latest/use/configure/configuration-files-deprecated).
This PR updates to a newer version of eslint & migrates the config to
the [flat file
version](https://eslint.org/docs/latest/use/configure/configuration-files#configuration-file)
(and now optional chaining is recognized 🥳).

Some code changes that came with the updated linting:
- [As of
ES6](https://262.ecma-international.org/6.0/#sec-strict-mode-code)
modules are always in strict mode, thus `"use strict"` isn't needed
- Removed unused var
- Removed unneeded `eslint-disable` comment

There are also a ton of JSDoc warnings now - I was going to remove them
but I think JSDoc is actually a weak point in our code documentation so
maybe the in-terminal annoyances will be a good reminder to do a quick
write up
  • Loading branch information
wes-otf authored Oct 15, 2024
1 parent e5bc532 commit 5e72bb4
Show file tree
Hide file tree
Showing 29 changed files with 657 additions and 359 deletions.
132 changes: 0 additions & 132 deletions .eslintrc.yaml

This file was deleted.

155 changes: 155 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import globals from "globals";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import jsdoc from "eslint-plugin-jsdoc";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all,
});

export default [
{
ignores: ["**/*.min.js", "**/esm/*.js"],
},
...compat.extends("eslint:recommended", "prettier"),
jsdoc.configs["flat/recommended"],
{
languageOptions: {
globals: {
...globals.browser,
...globals.commonjs,
jQuery: true,
},
},

plugins: {
jsdoc,
},

rules: {
"array-bracket-spacing": ["error", "never"],
"block-scoped-var": "error",
"comma-spacing": "error",
"comma-style": ["error", "last"],
"computed-property-spacing": ["error", "never"],
curly: ["error", "all"],
"eol-last": "error",
eqeqeq: ["error", "smart"],
"guard-for-in": "error",

"key-spacing": [
"error",
{
beforeColon: false,
afterColon: true,
},
],

"keyword-spacing": [
"error",
{
before: true,
after: true,
},
],

"linebreak-style": ["error", "unix"],

"lines-around-comment": [
"error",
{
beforeBlockComment: true,
afterBlockComment: false,
},
],

"new-parens": "error",
"no-array-constructor": "error",
"no-caller": "error",
"no-catch-shadow": "error",
"no-eval": "error",
"no-extend-native": "error",
"no-extra-bind": "error",
"no-extra-parens": ["error", "functions"],
"no-implied-eval": "error",
"no-iterator": "error",
"no-label-var": "error",
"no-labels": "error",
"no-lone-blocks": "error",
"no-loop-func": "error",
"no-multi-spaces": "error",
"no-multi-str": "error",
"no-native-reassign": "error",
"no-nested-ternary": "error",
"no-new-func": "error",
"no-new-object": "error",
"no-new-wrappers": "error",
"no-octal-escape": "error",
"no-process-exit": "error",
"no-proto": "error",
"no-return-assign": "error",
"no-script-url": "error",
"no-sequences": "error",
"no-shadow-restricted-names": "error",
"no-spaced-func": "error",
"no-trailing-spaces": "error",
"no-undef-init": "error",
"no-undefined": "error",
"no-unused-expressions": "error",

"no-unused-vars": [
"error",
{
vars: "all",
args: "none",
},
],

"no-with": "error",
"one-var": ["error", "never"],
semi: ["error", "always"],

"semi-spacing": [
"error",
{
before: false,
after: true,
},
],

"space-before-blocks": ["error", "always"],

"space-before-function-paren": [
"error",
{
anonymous: "always",
named: "never",
},
],

"space-in-parens": ["error", "never"],
"space-infix-ops": "error",

"space-unary-ops": [
"error",
{
words: true,
nonwords: false,
},
],

"spaced-comment": ["error", "always"],
strict: ["error", "function"],
yoda: ["error", "never"],
"max-nested-callbacks": ["warn", 3],
"jsdoc/require-description": "warn",
"jsdoc/tag-lines": ["off", "never"],
},
},
];
36 changes: 31 additions & 5 deletions hypha/cookieconsent/static/js/cookieconsent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
(function () {
"use strict";

// Used when an analytics cookie notice is enabled
const ACCEPT = "accept";
const DECLINE = "decline";
Expand All @@ -19,7 +17,9 @@
const CLASS_JS_LEARNMORE_EXPAND = `${CLASS_JS_LEARNMORE}-expand`;

const cookieconsent = document.querySelector(`.${CLASS_COOKIECONSENT}`);
if (!cookieconsent) return;
if (!cookieconsent) {
return;
}

const cookieButtons = cookieconsent.querySelectorAll(
"button[data-consent]"
Expand All @@ -28,10 +28,20 @@
".button--learn-more"
);

/**
* Pull the cookie consent value from localStorage
*
* @returns {(string|null)} A string if consent value exists, null if not
*/
function getConsentValue() {
return localStorage.getItem(COOKIECONSENT_KEY);
}

/**
* Set the cookie consent value in localStorage
*
* @param {("accept"|"decline"|"ack")} value - consent value to set
*/
function setConsentValue(value) {
if ([ACCEPT, DECLINE, ACK].includes(value)) {
localStorage.setItem(COOKIECONSENT_KEY, value);
Expand All @@ -41,10 +51,16 @@
}
}

/**
* Trigger the cookie consent prompt to open
*/
function openConsentPrompt() {
cookieconsent.classList.add(CLASS_JS_CONSENT_OPEN);
}

/**
* Trigger cookie consent prompt to close
*/
function closeConsentPrompt() {
cookieconsent.classList.remove(CLASS_JS_CONSENT_OPEN);
}
Expand All @@ -53,6 +69,11 @@
window.openConsentPrompt = openConsentPrompt;
window.closeConsentPrompt = closeConsentPrompt;

/**
* Trigger the "Learn More" prompt on the cookie banner to open/close
*
* @param {boolean} open - true to open learn more prompt, false to close
*/
function toggleLearnMore(open) {
const content = cookieconsent.querySelector(`.${CLASS_COOKIECONTENT}`);
if (open) {
Expand All @@ -66,7 +87,12 @@
setInputTabIndex(`.${CLASS_COOKIEBRIEF}`, open ? -1 : 0);
}

// Adds "tabability" to menu buttons/toggles
/**
* Adds "tabability" to menu buttons/toggles
*
* @param {string} wrapperClassSelector - wrapper class to set tabability of inputs on
* @param {number} tabValue - tab index value to set on all input items in the wrapper class
*/
function setInputTabIndex(wrapperClassSelector, tabValue) {
const wrapper = cookieconsent.querySelector(wrapperClassSelector);
const tabables = wrapper.querySelectorAll("button, input");
Expand All @@ -75,7 +101,7 @@

// Open the prompt if consent value is undefined OR if analytics has been added since the user ack'd essential cookies
if (
getConsentValue() == undefined ||
getConsentValue() == null ||
(getConsentValue() === ACK && cookieButtons.length > 2)
) {
openConsentPrompt();
Expand Down
2 changes: 0 additions & 2 deletions hypha/static_src/javascript/all-reviews-table.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
(function ($) {
"use strict";

// Allow click and drag scrolling within reviews table wrapper
$(".js-reviews-table").attachDragger();

Expand Down
2 changes: 0 additions & 2 deletions hypha/static_src/javascript/all-submissions-table.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
(function () {
"use strict";

let submission_arrow = document.createElement("span");
submission_arrow.classList.add("arrow");

Expand Down
Loading

0 comments on commit 5e72bb4

Please sign in to comment.