forked from emotion-js/emotion
/
label.js
152 lines (131 loc) · 3.75 KB
/
label.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
// @flow
import nodePath from 'path'
type LabelFormatOptions = {
name: string,
path: string
}
const invalidClassNameCharacters = /[!"#$%&'()*+,./:;<=>?@[\]^`|}~{]/g
const sanitizeLabelPart = (labelPart: string) =>
labelPart.trim().replace(invalidClassNameCharacters, '-')
function getLabel(
identifierName?: string,
autoLabel: boolean,
labelFormat?: string | (LabelFormatOptions => string),
filename: string
) {
if (!identifierName || !autoLabel) return null
const sanitizedName = sanitizeLabelPart(identifierName)
if (!labelFormat) {
return sanitizedName
}
if (typeof labelFormat === 'function') {
return labelFormat({
name: sanitizedName,
path: filename
})
}
const parsedPath = nodePath.parse(filename)
let localDirname = nodePath.basename(parsedPath.dir)
let localFilename = parsedPath.name
if (localFilename === 'index') {
localFilename = localDirname
}
return labelFormat
.replace(/\[local\]/gi, sanitizedName)
.replace(/\[filename\]/gi, sanitizeLabelPart(localFilename))
.replace(/\[dirname\]/gi, sanitizeLabelPart(localDirname))
}
export function getLabelFromPath(path: *, state: *, t: *) {
return getLabel(
getIdentifierName(path, t),
state.opts.autoLabel === undefined
? process.env.NODE_ENV !== 'production'
: state.opts.autoLabel,
state.opts.labelFormat,
state.file.opts.filename
)
}
let pascalCaseRegex = /^[A-Z][A-Za-z]+/
function getDeclaratorName(path, t) {
// $FlowFixMe
const parent = path.findParent(
p =>
p.isVariableDeclarator() ||
p.isFunctionDeclaration() ||
p.isFunctionExpression() ||
p.isArrowFunctionExpression() ||
p.isObjectProperty()
)
if (!parent) {
return ''
}
// we probably have a css call assigned to a variable
// so we'll just return the variable name
if (parent.isVariableDeclarator()) {
if (t.isIdentifier(parent.node.id)) {
return parent.node.id.name
}
return ''
}
// we probably have an inline css prop usage
if (parent.isFunctionDeclaration()) {
let { name } = parent.node.id
if (pascalCaseRegex.test(name)) {
return name
}
return ''
}
// we could also have an object property
if (parent.isObjectProperty() && !parent.node.computed) {
return parent.node.key.name
}
let variableDeclarator = path.findParent(p => p.isVariableDeclarator())
if (!variableDeclarator) {
return ''
}
let { name } = variableDeclarator.node.id
if (pascalCaseRegex.test(name)) {
return name
}
return ''
}
function getIdentifierName(path: *, t: *) {
let classOrClassPropertyParent
if (
t.isObjectProperty(path.parentPath) &&
path.parentPath.node.computed === false &&
(t.isIdentifier(path.parentPath.node.key) ||
t.isStringLiteral(path.parentPath.node.key))
) {
return path.parentPath.node.key.name || path.parentPath.node.key.value
}
if (path) {
// $FlowFixMe
classOrClassPropertyParent = path.findParent(
p => t.isClassProperty(p) || t.isClass(p)
)
}
if (classOrClassPropertyParent) {
if (
t.isClassProperty(classOrClassPropertyParent) &&
classOrClassPropertyParent.node.computed === false &&
t.isIdentifier(classOrClassPropertyParent.node.key)
) {
return classOrClassPropertyParent.node.key.name
}
if (
t.isClass(classOrClassPropertyParent) &&
classOrClassPropertyParent.node.id
) {
return t.isIdentifier(classOrClassPropertyParent.node.id)
? classOrClassPropertyParent.node.id.name
: ''
}
}
let declaratorName = getDeclaratorName(path, t)
// if the name starts with _ it was probably generated by babel so we should ignore it
if (declaratorName.charAt(0) === '_') {
return ''
}
return declaratorName
}