-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
Copy pathgetAttributes.js
54 lines (45 loc) · 1.49 KB
/
getAttributes.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
import * as t from '@babel/types'
import { isNumeric, kebabCase, replaceSpaces } from './util'
import stringToObjectStyle from './stringToObjectStyle'
import { ATTRIBUTE_MAPPING, ELEMENT_ATTRIBUTE_MAPPING } from './mappings'
function getKey(key, value, node) {
const kebabKey = kebabCase(key)
if (kebabKey.startsWith('aria-') || kebabKey.startsWith('data-')) {
return t.jsxIdentifier(kebabKey)
}
const lowerCaseKey = key.toLowerCase()
const mappedElementAttribute =
ELEMENT_ATTRIBUTE_MAPPING[node.name] &&
ELEMENT_ATTRIBUTE_MAPPING[node.name][lowerCaseKey]
const mappedAttribute = ATTRIBUTE_MAPPING[lowerCaseKey]
return t.jsxIdentifier(mappedElementAttribute || mappedAttribute || key)
}
function getValue(key, value) {
// Handle className
if (Array.isArray(value)) {
return t.stringLiteral(replaceSpaces(value.join(' ')))
}
if (key === 'style') {
return t.jsxExpressionContainer(stringToObjectStyle(value))
}
if (isNumeric(value)) {
return t.jsxExpressionContainer(t.numericLiteral(Number(value)))
}
return t.stringLiteral(replaceSpaces(value))
}
const getAttributes = node => {
const keys = Object.keys(node.properties)
const attributes = []
let index = -1
while (++index < keys.length) {
const key = keys[index]
const value = node.properties[key]
const attribute = t.jsxAttribute(
getKey(key, value, node),
getValue(key, value, node),
)
attributes.push(attribute)
}
return attributes
}
export default getAttributes