Improve naming of "val" field

This commit is contained in:
Guillaume Gomez 2022-01-03 16:43:30 +01:00
parent f5833e759e
commit bbcf1762dd
12 changed files with 58 additions and 58 deletions

View File

@ -21,7 +21,7 @@ var QueryElement;
/** /**
* @typedef {{ * @typedef {{
* original: string, * original: string,
* val: string, * userQuery: string,
* length: number, * length: number,
* pos: number, * pos: number,
* typeFilter: number, * typeFilter: number,

View File

@ -163,7 +163,7 @@ window.initSearch = function(rawSearchIndex) {
* @param {string} val - The user query * @param {string} val - The user query
* @return {ParsedQuery} - The parsed query * @return {ParsedQuery} - The parsed query
*/ */
function parseQuery(val) { function parseQuery(userQuery) {
function isWhitespace(c) { function isWhitespace(c) {
return " \t\n\r".indexOf(c) !== -1; return " \t\n\r".indexOf(c) !== -1;
} }
@ -182,8 +182,8 @@ window.initSearch = function(rawSearchIndex) {
throw new Error("Cannot use literal search when there is more than one element"); throw new Error("Cannot use literal search when there is more than one element");
} }
query.pos += 1; query.pos += 1;
while (query.pos < query.length && query.val[query.pos] !== "\"") { while (query.pos < query.length && query.userQuery[query.pos] !== "\"") {
if (query.val[query.pos] === "\\") { if (query.userQuery[query.pos] === "\\") {
// We ignore the next coming character. // We ignore the next coming character.
query.pos += 1; query.pos += 1;
} }
@ -198,7 +198,7 @@ window.initSearch = function(rawSearchIndex) {
} }
function skipWhitespaces(query) { function skipWhitespaces(query) {
while (query.pos < query.length) { while (query.pos < query.length) {
var c = query.val[query.pos]; var c = query.userQuery[query.pos];
if (!isWhitespace(c)) { if (!isWhitespace(c)) {
break; break;
} }
@ -207,7 +207,7 @@ window.initSearch = function(rawSearchIndex) {
} }
function skipStopCharacters(query) { function skipStopCharacters(query) {
while (query.pos < query.length) { while (query.pos < query.length) {
var c = query.val[query.pos]; var c = query.userQuery[query.pos];
if (!isStopCharacter(c)) { if (!isStopCharacter(c)) {
break; break;
} }
@ -216,11 +216,11 @@ window.initSearch = function(rawSearchIndex) {
} }
function isPathStart(query) { function isPathStart(query) {
var pos = query.pos; var pos = query.pos;
return pos + 1 < query.length && query.val[pos] === ':' && query.val[pos + 1] === ':'; return pos + 1 < query.length && query.userQuery[pos] === ':' && query.userQuery[pos + 1] === ':';
} }
function isReturnArrow(query) { function isReturnArrow(query) {
var pos = query.pos; var pos = query.pos;
return pos + 1 < query.length && query.val[pos] === '-' && query.val[pos + 1] === '>'; return pos + 1 < query.length && query.userQuery[pos] === '-' && query.userQuery[pos + 1] === '>';
} }
function removeEmptyStringsFromArray(x) { function removeEmptyStringsFromArray(x) {
for (var i = 0, len = x.length; i < len; ++i) { for (var i = 0, len = x.length; i < len; ++i) {
@ -230,19 +230,19 @@ window.initSearch = function(rawSearchIndex) {
} }
} }
} }
function createQueryElement(query, elems, val, generics) { function createQueryElement(query, elems, name, generics) {
removeEmptyStringsFromArray(generics); removeEmptyStringsFromArray(generics);
if (val === '*' || (val.length === 0 && generics.length === 0)) { if (name === '*' || (name.length === 0 && generics.length === 0)) {
return; return;
} }
var paths = val.split("::"); var paths = name.split("::");
removeEmptyStringsFromArray(paths); removeEmptyStringsFromArray(paths);
// In case we only have something like `<p>`, there is no name but it remains valid. // In case we only have something like `<p>`, there is no name but it remains valid.
if (paths.length === 0) { if (paths.length === 0) {
paths = [""]; paths = [""];
} }
elems.push({ elems.push({
name: val, name: name,
fullPath: paths, fullPath: paths,
pathWithoutLast: paths.slice(0, paths.length - 1), pathWithoutLast: paths.slice(0, paths.length - 1),
pathLast: paths[paths.length - 1], pathLast: paths[paths.length - 1],
@ -257,14 +257,14 @@ window.initSearch = function(rawSearchIndex) {
var start = query.pos; var start = query.pos;
var end = start; var end = start;
// We handle the strings on their own mostly to make code easier to follow. // We handle the strings on their own mostly to make code easier to follow.
if (query.val[query.pos] === "\"") { if (query.userQuery[query.pos] === "\"") {
start += 1; start += 1;
getStringElem(query, isInGenerics); getStringElem(query, isInGenerics);
end = query.pos - 1; end = query.pos - 1;
skipWhitespaces(query); skipWhitespaces(query);
} else { } else {
while (query.pos < query.length) { while (query.pos < query.length) {
var c = query.val[query.pos]; var c = query.userQuery[query.pos];
if (isStopCharacter(c) || isSpecialStartCharacter(c)) { if (isStopCharacter(c) || isSpecialStartCharacter(c)) {
break; break;
} }
@ -281,17 +281,17 @@ window.initSearch = function(rawSearchIndex) {
skipWhitespaces(query); skipWhitespaces(query);
} }
} }
if (query.pos < query.length && query.val[query.pos] === "<") { if (query.pos < query.length && query.userQuery[query.pos] === "<") {
getItemsBefore(query, generics, ">"); getItemsBefore(query, generics, ">");
} }
if (start >= end && generics.length === 0) { if (start >= end && generics.length === 0) {
return; return;
} }
createQueryElement(query, elems, query.val.slice(start, end), generics); createQueryElement(query, elems, query.userQuery.slice(start, end), generics);
} }
function getItemsBefore(query, elems, limit) { function getItemsBefore(query, elems, limit) {
while (query.pos < query.length) { while (query.pos < query.length) {
var c = query.val[query.pos]; var c = query.userQuery[query.pos];
if (c === limit) { if (c === limit) {
break; break;
} else if (isSpecialStartCharacter(c) || c === ":") { } else if (isSpecialStartCharacter(c) || c === ":") {
@ -307,7 +307,7 @@ window.initSearch = function(rawSearchIndex) {
var c, before; var c, before;
while (query.pos < query.length) { while (query.pos < query.length) {
c = query.val[query.pos]; c = query.userQuery[query.pos];
if (isStopCharacter(c)) { if (isStopCharacter(c)) {
if (c === ",") { if (c === ",") {
query.pos += 1; query.pos += 1;
@ -331,14 +331,14 @@ window.initSearch = function(rawSearchIndex) {
getNextElem(query, query.elems, false); getNextElem(query, query.elems, false);
if (query.elems.length === before) { if (query.elems.length === before) {
// Nothing was added, let's check it's not because of a solo ":"! // Nothing was added, let's check it's not because of a solo ":"!
if (query.pos >= query.length || query.val[query.pos] !== ":") { if (query.pos >= query.length || query.userQuery[query.pos] !== ":") {
break; break;
} }
query.pos += 1; query.pos += 1;
} }
} }
while (query.pos < query.length) { while (query.pos < query.length) {
c = query.val[query.pos]; c = query.userQuery[query.pos];
if (query.args.length === 0 && c === "(") { if (query.args.length === 0 && c === "(") {
if (query.elemName === null && query.elems.length === 1) { if (query.elemName === null && query.elems.length === 1) {
query.elemName = query.elems.pop(); query.elemName = query.elems.pop();
@ -364,11 +364,11 @@ window.initSearch = function(rawSearchIndex) {
return NO_TYPE_FILTER; return NO_TYPE_FILTER;
} }
val = val.trim(); userQuery = userQuery.trim();
var query = { var query = {
original: val, original: userQuery,
val: val.toLowerCase(), userQuery: userQuery.toLowerCase(),
length: val.length, length: userQuery.length,
pos: 0, pos: 0,
typeFilter: null, typeFilter: null,
elems: [], elems: [],
@ -386,7 +386,7 @@ window.initSearch = function(rawSearchIndex) {
literalSearch: false, literalSearch: false,
error: null, error: null,
}; };
query.id = val; query.id = userQuery;
try { try {
parseInput(query); parseInput(query);
} catch (err) { } catch (err) {
@ -405,9 +405,9 @@ window.initSearch = function(rawSearchIndex) {
if (query.elemName !== null) { if (query.elemName !== null) {
query.foundElems += 1; query.foundElems += 1;
} }
if (query.foundElems === 0 && val.length !== 0) { if (query.foundElems === 0 && userQuery.length !== 0) {
// In this case, we'll simply keep whatever was entered by the user... // In this case, we'll simply keep whatever was entered by the user...
createQueryElement(query, query.elems, val, []); createQueryElement(query, query.elems, userQuery, []);
query.foundElems += 1; query.foundElems += 1;
} }
if (query.typeFilter !== null) { if (query.typeFilter !== null) {
@ -422,8 +422,8 @@ window.initSearch = function(rawSearchIndex) {
query.elemName = null; query.elemName = null;
} }
if (query.elemName !== null || query.elems.length === 1) { if (query.elemName !== null || query.elems.length === 1) {
val = query.elemName || query.elems[0]; userQuery = query.elemName || query.elems[0];
query.nameSplit = typeof val.path === "undefined" ? null : val.path; query.nameSplit = typeof userQuery.path === "undefined" ? null : userQuery.path;
} }
return query; return query;
} }
@ -493,7 +493,7 @@ window.initSearch = function(rawSearchIndex) {
function sortResults(results, isType) { function sortResults(results, isType) {
var nameSplit = queryInfo.nameSplit; var nameSplit = queryInfo.nameSplit;
var query = queryInfo.val; var query = queryInfo.userQuery;
var ar = []; var ar = [];
for (var entry in results) { for (var entry in results) {
if (hasOwnPropertyRustdoc(results, entry)) { if (hasOwnPropertyRustdoc(results, entry)) {
@ -1344,7 +1344,7 @@ window.initSearch = function(rawSearchIndex) {
output.className = "search-failed" + extraClass; output.className = "search-failed" + extraClass;
output.innerHTML = "No results :(<br/>" + output.innerHTML = "No results :(<br/>" +
"Try on <a href=\"https://duckduckgo.com/?q=" + "Try on <a href=\"https://duckduckgo.com/?q=" +
encodeURIComponent("rust " + query.val) + encodeURIComponent("rust " + query.userQuery) +
"\">DuckDuckGo</a>?<br/><br/>" + "\">DuckDuckGo</a>?<br/><br/>" +
"Or try looking in one of these:<ul><li>The <a " + "Or try looking in one of these:<ul><li>The <a " +
"href=\"https://doc.rust-lang.org/reference/index.html\">Rust Reference</a> " + "href=\"https://doc.rust-lang.org/reference/index.html\">Rust Reference</a> " +
@ -1424,7 +1424,7 @@ window.initSearch = function(rawSearchIndex) {
} }
var output = `<div id="search-settings">` + var output = `<div id="search-settings">` +
`<h1 class="search-results-title">Results for ${escape(results.query.val)}$` + `<h1 class="search-results-title">Results for ${escape(results.query.userQuery)}` +
`${typeFilter}</h1> in ${crates} </div>` + `${typeFilter}</h1> in ${crates} </div>` +
`<div id="titles">` + `<div id="titles">` +
makeTabHeader(0, "In Names", ret_others[1]) + makeTabHeader(0, "In Names", ret_others[1]) +

View File

@ -11,7 +11,7 @@ const PARSED = [
original: "<\"P\">", original: "<\"P\">",
returned: [], returned: [],
typeFilter: null, typeFilter: null,
val: "<\"p\">", userQuery: "<\"p\">",
error: "`\"` cannot be used in generics", error: "`\"` cannot be used in generics",
}, },
{ {
@ -24,7 +24,7 @@ const PARSED = [
original: "\"P\" \"P\"", original: "\"P\" \"P\"",
returned: [], returned: [],
typeFilter: null, typeFilter: null,
val: "\"p\" \"p\"", userQuery: "\"p\" \"p\"",
error: "Cannot have more than one literal search element", error: "Cannot have more than one literal search element",
}, },
{ {
@ -37,7 +37,7 @@ const PARSED = [
original: "P \"P\"", original: "P \"P\"",
returned: [], returned: [],
typeFilter: null, typeFilter: null,
val: "p \"p\"", userQuery: "p \"p\"",
error: "Cannot use literal search when there is more than one element", error: "Cannot use literal search when there is more than one element",
}, },
]; ];

View File

@ -17,7 +17,7 @@ const PARSED = [
original: "fn:foo", original: "fn:foo",
returned: [], returned: [],
typeFilter: 5, typeFilter: 5,
val: "fn:foo", userQuery: "fn:foo",
error: null, error: null,
}, },
{ {
@ -36,7 +36,7 @@ const PARSED = [
original: "enum : foo", original: "enum : foo",
returned: [], returned: [],
typeFilter: 4, typeFilter: 4,
val: "enum : foo", userQuery: "enum : foo",
error: null, error: null,
}, },
{ {
@ -55,7 +55,7 @@ const PARSED = [
original: "macro<f>:foo", original: "macro<f>:foo",
returned: [], returned: [],
typeFilter: 14, typeFilter: 14,
val: "macro<f>:foo", userQuery: "macro<f>:foo",
error: null, error: null,
}, },
]; ];

View File

@ -25,7 +25,7 @@ const PARSED = [
original: "<P>", original: "<P>",
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: "<p>", userQuery: "<p>",
error: null, error: null,
}, },
{ {
@ -75,7 +75,7 @@ const PARSED = [
original: 'A<B<C<D>, E>', original: 'A<B<C<D>, E>',
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: 'a<b<c<d>, e>', userQuery: 'a<b<c<d>, e>',
error: null, error: null,
} }
]; ];

View File

@ -17,7 +17,7 @@ const PARSED = [
original: "(whatever)", original: "(whatever)",
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: "(whatever)", userQuery: "(whatever)",
error: null, error: null,
}, },
{ {
@ -44,7 +44,7 @@ const PARSED = [
original: "(<P>)", original: "(<P>)",
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: "(<p>)", userQuery: "(<p>)",
error: null, error: null,
}, },
]; ];

View File

@ -37,7 +37,7 @@ const PARSED = [
}, },
], ],
typeFilter: -1, typeFilter: -1,
val: "-> <p> (p2)", userQuery: "-> <p> (p2)",
error: null, error: null,
}, },
{ {
@ -65,7 +65,7 @@ const PARSED = [
original: "(p -> p2", original: "(p -> p2",
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: "(p -> p2", userQuery: "(p -> p2",
error: null, error: null,
}, },
{ {
@ -86,7 +86,7 @@ const PARSED = [
original: "a b", original: "a b",
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: "a b", userQuery: "a b",
error: null, error: null,
}, },
{ {
@ -122,7 +122,7 @@ const PARSED = [
original: "a,b(c)", original: "a,b(c)",
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: "a,b(c)", userQuery: "a,b(c)",
error: null, error: null,
}, },
]; ];

View File

@ -25,7 +25,7 @@ const PARSED = [
original: "\"R\"<P>", original: "\"R\"<P>",
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: "\"r\"<p>", userQuery: "\"r\"<p>",
error: null, error: null,
} }
]; ];

View File

@ -17,7 +17,7 @@ const PARSED = [
original: "A::B", original: "A::B",
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: "a::b", userQuery: "a::b",
error: null, error: null,
}, },
{ {
@ -36,7 +36,7 @@ const PARSED = [
original: '::A::B', original: '::A::B',
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: '::a::b', userQuery: '::a::b',
error: null, error: null,
}, },
{ {
@ -64,7 +64,7 @@ const PARSED = [
original: 'A::B::,C', original: 'A::B::,C',
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: 'a::b::,c', userQuery: 'a::b::,c',
error: null, error: null,
}, },
{ {
@ -100,7 +100,7 @@ const PARSED = [
original: 'A::B::<f>,C', original: 'A::B::<f>,C',
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: 'a::b::<f>,c', userQuery: 'a::b::<f>,c',
error: null, error: null,
}, },
]; ];

View File

@ -17,7 +17,7 @@ const PARSED = [
generics: [], generics: [],
}], }],
typeFilter: -1, typeFilter: -1,
val: "-> \"p\"", userQuery: "-> \"p\"",
error: null, error: null,
}, },
// This one checks that if quotes are used on the type filter, they're // This one checks that if quotes are used on the type filter, they're
@ -38,7 +38,7 @@ const PARSED = [
original: "\"const\": \"p\"", original: "\"const\": \"p\"",
returned: [], returned: [],
typeFilter: 17, typeFilter: 17,
val: "\"const\": \"p\"", userQuery: "\"const\": \"p\"",
error: null, error: null,
}, },
{ {
@ -57,7 +57,7 @@ const PARSED = [
original: "(\"p\")", original: "(\"p\")",
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: "(\"p\")", userQuery: "(\"p\")",
error: null, error: null,
}, },
// This test checks that a literal item can still have generics. // This test checks that a literal item can still have generics.
@ -83,7 +83,7 @@ const PARSED = [
original: "\"p\"<p>", original: "\"p\"<p>",
returned: [], returned: [],
typeFilter: -1, typeFilter: -1,
val: "\"p\"<p>", userQuery: "\"p\"<p>",
error: null, error: null,
}, },
]; ];

View File

@ -25,7 +25,7 @@ const PARSED = [
], ],
}], }],
typeFilter: -1, typeFilter: -1,
val: "-> <p>", userQuery: "-> <p>",
error: null, error: null,
}, },
{ {
@ -44,7 +44,7 @@ const PARSED = [
generics: [], generics: [],
}], }],
typeFilter: -1, typeFilter: -1,
val: "-> p", userQuery: "-> p",
error: null, error: null,
}, },
]; ];

View File

@ -296,7 +296,7 @@ function checkFieldNeededFields(fullPath, expected, error_text, queryName, posit
"original", "original",
"returned", "returned",
"typeFilter", "typeFilter",
"val", "userQuery",
"error", "error",
]; ];
} else if (fullPath.endsWith("elems") || fullPath.endsWith("generics")) { } else if (fullPath.endsWith("elems") || fullPath.endsWith("generics")) {