Skip to content

Commit f232bba

Browse files
authored
chore: initialize Records with null prototype objects (#7652)
1 parent 13c6862 commit f232bba

File tree

8 files changed

+18
-11
lines changed

8 files changed

+18
-11
lines changed

packages/compass-crud/src/components/change-view/bson-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function unBSON(value: any): any {
2121
if (shape === 'array') {
2222
return value.map(unBSON);
2323
} else if (shape === 'object') {
24-
const mapped: Record<string, any> = {};
24+
const mapped: Record<string, any> = Object.create(null);
2525
for (const [k, v] of Object.entries(value)) {
2626
mapped[k] = unBSON(v);
2727
}

packages/compass-crud/src/components/table-view/document-table-view.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export class DocumentTableView extends React.Component<DocumentTableViewProps> {
224224
onColumnResized(event: ColumnResizedEvent) {
225225
if (event.finished) {
226226
const columnState = this.columnApi?.getColumnState() || [];
227-
const currentColumnWidths: Record<string, number> = {};
227+
const currentColumnWidths: Record<string, number> = Object.create(null);
228228
for (const column of columnState) {
229229
if (column.width) currentColumnWidths[column.colId] = column.width;
230230
}
@@ -888,8 +888,11 @@ export class DocumentTableView extends React.Component<DocumentTableViewProps> {
888888
path: (string | number)[],
889889
types: TableHeaderType[]
890890
): ColDef[] => {
891-
const headers: Record<string, ColDef> = {};
892-
const headerTypes: Record<string, Record<string, TableHeaderType>> = {};
891+
const headers: Record<string, ColDef> = Object.create(null);
892+
const headerTypes: Record<
893+
string,
894+
Record<string, TableHeaderType>
895+
> = Object.create(null);
893896
const isEditable = this.props.isEditable;
894897
const parentType = types.length ? types[types.length - 1] : 'Object';
895898

@@ -944,7 +947,7 @@ export class DocumentTableView extends React.Component<DocumentTableViewProps> {
944947
the grid. This is handled here for the initial header values, and then
945948
in the GridStore for any subsequent updates. */
946949
const columnHeaders = Object.values(headers);
947-
const showing: Record<string, TableHeaderType> = {};
950+
const showing: Record<string, TableHeaderType> = Object.create(null);
948951

949952
map(headerTypes, function (oids, key) {
950953
const colTypes = Object.values(oids);

packages/compass-crud/src/stores/crud-store.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2719,7 +2719,7 @@ describe('store', function () {
27192719
let fakeSetItem: (key: string, value: string) => void;
27202720

27212721
beforeEach(function () {
2722-
const localStorageValues: Record<string, string> = {};
2722+
const localStorageValues: Record<string, string> = Object.create(null);
27232723
fakeGetItem = sinon.fake((key: string) => {
27242724
return localStorageValues[key];
27252725
});

packages/compass-crud/src/stores/grid-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class GridStoreImpl
333333
* @param {Boolean} isArray - If the parent of the element is an array.
334334
*/
335335
elementRemoved(key: string, oid: string, isArray: boolean) {
336-
const params: Record<string, unknown> = {};
336+
const params: Record<string, unknown> = Object.create(null);
337337
const newShowing: typeof this.showing = {};
338338

339339
/* If it's an array element, need to move subsequent elements up */

packages/compass-electron-menu/src/application-menu.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe('application-menu / useApplicationMenu', function () {
130130

131131
const handlerUndoA = () => {};
132132
const handlerRedoA = () => {};
133-
let roles: Record<string, () => void> = {
133+
let roles: Readonly<Record<string, () => void>> = {
134134
undo: handlerUndoA,
135135
redo: handlerRedoA,
136136
};

packages/data-service/src/data-service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
11941194

11951195
// Build the aggregation pipeline dynamically based on collection type
11961196
const groupStage: Record<string, unknown> = {
1197+
__proto__: null,
11971198
_id: null,
11981199
capped: { $first: '$storageStats.capped' },
11991200
count: { $sum: '$storageStats.count' },
@@ -1230,6 +1231,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
12301231

12311232
const addFieldsStage: Record<string, unknown> = {
12321233
// `avgObjSize` is the average of per-shard `avgObjSize` weighted by `count`
1234+
__proto__: null,
12331235
avgObjSize: {
12341236
$cond: {
12351237
if: { $ne: ['$count', 0] },

packages/hadron-document/src/object-generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class ObjectGenerator {
8989
options: ObjectGeneratorOptions = {}
9090
): Record<string, unknown> {
9191
if (elements) {
92-
const object: Record<string, unknown> = {};
92+
const object: Record<string, unknown> = Object.create(null);
9393
for (const element of elements) {
9494
if (options.excludeInternalFields && element.isInternalField()) {
9595
continue;
@@ -118,7 +118,7 @@ export class ObjectGenerator {
118118
options: ObjectGeneratorOptions = {}
119119
): Record<string, unknown> {
120120
if (elements) {
121-
const object: Record<string, unknown> = {};
121+
const object: Record<string, unknown> = Object.create(null);
122122
for (const element of elements) {
123123
if (options.excludeInternalFields && element.isInternalField()) {
124124
continue;

packages/mongodb-query-util/src/in-value-range.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export const inValueRange = (
3838
field: any,
3939
d: { value: any; dx?: number; bson?: any }
4040
) => {
41-
const compOperators: Record<string, (a: number, b: number) => boolean> = {
41+
const compOperators: Readonly<
42+
Record<string, (a: number, b: number) => boolean>
43+
> = {
4244
$gte: function (a: number, b: number) {
4345
return a >= b;
4446
},

0 commit comments

Comments
 (0)