Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] [Search v2] [App] Create Search input parser #45161

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ module.exports = {
'prettier',
],
plugins: ['@typescript-eslint', 'jsdoc', 'you-dont-need-lodash-underscore', 'react-native-a11y', 'react', 'testing-library', 'eslint-plugin-react-compiler'],
ignorePatterns: ['lib/**'],

// Ignore the searchParser.js file as it's generated by the peggy parser generator.
ignorePatterns: ['lib/**', 'src/lib/Search/searchParser.js'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: path.resolve(__dirname, './tsconfig.json'),
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,6 @@ web-build/

# Storage location for downloaded app source maps (see scripts/symbolicate-profile.ts)
.sourcemaps/

# Generated search parser
src/libs/Search/searchParser.js
63 changes: 63 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"workflow-test:generate": "ts-node workflow_tests/utils/preGenerateTest.ts",
"setup-https": "mkcert -install && mkcert -cert-file config/webpack/certificate.pem -key-file config/webpack/key.pem dev.new.expensify.com localhost 127.0.0.1",
"e2e-test-runner-build": "ncc build tests/e2e/testRunner.ts -o tests/e2e/dist/",
"react-compiler-healthcheck": "react-compiler-healthcheck --verbose"
"react-compiler-healthcheck": "react-compiler-healthcheck --verbose",
"generate-search-parser": "peggy --format es -o src/libs/Search/searchParser.js src/libs/Search/searchParser.peggy "
},
"dependencies": {
"@babel/plugin-proposal-private-methods": "^7.18.6",
Expand Down Expand Up @@ -287,6 +288,7 @@
"memfs": "^4.6.0",
"onchange": "^7.1.0",
"patch-package": "^8.0.0",
"peggy": "^4.0.3",
"portfinder": "^1.0.28",
"prettier": "^2.8.8",
"pusher-js-mock": "^0.3.3",
Expand Down
3 changes: 3 additions & 0 deletions scripts/postInstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ cd "$ROOT_DIR" || exit 1
# Apply packages using patch-package
scripts/applyPatches.sh

# Generate the search query parser
npm run generate-search-parser

# Install node_modules in subpackages, unless we're in a CI/CD environment,
# where the node_modules for subpackages are cached separately.
# See `.github/actions/composite/setupNode/action.yml` for more context.
Expand Down
87 changes: 87 additions & 0 deletions src/libs/Search/searchParser.peggy
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// src/libs/Search/searchParser.peggy
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
{
const defaultValues = {
"type": "expense",
"status": "all",
"sortBy": "date",
"sortOrder": "desc",
"offset": 0
};

function buildFilter(operator, left, right) {
return { operator, left, right };
}

function applyDefaults(filters) {
return {
...defaultValues,
filters
};
}
}

query
= _ filters:filterList { return applyDefaults(filters); }

filterList
= head:filter tail:(filter logicalAnd)* {
return tail.reduce((result, [right, op]) => buildFilter(op, result, right), head);
}

filter
= _ field:key? _ op:operator? _ value:identifier {
if (!field && !op) {
return buildFilter('eq', 'freeText', value.trim());
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
} else {
const values = value.split(',');
return values.slice(1).reduce((acc, val) => buildFilter('or', acc, buildFilter('eq', field, val.trim())), buildFilter('eq', field, values[0]));
}
}

operator
= (":" / "=") { return "eq"; }
/ "!=" { return "neq"; }
/ ">" { return "gt"; }
/ ">=" { return "gte"; }
/ "<" { return "lt"; }
/ "<=" { return "lte"; }

key
= "type" { return "type"; }
/ "status" { return "status"; }
/ "date" { return "date"; }
/ "amount" { return "amount"; }
/ "expenseType" { return "expenseType"; }
/ "in" { return "in"; }
/ "currency" { return "currency"; }
/ "merchant" { return "merchant"; }
/ "description" { return "description"; }
/ "from" { return "from"; }
/ "to" { return "to"; }
/ "category" { return "category"; }
/ "tag" { return "tag"; }
/ "taxRate" { return "taxRate"; }
/ "card" { return "card"; }
/ "reportID" { return "reportID"; }
/ "freeText" { return "freeText"; }
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
/ "sortBy" { return "sortBy"; }
/ "sortOrder" { return "sortOrder"; }
/ "offset" { return "offset"; }

identifier
= parts:(quotedString / alphanumeric)+ { return parts.join(''); }

quotedString
= '"' chars:[^"\r\n]* '"' { return chars.join(''); }

alphanumeric
= chars:[A-Za-z0-9_@./#&+\-\\',]+ { return chars.join(''); }

logicalAnd
= _ { return "and"; }

_ "whitespace"
= [ \t\r\n]*

start
= query
21 changes: 21 additions & 0 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import getTopmostCentralPaneRoute from './Navigation/getTopmostCentralPaneRoute';
import navigationRef from './Navigation/navigationRef';
import type {AuthScreensParamList, RootStackParamList, State} from './Navigation/types';
// This file is generated by the npm run generate-search-parser command.
import * as searchParser from './Search/searchParser';

Check failure on line 15 in src/libs/SearchUtils.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module './Search/searchParser' or its corresponding type declarations.

Check failure on line 15 in src/libs/SearchUtils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing file extension for "./Search/searchParser"
import * as TransactionUtils from './TransactionUtils';
import * as UserUtils from './UserUtils';

Expand Down Expand Up @@ -301,7 +303,26 @@
return !Object.keys(searchResults?.data).some((key) => key.startsWith(ONYXKEYS.COLLECTION.TRANSACTION));
}

// This function should replace the current getQueryParams after the search v2.1 implementation.
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
function __getQueryHash(query: string): number {

Check failure on line 307 in src/libs/SearchUtils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected dangling '_' in '__getQueryHash'

Check failure on line 307 in src/libs/SearchUtils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Function name `__getQueryHash` must match one of the following formats: camelCase, PascalCase
return UserUtils.hashText(query, 2 ** 32);
}

function buildJSONQuery(query: string) {
try {
// Add the full input and hash to the results
const result = searchParser.parse(query);

Check failure on line 314 in src/libs/SearchUtils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe assignment of an error typed value

Check failure on line 314 in src/libs/SearchUtils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe call of an `any` typed value

Check failure on line 314 in src/libs/SearchUtils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe member access .parse on an `error` typed value
result['input'] = query;

Check failure on line 315 in src/libs/SearchUtils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

["input"] is better written in dot notation

Check failure on line 315 in src/libs/SearchUtils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe member access ['input'] on an `error` typed value
result['hash'] = __getQueryHash(result);

Check failure on line 316 in src/libs/SearchUtils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

["hash"] is better written in dot notation

Check failure on line 316 in src/libs/SearchUtils.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe member access ['hash'] on an `error` typed value
return result;
} catch (e) {
// TODO: We need better error handling here.
luacmartins marked this conversation as resolved.
Show resolved Hide resolved
console.log(e);
}
}

export {
buildJSONQuery,
getListItem,
getQueryHash,
getSections,
Expand Down
Loading