Skip to content

Commit

Permalink
Add ESLint to project (#367)
Browse files Browse the repository at this point in the history
* Add ESLint to project

* Change name of JavaScript GitHub action and also break ESLint to test CI

* Unbreak ESLint

* Move eslint to precommit, add more rules

* alphabetize eslintrc rules

Co-authored-by: Sorin Sbarnea <[email protected]>
  • Loading branch information
gnikonorov and ssbarnea authored Nov 12, 2020
1 parent 03040fe commit 0e2c045
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 70 deletions.
63 changes: 63 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 12
},
"rules": {
"array-bracket-spacing": "error",
"block-scoped-var": "error",
"block-spacing": "error",
"brace-style": "error",
"class-methods-use-this": "error",
"consistent-return": "error",
"default-case": "error",
"default-case-last": "error",
"default-param-last": "error",
"grouped-accessor-pairs": "error",
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"no-caller": "error",
"no-console": "error",
"no-empty-function": "error",
"no-eval": "error",
"no-extra-parens": "error",
"no-labels": "error",
"no-new": "error",
"no-new-func": "error",
"no-new-wrappers": "error",
"no-return-await": "error",
"no-script-url": "error",
"no-self-compare": "error",
"no-shadow": "error",
"no-throw-literal": "error",
"no-undefined": "error",
"no-unreachable-loop": "error",
"no-unused-expressions": "error",
"no-useless-backreference": "error",
"no-useless-concat": "error",
"no-var": "error",
"prefer-const": "error",
"prefer-promise-reject-errors": "error",
"require-atomic-updates": "error",
"require-await": "error",
"semi": [
"error",
"always"
],
"quotes": [
"error",
"single"
],
"yoda": "error"
}
}
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ repos:
hooks:
- id: pyupgrade
args: [--py3-plus]
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v7.13.0
hooks:
- id: eslint
additional_dependencies:
- [email protected]
args: [pytest_html]
- repo: local
hooks:
- id: rst
Expand Down
139 changes: 69 additions & 70 deletions pytest_html/resources/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function toArray(iter) {
return Array.prototype.slice.call(iter);
}

