-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathget-styled-options.js
57 lines (47 loc) · 1.47 KB
/
get-styled-options.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
// @flow
import { getLabelFromPath } from './label'
import { getTargetClassName } from './get-target-class-name'
const getKnownProperties = (t: *, node: *) =>
new Set(
node.properties
.filter(n => t.isObjectProperty(n) && !n.computed)
.map(n => (t.isIdentifier(n.key) ? n.key.name : n.key.value))
)
export let getStyledOptions = (t: *, path: *, state: *) => {
const autoLabel = state.opts.autoLabel || 'dev-only'
let args = path.node.arguments
let optionsArgument = args.length >= 2 ? args[1] : null
let properties = []
let knownProperties =
optionsArgument && t.isObjectExpression(optionsArgument)
? getKnownProperties(t, optionsArgument)
: new Set()
if (!knownProperties.has('target')) {
properties.push(
t.objectProperty(
t.identifier('target'),
t.stringLiteral(getTargetClassName(state, t))
)
)
}
let label = autoLabel !== 'never' ? getLabelFromPath(path, state, t) : null
if (label && !knownProperties.has('label')) {
properties.push(
t.objectProperty(t.identifier('label'), t.stringLiteral(label))
)
}
if (optionsArgument) {
if (!t.isObjectExpression(optionsArgument)) {
return t.callExpression(state.file.addHelper('extends'), [
t.objectExpression([]),
t.objectExpression(properties),
optionsArgument
])
}
properties.unshift(...optionsArgument.properties)
}
return t.objectExpression(
// $FlowFixMe
properties
)
}