Skip to content

Commit adb1b4f

Browse files
committed
refactor: not to matain ReactSymbols
add `react-is` to peerDependencies
1 parent 4ebe15f commit adb1b4f

File tree

4 files changed

+46
-106
lines changed

4 files changed

+46
-106
lines changed

package.json

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
"smoke": "node tests/smoke/run"
2525
},
2626
"lint-staged": {
27-
"*.js": [
28-
"prettier --write \"**/*.{js,json}\"",
29-
"git add"
30-
]
27+
"*.js": ["prettier --write \"**/*.{js,json}\"", "git add"]
3128
},
3229
"author": {
3330
"name": "Algolia, Inc.",
@@ -80,15 +77,13 @@
8077
},
8178
"peerDependencies": {
8279
"react": "^0.14.8 || ^15.0.1 || ^16.0.0 || ^17.0.1",
83-
"react-dom": "^0.14.8 || ^15.0.1 || ^16.0.0 || ^17.0.1"
80+
"react-dom": "^0.14.8 || ^15.0.1 || ^16.0.0 || ^17.0.1",
81+
"react-is": "^16.0.0"
8482
},
8583
"dependencies": {
86-
"@base2/pretty-print-object": "1.0.0",
87-
"is-plain-object": "3.0.1"
84+
"@base2/pretty-print-object": "1.0.0"
8885
},
8986
"jest": {
90-
"setupFilesAfterEnv": [
91-
"<rootDir>tests/setupTests.js"
92-
]
87+
"setupFilesAfterEnv": ["<rootDir>tests/setupTests.js"]
9388
}
9489
}

src/libs/ReactSymbols.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/libs/getComponentNameFromType.js

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
/**
2-
* Core logic copy from https://github.com/facebook/react/blob/28625c6f45423e6edc5ca0e2932281769c0d431e/packages/shared/getComponentNameFromType.js
2+
* Reference from https://github.com/facebook/react/blob/28625c6f45423e6edc5ca0e2932281769c0d431e/packages/shared/getComponentNameFromType.js
33
*
44
* @flow
55
*/
66

77
import type { LazyComponent, ReactContext, ReactProviderType } from 'react';
8-
8+
import { Fragment } from 'react';
99
import {
10-
REACT_CONTEXT_TYPE,
11-
REACT_FORWARD_REF_TYPE,
12-
REACT_FRAGMENT_TYPE,
13-
REACT_PORTAL_TYPE,
14-
REACT_MEMO_TYPE,
15-
REACT_PROFILER_TYPE,
16-
REACT_PROVIDER_TYPE,
17-
REACT_STRICT_MODE_TYPE,
18-
REACT_SUSPENSE_TYPE,
19-
REACT_SUSPENSE_LIST_TYPE,
20-
REACT_LAZY_TYPE,
21-
REACT_CACHE_TYPE,
22-
} from './ReactSymbols';
10+
ContextConsumer,
11+
ContextProvider,
12+
Element,
13+
ForwardRef,
14+
Portal,
15+
Memo,
16+
Profiler,
17+
StrictMode,
18+
Suspense,
19+
SuspenseList,
20+
Lazy,
21+
} from 'react-is';
2322

2423
// Keep in sync with react-reconciler/getComponentNameFromFiber
2524
function getWrappedName(
@@ -55,47 +54,42 @@ function getComponentNameFromType(type: mixed): string | null {
5554
}
5655
// eslint-disable-next-line default-case
5756
switch (type) {
58-
case REACT_FRAGMENT_TYPE:
57+
case Fragment:
5958
return 'Fragment';
60-
case REACT_PORTAL_TYPE:
59+
case Portal:
6160
return 'Portal';
62-
case REACT_PROFILER_TYPE:
61+
case Profiler:
6362
return 'Profiler';
64-
case REACT_STRICT_MODE_TYPE:
63+
case StrictMode:
6564
return 'StrictMode';
66-
case REACT_SUSPENSE_TYPE:
65+
case Suspense:
6766
return 'Suspense';
68-
case REACT_SUSPENSE_LIST_TYPE:
67+
case SuspenseList:
6968
return 'SuspenseList';
70-
case REACT_CACHE_TYPE:
71-
return 'Cache';
69+
// case REACT_CACHE_TYPE:
70+
// return 'Cache';
7271
}
7372
if (typeof type === 'object') {
7473
// eslint-disable-next-line default-case
7574
switch (type.$$typeof) {
76-
case REACT_CONTEXT_TYPE:
77-
// eslint-disable-next-line no-case-declarations
78-
const context: ReactContext<any> = (type: any);
75+
case ContextConsumer:
7976
/**
8077
* in DEV, should get context from `_context`.
8178
* https://github.com/facebook/react/blob/e16d61c3000e2de6217d06b9afad162e883f73c4/packages/react/src/ReactContext.js#L44-L125
8279
*/
83-
return `${getContextName(context._context ?? context)}.Consumer`;
84-
case REACT_PROVIDER_TYPE:
85-
// eslint-disable-next-line no-case-declarations
86-
const provider: ReactProviderType<any> = (type: any);
87-
return `${getContextName(provider._context)}.Provider`;
88-
case REACT_FORWARD_REF_TYPE:
80+
return `${getContextName(type._context ?? type)}.Consumer`;
81+
case ContextProvider:
82+
return `${getContextName(type._context)}.Provider`;
83+
case ForwardRef:
8984
// eslint-disable-next-line no-case-declarations
9085
return getWrappedName(type, type.render, 'ForwardRef');
91-
case REACT_MEMO_TYPE:
92-
// eslint-disable-next-line no-case-declarations
86+
case Memo:
9387
const outerName = (type: any).displayName || null;
9488
if (outerName !== null) {
9589
return outerName;
9690
}
9791
return getComponentNameFromType(type.type) || 'Memo';
98-
case REACT_LAZY_TYPE: {
92+
case Lazy: {
9993
const lazyComponent: LazyComponent<any, any> = (type: any);
10094
const payload = lazyComponent._payload;
10195
const init = lazyComponent._init;

src/parser/parseReactElement.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
/* @flow */
22

33
import React, { type Element as ReactElement, Fragment } from 'react';
4+
import {
5+
isElement,
6+
isForwardRef,
7+
isFragment,
8+
isLazy,
9+
isMemo,
10+
isPortal,
11+
isProfiler,
12+
isStrictMode,
13+
isSuspense,
14+
isSuspenseList,
15+
} from 'react-is';
416
import type { Options } from './../options';
517
import {
618
createStringTreeNode,

0 commit comments

Comments
 (0)