Skip to content

Commit

Permalink
fix(type): babel-types was not required
Browse files Browse the repository at this point in the history
  • Loading branch information
jean343 committed Aug 24, 2018
1 parent f24289a commit c8a91a4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"parcel-bundler": "^1.9.7"
},
"dependencies": {
"babel-plugin-styless": "^1.1.0",
"babel-plugin-styless": "^1.2.0",
"less-vars-to-js": "^1.3.0",
"react": "^16.4.2",
"react-dom": "^16.4.2",
Expand Down
6 changes: 3 additions & 3 deletions examples/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,9 @@ babel-plugin-check-es2015-constants@^6.22.0:
dependencies:
babel-runtime "^6.22.0"

babel-plugin-styless@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/babel-plugin-styless/-/babel-plugin-styless-1.1.0.tgz#617fd43def7d8a775ce338bc7854c3d8fdcdc84b"
babel-plugin-styless@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/babel-plugin-styless/-/babel-plugin-styless-1.2.0.tgz#af7a864d88441f847691bd99c5ce28f9f707b66f"
dependencies:
less "^3.8.1"
less-vars-to-js "^1.3.0"
Expand Down
33 changes: 19 additions & 14 deletions src/utils/detectors.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import * as t from 'babel-types';
const VALID_TOP_LEVEL_IMPORT_PATHS = [
'styled-components',
'styled-components/no-tags',
'styled-components/native',
'styled-components/primitives',
];

export const isValidTopLevelImport = x => VALID_TOP_LEVEL_IMPORT_PATHS.includes(x);

const importLocalName = (name, state) => {
let localName = name === 'default' ? 'styled' : name
let localName = name === 'default' ? 'styled' : name;

state.file.path.traverse({
ImportDeclaration: {
exit(path) {
const {node} = path
const {node} = path;

if (node.source.value === 'styled-components') {
if (isValidTopLevelImport(node.source.value)) {
for (const specifier of path.get('specifiers')) {
if (specifier.isImportDefaultSpecifier()) {
localName = specifier.node.local.name
Expand All @@ -33,7 +40,7 @@ const importLocalName = (name, state) => {
return localName
};

export const isStyled = (tag, state) => {
export const isStyled = t => (tag, state) => {
/* Matches the extend blocks such as
const Block = Div.extend`
color: @color
Expand All @@ -49,7 +56,7 @@ export const isStyled = (tag, state) => {
tag.callee.property.name !== 'default' /** ignore default for #93 below */
) {
// styled.something()
return isStyled(tag.callee.object, state)
return isStyled(t)(tag.callee.object, state)
} else {
return (
(t.isMemberExpression(tag) &&
Expand Down Expand Up @@ -77,14 +84,12 @@ export const isStyled = (tag, state) => {
}
}

export const isCSSHelper = (tag, state) =>
t.isIdentifier(tag) && tag.name === importLocalName('css', state)
export const isCSSHelper = t => (tag, state) => t.isIdentifier(tag) && tag.name === importLocalName('css', state);

export const isCreateGlobalStyleHelper = t => (tag, state) => t.isIdentifier(tag) && tag.name === importLocalName('createGlobalStyle', state);

export const isInjectGlobalHelper = (tag, state) =>
t.isIdentifier(tag) && tag.name === importLocalName('injectGlobal', state)
export const isInjectGlobalHelper = t => (tag, state) => t.isIdentifier(tag) && tag.name === importLocalName('injectGlobal', state);

export const isKeyframesHelper = (tag, state) =>
t.isIdentifier(tag) && tag.name === importLocalName('keyframes', state)
export const isKeyframesHelper = t => (tag, state) => t.isIdentifier(tag) && tag.name === importLocalName('keyframes', state);

export const isHelper = (tag, state) =>
isCSSHelper(tag, state) || isKeyframesHelper(tag, state)
export const isHelper = t => (tag, state) => isCSSHelper(t)(tag, state) || isKeyframesHelper(t)(tag, state);
2 changes: 1 addition & 1 deletion src/visitors/taggedTemplateExpressionVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import transpileLess from "./transpileLess";
const regex = /`([\s\S]*)`/;

export default (path, state, {types: t}) => {
if (!isStyled(path.node.tag, state)) {
if (!isStyled(t)(path.node.tag, state)) {
return;
}

Expand Down

0 comments on commit c8a91a4

Please sign in to comment.