Feature - added in eslint and prettier to control code quality and format of code.

This commit is contained in:
david 2023-01-24 13:57:18 -06:00
parent 88e1d32920
commit c7187a92fe
10 changed files with 4151 additions and 1684 deletions

34
.editorconfig Normal file
View File

@ -0,0 +1,34 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org
root = true
[*]
# change these settings to your own preference
indent_style = space
indent_size = 4
# keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.{markdown,md}]
trim_trailing_whitespace = false
[{package,bower}.json]
indent_style = space
indent_size = 4
# Force JavaScript settings to match ESLint rules
[*.{js,jsx}]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

31
.eslintrc Normal file
View File

@ -0,0 +1,31 @@
{
"extends": "standard",
"parserOptions": {
"ecmaFeatures": {
"jsx": false
},
"ecmaVersion": 2022,
"sourceType": "module"
},
"rules": {
"indent": "off",
"no-throw-literal": "off",
"no-undef": "off",
"new-cap": "off",
"space-before-function-paren": [
"error",
{
"anonymous": "always",
"named": "never",
"asyncArrow": "ignore"
}
],
"no-multiple-empty-lines": [
"error",
{
"max": 2
}
]
}
}

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules/*
.idea/*

23
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,23 @@
default:
image: node:18-alpine
eslint:
script:
- npm ci
- npx eslint --format gitlab wayfarer-exporter.user.js
- npx eslint --format gitlab wayfarer-planner.user.js
- npx eslint --format gitlab wayfarer-translate.user.js
artifacts:
reports:
codequality: gl-codequality.json
prettier:
script:
- npm ci
- npx prettier --check wayfarer-translate.user.js
- npx prettier --check wayfarer-planner.user.js
- npx prettier --check wayfarer-exporter.user.js
artifacts:
reports:
codequality: gl-codequality.json

20
.prettierrc.json Normal file
View File

@ -0,0 +1,20 @@
{
"arrowParens": "always",
"bracketSameLine": true,
"bracketSpacing": true,
"embeddedLanguageFormatting": "auto",
"endOfLine": "lf",
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxSingleQuote": false,
"printWidth": 80,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": false,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "none",
"useTabs": false,
"vueIndentScriptAndStyle": false
}

2176
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

10
package.json Normal file
View File

@ -0,0 +1,10 @@
{
"dependencies": {
"eslint-config-standard": "^17.0.0",
"prettier": "^2.8.3"
},
"devDependencies": {
"eslint": "^8.32.0",
"eslint-formatter-gitlab": "^4.0.0"
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -12,151 +12,178 @@
/* eslint no-var: "error" */
function init() {
let tryNumber = 15;
let tryNumber = 15
let translateButton;
let candidate;
let translateButton
let candidate
const SPACING = '\r\n\r\n';
const SPACING = '\r\n\r\n'
let engine = localStorage['translate-engine'];
let engine = localStorage['translate-engine']
if (!engine) {
engine = 'Google';
localStorage['translate-engine'] = engine;
engine = 'Google'
localStorage['translate-engine'] = engine
}
/**
* Overwrite the open method of the XMLHttpRequest.prototype to intercept the server calls
*/
(function (open) {
;(function (open) {
XMLHttpRequest.prototype.open = function (method, url) {
if (url == '/api/v1/vault/review') {
if (method == 'GET') {
this.addEventListener('load', parseCandidate, false);
if (url === '/api/v1/vault/review') {
if (method === 'GET') {
this.addEventListener('load', parseCandidate, false)
}
if (method == 'POST') {
hideButton();
if (method === 'POST') {
hideButton()
}
}
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
open.apply(this, arguments)
}
})(XMLHttpRequest.prototype.open)
addCss();
addCss()
function parseCandidate(e) {
try {
const response = this.response;
const json = JSON.parse(response);
const response = this.response
const json = JSON.parse(response)
if (!json) {
console.log(response);
alert('Failed to parse response from Wayfarer');
return;
console.log(response)
alert('Failed to parse response from Wayfarer')
return
}
// ignore if it's related to captchas
if (json.captcha)
return;
if (json.captcha) {
return
}
if (json.code != 'OK')
return;
if (json.code !== 'OK') {
return
}
candidate = json.result;
candidate = json.result
if (!candidate) {
console.log(json);
alert('Wayfarer\'s response didn\'t include a candidate.');
return;
console.log(json)
alert("Wayfarer's response didn't include a candidate.")
return
}
addTranslateButton();
addTranslateButton()
} catch (e) {
console.log(e); // eslint-disable-line no-console
console.log(e) // eslint-disable-line no-console
}
}
function getTranslatorLink() {
switch (engine) {
case 'Google':
return 'https://translate.google.com/?sl=auto&q=';
return 'https://translate.google.com/?sl=auto&q='
default:
return 'https://www.deepl.com/translator#auto/' + navigator.language + '/';
return (
'https://www.deepl.com/translator#auto/' +
navigator.language +
'/'
)
}
}
function createButton(ref) {
if (!translateButton) {
const div = document.createElement('div');
div.className = 'wayfarertranslate';
const link = document.createElement('a');
link.className = '';
link.title = 'Translate nomination';
link.innerHTML = '<svg viewBox="0 0 24 24"><path d="M12.87 15.07l-2.54-2.51.03-.03A17.52 17.52 0 0014.07 6H17V4h-7V2H8v2H1v2h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04M18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12m-2.62 7l1.62-4.33L19.12 17h-3.24z"/></svg>';
link.target = '_blank';
const div = document.createElement('div')
div.className = 'wayfarertranslate'
const link = document.createElement('a')
link.className = ''
link.title = 'Translate nomination'
link.innerHTML =
'<svg viewBox="0 0 24 24"><path d="M12.87 15.07l-2.54-2.51.03-.03A17.52 17.52 0 0014.07 6H17V4h-7V2H8v2H1v2h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04M18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12m-2.62 7l1.62-4.33L19.12 17h-3.24z"/></svg>'
link.target = '_blank'
const select = document.createElement('select');
select.title = 'Select translation engine';
const select = document.createElement('select')
select.title = 'Select translation engine'
const engines = [
{name: 'Google', title: 'Google Translate'},
{name: 'DeepL', title: 'DeepL Translate'}
];
{ name: 'Google', title: 'Google Translate' },
{ name: 'DeepL', title: 'DeepL Translate' }
]
select.innerHTML = engines.map(item => `<option value="${item.name}" ${item.name == engine ? 'selected' : ''}>${item.title}</option>`).join('');
select.innerHTML = engines
.map(
(item) =>
`<option value="${item.name}" ${
item.name === engine ? 'selected' : ''
}>${item.title}</option>`
)
.join('')
select.addEventListener('change', function () {
engine = select.value;
localStorage['translate-engine'] = engine;
link.href = getTranslatorLink() + encodeURIComponent(link.dataset.text);
});
engine = select.value
localStorage['translate-engine'] = engine
link.href =
getTranslatorLink() + encodeURIComponent(link.dataset.text)
})
div.appendChild(link);
div.appendChild(select);
translateButton = div;
div.appendChild(link)
div.appendChild(select)
translateButton = div
}
const container = ref.parentNode.parentNode;
if (!container.contains(translateButton))
container.appendChild(translateButton);
const container = ref.parentNode.parentNode
if (!container.contains(translateButton)) {
container.appendChild(translateButton)
}
}
function addTranslateButton() {
const ref = document.querySelector('wf-logo');
const ref = document.querySelector('wf-logo')
if (!ref) {
if (tryNumber === 0) {
document.querySelector('body')
.insertAdjacentHTML('afterBegin', '<div class="alert alert-danger"><strong><span class="glyphicon glyphicon-remove"></span> Wayfarer Translate initialization failed, refresh page</strong></div>');
return;
document
.querySelector('body')
.insertAdjacentHTML(
'afterBegin',
'<div class="alert alert-danger"><strong><span class="glyphicon glyphicon-remove"></span> Wayfarer Translate initialization failed, refresh page</strong></div>'
)
return
}
setTimeout(addTranslateButton, 1000);
tryNumber--;
return;
setTimeout(addTranslateButton, 1000)
tryNumber--
return
}
let text = '';
if (candidate.type == 'NEW') {
text = candidate.title + SPACING + candidate.description + SPACING + candidate.statement;
let text = ''
if (candidate.type === 'NEW') {
text =
candidate.title +
SPACING +
candidate.description +
SPACING +
candidate.statement
}
if (candidate.type == 'EDIT') {
const title = candidate.title || candidate.titleEdits.map(d=>d.value).join(SPACING);
const description = candidate.description || candidate.descriptionEdits.map(d=>d.value).join(SPACING);
text = title + SPACING + SPACING + description;
if (candidate.type === 'EDIT') {
const title =
candidate.title ||
candidate.titleEdits.map((d) => d.value).join(SPACING)
const description =
candidate.description ||
candidate.descriptionEdits.map((d) => d.value).join(SPACING)
text = title + SPACING + SPACING + description
}
if (candidate.type == 'PHOTO') {
text = candidate.title + SPACING + candidate.description;
if (candidate.type === 'PHOTO') {
text = candidate.title + SPACING + candidate.description
}
if (text != '') {
createButton(ref);
const link = translateButton.querySelector('a');
link.dataset.text = text;
link.href = getTranslatorLink() + encodeURIComponent(text);
translateButton.classList.add('wayfarertranslate__visible');
if (text !== '') {
createButton(ref)
const link = translateButton.querySelector('a')
link.dataset.text = text
link.href = getTranslatorLink() + encodeURIComponent(text)
translateButton.classList.add('wayfarertranslate__visible')
}
}
function hideButton() {
translateButton.classList.remove('wayfarertranslate__visible');
translateButton.classList.remove('wayfarertranslate__visible')
}
function addCss() {
@ -190,14 +217,12 @@ function init() {
.dark .wayfarertranslate option {
background: #000;
}
`;
const style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
document.querySelector('head').appendChild(style);
`
const style = document.createElement('style')
style.type = 'text/css'
style.innerHTML = css
document.querySelector('head').appendChild(style)
}
}
init();
init()