Skip to content

Commit 69149e8

Browse files
authored
Merge pull request #29 from shepitchakpavlo/dont-override-attribute
Don't override attribute
2 parents 1218518 + 1b83b57 commit 69149e8

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

src/index.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ function isReactFragment(openingElement) {
1010
)
1111
}
1212

13+
function applyAttribute({ openingElement, t, name, options }) {
14+
if (!openingElement || isReactFragment(openingElement)) return
15+
16+
const isAttributeAlreadySet = openingElement.node.attributes.find(
17+
node => node.name.name === options.attribute
18+
)
19+
20+
if (isAttributeAlreadySet) return
21+
22+
openingElement.node.attributes.push(
23+
t.jSXAttribute(
24+
t.jSXIdentifier(options.attribute),
25+
t.stringLiteral(options.format(name))
26+
)
27+
)
28+
}
29+
1330
function functionBodyPushAttributes(t, path, options, componentName) {
1431
let openingElement = null
1532
const functionBody = path.get('body').get('body')
@@ -31,15 +48,7 @@ function functionBodyPushAttributes(t, path, options, componentName) {
3148
openingElement = arg.get('openingElement')
3249
}
3350

34-
if (!openingElement) return
35-
if (isReactFragment(openingElement)) return
36-
37-
openingElement.node.attributes.push(
38-
t.jSXAttribute(
39-
t.jSXIdentifier(options.attribute),
40-
t.stringLiteral(options.format(componentName))
41-
)
42-
)
51+
applyAttribute({ openingElement, t, name: componentName, options })
4352
}
4453

4554
export default function({ types: t }) {
@@ -84,14 +93,13 @@ export default function({ types: t }) {
8493
if (!arg.isJSXElement()) return
8594

8695
const openingElement = arg.get('openingElement')
87-
if (isReactFragment(openingElement)) return
88-
89-
openingElement.node.attributes.push(
90-
t.jSXAttribute(
91-
t.jSXIdentifier(options.attribute),
92-
t.stringLiteral(options.format(name.node && name.node.name))
93-
)
94-
)
96+
97+
applyAttribute({
98+
openingElement,
99+
t,
100+
name: name.node && name.node.name,
101+
options
102+
})
95103
}
96104
})
97105
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
3+
class ComponentName extends React.Component {
4+
render() {
5+
return <div data-qa="custom-attribute">
6+
<h1>Hello world</h1>
7+
</div>;
8+
}
9+
}
10+
11+
export default ComponentName;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
3+
class ComponentName extends React.Component {
4+
render() {
5+
return <div data-qa="custom-attribute">
6+
<h1>Hello world</h1>
7+
</div>;
8+
}
9+
}
10+
11+
export default ComponentName;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"plugins": [
3+
"syntax-jsx",
4+
["../../../../src"]
5+
]
6+
}

0 commit comments

Comments
 (0)