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 1 commit
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 any;
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
} catch (_: unknown) {
hasEval = false;
}

return hasEval;
};
}

type GridFilterItemApplier = {
fn: (row: GridValidRowModel) => boolean;
Expand Down Expand Up @@ -263,36 +259,37 @@ 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>(
const filterItemTemplate = `"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$$;`;

const filterItemCore = new Function(
'appliers',
'row',
'shouldApplyFilter',
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
filterItemTemplate.replaceAll('$$', String(filterItemsApplierId)),
);
const filterItem: GridFilterItemApplierNotAggregated = (row, shouldApplyItem) => {
return filterItemCore(apiRef.current.getRowId, appliers, row, shouldApplyItem);
return filterItemCore(appliers, row, shouldApplyItem);
};
filterItemsApplierId += 1;

Expand Down
Loading