/
create-serializer.js
236 lines (214 loc) Β· 5.56 KB
/
create-serializer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// @flow
import prettify from '@emotion/css-prettifier'
import { replaceClassNames } from './replace-class-names'
import {
getClassNamesFromNodes,
isReactElement,
isEmotionCssPropElementType,
isEmotionCssPropEnzymeElement,
isDOMElement,
getStylesFromClassNames,
getStyleElements,
getKeys,
flatMap,
isPrimitive,
hasIntersection
} from './utils'
function getNodes(node, nodes = []) {
if (Array.isArray(node)) {
for (let child of node) {
getNodes(child, nodes)
}
return nodes
}
if (typeof node === 'object') {
nodes.push(node)
}
if (node.children) {
for (let child of node.children) {
getNodes(child, nodes)
}
}
return nodes
}
function copyProps(src, target) {
return Object.defineProperties(src, {
...Object.getOwnPropertyDescriptors(target)
})
}
function deepTransform(node, transform) {
if (Array.isArray(node)) {
return node.map(child => deepTransform(child, transform))
}
const transformed: any = transform(node)
if (transformed !== node) {
if (transformed.props) {
copyProps(transformed, {
props: Object.entries(transformed.props).reduce(
(props, [key, value]) =>
Object.assign(props, {
[key]: deepTransform(value, transform)
}),
{}
)
})
}
if (transformed.children) {
return copyProps(transformed, {
// flatMap to allow a child of <A><B /><C /></A> to be transformed to <B /><C />
children: flatMap(
(deepTransform(transformed.children, transform): any),
id => id
)
})
}
}
return transformed
}
function getPrettyStylesFromClassNames(
classNames: Array<string>,
elements: Array<HTMLStyleElement>,
indentation: string
) {
return prettify(getStylesFromClassNames(classNames, elements), indentation)
}
export type Options = {
classNameReplacer?: (className: string, index: number) => string,
DOMElements?: boolean
}
function filterEmotionProps(props = {}) {
const {
css,
__EMOTION_TYPE_PLEASE_DO_NOT_USE__,
__EMOTION_LABEL_PLEASE_DO_NOT_USE__,
...rest
} = props
rest.css = 'unknown styles'
return rest
}
function isShallowEnzymeElement(element: any, classNames: string[]) {
const delimiter = ' '
const childClassNames = flatMap(element.children || [], ({ props = {} }) =>
(props.className || '').split(delimiter)
).filter(Boolean)
return !hasIntersection(classNames, childClassNames)
}
const createConvertEmotionElements = (keys: string[], printer: *) => (
node: any
) => {
if (isPrimitive(node)) {
return node
}
if (isEmotionCssPropEnzymeElement(node)) {
const cssClassNames = (node.props.css.name || '').split(' ')
const expectedClassNames = flatMap(cssClassNames, cssClassName =>
keys.map(key => `${key}-${cssClassName}`)
)
// if this is a shallow element, we need to manufacture the className
// since the underlying component is not rendered.
if (isShallowEnzymeElement(node, expectedClassNames)) {
const className = [node.props.className]
.concat(expectedClassNames)
.filter(Boolean)
.join(' ')
const emotionType = node.props.__EMOTION_TYPE_PLEASE_DO_NOT_USE__
// emotionType will be a string for DOM elements
const type =
typeof emotionType === 'string' ? emotionType : emotionType.name
return {
...node,
props: filterEmotionProps({
...node.props,
className
}),
type
}
} else {
return node.children
}
}
if (isEmotionCssPropElementType(node)) {
return {
...node,
props: filterEmotionProps(node.props),
type: node.props.__EMOTION_TYPE_PLEASE_DO_NOT_USE__
}
}
if (isReactElement(node)) {
return copyProps({}, node)
}
return node
}
function clean(node: any, classNames: string[]) {
if (Array.isArray(node)) {
for (const child of node) {
clean(child, classNames)
}
return
}
if (node.children) {
for (const child of node.children) {
clean(child, classNames)
}
}
if (node.props) {
const { className } = node.props
if (!className) {
// if it's empty, remove it
delete node.props.className
} else {
const hasKnownClass = hasIntersection(className.split(' '), classNames)
if (hasKnownClass) {
delete node.props.css
}
}
}
}
export function createSerializer({
classNameReplacer,
DOMElements = true
}: Options = {}) {
const cache = new WeakSet()
const isTransformed = val => cache.has(val)
function serialize(
val: *,
config: *,
indentation: string,
depth: number,
refs: *,
printer: Function
) {
const elements = getStyleElements()
const keys = getKeys(elements)
const convertEmotionElements = createConvertEmotionElements(keys, printer)
const converted = deepTransform(val, convertEmotionElements)
const nodes = getNodes(converted)
const classNames = getClassNamesFromNodes(nodes)
const styles = getPrettyStylesFromClassNames(
classNames,
elements,
config.indent
)
clean(converted, classNames)
nodes.forEach(cache.add, cache)
const printedVal = printer(converted, config, indentation, depth, refs)
nodes.forEach(cache.delete, cache)
return replaceClassNames(
classNames,
styles,
printedVal,
keys,
classNameReplacer
)
}
return {
test(val: *) {
return (
val &&
(!isTransformed(val) &&
(isReactElement(val) || (DOMElements && isDOMElement(val))))
)
},
serialize
}
}