Skip to content

Commit

Permalink
fix: add no-prototype-builtins lint rule and fix occurrences (#12414)
Browse files Browse the repository at this point in the history
  • Loading branch information
Biki-das committed Feb 17, 2022
1 parent 2114b1d commit efc9e67
Show file tree
Hide file tree
Showing 20 changed files with 33 additions and 23 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ module.exports = {
'no-process-env': 'off',
'no-process-exit': 'off',
'no-proto': 'error',
'no-prototype-builtins': 'error',
'no-redeclare': 'warn',
'no-regex-spaces': 'warn',
'no-restricted-imports': [
Expand Down
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ module.exports = {
},

test(val) {
return val && val.hasOwnProperty('foo');
return val && Object.prototype.hasOwnProperty.call(val, 'foo');
},
};
```
Expand Down
4 changes: 2 additions & 2 deletions e2e/snapshot-serializers/__tests__/snapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('snapshot serializers', () => {
// Add plugin that overrides foo specified by Jest config in package.json
expect.addSnapshotSerializer({
print: (val, serialize) => `Foo: ${serialize(val.foo)}`,
test: val => val && val.hasOwnProperty('foo'),
test: val => val && Object.prototype.hasOwnProperty.call(val, 'foo'),
});
expect(test).toMatchSnapshot();
});
Expand All @@ -87,7 +87,7 @@ describe('snapshot serializers', () => {
// Add plugin that overrides preceding added plugin
expect.addSnapshotSerializer({
print: (val, serialize) => `FOO: ${serialize(val.foo)}`,
test: val => val && val.hasOwnProperty('foo'),
test: val => val && Object.prototype.hasOwnProperty.call(val, 'foo'),
});
expect(test).toMatchSnapshot();
});
Expand Down
2 changes: 1 addition & 1 deletion e2e/snapshot-serializers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

exports.createPlugin = prop => ({
print: (val, serialize) => `${prop} - ${serialize(val[prop])}`,
test: val => val && val.hasOwnProperty(prop),
test: val => val && Object.prototype.hasOwnProperty.call(val, prop),
});
2 changes: 1 addition & 1 deletion packages/expect/src/jestMatchersObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object');
// Jest may override the stack trace of Errors thrown by internal matchers.
export const INTERNAL_MATCHER_FLAG = Symbol.for('$$jest-internal-matcher');

if (!global.hasOwnProperty(JEST_MATCHERS_OBJECT)) {
if (!Object.prototype.hasOwnProperty.call(global, JEST_MATCHERS_OBJECT)) {
const defaultState: Partial<MatcherState> = {
assertionCalls: 0,
expectedAssertionsNumber: null,
Expand Down
10 changes: 8 additions & 2 deletions packages/jest-cli/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import type {Config} from '@jest/types';
import {constants, isJSONString} from 'jest-config';

export function check(argv: Config.Argv): true {
if (argv.runInBand && argv.hasOwnProperty('maxWorkers')) {
if (
argv.runInBand &&
Object.prototype.hasOwnProperty.call(argv, 'maxWorkers')
) {
throw new Error(
'Both --runInBand and --maxWorkers were specified, but these two ' +
'options do not make sense together. Which is it?',
Expand Down Expand Up @@ -46,7 +49,10 @@ export function check(argv: Config.Argv): true {
);
}

if (argv.hasOwnProperty('maxWorkers') && argv.maxWorkers === undefined) {
if (
Object.prototype.hasOwnProperty.call(argv, 'maxWorkers') &&
argv.maxWorkers === undefined
) {
throw new Error(
'The --maxWorkers (-w) option requires a number or string to be specified.\n' +
'Example usage: jest --maxWorkers 2\n' +
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-each/src/table/interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function getPath(
template: Template,
[head, ...tail]: Array<string>,
): unknown {
if (!head || !template.hasOwnProperty || !template.hasOwnProperty(head))
if (!head || !Object.prototype.hasOwnProperty.call(template, head))
return template;
return getPath(template[head] as Template, tail);
}
6 changes: 4 additions & 2 deletions packages/jest-fake-timers/src/legacyFakeTimers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ export default class FakeTimers<TimerRef> {
break;
}

if (!this._cancelledTicks.hasOwnProperty(tick.uuid)) {
if (
!Object.prototype.hasOwnProperty.call(this._cancelledTicks, tick.uuid)
) {
// Callback may throw, so update the map prior calling.
this._cancelledTicks[tick.uuid] = true;
tick.callback();
Expand Down Expand Up @@ -472,7 +474,7 @@ export default class FakeTimers<TimerRef> {

const cancelledTicks = this._cancelledTicks;
this._timerAPIs.nextTick(() => {
if (!cancelledTicks.hasOwnProperty(uuid)) {
if (!Object.prototype.hasOwnProperty.call(cancelledTicks, uuid)) {
// Callback may throw, so update the map prior calling.
cancelledTicks[uuid] = true;
callback.apply(null, args);
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-mock/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ describe('moduleMocker', () => {

moduleMocker.spyOn(child, 'func').mockReturnValue('efgh');

expect(child.hasOwnProperty('func')).toBe(true);
expect(Object.prototype.hasOwnProperty.call(child, 'func')).toBe(true);
expect(child.func()).toEqual('efgh');
expect(parent.func()).toEqual('abcd');
});
Expand All @@ -569,7 +569,7 @@ describe('moduleMocker', () => {

moduleMocker.spyOn(parent, 'func').mockReturnValue('jklm');

expect(child.hasOwnProperty('func')).toBe(false);
expect(Object.prototype.hasOwnProperty.call(child, 'func')).toBe(false);
expect(child.func()).toEqual('jklm');
});

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-util/src/__tests__/createProcessObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ it('checks that process.env works as expected in Windows platforms', () => {
// You can delete through case-insensitiveness too.
delete fake.prop_string;

expect(fake.hasOwnProperty('PROP_STRING')).toBe(false);
expect(fake.hasOwnProperty('PROP_string')).toBe(false);
expect(Object.prototype.hasOwnProperty.call('PROP_string')).toBe(false);
expect(Object.prototype.hasOwnProperty.call('PROP_string')).toBe(false);
});
2 changes: 1 addition & 1 deletion packages/jest-util/src/createProcessObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createProcessEnv(): NodeJS.ProcessEnv {

function deletePropertyWin32(_target: unknown, key: unknown) {
for (const name in real) {
if (real.hasOwnProperty(name)) {
if (Object.prototype.hasOwnProperty.call(real, name)) {
if (typeof key === 'string') {
if (name.toLowerCase() === key.toLowerCase()) {
delete real[name];
Expand Down
1 change: 1 addition & 0 deletions packages/jest-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class Worker {
return;
}

// eslint-disable-next-line no-prototype-builtins
if (this.constructor.prototype.hasOwnProperty(name)) {
throw new TypeError('Cannot define a method called ' + name);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ export const DEFAULT_OPTIONS: Options = {

function validateOptions(options: OptionsReceived) {
Object.keys(options).forEach(key => {
if (!DEFAULT_OPTIONS.hasOwnProperty(key)) {
if (!Object.prototype.hasOwnProperty.call(DEFAULT_OPTIONS, key)) {
throw new Error(`pretty-format: Unknown option "${key}".`);
}
});
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-25.x/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ module.exports = {
},

test(val) {
return val && val.hasOwnProperty('foo');
return val && Object.prototype.hasOwnProperty.call(val, 'foo');
},
};
```
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-26.x/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ module.exports = {
},

test(val) {
return val && val.hasOwnProperty('foo');
return val && Object.prototype.hasOwnProperty.call(val, 'foo');
},
};
```
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-27.0/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ module.exports = {
},

test(val) {
return val && val.hasOwnProperty('foo');
return val && Object.prototype.hasOwnProperty.call(val, 'foo');
},
};
```
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-27.1/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ module.exports = {
},

test(val) {
return val && val.hasOwnProperty('foo');
return val && Object.prototype.hasOwnProperty.call(val, 'foo');
},
};
```
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-27.2/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ module.exports = {
},

test(val) {
return val && val.hasOwnProperty('foo');
return val && Object.prototype.hasOwnProperty.call(val, 'foo');
},
};
```
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-27.4/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ module.exports = {
},

test(val) {
return val && val.hasOwnProperty('foo');
return val && Object.prototype.hasOwnProperty.call(val, 'foo');
},
};
```
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-27.5/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ module.exports = {
},

test(val) {
return val && val.hasOwnProperty('foo');
return val && Object.prototype.hasOwnProperty.call(val, 'foo');
},
};
```
Expand Down

0 comments on commit efc9e67

Please sign in to comment.