Skip to content

Commit e5e4103

Browse files
authored
Merge pull request #355 from shortwave/rockwood/latter
When there are multiple assignments perfer the outer name.
2 parents 2d5078d + 951b96d commit e5e4103

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/utils/getName.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default t => path => {
1010
let namedNode
1111

1212
path.find(path => {
13-
// const X = styled
13+
// X = styled
1414
if (path.isAssignmentExpression()) {
1515
namedNode = path.node.left
1616
// const X = { Y: styled }
@@ -19,7 +19,7 @@ export default t => path => {
1919
// class Y { (static) X = styled }
2020
} else if (path.isClassProperty()) {
2121
namedNode = path.node.key
22-
// let X; X = styled
22+
// const X = styled
2323
} else if (path.isVariableDeclarator()) {
2424
namedNode = path.node.id
2525
} else if (path.isStatement()) {
@@ -28,7 +28,12 @@ export default t => path => {
2828
}
2929

3030
// we've got an displayName (if we need it) no need to continue
31-
if (namedNode) return true
31+
// However if this is an assignment expression like X = styled then we
32+
// want to keep going up incase there is Y = X = styled; in this case we
33+
// want to pick the outer name because react-refresh will add HMR variables
34+
// like this: X = _a = styled. We could also consider only doing this if the
35+
// name starts with an underscore.
36+
if (namedNode && !path.isAssignmentExpression()) return true
3237
})
3338

3439
// foo.bar -> bar

test/fixtures/add-display-names/code.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ const WrappedComponent = styled(Inner)``
1212
class ClassComponent {
1313
static Child = styled.div``
1414
}
15+
var GoodName = BadName = styled.div``;

test/fixtures/add-display-names/output.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ class ClassComponent {}
2828
ClassComponent.Child = styled.div.withConfig({
2929
displayName: "Child"
3030
})``;
31+
var GoodName = BadName = styled.div.withConfig({
32+
displayName: "GoodName"
33+
})``;

0 commit comments

Comments
 (0)