Skip to content

Commit 856419b

Browse files
authored
Merge pull request #97 from phyllisstein/babel-7-beta-3
Support Babel v7.0.0-beta.3 (fixes #96).
2 parents 4a480d2 + 2887fd4 commit 856419b

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/utils/detectors.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
import * as t from 'babel-types'
22

33
const importLocalName = (name, state) => {
4-
const imports = state.file.metadata.modules.imports
5-
const styledImports = imports.find(x => x.source === 'styled-components')
6-
if (styledImports) {
7-
const specifier = styledImports.specifiers.find(x => x.imported === name)
8-
if (specifier) {
9-
return specifier.local
4+
let localName = name === 'default' ? 'styled' : name
5+
6+
state.file.path.traverse({
7+
ImportDeclaration: {
8+
exit(path) {
9+
const { node } = path
10+
11+
if (node.source.value === 'styled-components') {
12+
for (const specifier of path.get('specifiers')) {
13+
if (specifier.isImportDefaultSpecifier()) {
14+
localName = specifier.node.local.name
15+
}
16+
17+
if (specifier.isImportSpecifier() && specifier.node.imported.name === name) {
18+
localName = specifier.node.local.name
19+
}
20+
21+
if (specifier.isImportNamespaceSpecifier()) {
22+
localName = specifier.node.local.name
23+
}
24+
}
25+
}
26+
}
1027
}
11-
}
12-
// import not found - return default name
13-
return name === 'default' ? 'styled' : name
28+
})
29+
30+
return localName
1431
}
1532

1633
export const isStyled = (tag, state) => {

test/fixtures/19-transpile-require-default/after.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ const styled_default = require("styled-components/no-parser");
33
const TestNormal = styled.div.withConfig({
44
displayName: "before__TestNormal",
55
componentId: "y0c69d-0"
6-
})([["{width: 100%;}"]]);
6+
})([["{width:100%;}"]]);
77

88
const Test = styled_default.default.div.withConfig({
99
displayName: "before__Test",
1010
componentId: "y0c69d-1"
11-
})([["{width: 100%;}"]]);
11+
})([["{width:100%;}"]]);
1212

1313
const TestCallExpression = styled_default.default(Test).withConfig({
1414
displayName: "before__TestCallExpression",
1515
componentId: "y0c69d-2"
16-
})([["{height: 20px;}"]]);
16+
})([["{height:20px;}"]]);

0 commit comments

Comments
 (0)