Skip to content

Commit 4e28bb9

Browse files
authored
do not try to transpile things we don't own (#230)
* add failing test * do not try to transpile things we don't own * a little extra proof that the variable from s-c is tracked properly
1 parent 9bc3c08 commit 4e28bb9

File tree

22 files changed

+81
-22
lines changed

22 files changed

+81
-22
lines changed

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export default function({ types: t }) {
1616
JSXAttribute(path, state) {
1717
transpileCssProp(t)(path, state)
1818
},
19+
VariableDeclarator(path, state) {
20+
assignStyledRequired(t)(path, state)
21+
},
1922
},
2023
state
2124
)
@@ -30,9 +33,6 @@ export default function({ types: t }) {
3033
templateLiterals(t)(path, state)
3134
pureAnnotation(t)(path, state)
3235
},
33-
VariableDeclarator(path, state) {
34-
assignStyledRequired(t)(path, state)
35-
},
3636
},
3737
}
3838
}

src/utils/detectors.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ export const importLocalName = (name, state, bypassCache = false) => {
1717
return localNameCache[cacheKey]
1818
}
1919

20-
let localName = name === 'default' ? 'styled' : name
20+
let localName = state.styledRequired
21+
? name === 'default'
22+
? 'styled'
23+
: name
24+
: false
2125

2226
state.file.path.traverse({
2327
ImportDeclaration: {

src/visitors/transpileCssProp.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ export default t => (path, state) => {
2424

2525
// state.customImportName is passed through from styled-components/macro if it's used
2626
// since the macro also inserts the import
27-
let importName =
28-
state.customImportName || t.identifier(importLocalName('default', state))
27+
let importName = state.customImportName || importLocalName('default', state)
2928

3029
const { bindings } = program.scope
3130

3231
// Insert import if it doesn't exist yet
33-
if (!bindings[importName.name]) {
32+
if (!importName || !bindings[importName.name]) {
3433
addDefault(path, 'styled-components', {
3534
nameHint: 'styled',
3635
})
3736

3837
importName = t.identifier(importLocalName('default', state, true))
3938
}
4039

40+
if (!t.isIdentifier(importName)) importName = t.identifier(importName)
41+
4142
const elem = path.parentPath
4243
const name = getName(elem.node.name, t)
4344
const id = path.scope.generateUidIdentifier(
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
const Test = styled.div`width: 100%;`;
2-
const Test2 = styled('div')``;
3-
const Test3 = true ? styled.div`` : styled.div``;
1+
import styled from 'styled-components'
2+
3+
const Test = styled.div`
4+
width: 100%;
5+
`
6+
const Test2 = styled('div')``
7+
const Test3 = true ? styled.div`` : styled.div``
48
const styles = { One: styled.div`` }
5-
let Component;
6-
Component = styled.div``;
7-
const WrappedComponent = styled(Inner)``;
8-
class ClassComponent { static Child = styled.div`` }
9+
let Component
10+
Component = styled.div``
11+
const WrappedComponent = styled(Inner)``
12+
class ClassComponent {
13+
static Child = styled.div``
14+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import styled from 'styled-components';
12
const Test = styled.div.withConfig({
23
displayName: "Test"
34
})`width:100%;`;

test/fixtures/add-identifier-and-display-name/code.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import styled from 'styled-components'
2+
13
const Test = styled.div`
24
width: 100%;
35
`

test/fixtures/add-identifier-and-display-name/output.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import styled from 'styled-components';
12
const Test = styled.div.withConfig({
23
displayName: "Test",
34
componentId: "sc-1cza72q-0"
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
const Test = styled.div`width: 100%;`;
2-
const Test2 = true ? styled.div`` : styled.div``;
1+
import styled from 'styled-components'
2+
3+
const Test = styled.div`
4+
width: 100%;
5+
`
6+
const Test2 = true ? styled.div`` : styled.div``
37
const styles = { One: styled.div`` }
4-
let Component;
5-
Component = styled.div``;
6-
const WrappedComponent = styled(Inner)``;
8+
let Component
9+
Component = styled.div``
10+
const WrappedComponent = styled(Inner)``

test/fixtures/add-identifier/output.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import styled from 'styled-components';
12
const Test = styled.div.withConfig({
23
componentId: "sc-1289dod-0"
34
})`width:100%;`;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
const WithAttrs = styled.div.attrs({ some: 'value'})``
2-
const WithAttrsWrapped = styled(Inner).attrs({ some: 'value'})``
1+
import styled from 'styled-components'
2+
3+
const WithAttrs = styled.div.attrs({ some: 'value' })``
4+
const WithAttrsWrapped = styled(Inner).attrs({ some: 'value' })``

0 commit comments

Comments
 (0)