-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
182 lines (166 loc) · 5.81 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
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
const webComponentName = 'data-component';
const webElementName = 'data-element';
const webSourceFileName = 'data-source-file';
const nativeComponentName = 'dataComponent';
const nativeElementName = 'dataElement';
const nativeSourceFileName = 'dataSourceFile';
const nativeOptionName = 'native';
module.exports = function({ types: t }) {
return {
visitor: {
FunctionDeclaration(path, state) {
if (!path.node.id || !path.node.id.name) return
functionBodyPushAttributes(
t,
path,
path.node.id.name,
sourceFileNameFromState(state),
...attributeNamesFromState(state)
)
},
ArrowFunctionExpression(path, state) {
if (!path.parent.id || !path.parent.id.name) return
functionBodyPushAttributes(
t,
path,
path.parent.id.name,
sourceFileNameFromState(state),
...attributeNamesFromState(state)
)
},
ClassDeclaration(path, state) {
const name = path.get('id')
const properties = path.get('body').get('body')
const render = properties.find(prop => {
return (
prop.isClassMethod() &&
prop.get('key').isIdentifier({ name: 'render' })
)
})
if (!render || !render.traverse) return
render.traverse({
ReturnStatement(returnStatement) {
const arg = returnStatement.get('argument')
if (!arg.isJSXElement()) return
processJSXElement(
t,
arg,
name.node && name.node.name,
sourceFileNameFromState(state),
...attributeNamesFromState(state)
)
}
})
},
}
}
}
function sourceFileNameFromState(state) {
const name = state.file.opts.parserOpts.sourceFileName
if (typeof name === 'string') return name.split('/').pop()
return undefined
}
function attributeNamesFromState(state) {
if(state.opts[nativeOptionName] === true) {
return [nativeComponentName, nativeElementName, nativeSourceFileName]
}
return [webComponentName, webElementName, webSourceFileName]
}
function isReactFragment(openingElement) {
if (
!openingElement.node ||
!openingElement.node.name ||
!openingElement.node.name.name ||
!openingElement.node.name.type ||
!openingElement.node.name.object ||
!openingElement.node.name.property
) return
return (
openingElement.node.name.name === 'Fragment' ||
(openingElement.node.name.type === 'JSXMemberExpression' &&
openingElement.node.name.object.name === 'React' &&
openingElement.node.name.property.name === 'Fragment')
)
}
function applyAttributes(t, openingElement, componentName, sourceFileName, componentAttributeName, elementAttributeName, sourceFileAttributeName) {
if (!openingElement
|| isReactFragment(openingElement)
|| !openingElement.node
|| !openingElement.node.name
) {
return
}
if (!openingElement.node.attributes) openingElement.node.attributes = {}
// Add a stable attribute for the element name
if(openingElement.node.attributes.find(node => {
if (!node.name) return
return node.name.name === elementAttributeName
}) == null){
openingElement.node.attributes.push(
t.jSXAttribute(
t.jSXIdentifier(elementAttributeName),
t.stringLiteral(openingElement.node.name.name || 'unknown')
)
)
}
// Add a stable attribute for the component name (absent for non-root elements)
if(componentName && (openingElement.node.attributes.find(node => {
if (!node.name) return
return node.name.name === componentAttributeName
}) == null)){
openingElement.node.attributes.push(
t.jSXAttribute(
t.jSXIdentifier(componentAttributeName),
t.stringLiteral(componentName)
)
)
}
// Add a stable attribute for the source file name (absent for non-root elements)
if(sourceFileName && (openingElement.node.attributes.find(node => {
if (!node.name) return
return node.name.name === sourceFileAttributeName
}) == null)){
openingElement.node.attributes.push(
t.jSXAttribute(
t.jSXIdentifier(sourceFileAttributeName),
t.stringLiteral(sourceFileName)
)
)
}
}
function processJSXElement(t, jsxElement, componentName, sourceFileName, componentAttributeName, elementAttributeName, sourceFileAttributeName) {
if (!jsxElement) {
return
}
applyAttributes(t, jsxElement.get('openingElement'), componentName, sourceFileName, componentAttributeName, elementAttributeName, sourceFileAttributeName)
const children = jsxElement.get('children')
if (children && children.length) {
// Children don't receive the data-component or data-source-file attribute so we pass null for componentName and sourceFileName
children.forEach(element => processJSXElement(t, element, null, null, componentAttributeName, elementAttributeName, sourceFileAttributeName))
}
}
function functionBodyPushAttributes(t, path, componentName, sourceFileName, componentAttributeName, elementAttributeName, sourceFileAttributeName) {
let jsxElement = null
const functionBody = path.get('body').get('body')
if (functionBody.parent && functionBody.parent.type === 'JSXElement') {
const maybeJsxElement = functionBody.find(c => {
return c.type === 'JSXElement'
})
if (!maybeJsxElement) return
jsxElement = maybeJsxElement
} else {
const returnStatement = functionBody.find(c => {
return c.type === 'ReturnStatement'
})
if (!returnStatement) {
return
}
const arg = returnStatement.get('argument')
if (!arg || !arg.isJSXElement()) {
return
}
jsxElement = arg
}
if (!jsxElement) return
processJSXElement(t, jsxElement, componentName, sourceFileName, componentAttributeName, elementAttributeName, sourceFileAttributeName)
}