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

[DataGrid] Replace eval with new Function #11557

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,21 @@ import {
gridVisibleColumnFieldsSelector,
} from '../columns';

// Fixes https://github.com/mui/mui-x/issues/10056
const globalScope = (typeof window === 'undefined' ? globalThis : window) as any;
const evalCode = globalScope[atob('ZXZhbA==')] as <T>(source: string) => T;
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
let hasEval: boolean;

let hasEval: boolean | undefined;

const getHasEval = () => {
function getHasEval() {
if (hasEval !== undefined) {
return hasEval;
}

try {
hasEval = evalCode<boolean>('true');
hasEval = new Function('return true')() as boolean;
} catch (_: unknown) {
hasEval = false;
}

return hasEval;
};
}

type GridFilterItemApplier = {
fn: (row: GridValidRowModel) => boolean;
Expand Down Expand Up @@ -263,40 +259,36 @@ const buildAggregatedFilterItemsApplier = (

// We generate a new function with `eval()` to avoid expensive patterns for JS engines
// such as a dynamic object assignment, e.g. `{ [dynamicKey]: value }`.
const filterItemTemplate = `(function filterItem$$(getRowId, appliers, row, shouldApplyFilter) {
${appliers
.map(
(applier, i) =>
`const shouldApply${i} = !shouldApplyFilter || shouldApplyFilter(${JSON.stringify(
applier.item.field,
)});`,
)
.join('\n')}

const result$$ = {
${appliers
.map(
(applier, i) =>
`${JSON.stringify(String(applier.item.id))}:
!shouldApply${i} ?
false :
appliers[${i}].fn(row),
`,
)
.join('\n')}};

return result$$;
})`;

const filterItemCore = evalCode<Function>(
filterItemTemplate.replaceAll('$$', String(filterItemsApplierId)),
const filterItemCore = new Function(
'appliers',
'row',
'shouldApplyFilter',
`"use strict";
${appliers
.map(
(applier, i) =>
`const shouldApply${i} = !shouldApplyFilter || shouldApplyFilter(${JSON.stringify(
applier.item.field,
)});`,
)
.join('\n')}

const result$$ = {
${appliers
.map(
(applier, i) =>
` ${JSON.stringify(
String(applier.item.id),
)}: !shouldApply${i} ? false : appliers[${i}].fn(row),`,
)
.join('\n')}
};

return result$$;`.replaceAll('$$', String(filterItemsApplierId)),
);
const filterItem: GridFilterItemApplierNotAggregated = (row, shouldApplyItem) => {
return filterItemCore(apiRef.current.getRowId, appliers, row, shouldApplyItem);
};
filterItemsApplierId += 1;

return filterItem;
return (row, shouldApplyItem) => filterItemCore(appliers, row, shouldApplyItem);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes I like assigning functions to a variable because it gives a better debugging experience, the stack entry would read filterItem - .../gridFilterUtils.js:294 instead of (anonymous) - .../gridFilterUtils.js:291. It's useful when stepping through code but also when collecting performance profiles. With this change I'm also not sure what the name of filterItemCore becomes.

It's fine to leave as it is, but just fyi.

};

export const shouldQuickFilterExcludeHiddenColumns = (filterModel: GridFilterModel) => {
Expand Down