-
-
Notifications
You must be signed in to change notification settings - Fork 425
/
Copy pathindex.js
42 lines (35 loc) · 1.06 KB
/
index.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
const elements = ['svg', 'Svg']
const plugin = ({ types: t }) => ({
visitor: {
JSXOpeningElement: {
enter(path) {
if (
!elements.some((element) =>
path.get('name').isJSXIdentifier({ name: element }),
)
)
return
const requiredAttributes = ['width', 'height']
const attributeValue = '1em'
path.get('attributes').forEach((attributePath) => {
if (!attributePath.isJSXAttribute()) return
const index = requiredAttributes.indexOf(attributePath.node.name.name)
if (index === -1) return
const value = attributePath.get('value')
value.replaceWith(t.stringLiteral(attributeValue))
requiredAttributes.splice(index, 1)
})
requiredAttributes.forEach((attribute) => {
path.pushContainer(
'attributes',
t.jsxAttribute(
t.jsxIdentifier(attribute),
t.stringLiteral(attributeValue),
),
)
})
},
},
},
})
export default plugin