Skip to content

Commit e04eb6f

Browse files
authored
Merge pull request #134 from styled-components/prepare/babel-seven
Pass-through babel-types in visitors to be version agnostic
2 parents c0c4179 + f242e01 commit e04eb6f

File tree

11 files changed

+45
-59
lines changed

11 files changed

+45
-59
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
},
2525
"dependencies": {
2626
"@babel/helper-annotate-as-pure": "^7.0.0-beta.37",
27-
"babel-types": "^6.26.0",
2827
"lodash": "^4.17.10"
2928
},
3029
"peerDependencies": {

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ export default function({ types: t }) {
99
return {
1010
visitor: {
1111
ImportDeclaration(path, state) {
12-
rewriteStyledImport(path, state)
12+
rewriteStyledImport(t)(path, state)
1313
},
1414
MemberExpression(path, state) {
15-
desugarStyled(path, state)
15+
desugarStyled(t)(path, state)
1616
},
1717
TaggedTemplateExpression(path, state) {
18-
minify(path, state)
19-
displayNameAndId(path, state)
20-
templateLiterals(path, state)
18+
minify(t)(path, state)
19+
displayNameAndId(t)(path, state)
20+
templateLiterals(t)(path, state)
2121
},
2222
VariableDeclarator(path, state) {
23-
assignStyledRequired(path, state)
23+
assignStyledRequired(t)(path, state)
2424
},
2525
},
2626
}

src/utils/detectors.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as t from 'babel-types'
2-
31
const VALID_TOP_LEVEL_IMPORT_PATHS = [
42
'styled-components',
53
'styled-components/no-tags',
@@ -43,14 +41,14 @@ const importLocalName = (name, state) => {
4341
return localName
4442
}
4543

46-
export const isStyled = (tag, state) => {
44+
export const isStyled = t => (tag, state) => {
4745
if (
4846
t.isCallExpression(tag) &&
4947
t.isMemberExpression(tag.callee) &&
5048
tag.callee.property.name !== 'default' /** ignore default for #93 below */
5149
) {
5250
// styled.something()
53-
return isStyled(tag.callee.object, state)
51+
return isStyled(t)(tag.callee.object, state)
5452
} else {
5553
return (
5654
(t.isMemberExpression(tag) &&
@@ -78,18 +76,18 @@ export const isStyled = (tag, state) => {
7876
}
7977
}
8078

81-
export const isCSSHelper = (tag, state) =>
79+
export const isCSSHelper = t => (tag, state) =>
8280
t.isIdentifier(tag) && tag.name === importLocalName('css', state)
8381

84-
export const isCreateGlobalStyleHelper = (tag, state) =>
82+
export const isCreateGlobalStyleHelper = t => (tag, state) =>
8583
t.isIdentifier(tag) &&
8684
tag.name === importLocalName('createGlobalStyle', state)
8785

88-
export const isInjectGlobalHelper = (tag, state) =>
86+
export const isInjectGlobalHelper = t => (tag, state) =>
8987
t.isIdentifier(tag) && tag.name === importLocalName('injectGlobal', state)
9088

91-
export const isKeyframesHelper = (tag, state) =>
89+
export const isKeyframesHelper = t => (tag, state) =>
9290
t.isIdentifier(tag) && tag.name === importLocalName('keyframes', state)
9391

94-
export const isHelper = (tag, state) =>
95-
isCSSHelper(tag, state) || isKeyframesHelper(tag, state)
92+
export const isHelper = t => (tag, state) =>
93+
isCSSHelper(t)(tag, state) || isKeyframesHelper(t)(tag, state)

src/utils/getName.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as t from 'babel-types'
2-
31
/**
42
* Get the name of variable that contains node
53
*
@@ -8,7 +6,7 @@ import * as t from 'babel-types'
86
* @return {String} The target
97
*/
108

11-
export default path => {
9+
export default t => path => {
1210
let namedNode
1311

1412
path.find(path => {

src/visitors/assignStyledRequired.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import * as t from 'babel-types'
21
import { isValidTopLevelImport } from '../utils/detectors'
32

4-
export default (path, state) => {
3+
export default t => (path, state) => {
54
if (
65
t.isCallExpression(path.node.init) &&
76
t.isIdentifier(path.node.init.callee) &&

src/visitors/desugarStyled.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import * as t from 'babel-types'
21
import get from 'lodash/get'
32
import { isStyled } from '../utils/detectors'
43

5-
export default (path, state) => {
4+
export default t => (path, state) => {
65
/**
76
* Handles both "styled.div" and "styled_default.default.div" (transpiled output)
87
*/
9-
if (isStyled(path.node, state)) {
8+
if (isStyled(t)(path.node, state)) {
109
/**
1110
* e.g. "div"
1211
*/

src/visitors/displayNameAndId.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import * as t from 'babel-types'
1+
import path from 'path'
2+
import fs from 'fs'
23
import { useFileName, useDisplayName, useSSR } from '../utils/options'
34
import getName from '../utils/getName'
45
import prefixLeadingDigit from '../utils/prefixDigit'
5-
import path from 'path'
6-
import fs from 'fs'
76
import hash from '../utils/hash'
87
import { isStyled } from '../utils/detectors'
98

10-
const addConfig = (path, displayName, componentId) => {
9+
const addConfig = t => (path, displayName, componentId) => {
1110
if (!displayName && !componentId) {
1211
return
1312
}
@@ -47,9 +46,9 @@ const getBlockName = file => {
4746
: path.basename(path.dirname(file.opts.filename))
4847
}
4948

50-
const getDisplayName = (path, state) => {
49+
const getDisplayName = t => (path, state) => {
5150
const { file } = state
52-
const componentName = getName(path)
51+
const componentName = getName(t)(path)
5352
if (file) {
5453
const blockName = getBlockName(file)
5554
if (blockName === componentName) {
@@ -122,12 +121,13 @@ const getComponentId = state => {
122121
return `${prefixLeadingDigit(getFileHash(state))}-${getNextId(state)}`
123122
}
124123

125-
export default (path, state) => {
126-
if (isStyled(path.node.tag, state)) {
124+
export default t => (path, state) => {
125+
if (isStyled(t)(path.node.tag, state)) {
127126
const displayName =
128-
useDisplayName(state) && getDisplayName(path, useFileName(state) && state)
127+
useDisplayName(state) &&
128+
getDisplayName(t)(path, useFileName(state) && state)
129129

130-
addConfig(
130+
addConfig(t)(
131131
path,
132132
displayName && displayName.replace(/[^_a-zA-Z0-9-]/g, ''),
133133
useSSR(state) && getComponentId(state)

src/visitors/minify.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
import * as t from 'babel-types'
2-
31
import { useMinify } from '../utils/options'
42
import { isStyled, isHelper } from '../utils/detectors'
53
import { minifyRawValues, minifyCookedValues } from '../minify'
64

7-
export default (path, state) => {
5+
export default t => (path, state) => {
86
if (
97
useMinify(state) &&
10-
(
11-
isStyled(path.node.tag, state) ||
12-
isHelper(path.node.tag, state)
13-
)
8+
(isStyled(t)(path.node.tag, state) || isHelper(t)(path.node.tag, state))
149
) {
1510
const templateLiteral = path.node.quasi
1611
const quasisLength = templateLiteral.quasis.length

src/visitors/rewriteStyledImport.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import * as t from 'babel-types'
2-
import get from 'lodash/get'
3-
import { isStyled } from '../utils/detectors'
4-
51
/**
62
* When using the Babel plugin, we desugar styled.div to styled('div'), which means we can
73
* then use a lighter-weight version of s-c since those element names don't need to be kept around
84
* ahead of time.
95
*/
10-
export default path => {
6+
export default t => path => {
117
if (path.node.source.value === 'styled-components') {
128
path.node.source = t.stringLiteral('styled-components/no-tags')
139
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { useTranspileTemplateLiterals } from '../../utils/options'
2-
32
import transpile from './transpile'
43

5-
export default (path, state) => {
4+
export default t => (path, state) => {
65
if (useTranspileTemplateLiterals(state)) {
7-
transpile(path, state)
6+
transpile(t)(path, state)
87
}
98
}

0 commit comments

Comments
 (0)