Skip to content

Commit

Permalink
[*] [refactor] use hasown instead of has
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 9, 2024
1 parent 9ddc423 commit f2401cc
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/enzyme-adapter-react-16/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"dependencies": {
"enzyme-adapter-utils": "^1.14.1",
"enzyme-shallow-equal": "^1.0.5",
"has": "^1.0.3",
"hasown": "^2.0.0",
"object.assign": "^4.1.5",
"object.values": "^1.1.7",
"prop-types": "^15.8.1",
Expand Down
8 changes: 4 additions & 4 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { version as testRendererVersion } from 'react-test-renderer/package.json
import TestUtils from 'react-dom/test-utils';
import semver from 'semver';
import checkPropTypes from 'prop-types/checkPropTypes';
import has from 'has';
import hasOwn from 'hasown';
import {
AsyncMode,
ConcurrentMode,
Expand Down Expand Up @@ -458,7 +458,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {

createMountRenderer(options) {
assertDomAvailable('mount');
if (has(options, 'suspenseFallback')) {
if (hasOwn(options, 'suspenseFallback')) {
throw new TypeError('`suspenseFallback` is not supported by the `mount` renderer');
}
if (FiberTags === null) {
Expand Down Expand Up @@ -613,7 +613,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
// Wrap functional components on versions prior to 16.5,
// to avoid inadvertently pass a `this` instance to it.
const wrapFunctionalComponent = (Component) => {
if (is166 && has(Component, 'defaultProps')) {
if (is166 && hasOwn(Component, 'defaultProps')) {
if (lastComponent !== Component) {
wrappedComponent = Object.assign(
// eslint-disable-next-line new-cap
Expand Down Expand Up @@ -819,7 +819,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
}

createStringRenderer(options) {
if (has(options, 'suspenseFallback')) {
if (hasOwn(options, 'suspenseFallback')) {
throw new TypeError('`suspenseFallback` should not be specified in options of string renderer');
}
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme-adapter-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"dependencies": {
"airbnb-prop-types": "^2.16.0",
"function.prototype.name": "^1.1.6",
"has": "^1.0.3",
"hasown": "^2.0.0",
"object.assign": "^4.1.5",
"object.fromentries": "^2.0.7",
"prop-types": "^15.8.1",
Expand Down
14 changes: 7 additions & 7 deletions packages/enzyme-adapter-utils/src/Utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import functionName from 'function.prototype.name';
import fromEntries from 'object.fromentries';
import has from 'has';
import hasOwn from 'hasown';
import createMountWrapper from './createMountWrapper';
import createRenderWrapper from './createRenderWrapper';
import wrap from './wrapWithSimpleWrapper';
Expand Down Expand Up @@ -380,9 +380,9 @@ export function compareNodeTypeOf(node, matchingTypeOf) {
export function spyMethod(instance, methodName, getStub = () => {}) {
let lastReturnValue;
const originalMethod = instance[methodName];
const hasOwn = has(instance, methodName);
const hasOwnProp = hasOwn(instance, methodName);
let descriptor;
if (hasOwn) {
if (hasOwnProp) {
descriptor = Object.getOwnPropertyDescriptor(instance, methodName);
}
Object.defineProperty(instance, methodName, {
Expand All @@ -396,7 +396,7 @@ export function spyMethod(instance, methodName, getStub = () => {}) {
});
return {
restore() {
if (hasOwn) {
if (hasOwnProp) {
if (descriptor) {
Object.defineProperty(instance, methodName, descriptor);
} else {
Expand All @@ -419,9 +419,9 @@ export function spyMethod(instance, methodName, getStub = () => {}) {
// TODO: when enzyme v3.12.0 is required, delete this
export function spyProperty(instance, propertyName, handlers = {}) {
const originalValue = instance[propertyName];
const hasOwn = has(instance, propertyName);
const hasOwnProp = hasOwn(instance, propertyName);
let descriptor;
if (hasOwn) {
if (hasOwnProp) {
descriptor = Object.getOwnPropertyDescriptor(instance, propertyName);
}
let wasAssigned = false;
Expand Down Expand Up @@ -450,7 +450,7 @@ export function spyProperty(instance, propertyName, handlers = {}) {

return {
restore() {
if (hasOwn) {
if (hasOwnProp) {
if (descriptor) {
Object.defineProperty(instance, propertyName, descriptor);
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme-shallow-equal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"license": "MIT",
"dependencies": {
"has": "^1.0.3",
"hasown": "^2.0.0",
"object-is": "^1.1.5"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/enzyme-shallow-equal/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import is from 'object-is';
import has from 'has';
import hasOwn from 'hasown';

// adapted from https://github.com/facebook/react/blob/144328fe81719e916b946e22660479e31561bb0b/packages/shared/shallowEqual.js#L36-L68
export default function shallowEqual(objA, objB) {
Expand All @@ -23,7 +23,7 @@ export default function shallowEqual(objA, objB) {

// Test for A's keys different from B.
for (let i = 0; i < keysA.length; i += 1) {
if (!has(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
if (!hasOwn(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"cheerio": "=1.0.0-rc.3",
"enzyme-shallow-equal": "^1.0.5",
"function.prototype.name": "^1.1.6",
"has": "^1.0.3",
"hasown": "^2.0.0",
"html-element-map": "^1.3.1",
"is-boolean-object": "^1.1.2",
"is-callable": "^1.2.7",
Expand Down
14 changes: 7 additions & 7 deletions packages/enzyme/src/Debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import isNumber from 'is-number-object';
import isCallable from 'is-callable';
import isBoolean from 'is-boolean-object';
import inspect from 'object-inspect';
import has from 'has';
import hasOwn from 'hasown';

import {
propsOfNode,
Expand Down Expand Up @@ -69,13 +69,13 @@ function indentChildren(childrenStrs, indentLength) {
}

function isRSTNodeLike(node) {
return has(node, 'nodeType')
return hasOwn(node, 'nodeType')
&& typeof node.nodeType === 'string'
&& has(node, 'type')
&& has(node, 'key')
&& has(node, 'ref')
&& has(node, 'instance')
&& has(node, 'rendered');
&& hasOwn(node, 'type')
&& hasOwn(node, 'key')
&& hasOwn(node, 'ref')
&& hasOwn(node, 'instance')
&& hasOwn(node, 'rendered');
}

export function debugNode(node, indentLength = 2, options = {}) {
Expand Down
4 changes: 2 additions & 2 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flat from 'array.prototype.flat';
import has from 'has';
import hasOwn from 'hasown';
import trim from 'string.prototype.trim';

import {
Expand Down Expand Up @@ -873,7 +873,7 @@ class ReactWrapper {
throw new TypeError('ReactWrapper::renderProp(): `propName` must be a string');
}
const props = this.props();
if (!has(props, propName)) {
if (!hasOwn(props, propName)) {
throw new Error(`ReactWrapper::renderProp(): no prop called “${propName}“ found`);
}
const propValue = props[propName];
Expand Down
4 changes: 2 additions & 2 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flat from 'array.prototype.flat';
import has from 'has';
import hasOwn from 'hasown';
import shallowEqual from 'enzyme-shallow-equal';
import trim from 'string.prototype.trim';

Expand Down Expand Up @@ -1351,7 +1351,7 @@ class ShallowWrapper {
throw new TypeError('ShallowWrapper::renderProp(): `propName` must be a string');
}
const props = this.props();
if (!has(props, propName)) {
if (!hasOwn(props, propName)) {
throw new Error(`ShallowWrapper::renderProp(): no prop called “${propName}“ found`);
}
const propValue = props[propName];
Expand Down
14 changes: 7 additions & 7 deletions packages/enzyme/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import is from 'object-is';
import entries from 'object.entries';
import fromEntries from 'object.fromentries';
import functionName from 'function.prototype.name';
import has from 'has';
import hasOwn from 'hasown';
import flat from 'array.prototype.flat';
import trim from 'string.prototype.trim';
import cheerio from 'cheerio';
Expand Down Expand Up @@ -298,9 +298,9 @@ export function cloneElement(adapter, el, props) {
export function spyMethod(instance, methodName, getStub = () => {}) {
let lastReturnValue;
const originalMethod = instance[methodName];
const hasOwn = has(instance, methodName);
const hasOwnProp = hasOwn(instance, methodName);
let descriptor;
if (hasOwn) {
if (hasOwnProp) {
descriptor = Object.getOwnPropertyDescriptor(instance, methodName);
}
Object.defineProperty(instance, methodName, {
Expand All @@ -314,7 +314,7 @@ export function spyMethod(instance, methodName, getStub = () => {}) {
});
return {
restore() {
if (hasOwn) {
if (hasOwnProp) {
if (descriptor) {
Object.defineProperty(instance, methodName, descriptor);
} else {
Expand All @@ -336,9 +336,9 @@ export function spyMethod(instance, methodName, getStub = () => {}) {

export function spyProperty(instance, propertyName, handlers = {}) {
const originalValue = instance[propertyName];
const hasOwn = has(instance, propertyName);
const hasOwnProp = hasOwn(instance, propertyName);
let descriptor;
if (hasOwn) {
if (hasOwnProp) {
descriptor = Object.getOwnPropertyDescriptor(instance, propertyName);
}
let wasAssigned = false;
Expand Down Expand Up @@ -367,7 +367,7 @@ export function spyProperty(instance, propertyName, handlers = {}) {

return {
restore() {
if (hasOwn) {
if (hasOwnProp) {
if (descriptor) {
Object.defineProperty(instance, propertyName, descriptor);
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/enzyme/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createParser } from 'rst-selector-parser';
import values from 'object.values';
import flat from 'array.prototype.flat';
import is from 'object-is';
import has from 'has';
import hasOwn from 'hasown';
import elementsByConstructor from 'html-element-map/byConstructor';
import {
treeFilter,
Expand Down Expand Up @@ -81,7 +81,7 @@ function matchAttributeSelector(node, token) {
return false;
}
if (token.type === ATTRIBUTE_PRESENCE) {
return has(nodeProps, token.name);
return hasOwn(nodeProps, token.name);
}
// Only the exact value operator ("=") can match non-strings
if (typeof nodePropValue !== 'string' || typeof value !== 'string') {
Expand Down

0 comments on commit f2401cc

Please sign in to comment.