description and source-codedefault = function (_ref) {
var t = _ref.types;
var isGlobalEl = function isGlobalEl(el) {
return el.attributes.some(function (_ref2) {
var name = _ref2.name;
return name && name.name === GLOBAL_ATTRIBUTE;
});
};
var isStyledJsx = function isStyledJsx(_ref3) {
var el = _ref3.node;
return t.isJSXElement(el) && el.openingElement.name.name === 'style' && el.openingElement.attributes.some(function (attr) {
return attr.name.name === STYLE_ATTRIBUTE;
});
};
var findStyles = function findStyles(path) {
if (isStyledJsx(path)) {
var node = path.node;
return isGlobalEl(node.openingElement) ? [path] : [];
}
return path.get('children').filter(isStyledJsx);
};
// We only allow constants to be used in template literals.
// The following visitor ensures that MemberExpressions and Identifiers
// are not in the scope of the current Method (render) or function (Component).
var validateExpressionVisitor = {
MemberExpression: function MemberExpression(path) {
var node = path.node;
if (t.isThisExpression(node.object) && t.isIdentifier(node.property) && (node.property.name === 'props' || node.property.name
=== 'state')) {
throw path.buildCodeFrameError('Expected a constant ' + 'as part of the template literal expression ' + '(eg: <style jsx
>{`p { color: ${myColor}`}</style>), ' + ('but got a MemberExpression: this.' + node.property.name));
}
},
Identifier: function Identifier(path, scope) {
var name = path.node.name;
if (scope.hasOwnBinding(name)) {
throw path.buildCodeFrameError('Expected `' + name + '` ' + 'to not come from the closest scope.\n' + 'Styled JSX encourages
the use of constants ' + 'instead of `props` or dynamic values ' + 'which are better set via inline styles or `className` toggling
. ' + 'See https://github.com/zeit/styled-jsx#dynamic-styles');
}
}
};
var getExpressionText = function getExpressionText(expr) {
var node = expr.node;
// assume string literal
if (t.isStringLiteral(node)) {
return node.value;
}
var expressions = expr.get('expressions');
// simple template literal without expressions
if (expressions.length === 0) {
return node.quasis[0].value.cooked;
}
// Special treatment for template literals that contain expressions:
//
// Expressions are replaced with a placeholder
// so that the CSS compiler can parse and
// transform the css source string
// without having to know about js literal expressions.
// Later expressions are restored
// by doing a replacement on the transformed css string.
//
// e.g.
// p { color: ${myConstant}; }
// becomes
// p { color: ___styledjsxexpression0___; }
var replacements = expressions.map(function (e, id) {
return {
pattern: new RegExp('\\$\\{\\s*' + (0, _escapeStringRegexp2.default)(e.getSource()) + '\\s*\\}'),
replacement: '___styledjsxexpression_' + id + '___',
initial: '${' + e.getSource() + '}'
};
}).sort(function (a, b) {
return a.initial.length < b.initial.length;
});
var source = expr.getSource().slice(1, -1);
var modified = replacements.reduce(function (source, currentReplacement) {
source = source.replace(currentReplacement.pattern, currentReplacement.replacement);
return source;
}, source);
return {
source: source,
modified: modified,
replacements: replacements
};
};
var makeStyledJsxTag = function makeStyledJsxTag(id, transformedCss, isTemplateLiteral) {
var css = void 0;
if (isTemplateLiteral) {
// build the expression from transformedCss
(0, _babelTraverse2.default)((0, _babylon.parse)('`' + transformedCss + '`'), {
TemplateLiteral: function TemplateLiteral(path) {
if (!css) {
css = path.node;
}
}
});
} else {
css = t.stringLiteral(transformedCss);
}
return t.JSXElement(t.JSXOpeningElement(t.JSXIdentifier(STYLE_COM ...