function find(selector, elem) {
function find(selector, elem) { // eslint-disable-line no-redeclare
if (!elem) {
elem = document;
}
Expand All @@ -26,8 +26,8 @@ function find_all(selector, elem) {

function sort_column(elem) {
toggle_sort_states(elem);
var colIndex = toArray(elem.parentNode.childNodes).indexOf(elem);
var key;
const colIndex = toArray(elem.parentNode.childNodes).indexOf(elem);
let key;
if (elem.classList.contains('numeric')) {
key = key_num;
} else if (elem.classList.contains('result')) {
Expand All @@ -40,75 +40,75 @@ function sort_column(elem) {
sort_table(elem, key(colIndex));
}

function show_all_extras() {
function show_all_extras() { // eslint-disable-line no-unused-vars
find_all('.col-result').forEach(show_extras);
}

function hide_all_extras() {
function hide_all_extras() { // eslint-disable-line no-unused-vars
find_all('.col-result').forEach(hide_extras);
}

function show_extras(colresult_elem) {
var extras = colresult_elem.parentNode.nextElementSibling;
var expandcollapse = colresult_elem.firstElementChild;
extras.classList.remove("collapsed");
expandcollapse.classList.remove("expander");
expandcollapse.classList.add("collapser");
const extras = colresult_elem.parentNode.nextElementSibling;
const expandcollapse = colresult_elem.firstElementChild;
extras.classList.remove('collapsed');
expandcollapse.classList.remove('expander');
expandcollapse.classList.add('collapser');
}

function hide_extras(colresult_elem) {
var extras = colresult_elem.parentNode.nextElementSibling;
var expandcollapse = colresult_elem.firstElementChild;
extras.classList.add("collapsed");
expandcollapse.classList.remove("collapser");
expandcollapse.classList.add("expander");
const extras = colresult_elem.parentNode.nextElementSibling;
const expandcollapse = colresult_elem.firstElementChild;
extras.classList.add('collapsed');
expandcollapse.classList.remove('collapser');
expandcollapse.classList.add('expander');
}

function show_filters() {
var filter_items = document.getElementsByClassName('filter');
for (var i = 0; i < filter_items.length; i++)
const filter_items = document.getElementsByClassName('filter');
for (let i = 0; i < filter_items.length; i++)
filter_items[i].hidden = false;
}

function add_collapse() {
// Add links for show/hide all
var resulttable = find('table#results-table');
var showhideall = document.createElement("p");
const resulttable = find('table#results-table');
const showhideall = document.createElement('p');
showhideall.innerHTML = '<a href="javascript:show_all_extras()">Show all details</a> / ' +
'<a href="javascript:hide_all_extras()">Hide all details</a>';
resulttable.parentElement.insertBefore(showhideall, resulttable);

// Add show/hide link to each result
find_all('.col-result').forEach(function(elem) {
var collapsed = get_query_parameter('collapsed') || 'Passed';
var extras = elem.parentNode.nextElementSibling;
var expandcollapse = document.createElement("span");
if (extras.classList.contains("collapsed")) {
expandcollapse.classList.add("expander")
const collapsed = get_query_parameter('collapsed') || 'Passed';
const extras = elem.parentNode.nextElementSibling;
const expandcollapse = document.createElement('span');
if (extras.classList.contains('collapsed')) {
expandcollapse.classList.add('expander');
} else if (collapsed.includes(elem.innerHTML)) {
extras.classList.add("collapsed");
expandcollapse.classList.add("expander");
extras.classList.add('collapsed');
expandcollapse.classList.add('expander');
} else {
expandcollapse.classList.add("collapser");
expandcollapse.classList.add('collapser');
}
elem.appendChild(expandcollapse);

elem.addEventListener("click", function(event) {
if (event.currentTarget.parentNode.nextElementSibling.classList.contains("collapsed")) {
elem.addEventListener('click', function(event) {
if (event.currentTarget.parentNode.nextElementSibling.classList.contains('collapsed')) {
show_extras(event.currentTarget);
} else {
hide_extras(event.currentTarget);
}
});
})
});
}

function get_query_parameter(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
const match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

function init () {
function init () { // eslint-disable-line no-unused-vars
reset_sort_headers();

add_collapse();
Expand All @@ -118,52 +118,51 @@ function init () {
sort_column(find('.initial-sort'));

find_all('.sortable').forEach(function(elem) {
elem.addEventListener("click",
function(event) {
sort_column(elem);
}, false)
elem.addEventListener('click',
function() {
sort_column(elem);
}, false);
});

};
}

function sort_table(clicked, key_func) {
var rows = find_all('.results-table-row');
var reversed = !clicked.classList.contains('asc');
var sorted_rows = sort(rows, key_func, reversed);
const rows = find_all('.results-table-row');
const reversed = !clicked.classList.contains('asc');
const sorted_rows = sort(rows, key_func, reversed);
/* Whole table is removed here because browsers acts much slower
* when appending existing elements.
*/
var thead = document.getElementById("results-table-head");
const thead = document.getElementById('results-table-head');
document.getElementById('results-table').remove();
var parent = document.createElement("table");
parent.id = "results-table";
const parent = document.createElement('table');
parent.id = 'results-table';
parent.appendChild(thead);
sorted_rows.forEach(function(elem) {
parent.appendChild(elem);
});
document.getElementsByTagName("BODY")[0].appendChild(parent);
document.getElementsByTagName('BODY')[0].appendChild(parent);
}

function sort(items, key_func, reversed) {
var sort_array = items.map(function(item, i) {
const sort_array = items.map(function(item, i) {
return [key_func(item), i];
});

sort_array.sort(function(a, b) {
var key_a = a[0];
var key_b = b[0];
const key_a = a[0];
const key_b = b[0];

if (key_a == key_b) return 0;

if (reversed) {
return (key_a < key_b ? 1 : -1);
return key_a < key_b ? 1 : -1;
} else {
return (key_a > key_b ? 1 : -1);
return key_a > key_b ? 1 : -1;
}
});

return sort_array.map(function(item) {
var index = item[1];
const index = item[1];
return items[index];
});
}
Expand All @@ -182,15 +181,15 @@ function key_num(col_index) {

function key_link(col_index) {
return function(elem) {
dataCell = elem.childNodes[1].childNodes[col_index].firstChild
return dataCell == null ? "" : dataCell.innerText.toLowerCase();
const dataCell = elem.childNodes[1].childNodes[col_index].firstChild;
return dataCell == null ? '' : dataCell.innerText.toLowerCase();
};
}

function key_result(col_index) {
return function(elem) {
var strings = ['Error', 'Failed', 'Rerun', 'XFailed', 'XPassed',
'Skipped', 'Passed'];
const strings = ['Error', 'Failed', 'Rerun', 'XFailed', 'XPassed',
'Skipped', 'Passed'];
return strings.indexOf(elem.childNodes[1].childNodes[col_index].firstChild.data);
};
}
Expand All @@ -200,12 +199,12 @@ function reset_sort_headers() {
elem.parentNode.removeChild(elem);
});
find_all('.sortable').forEach(function(elem) {
var icon = document.createElement("div");
icon.className = "sort-icon";
icon.textContent = "vvv";
const icon = document.createElement('div');
icon.className = 'sort-icon';
icon.textContent = 'vvv';
elem.insertBefore(icon, elem.firstChild);
elem.classList.remove("desc", "active");
elem.classList.add("asc", "inactive");
elem.classList.remove('desc', 'active');
elem.classList.add('asc', 'inactive');
});
}

Expand All @@ -225,21 +224,21 @@ function toggle_sort_states(elem) {
}

function is_all_rows_hidden(value) {
return value.hidden == false;
return value.hidden == false;
}

function filter_table(elem) {
var outcome_att = "data-test-result";
var outcome = elem.getAttribute(outcome_att);
class_outcome = outcome + " results-table-row";
var outcome_rows = document.getElementsByClassName(class_outcome);
function filter_table(elem) { // eslint-disable-line no-unused-vars
const outcome_att = 'data-test-result';
const outcome = elem.getAttribute(outcome_att);
const class_outcome = outcome + ' results-table-row';
const outcome_rows = document.getElementsByClassName(class_outcome);

for(var i = 0; i < outcome_rows.length; i++){
for(let i = 0; i < outcome_rows.length; i++){
outcome_rows[i].hidden = !elem.checked;
}

var rows = find_all('.results-table-row').filter(is_all_rows_hidden);
var all_rows_hidden = rows.length == 0 ? true : false;
var not_found_message = document.getElementById("not-found-message");
const rows = find_all('.results-table-row').filter(is_all_rows_hidden);
const all_rows_hidden = rows.length == 0 ? true : false;
const not_found_message = document.getElementById('not-found-message');
not_found_message.hidden = !all_rows_hidden;
}

0 comments on commit 0e2c045

Please sign in to comment.