Repository URL to install this package:
|
Version:
3.0.0 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isPureHelper = exports.isHelper = exports.isKeyframesHelper = exports.isInjectGlobalHelper = exports.isCreateGlobalStyleHelper = exports.isCSSHelper = exports.isStyled = exports.isStylehHelper = exports.isBlackListedPropertyName = exports.isValidTopLevelImport = void 0;
const VALID_TOP_LEVEL_IMPORT_PATHS = ['styleh-components', 'styled-components', 'styled-components/no-tags', 'styled-components/native', 'styled-components/primitives'];
const isValidTopLevelImport = x => VALID_TOP_LEVEL_IMPORT_PATHS.includes(x);
exports.isValidTopLevelImport = isValidTopLevelImport;
const localNameCache = {};
const importLocalName = (name, state) => {
const cacheKey = name + state.file.opts.filename;
if (localNameCache[cacheKey]) {
return localNameCache[cacheKey];
}
let localName = name === 'default' ? 'styled' : name;
state.file.path.traverse({
ImportDeclaration: {
exit(path) {
const {
node
} = path;
if (isValidTopLevelImport(node.source.value)) {
for (const specifier of path.get('specifiers')) {
if (specifier.isImportDefaultSpecifier()) {
localName = specifier.node.local.name;
}
if (specifier.isImportSpecifier() && specifier.node.imported.name === name) {
localName = specifier.node.local.name;
}
if (specifier.isImportNamespaceSpecifier()) {
localName = specifier.node.local.name;
}
}
}
}
}
});
localNameCache[cacheKey] = localName;
return localName;
};
/**
* @@fork @todo this means everything needs to remove .withComponent
*/
/**
* @todo @@fork need to add support for `keyframes` & `css` as properties
*/
const isBlackListedPropertyName = name => {
return ['todo', 'withComponent', 'css', 'keyframes'].includes(name);
};
exports.isBlackListedPropertyName = isBlackListedPropertyName;
const isStylehHelper = t => (tag, state) => {
// console.log(tag)
return t.isIdentifier(tag) && tag.name === 'styled';
};
/**
* @todo check if the variable is named `styled` from the import, in styleh
*/
exports.isStylehHelper = isStylehHelper;
const isStyled = t => (tag, state) => {
// if (isStylehHelper(tag, state)) {
// console.warn('@@todo')
// // return true
// }
if (t.isCallExpression(tag) && t.isMemberExpression(tag.callee) && tag.callee.property.name !== 'default'
/** ignore default for #93 below */
) {
// if (isBlackListedPropertyName(tag.callee.property.name)) {
// console.warn('@@fork skipping: ' + tag.callee.property.name)
// return false
// }
// console.warn('@@fork: isStyled? ' + tag.callee.property.name)
// styled.something()
return isStyled(t)(tag.callee.object, state);
} else {
// console.warn('@@fork: #2 ', propertyName)
// if (isBlackListedPropertyName(propertyName)) {
// return false
// }
/**
* @@fork added this to ignore bad compiles
* @todo need to add support for logging the line where the code is so we can fix it, or codegen a fix
*/
// const isBlackListedList = [
// t.isMemberExpression(tag) && isBlackListedPropertyName(tag.object.name),
// t.isCallExpression(tag) && isBlackListedPropertyName(tag.callee.name),
// // @note these 2 were from `required`...
// t.isMemberExpression(tag) &&
// t.isMemberExpression(tag.object) &&
// isBlackListedPropertyName(tag.object.property.name),
// t.isCallExpression(tag) &&
// t.isMemberExpression(tag.callee) &&
// isBlackListedPropertyName(tag.callee.property.name),
// ]
// if (isBlackListedList.includes(true)) {
// // console.error('ignore this')
// return false
// }
return t.isMemberExpression(tag) && tag.object.name === importLocalName('default', state) || t.isCallExpression(tag) && tag.callee.name === importLocalName('default', state) ||
/**
* @@fork adding import for `styled` import
*/
t.isMemberExpression(tag) && tag.object.name === importLocalName('styled', state) || t.isCallExpression(tag) && tag.callee.name === importLocalName('styled', state) ||
/**
* #93 Support require()
* styled-components might be imported using a require()
* call and assigned to a variable of any name.
* - styled.default.div``
* - styled.default.something()
*/
state.styledRequired && t.isMemberExpression(tag) && t.isMemberExpression(tag.object) && tag.object.property.name === 'default' && tag.object.object.name === state.styledRequired || state.styledRequired && t.isCallExpression(tag) && t.isMemberExpression(tag.callee) && tag.callee.property.name === 'default' && tag.callee.object.name === state.styledRequired;
}
};
exports.isStyled = isStyled;
const isCSSHelper = t => (tag, state) => t.isIdentifier(tag) && tag.name === importLocalName('css', state);
exports.isCSSHelper = isCSSHelper;
const isCreateGlobalStyleHelper = t => (tag, state) => t.isIdentifier(tag) && tag.name === importLocalName('createGlobalStyle', state);
exports.isCreateGlobalStyleHelper = isCreateGlobalStyleHelper;
const isInjectGlobalHelper = t => (tag, state) => t.isIdentifier(tag) && tag.name === importLocalName('injectGlobal', state);
exports.isInjectGlobalHelper = isInjectGlobalHelper;
const isKeyframesHelper = t => (tag, state) => t.isIdentifier(tag) && tag.name === importLocalName('keyframes', state);
exports.isKeyframesHelper = isKeyframesHelper;
const isHelper = t => (tag, state) => isCSSHelper(t)(tag, state) || isKeyframesHelper(t)(tag, state);
exports.isHelper = isHelper;
const isPureHelper = t => (tag, state) => isCSSHelper(t)(tag, state) || isKeyframesHelper(t)(tag, state) || isCreateGlobalStyleHelper(t)(tag, state);
exports.isPureHelper = isPureHelper;