Skip to content

Commit b22a7b7

Browse files
committed
Made fragment/async/strict mode checks more strict
1 parent a06e6f7 commit b22a7b7

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

packages/react-is/src/ReactIs.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export function typeOf(object: any) {
4545
case REACT_ASYNC_MODE_TYPE:
4646
case REACT_FRAGMENT_TYPE:
4747
case REACT_STRICT_MODE_TYPE:
48-
return maybeType;
48+
if (getTypeOf(object) === REACT_ELEMENT_TYPE) {
49+
return maybeType;
50+
}
4951
}
5052

5153
maybeType = getTypeTypeOf(object);
@@ -72,7 +74,10 @@ export const Portal = REACT_PORTAL_TYPE;
7274
export const StrictMode = REACT_STRICT_MODE_TYPE;
7375

7476
export function isAsyncMode(object: any) {
75-
return getType(object) === REACT_ASYNC_MODE_TYPE;
77+
return (
78+
getType(object) === REACT_ASYNC_MODE_TYPE &&
79+
getTypeOf(object) === REACT_ELEMENT_TYPE
80+
);
7681
}
7782
export function isContextConsumer(object: any) {
7883
return getTypeTypeOf(object) === REACT_CONTEXT_TYPE;
@@ -84,11 +89,17 @@ export function isElement(object: any) {
8489
return getTypeOf(object) === REACT_ELEMENT_TYPE;
8590
}
8691
export function isFragment(object: any) {
87-
return getType(object) === REACT_FRAGMENT_TYPE;
92+
return (
93+
getType(object) === REACT_FRAGMENT_TYPE &&
94+
getTypeOf(object) === REACT_ELEMENT_TYPE
95+
);
8896
}
8997
export function isPortal(object: any) {
9098
return getTypeOf(object) === REACT_PORTAL_TYPE;
9199
}
92100
export function isStrictMode(object: any) {
93-
return getType(object) === REACT_STRICT_MODE_TYPE;
101+
return (
102+
getType(object) === REACT_STRICT_MODE_TYPE &&
103+
getTypeOf(object) === REACT_ELEMENT_TYPE
104+
);
94105
}

packages/react-is/src/__tests__/ReactIs-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('ReactIs', () => {
2626
ReactIs.AsyncMode,
2727
);
2828
expect(ReactIs.isAsyncMode(<React.unstable_AsyncMode />)).toBe(true);
29+
expect(ReactIs.isAsyncMode({type: ReactIs.AsyncMode})).toBe(false);
2930
expect(ReactIs.isAsyncMode(<React.StrictMode />)).toBe(false);
3031
expect(ReactIs.isAsyncMode(<div />)).toBe(false);
3132
});
@@ -65,6 +66,7 @@ describe('ReactIs', () => {
6566
it('should identify fragments', () => {
6667
expect(ReactIs.typeOf(<React.Fragment />)).toBe(ReactIs.Fragment);
6768
expect(ReactIs.isFragment(<React.Fragment />)).toBe(true);
69+
expect(ReactIs.isFragment({type: ReactIs.Fragment})).toBe(false);
6870
expect(ReactIs.isFragment('React.Fragment')).toBe(false);
6971
expect(ReactIs.isFragment(<div />)).toBe(false);
7072
expect(ReactIs.isFragment([])).toBe(false);
@@ -81,6 +83,7 @@ describe('ReactIs', () => {
8183
it('should identify strict mode', () => {
8284
expect(ReactIs.typeOf(<React.StrictMode />)).toBe(ReactIs.StrictMode);
8385
expect(ReactIs.isStrictMode(<React.StrictMode />)).toBe(true);
86+
expect(ReactIs.isStrictMode({type: ReactIs.StrictMode})).toBe(false);
8487
expect(ReactIs.isStrictMode(<React.unstable_AsyncMode />)).toBe(false);
8588
expect(ReactIs.isStrictMode(<div />)).toBe(false);
8689
});

0 commit comments

Comments
 (0)