@@ -15,9 +15,9 @@ export const isValidTopLevelImport = (x, state) =>
1515const localNameCache = { }
1616
1717export const importLocalName = ( name , state , options = { } ) => {
18- const { cacheIdentifier, bypassCache = false } = options ;
19- const cacheKeyAffix = cacheIdentifier ? `|${ cacheIdentifier } ` : '' ;
20- const cacheKey = name + state . file . opts . filename + cacheKeyAffix ;
18+ const { cacheIdentifier, bypassCache = false } = options
19+ const cacheKeyAffix = cacheIdentifier ? `|${ cacheIdentifier } ` : ''
20+ const cacheKey = name + state . file . opts . filename + cacheKeyAffix
2121
2222 if ( ! bypassCache && cacheKey in localNameCache ) {
2323 return localNameCache [ cacheKey ]
@@ -36,10 +36,13 @@ export const importLocalName = (name, state, options = {}) => {
3636
3737 if ( isValidTopLevelImport ( node . source . value , state ) ) {
3838 for ( const specifier of path . get ( 'specifiers' ) ) {
39- if ( specifier . isImportSpecifier ( ) && specifier . node . imported . name === 'styled' ) {
39+ if (
40+ specifier . isImportSpecifier ( ) &&
41+ specifier . node . imported . name === 'styled'
42+ ) {
4043 localName = 'styled'
4144 }
42-
45+
4346 if ( specifier . isImportDefaultSpecifier ( ) ) {
4447 localName = specifier . node . local . name
4548 }
@@ -52,7 +55,7 @@ export const importLocalName = (name, state, options = {}) => {
5255 }
5356
5457 if ( specifier . isImportNamespaceSpecifier ( ) ) {
55- localName = specifier . node . local . name
58+ localName = name === 'default' ? specifier . node . local . name : name
5659 }
5760 }
5861 }
@@ -76,9 +79,16 @@ export const isStyled = t => (tag, state) => {
7679 } else {
7780 return (
7881 ( t . isMemberExpression ( tag ) &&
79- tag . object . name === importLocalName ( 'default' , state , { cacheIdentifier : tag . object . name } ) ) ||
82+ tag . object . name ===
83+ importLocalName ( 'default' , state , {
84+ cacheIdentifier : tag . object . name ,
85+ } ) &&
86+ ! isHelper ( t ) ( tag . property , state ) ) ||
8087 ( t . isCallExpression ( tag ) &&
81- tag . callee . name === importLocalName ( 'default' , state , { cacheIdentifier : tag . callee . name } ) ) ||
88+ tag . callee . name ===
89+ importLocalName ( 'default' , state , {
90+ cacheIdentifier : tag . callee . name ,
91+ } ) ) ||
8292 /**
8393 * #93 Support require()
8494 * styled-components might be imported using a require()
@@ -95,7 +105,17 @@ export const isStyled = t => (tag, state) => {
95105 t . isCallExpression ( tag ) &&
96106 t . isMemberExpression ( tag . callee ) &&
97107 tag . callee . property . name === 'default' &&
98- tag . callee . object . name === state . styledRequired )
108+ tag . callee . object . name === state . styledRequired ) ||
109+ ( importLocalName ( 'default' , state ) &&
110+ t . isMemberExpression ( tag ) &&
111+ t . isMemberExpression ( tag . object ) &&
112+ tag . object . property . name === 'default' &&
113+ tag . object . object . name === importLocalName ( 'default' , state ) ) ||
114+ ( importLocalName ( 'default' , state ) &&
115+ t . isCallExpression ( tag ) &&
116+ t . isMemberExpression ( tag . callee ) &&
117+ tag . object . property . name === 'default' &&
118+ tag . object . object . name === importLocalName ( 'default' , state ) )
99119 )
100120 }
101121}
@@ -116,13 +136,20 @@ export const isKeyframesHelper = t => (tag, state) =>
116136export const isWithThemeHelper = t => ( tag , state ) =>
117137 t . isIdentifier ( tag ) && tag . name === importLocalName ( 'withTheme' , state )
118138
139+ export const isUseTheme = t => ( tag , state ) =>
140+ t . isIdentifier ( tag ) && tag . name === importLocalName ( 'useTheme' , state )
141+
119142export const isHelper = t => ( tag , state ) =>
143+ isCreateGlobalStyleHelper ( t ) ( tag , state ) ||
120144 isCSSHelper ( t ) ( tag , state ) ||
145+ isInjectGlobalHelper ( t ) ( tag , state ) ||
146+ isUseTheme ( t ) ( tag , state ) ||
121147 isKeyframesHelper ( t ) ( tag , state ) ||
122148 isWithThemeHelper ( t ) ( tag , state )
123149
124150export const isPureHelper = t => ( tag , state ) =>
151+ isCreateGlobalStyleHelper ( t ) ( tag , state ) ||
125152 isCSSHelper ( t ) ( tag , state ) ||
126153 isKeyframesHelper ( t ) ( tag , state ) ||
127- isCreateGlobalStyleHelper ( t ) ( tag , state ) ||
154+ isUseTheme ( t ) ( tag , state ) ||
128155 isWithThemeHelper ( t ) ( tag , state )
0 commit comments