Skip to content

Commit 54c95a8

Browse files
authored
Rollup merge of rust-lang#109028 - GuillaumeGomez:rustdoc-js-tester-eslint, r=notriddle
Add eslint checks for rustdoc-js tester r? ``@notriddle``
2 parents 7ad471b + ca9b618 commit 54c95a8

File tree

3 files changed

+144
-45
lines changed

3 files changed

+144
-45
lines changed

src/ci/docker/host-x86_64/mingw-check/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ ENV SCRIPT python3 ../x.py --stage 2 test src/tools/expand-yaml-anchors && \
5252
reuse lint && \
5353
# Runs checks to ensure that there are no ES5 issues in our JS code.
5454
es-check es6 ../src/librustdoc/html/static/js/*.js && \
55-
eslint -c ../src/librustdoc/html/static/.eslintrc.js ../src/librustdoc/html/static/js/*.js
55+
eslint -c ../src/librustdoc/html/static/.eslintrc.js ../src/librustdoc/html/static/js/*.js && \
56+
eslint -c ../src/tools/rustdoc-js/.eslintrc.js ../src/tools/rustdoc-js/tester.js

src/tools/rustdoc-js/.eslintrc.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
module.exports = {
2+
"env": {
3+
"browser": true,
4+
"node": true,
5+
"es6": true
6+
},
7+
"extends": "eslint:recommended",
8+
"parserOptions": {
9+
"ecmaVersion": 2015,
10+
"sourceType": "module"
11+
},
12+
"rules": {
13+
"linebreak-style": [
14+
"error",
15+
"unix"
16+
],
17+
"semi": [
18+
"error",
19+
"always"
20+
],
21+
"quotes": [
22+
"error",
23+
"double"
24+
],
25+
"linebreak-style": [
26+
"error",
27+
"unix"
28+
],
29+
"no-trailing-spaces": "error",
30+
"no-var": ["error"],
31+
"prefer-const": ["error"],
32+
"prefer-arrow-callback": ["error"],
33+
"brace-style": [
34+
"error",
35+
"1tbs",
36+
{ "allowSingleLine": false }
37+
],
38+
"keyword-spacing": [
39+
"error",
40+
{ "before": true, "after": true }
41+
],
42+
"arrow-spacing": [
43+
"error",
44+
{ "before": true, "after": true }
45+
],
46+
"key-spacing": [
47+
"error",
48+
{ "beforeColon": false, "afterColon": true, "mode": "strict" }
49+
],
50+
"func-call-spacing": ["error", "never"],
51+
"space-infix-ops": "error",
52+
"space-before-function-paren": ["error", "never"],
53+
"space-before-blocks": "error",
54+
"comma-dangle": ["error", "always-multiline"],
55+
"comma-style": ["error", "last"],
56+
"max-len": ["error", { "code": 100, "tabWidth": 4 }],
57+
"eol-last": ["error", "always"],
58+
"arrow-parens": ["error", "as-needed"],
59+
"no-unused-vars": [
60+
"error",
61+
{
62+
"argsIgnorePattern": "^_",
63+
"varsIgnorePattern": "^_"
64+
}
65+
],
66+
"eqeqeq": "error",
67+
"no-const-assign": "error",
68+
"no-debugger": "error",
69+
"no-dupe-args": "error",
70+
"no-dupe-else-if": "error",
71+
"no-dupe-keys": "error",
72+
"no-duplicate-case": "error",
73+
"no-ex-assign": "error",
74+
"no-fallthrough": "error",
75+
"no-invalid-regexp": "error",
76+
"no-import-assign": "error",
77+
"no-self-compare": "error",
78+
"no-template-curly-in-string": "error",
79+
"block-scoped-var": "error",
80+
"guard-for-in": "error",
81+
"no-alert": "error",
82+
"no-confusing-arrow": "error",
83+
"no-div-regex": "error",
84+
"no-floating-decimal": "error",
85+
"no-implicit-globals": "error",
86+
"no-implied-eval": "error",
87+
"no-label-var": "error",
88+
"no-lonely-if": "error",
89+
"no-mixed-operators": "error",
90+
"no-multi-assign": "error",
91+
"no-return-assign": "error",
92+
"no-script-url": "error",
93+
"no-sequences": "error",
94+
"no-div-regex": "error",
95+
}
96+
};

src/tools/rustdoc-js/tester.js

+46-44
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const fs = require('fs');
2-
const path = require('path');
1+
const fs = require("fs");
2+
const path = require("path");
33

44
function loadContent(content) {
55
const Module = module.constructor;
@@ -15,7 +15,7 @@ function loadContent(content) {
1515
}
1616

1717
function readFile(filePath) {
18-
return fs.readFileSync(filePath, 'utf8');
18+
return fs.readFileSync(filePath, "utf8");
1919
}
2020

2121
function contentToDiffLine(key, value) {
@@ -25,41 +25,41 @@ function contentToDiffLine(key, value) {
2525
// This function is only called when no matching result was found and therefore will only display
2626
// the diff between the two items.
2727
function betterLookingDiff(entry, data) {
28-
let output = ' {\n';
29-
const spaces = ' ';
28+
let output = " {\n";
29+
const spaces = " ";
3030
for (const key in entry) {
31-
if (!entry.hasOwnProperty(key)) {
31+
if (!Object.prototype.hasOwnProperty.call(entry, key)) {
3232
continue;
3333
}
34-
if (!data || !data.hasOwnProperty(key)) {
35-
output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
34+
if (!data || !Object.prototype.hasOwnProperty.call(data, key)) {
35+
output += "-" + spaces + contentToDiffLine(key, entry[key]) + "\n";
3636
continue;
3737
}
3838
const value = data[key];
3939
if (value !== entry[key]) {
40-
output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
41-
output += '+' + spaces + contentToDiffLine(key, value) + '\n';
40+
output += "-" + spaces + contentToDiffLine(key, entry[key]) + "\n";
41+
output += "+" + spaces + contentToDiffLine(key, value) + "\n";
4242
} else {
43-
output += spaces + contentToDiffLine(key, value) + '\n';
43+
output += spaces + contentToDiffLine(key, value) + "\n";
4444
}
4545
}
46-
return output + ' }';
46+
return output + " }";
4747
}
4848

4949
function lookForEntry(entry, data) {
5050
return data.findIndex(data_entry => {
5151
let allGood = true;
5252
for (const key in entry) {
53-
if (!entry.hasOwnProperty(key)) {
53+
if (!Object.prototype.hasOwnProperty.call(entry, key)) {
5454
continue;
5555
}
5656
let value = data_entry[key];
5757
// To make our life easier, if there is a "parent" type, we add it to the path.
58-
if (key === 'path' && data_entry['parent'] !== undefined) {
58+
if (key === "path" && data_entry["parent"] !== undefined) {
5959
if (value.length > 0) {
60-
value += '::' + data_entry['parent']['name'];
60+
value += "::" + data_entry["parent"]["name"];
6161
} else {
62-
value = data_entry['parent']['name'];
62+
value = data_entry["parent"]["name"];
6363
}
6464
}
6565
if (value !== entry[key]) {
@@ -95,7 +95,7 @@ function checkNeededFields(fullPath, expected, error_text, queryName, position)
9595
fieldsToCheck = [];
9696
}
9797
for (const field of fieldsToCheck) {
98-
if (!expected.hasOwnProperty(field)) {
98+
if (!Object.prototype.hasOwnProperty.call(expected, field)) {
9999
let text = `${queryName}==> Mandatory key \`${field}\` is not present`;
100100
if (fullPath.length > 0) {
101101
text += ` in field \`${fullPath}\``;
@@ -117,22 +117,22 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
117117
error_text.push(`${queryName}==> EXPECTED has extra value in array from field ` +
118118
`\`${fullPath}\` (position ${i}): \`${JSON.stringify(expected[i])}\``);
119119
} else {
120-
valueCheck(fullPath + '[' + i + ']', expected[i], result[i], error_text, queryName);
120+
valueCheck(fullPath + "[" + i + "]", expected[i], result[i], error_text, queryName);
121121
}
122122
}
123123
for (; i < result.length; ++i) {
124124
error_text.push(`${queryName}==> RESULT has extra value in array from field ` +
125125
`\`${fullPath}\` (position ${i}): \`${JSON.stringify(result[i])}\` ` +
126-
'compared to EXPECTED');
126+
"compared to EXPECTED");
127127
}
128128
} else if (expected !== null && typeof expected !== "undefined" &&
129-
expected.constructor == Object) {
129+
expected.constructor == Object) { // eslint-disable-line eqeqeq
130130
for (const key in expected) {
131-
if (!expected.hasOwnProperty(key)) {
131+
if (!Object.prototype.hasOwnProperty.call(expected, key)) {
132132
continue;
133133
}
134-
if (!result.hasOwnProperty(key)) {
135-
error_text.push('==> Unknown key "' + key + '"');
134+
if (!Object.prototype.hasOwnProperty.call(result, key)) {
135+
error_text.push("==> Unknown key \"" + key + "\"");
136136
break;
137137
}
138138
let result_v = result[key];
@@ -147,13 +147,13 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
147147
});
148148
result_v = result_v.join("");
149149
}
150-
const obj_path = fullPath + (fullPath.length > 0 ? '.' : '') + key;
150+
const obj_path = fullPath + (fullPath.length > 0 ? "." : "") + key;
151151
valueCheck(obj_path, expected[key], result_v, error_text, queryName);
152152
}
153153
} else {
154154
const expectedValue = JSON.stringify(expected);
155155
const resultValue = JSON.stringify(result);
156-
if (expectedValue != resultValue) {
156+
if (expectedValue !== resultValue) {
157157
error_text.push(`${queryName}==> Different values for field \`${fullPath}\`:\n` +
158158
`EXPECTED: \`${expectedValue}\`\nRESULT: \`${resultValue}\``);
159159
}
@@ -164,7 +164,7 @@ function runParser(query, expected, parseQuery, queryName) {
164164
const error_text = [];
165165
checkNeededFields("", expected, error_text, queryName, null);
166166
if (error_text.length === 0) {
167-
valueCheck('', expected, parseQuery(query), error_text, queryName);
167+
valueCheck("", expected, parseQuery(query), error_text, queryName);
168168
}
169169
return error_text;
170170
}
@@ -177,16 +177,16 @@ function runSearch(query, expected, doSearch, loadedFile, queryName) {
177177
const error_text = [];
178178

179179
for (const key in expected) {
180-
if (!expected.hasOwnProperty(key)) {
180+
if (!Object.prototype.hasOwnProperty.call(expected, key)) {
181181
continue;
182182
}
183-
if (!results.hasOwnProperty(key)) {
184-
error_text.push('==> Unknown key "' + key + '"');
183+
if (!Object.prototype.hasOwnProperty.call(results, key)) {
184+
error_text.push("==> Unknown key \"" + key + "\"");
185185
break;
186186
}
187187
const entry = expected[key];
188188

189-
if (exact_check == true && entry.length !== results[key].length) {
189+
if (exact_check && entry.length !== results[key].length) {
190190
error_text.push(queryName + "==> Expected exactly " + entry.length +
191191
" results but found " + results[key].length + " in '" + key + "'");
192192
}
@@ -268,7 +268,7 @@ function runCheck(loadedFile, key, callback) {
268268
function runChecks(testFile, doSearch, parseQuery) {
269269
let checkExpected = false;
270270
let checkParsed = false;
271-
let testFileContent = readFile(testFile) + 'exports.QUERY = QUERY;';
271+
let testFileContent = readFile(testFile) + "exports.QUERY = QUERY;";
272272

273273
if (testFileContent.indexOf("FILTER_CRATE") !== -1) {
274274
testFileContent += "exports.FILTER_CRATE = FILTER_CRATE;";
@@ -277,11 +277,11 @@ function runChecks(testFile, doSearch, parseQuery) {
277277
}
278278

279279
if (testFileContent.indexOf("\nconst EXPECTED") !== -1) {
280-
testFileContent += 'exports.EXPECTED = EXPECTED;';
280+
testFileContent += "exports.EXPECTED = EXPECTED;";
281281
checkExpected = true;
282282
}
283283
if (testFileContent.indexOf("\nconst PARSED") !== -1) {
284-
testFileContent += 'exports.PARSED = PARSED;';
284+
testFileContent += "exports.PARSED = PARSED;";
285285
checkParsed = true;
286286
}
287287
if (!checkParsed && !checkExpected) {
@@ -325,7 +325,7 @@ function loadSearchJS(doc_folder, resource_suffix) {
325325
const searchWords = searchModule.initSearch(searchIndex.searchIndex);
326326

327327
return {
328-
doSearch: function (queryStr, filterCrate, currentCrate) {
328+
doSearch: function(queryStr, filterCrate, currentCrate) {
329329
return searchModule.execQuery(searchModule.parseQuery(queryStr), searchWords,
330330
filterCrate, currentCrate);
331331
},
@@ -361,22 +361,24 @@ function parseOptions(args) {
361361
};
362362

363363
for (let i = 0; i < args.length; ++i) {
364-
if (correspondences.hasOwnProperty(args[i])) {
364+
const arg = args[i];
365+
if (Object.prototype.hasOwnProperty.call(correspondences, arg)) {
365366
i += 1;
366367
if (i >= args.length) {
367-
console.log("Missing argument after `" + args[i - 1] + "` option.");
368+
console.log("Missing argument after `" + arg + "` option.");
368369
return null;
369370
}
370-
if (args[i - 1] !== "--test-file") {
371-
opts[correspondences[args[i - 1]]] = args[i];
371+
const arg_value = args[i];
372+
if (arg !== "--test-file") {
373+
opts[correspondences[arg]] = arg_value;
372374
} else {
373-
opts[correspondences[args[i - 1]]].push(args[i]);
375+
opts[correspondences[arg]].push(arg_value);
374376
}
375-
} else if (args[i] === "--help") {
377+
} else if (arg === "--help") {
376378
showHelp();
377379
process.exit(0);
378380
} else {
379-
console.log("Unknown option `" + args[i] + "`.");
381+
console.log("Unknown option `" + arg + "`.");
380382
console.log("Use `--help` to see the list of options");
381383
return null;
382384
}
@@ -405,17 +407,17 @@ function main(argv) {
405407
);
406408
let errors = 0;
407409

408-
const doSearch = function (queryStr, filterCrate) {
410+
const doSearch = function(queryStr, filterCrate) {
409411
return parseAndSearch.doSearch(queryStr, filterCrate, opts["crate_name"]);
410412
};
411413

412414
if (opts["test_file"].length !== 0) {
413-
opts["test_file"].forEach(function (file) {
415+
opts["test_file"].forEach(file => {
414416
process.stdout.write(`Testing ${file} ... `);
415417
errors += runChecks(file, doSearch, parseAndSearch.parseQuery);
416418
});
417419
} else if (opts["test_folder"].length !== 0) {
418-
fs.readdirSync(opts["test_folder"]).forEach(function (file) {
420+
fs.readdirSync(opts["test_folder"]).forEach(file => {
419421
if (!file.endsWith(".js")) {
420422
return;
421423
}

0 commit comments

Comments
 (0)