frontend linting

This commit is contained in:
realaravinth
2021-10-08 15:24:29 +05:30
parent f7afc72d81
commit 53720ff740
91 changed files with 2158 additions and 1677 deletions

View File

@@ -14,13 +14,13 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {LEVELS} from '../levels/index';
import updateLevelNumbersOnDOM from './updateDom';
import CONST from '../const';
import { LEVELS } from "../levels/index";
import updateLevelNumbersOnDOM from "./updateDom";
import CONST from "../const";
import log from '../../../../../logger';
import log from "../../../../../logger";
const REMOVE_LEVEL_BUTTON = 'sitekey-form__level-remove-level-button';
const REMOVE_LEVEL_BUTTON = "sitekey-form__level-remove-level-button";
/**
* Gets executed when 'Remove' Button is clicked to remove levels
@@ -31,12 +31,12 @@ const removeLevel = (e: Event) => {
const FIELDSET = <HTMLElement>PARENT.parentElement;
const levelNum = parseInt(
eventTarget.id.slice(CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL.length),
eventTarget.id.slice(CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL.length)
);
if (Number.isNaN(levelNum)) {
const msg =
'[removeLevelButton.ts] error in parsing level number from remove button ID';
"[removeLevelButton.ts] error in parsing level number from remove button ID";
//log.error(msg);
throw new Error(msg);
}
@@ -47,19 +47,19 @@ const removeLevel = (e: Event) => {
};
/** adds onclick event listener */
export const addRemoveLevelButtonEventListener = (level: number) => {
export const addRemoveLevelButtonEventListener = (level: number): void => {
const removeButton = document.getElementById(
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${level}`,
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${level}`
);
removeButton.addEventListener('click', removeLevel);
removeButton.addEventListener("click", removeLevel);
};
/** adds onclick event listener to all remove buttons */
export const addRemoveLevelButtonEventListenerAll = () => {
export const addRemoveLevelButtonEventListenerAll = (): void => {
const removeButtons = document.querySelectorAll(`.${REMOVE_LEVEL_BUTTON}`);
removeButtons.forEach(button =>
button.addEventListener('click', removeLevel),
removeButtons.forEach((button) =>
button.addEventListener("click", removeLevel)
);
};
@@ -67,20 +67,20 @@ export const addRemoveLevelButtonEventListenerAll = () => {
* Generate Remove button HTML. On-click handler should be added
* seprately
*/
export const getRemoveButtonHTML = (level: number) => {
export const getRemoveButtonHTML = (level: number): HTMLLabelElement => {
log.log(`[generating HTML getHtml]level: ${level}`);
const btn = document.createElement('input');
const btn = document.createElement("input");
btn.className = CONST.REMOVE_LEVEL_BUTTON_CLASS;
btn.type = 'button';
btn.type = "button";
const id = `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${level}`;
btn.name = id;
btn.id = id;
btn.value = 'x';
btn.value = "x";
const removeLabel = document.createElement('label');
const removeLabel = document.createElement("label");
removeLabel.className = CONST.REMOVE_LEVEL_LABEL_CLASS;
const removeLabelText = document.createTextNode('RemoveLevel');
const removeLabelText = document.createTextNode("RemoveLevel");
removeLabel.appendChild(removeLabelText);
removeLabel.appendChild(btn);
removeLabel.htmlFor = id;

View File

@@ -15,17 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from '../levels/getNumLevels';
import {
getAddForm,
getRemoveButtonHTMLForm,
trim,
addLevel,
} from '../setupTests';
import CONST from '../const';
import getNumLevels from "../levels/getNumLevels";
import { getAddForm, addLevel } from "../setupTests";
import CONST from "../const";
import log from '../../../../../logger';
import {MODE} from '../../../../../logger';
import log from "../../../../../logger";
import { MODE } from "../../../../../logger";
document.body.innerHTML = getAddForm();
@@ -46,13 +41,13 @@ const setUp = () => {
log.setMode(MODE.none);
it('removeLevelButton works', () => {
it("removeLevelButton works", () => {
setUp();
for (let i = 1; i < 4; i++) {
const l1 = <HTMLButtonElement>(
document.getElementById(
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${1}`,
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${1}`
)
);

View File

@@ -14,25 +14,25 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from '../../levels/getNumLevels';
import CONST from '../../const';
import log from '../../../../../../logger';
import getNumLevels from "../../levels/getNumLevels";
import CONST from "../../const";
import log from "../../../../../../logger";
import updateLabels from './updateLabel';
import updateInputs from './updateInputs';
import updateRemoveButton from './updateRemoveButton';
import updateLevelGroup from './updateLevelGroup';
import updateLabels from "./updateLabel";
import updateInputs from "./updateInputs";
import updateRemoveButton from "./updateRemoveButton";
import updateLevelGroup from "./updateLevelGroup";
/**
* update level number on fieldset legends and their ids too
* @param {number} id - level number that was ordered to remove.
* All updates are made relative to id
* */
const updateLevelNumbersOnDOM = (id: number) => {
const updateLevelNumbersOnDOM = (id: number): void => {
const numLevels = getNumLevels();
if (id == numLevels) {
throw new Error(
"Can't remove the very fist element, it has to be first added to DOM",
"Can't remove the very fist element, it has to be first added to DOM"
);
}
@@ -42,7 +42,7 @@ const updateLevelNumbersOnDOM = (id: number) => {
const newLevel = i - 1;
const levelGroup = document.querySelector(
`#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${i}`,
`#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${i}`
);
if (levelGroup === null) {
@@ -53,9 +53,9 @@ const updateLevelNumbersOnDOM = (id: number) => {
}
// rename legend
const legend = levelGroup.getElementsByTagName('legend')[0];
const legend = levelGroup.getElementsByTagName("legend")[0];
const legendText = document.createTextNode(`Level ${newLevel}`);
const newLegend = document.createElement('legend');
const newLegend = document.createElement("legend");
newLegend.className = legend.className;
newLegend.appendChild(legendText);
legend.replaceWith(newLegend);

View File

@@ -15,12 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from '../../levels/getNumLevels';
import {getAddForm, addLevel} from '../../setupTests';
import getNumLevels from "../../levels/getNumLevels";
import { getAddForm, addLevel } from "../../setupTests";
document.body.innerHTML = getAddForm();
export const setupAddlevels = () => {
export const setupAddlevels = (): void => {
expect(getNumLevels()).toBe(1);
// add a level
addLevel(2, 2);

View File

@@ -15,20 +15,20 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {getAddForm, trim} from '../../setupTests';
import updateInputs from './updateInputs';
import CONST from '../../const';
import {getAddForm, trim} from "../../setupTests";
import updateInputs from "./updateInputs";
import CONST from "../../const";
import log from '../../../../../../logger';
import {MODE} from '../../../../../../logger';
import log from "../../../../../../logger";
import {MODE} from "../../../../../../logger";
import {setupAddlevels} from './setupTests';
import {setupAddlevels} from "./setupTests";
document.body.innerHTML = getAddForm();
log.setMode(MODE.none);
it('updateInputs works', () => {
it("updateInputs works", () => {
setupAddlevels();
// removing level 2
const level = 2;
@@ -58,7 +58,7 @@ it('updateInputs works', () => {
});
/** get initial form to test remove button functionality */
export const update = () => {
export const update = (): string => {
return `
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/add" method="post">
<h1 class="form__title">

View File

@@ -14,11 +14,11 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from '../../const';
import log from '../../../../../../logger';
import CONST from "../../const";
import log from "../../../../../../logger";
/** update input IDs with new level */
const updateInput = (levelGroup: Element, newLevel: number) => {
const updateInput = (levelGroup: Element, newLevel: number): void => {
const inputs = <NodeListOf<HTMLInputElement>>(
levelGroup.querySelectorAll(`.${CONST.LEVEL_INPUT_CLASS}`)
);
@@ -26,17 +26,17 @@ const updateInput = (levelGroup: Element, newLevel: number) => {
inputs.forEach(input => {
if (input.id.includes(CONST.VISITOR_WITHOUT_LEVEL)) {
log.log(`${input.id}`);
log.log('changing visitor_threshold input');
log.log("changing visitor_threshold input");
const id = `${CONST.VISITOR_WITHOUT_LEVEL}${newLevel}`;
input.id = id;
input.name = id;
} else if (input.id.includes(CONST.DIFFICULTY_WITHOUT_LEVEL)) {
log.log('changing difficulty input');
log.log("changing difficulty input");
const id = `${CONST.DIFFICULTY_WITHOUT_LEVEL}${newLevel}`;
input.id = id;
input.name = id;
} else {
if (input.id != 'add') {
if (input.id != "add") {
throw new Error(`Did you add an extra input to DOM? ${input.id} ${input.className} ${input.name}`);
}
}

View File

@@ -15,18 +15,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from '../../levels/getNumLevels';
import {getAddForm, trim} from '../../setupTests';
import updateLabels from './updateLabel';
import CONST from '../../const';
import { trim } from "../../setupTests";
import updateLabels from "./updateLabel";
import CONST from "../../const";
import log from '../../../../../../logger';
import {MODE} from '../../../../../../logger';
import {setupAddlevels} from './setupTests';
import log from "../../../../../../logger";
import { MODE } from "../../../../../../logger";
/** get initial form to test remove button functionality */
export const labelLevel = (level: number) => {
export const labelLevel = (level: number): string => {
return `
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/add" method="post">
<fieldset class="sitekey__level-container" id="level-group-2">
@@ -85,11 +82,11 @@ document.body.innerHTML = labelLevel(2);
log.setMode(MODE.none);
it('addLevelButton works', () => {
it("addLevelButton works", () => {
// removing level 2
const level = 2;
const levelGroup = document.querySelector(
`#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${level}`,
`#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${level}`
);
const newLevel = 20;
@@ -100,22 +97,22 @@ it('addLevelButton works', () => {
levelGroup.querySelectorAll(`.${CONST.LABEL_CLASS}`)
);
log.log(labels);
labels.forEach(label => {
labels.forEach((label) => {
log.log(`${label.htmlFor}`);
if (label.htmlFor.includes(CONST.VISITOR_WITHOUT_LEVEL)) {
expect(label.htmlFor).toBe(`${CONST.VISITOR_WITHOUT_LEVEL}${newLevel}`);
} else if (label.htmlFor.includes(CONST.DIFFICULTY_WITHOUT_LEVEL)) {
expect(label.htmlFor).toBe(
`${CONST.DIFFICULTY_WITHOUT_LEVEL}${newLevel}`,
`${CONST.DIFFICULTY_WITHOUT_LEVEL}${newLevel}`
);
} else if (
label.htmlFor.includes(CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL)
) {
expect(label.htmlFor).toBe(
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${newLevel}`,
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${newLevel}`
);
} else {
throw new Error('Did you add an extra label to DOM?');
throw new Error("Did you add an extra label to DOM?");
}
});

View File

@@ -14,17 +14,17 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from '../../const';
import log from '../../../../../../logger';
import CONST from "../../const";
import log from "../../../../../../logger";
/** update level lables to match new level */
const updateLabels = (levelGroup: Element, newLevel: number) => {
const updateLabels = (levelGroup: Element, newLevel: number): void => {
// rename labels
const labels = <NodeListOf<HTMLLabelElement>>(
levelGroup.querySelectorAll(`label`)
levelGroup.querySelectorAll("label")
);
log.log(labels);
labels.forEach(label => {
labels.forEach((label) => {
log.log(`${label.htmlFor}`);
const currentFor = label.htmlFor;
if (currentFor.includes(CONST.VISITOR_WITHOUT_LEVEL)) {
@@ -36,9 +36,9 @@ const updateLabels = (levelGroup: Element, newLevel: number) => {
) {
label.htmlFor = `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${newLevel}`;
} else {
if (currentFor != 'add') {
if (currentFor != "add") {
throw new Error(
`Did you add an extra label to DOM? Found label with for: ${currentFor}`,
`Did you add an extra label to DOM? Found label with for: ${currentFor}`
);
}
}

View File

@@ -15,18 +15,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from '../../levels/getNumLevels';
import {getAddForm, trim} from '../../setupTests';
import updateLevelGroup from './updateLevelGroup';
import CONST from '../../const';
import { trim} from "../../setupTests";
import updateLevelGroup from "./updateLevelGroup";
import CONST from "../../const";
import log from '../../../../../../logger';
import {MODE} from '../../../../../../logger';
import log from "../../../../../../logger";
import {MODE} from "../../../../../../logger";
import {setupAddlevels} from './setupTests';
/** get initial form to test remove button functionality */
export const labelLevel = (level: number) => {
export const labelLevel = (level: number): string => {
return `
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/add" method="post">
<fieldset class="sitekey__level-container" id="level-group-${level}">
@@ -85,7 +83,7 @@ document.body.innerHTML = labelLevel(2);
log.setMode(MODE.none);
it('update levelGroup works', () => {
it("update levelGroup works", () => {
// removing level 2
const level = 2;
const levelGroup = document.querySelector(

View File

@@ -14,10 +14,10 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from '../../const';
import CONST from "../../const";
/** update level grup to match new level */
const updateLevelGroup = (levelGroup: Element, newLevel: number) =>
const updateLevelGroup = (levelGroup: Element, newLevel: number): string =>
(levelGroup.id = `${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${newLevel}`);
export default updateLevelGroup;

View File

@@ -15,15 +15,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {trim} from '../../setupTests';
import updateRemoveButton from './updateRemoveButton';
import CONST from '../../const';
import {trim} from "../../setupTests";
import updateRemoveButton from "./updateRemoveButton";
import CONST from "../../const";
import log from '../../../../../../logger';
import {MODE} from '../../../../../../logger';
import log from "../../../../../../logger";
import {MODE} from "../../../../../../logger";
/** get initial form to test remove button functionality */
export const labelLevel = (level: number) => {
export const labelLevel = (level: number): string => {
return `
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/add" method="post">
<fieldset class="sitekey__level-container" id="level-group-">
@@ -83,7 +83,7 @@ document.body.innerHTML = labelLevel(level);
log.setMode(MODE.none);
it('update remove button works', () => {
it("update remove button works", () => {
// removing level 2
const levelGroup = document.getElementById(

View File

@@ -14,10 +14,10 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from '../../const';
import CONST from "../../const";
/** update remove level button's ID */
const updateRemoveButton = (levelGroup: Element, newLevel: number) => {
const updateRemoveButton = (levelGroup: Element, newLevel: number): void => {
// rename button
const button = <HTMLInputElement>(
levelGroup.querySelector(`.${CONST.REMOVE_LEVEL_BUTTON_CLASS}`)