Skip to content

Commit

Permalink
[DataGrid] Replace eval with new Function (#11962)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherniavskii authored Feb 7, 2024
1 parent 4b99220 commit fe29d6f
Showing 1 changed file with 35 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,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;
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 =
| {
Expand Down Expand Up @@ -299,41 +295,41 @@ const buildAggregatedFilterItemsApplier = (
};
}

// We generate a new function with `eval()` to avoid expensive patterns for JS engines
// We generate a new function with `new Function()` 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 :
${applier.v7 ? `appliers[${i}].fn(row)` : `appliers[${i}].fn(getRowId(row))`},
`,
)
.join('\n')}};
return result$$;
})`;

const filterItemCore = evalCode<Function>(
filterItemTemplate.replaceAll('$$', String(filterItemsApplierId)),
const filterItemCore = new Function(
'getRowId',
'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
: ${applier.v7 ? `appliers[${i}].fn(row)` : `appliers[${i}].fn(getRowId(row))`},`,
)
.join('\n')}
};
return result$$;`.replaceAll('$$', String(filterItemsApplierId)),
);
const filterItem: GridFilterItemApplierNotAggregated = (row, shouldApplyItem) => {
return filterItemCore(apiRef.current.getRowId, appliers, row, shouldApplyItem);
};
filterItemsApplierId += 1;

// Assign to the arrow function a name to help debugging
const filterItem: GridFilterItemApplierNotAggregated = (row, shouldApplyItem) =>
filterItemCore(apiRef.current.getRowId, appliers, row, shouldApplyItem);
return filterItem;
};

Expand Down

0 comments on commit fe29d6f

Please sign in to comment.