Add an extra check over filter type

This commit is contained in:
Guillaume Gomez 2022-04-17 12:05:31 +02:00
parent d7d538a8c4
commit 299e8ee25e
4 changed files with 92 additions and 4 deletions

View File

@ -235,7 +235,18 @@ window.initSearch = function(rawSearchIndex) {
* @return {boolean}
*/
function isSeparatorCharacter(c) {
return ", \t".indexOf(c) !== -1;
return c === "," || isWhitespaceCharacter(c);
}
/**
* Returns `true` if the given `c` character is a whitespace.
*
* @param {string} c
*
* @return {boolean}
*/
function isWhitespaceCharacter(c) {
return c === " " || c === "\t";
}
/**
@ -424,6 +435,22 @@ window.initSearch = function(rawSearchIndex) {
parserState.pos += 1;
}
/**
* Checks that the type filter doesn't have unwanted characters like `<>` (which are ignored
* if empty).
*
* @param {ParserState} parserState
*/
function checkExtraTypeFilterCharacters(parserState) {
var query = parserState.userQuery;
for (var pos = 0; pos < parserState.pos; ++pos) {
if (!isIdentCharacter(query[pos]) && !isWhitespaceCharacter(query[pos])) {
throw new Error(`Unexpected \`${query[pos]}\` in type filter`);
}
}
}
/**
* Parses the provided `query` input to fill `parserState`. If it encounters an error while
* parsing `query`, it'll throw an error.
@ -457,10 +484,10 @@ window.initSearch = function(rawSearchIndex) {
throw new Error("Expected type filter before `:`");
} else if (query.elems.length !== 1 || parserState.totalElems !== 1) {
throw new Error("Unexpected `:`");
}
if (query.literalSearch) {
} else if (query.literalSearch) {
throw new Error("You cannot use quotes on type filter");
}
checkExtraTypeFilterCharacters(parserState);
// The type filter doesn't count as an element since it's a modifier.
parserState.typeFilter = query.elems.pop().name;
parserState.pos += 1;

View File

@ -30,6 +30,10 @@ const QUERY = [
"a<->",
"a:: a",
"a ::a",
"a<a>:",
"a<>:",
"a,:",
" a<> :",
];
const PARSED = [
@ -312,4 +316,40 @@ const PARSED = [
userQuery: 'a ::a',
error: 'Paths cannot start with `::`',
},
{
elems: [],
foundElems: 0,
original: "a<a>:",
returned: [],
typeFilter: -1,
userQuery: "a<a>:",
error: 'Unexpected `:`',
},
{
elems: [],
foundElems: 0,
original: "a<>:",
returned: [],
typeFilter: -1,
userQuery: "a<>:",
error: 'Unexpected `<` in type filter',
},
{
elems: [],
foundElems: 0,
original: "a,:",
returned: [],
typeFilter: -1,
userQuery: "a,:",
error: 'Unexpected `,` in type filter',
},
{
elems: [],
foundElems: 0,
original: "a<> :",
returned: [],
typeFilter: -1,
userQuery: "a<> :",
error: 'Unexpected `<` in type filter',
},
];

View File

@ -7,6 +7,8 @@ const QUERY = [
'a,b(c)',
'aaa,a',
',,,,',
'mod :',
'mod\t:',
];
const PARSED = [
@ -100,4 +102,22 @@ const PARSED = [
userQuery: ",,,,",
error: null,
},
{
elems: [],
foundElems: 0,
original: 'mod :',
returned: [],
typeFilter: 0,
userQuery: 'mod :',
error: null,
},
{
elems: [],
foundElems: 0,
original: 'mod\t:',
returned: [],
typeFilter: 0,
userQuery: 'mod\t:',
error: null,
},
];

View File

@ -275,7 +275,8 @@ function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) {
"parseInput", "getItemsBefore", "getNextElem", "createQueryElement",
"isReturnArrow", "isPathStart", "getStringElem", "newParsedQuery",
"itemTypeFromName", "isEndCharacter", "isErrorCharacter",
"isIdentCharacter", "isSeparatorCharacter", "getIdentEndPosition"];
"isIdentCharacter", "isSeparatorCharacter", "getIdentEndPosition",
"checkExtraTypeFilterCharacters", "isWhitespaceCharacter"];
const functions = ["hasOwnPropertyRustdoc", "onEach"];
ALIASES = {};