Skip to content

Commit

Permalink
feat: add more type protection
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinhe998 committed Jun 11, 2022
1 parent d18d611 commit b131441
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
38 changes: 32 additions & 6 deletions src/call-expression.ts
Expand Up @@ -35,13 +35,24 @@ function getJSXAttributeValue(node: any) {
/**
* Tests if a node is `null` or `undefined`
*/
const isNullLikeNode = (node: any) =>
t.isNullLiteral(node) || t.isIdentifier(node, { name: 'undefined' });
const isNullLikeNode = (
node:
| t.Expression
| t.SpreadElement
| t.JSXNamespacedName
| t.ArgumentPlaceholder
) => t.isNullLiteral(node) || t.isIdentifier(node, { name: 'undefined' });

/**
* Tests if a node is an object expression with noncomputed, nonmethod attrs
*/
const isPlainObjectExpression = (node: any) =>
const isPlainObjectExpression = (
node:
| t.Expression
| t.SpreadElement
| t.JSXNamespacedName
| t.ArgumentPlaceholder
) =>
t.isObjectExpression(node) &&
node.properties.every(
property =>
Expand All @@ -54,7 +65,13 @@ const isPlainObjectExpression = (node: any) =>
/**
* Tests if a node is a CallExpression with callee `React.createElement`
*/
const isReactCreateElement = (node: any) =>
const isReactCreateElement = (
node:
| t.Expression
| t.SpreadElement
| t.JSXNamespacedName
| t.ArgumentPlaceholder
) =>
t.isCallExpression(node) &&
t.isMemberExpression(node.callee) &&
t.isIdentifier(node.callee.object, { name: 'React' }) &&
Expand All @@ -64,7 +81,14 @@ const isReactCreateElement = (node: any) =>
/**
* Tests if a node is a MemberExpression with object `Object.assign`
*/
const isObjectAssignMemberExpression = (node: any) =>
const isObjectAssignMemberExpression = (
node:
| t.Expression
| t.SpreadElement
| t.JSXNamespacedName
| t.ArgumentPlaceholder
) =>
t.isCallExpression(node) &&
t.isMemberExpression(node.callee) &&
t.isIdentifier(node.callee.object, { name: 'Object' }) &&
t.isIdentifier(node.callee.property, { name: 'assign' });
Expand Down Expand Up @@ -172,7 +196,9 @@ export default function CallExpression(
path: NodePath<CallExpression>,
{ opts }: PluginTransformState
) {
const customClassName = opts.className.toString().replace(',', ' ');
const customClassName = Array.isArray(opts.className)
? opts.className.join(' ')
: opts.className;
const node = path.node;

if (!isReactCreateElement(node)) {
Expand Down
4 changes: 3 additions & 1 deletion src/styled-components.ts
Expand Up @@ -6,7 +6,9 @@ export default function TaggedTemplateExpression(
{ opts }: PluginTransformState
) {
const scope = path.scope;
const customClassName = opts.className.toString().replace(',', ' ');
const customClassName = Array.isArray(opts.className)
? opts.className.join(' ')
: opts.className;

try {
// simple case
Expand Down

0 comments on commit b131441

Please sign in to comment.