Skip to content

Commit a159c31

Browse files
silverwindzeripathtechknowlogickwxiaoguang
authored
Add new JS linter rules (#17699)
* Add new JS linter rules Adds a few useful rules from eslint-plugin-github. Notable changes: - Forbid dataset usage, its camel-casing behaviour makes it hard to grep for attributes. - Forbid .then() and .catch(), we should generally prefer await for new code. For rare cases where they are useful, a eslint-disable-line directive can be set. - Add docs js to linting * also enable github/array-foreach * small tweak Co-authored-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 7743f13 commit a159c31

23 files changed

+845
-109
lines changed

.eslintrc

+18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ plugins:
1313
- eslint-plugin-import
1414
- eslint-plugin-vue
1515
- eslint-plugin-html
16+
- eslint-plugin-github
1617

1718
extends:
1819
- plugin:vue/recommended
@@ -96,6 +97,23 @@ rules:
9697
function-paren-newline: [0]
9798
generator-star-spacing: [0]
9899
getter-return: [2]
100+
github/array-foreach: [2]
101+
github/async-currenttarget: [2]
102+
github/async-preventdefault: [2]
103+
github/authenticity-token: [0]
104+
github/get-attribute: [2]
105+
github/js-class-name: [0]
106+
github/no-blur: [0]
107+
github/no-d-none: [0]
108+
github/no-dataset: [2]
109+
github/no-implicit-buggy-globals: [0]
110+
github/no-inner-html: [0]
111+
github/no-innerText: [2]
112+
github/no-then: [2]
113+
github/no-useless-passive: [2]
114+
github/prefer-observers: [0]
115+
github/require-passive-events: [2]
116+
github/unescaped-html-literal: [0]
99117
grouped-accessor-pairs: [2]
100118
guard-for-in: [0]
101119
id-blacklist: [0]

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ lint: lint-frontend lint-backend
328328

329329
.PHONY: lint-frontend
330330
lint-frontend: node_modules
331-
npx eslint --color --max-warnings=0 web_src/js build templates *.config.js
331+
npx eslint --color --max-warnings=0 web_src/js build templates *.config.js docs/assets/js
332332
npx stylelint --color --max-warnings=0 web_src/less
333333
npx editorconfig-checker templates
334334

build/generate-images.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ async function main() {
8080
]);
8181
}
8282

83-
main().then(exit).catch(exit);
83+
main().then(exit).catch(exit); // eslint-disable-line github/no-then
8484

build/generate-svg.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ async function main() {
5454
]);
5555
}
5656

57-
main().then(exit).catch(exit);
57+
main().then(exit).catch(exit); // eslint-disable-line github/no-then
5858

docs/assets/js/search.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const fuseOptions = {
1515
shouldSort: true,
1616
includeMatches: true,
1717
matchAllTokens: true,
18-
threshold: 0.0, // for parsing diacritics
18+
threshold: 0, // for parsing diacritics
1919
tokenize: true,
2020
location: 0,
2121
distance: 100,
@@ -52,25 +52,25 @@ function doSearch() {
5252
executeSearch(searchQuery);
5353
} else {
5454
const para = document.createElement('P');
55-
para.innerText = 'Please enter a word or phrase above';
55+
para.textContent = 'Please enter a word or phrase above';
5656
document.getElementById('search-results').appendChild(para);
5757
}
5858
}
5959

6060
function getJSON(url, fn) {
6161
const request = new XMLHttpRequest();
6262
request.open('GET', url, true);
63-
request.onload = function () {
63+
request.addEventListener('load', () => {
6464
if (request.status >= 200 && request.status < 400) {
6565
const data = JSON.parse(request.responseText);
6666
fn(data);
6767
} else {
6868
console.error(`Target reached on ${url} with error ${request.status}`);
6969
}
70-
};
71-
request.onerror = function () {
70+
});
71+
request.addEventListener('error', () => {
7272
console.error(`Connection error ${request.status}`);
73-
};
73+
});
7474
request.send();
7575
}
7676

@@ -84,20 +84,20 @@ function executeSearch(searchQuery) {
8484
populateResults(result);
8585
} else {
8686
const para = document.createElement('P');
87-
para.innerText = 'No matches found';
87+
para.textContent = 'No matches found';
8888
document.getElementById('search-results').appendChild(para);
8989
}
9090
});
9191
}
9292

9393
function populateResults(result) {
94-
result.forEach((value, key) => {
94+
for (const [key, value] of result.entries()) {
9595
const content = value.item.contents;
9696
let snippet = '';
9797
const snippetHighlights = [];
9898
if (fuseOptions.tokenize) {
9999
snippetHighlights.push(searchQuery);
100-
value.matches.forEach((mvalue) => {
100+
for (const mvalue of value.matches) {
101101
if (mvalue.key === 'tags' || mvalue.key === 'categories') {
102102
snippetHighlights.push(mvalue.value);
103103
} else if (mvalue.key === 'contents') {
@@ -111,7 +111,7 @@ function populateResults(result) {
111111
snippetHighlights.push(mvalue.value.substring(mvalue.indices[0][0], mvalue.indices[0][1] - mvalue.indices[0][0] + 1));
112112
}
113113
}
114-
});
114+
}
115115
}
116116

117117
if (snippet.length < 1) {
@@ -130,10 +130,10 @@ function populateResults(result) {
130130
});
131131
document.getElementById('search-results').appendChild(htmlToElement(output));
132132

133-
snippetHighlights.forEach((snipvalue) => {
133+
for (const snipvalue of snippetHighlights) {
134134
new Mark(document.getElementById(`summary-${key}`)).mark(snipvalue);
135-
});
136-
});
135+
}
136+
}
137137
}
138138

139139
function render(templateString, data) {

0 commit comments

Comments
 (0)