Skip to content

Commit

Permalink
Fix convert-to-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmielnik committed Aug 11, 2018
1 parent f3a4da5 commit 65d7744
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/utils/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@ const {
isProgram
} = require('@babel/types');

const returnFalse = () => false;

const classExtendsSomething = (node) => Boolean(node.superClass);

const containsNode = (ast, condition) => {
if (condition(ast)) {
const containsNode = (ast, predicate, stopPredicate = returnFalse) => {
if (stopPredicate(ast)) {
return false;
}

if (predicate(ast)) {
return true;
}

return Object.keys(ast).some((key) => {
const value = ast[key];
if (Array.isArray(value)) {
return value.some((node) => containsNode(node, condition));
return value.some((node) => containsNode(node, predicate, stopPredicate));
}
if (value && value.type) {
return containsNode(value, condition);
return containsNode(value, predicate, stopPredicate);
}
return false;
});
Expand All @@ -41,8 +47,8 @@ const isArrowFunctionDeclaration = (node) => node.type === 'VariableDeclaration'

const isArrowComponentExpression = (node) => isArrowFunctionExpression(node)
&& isFunctionalComponentBody(node.body)
&& !containsNode(node.body, isArrowFunctionExpression)
&& !containsNode(node.body, isFunctionComponentDeclaration);
&& !containsNode(node.body, isArrowFunctionExpression, isJsxNode)
&& !containsNode(node.body, isFunctionComponentDeclaration, isJsxNode);

const isArrowComponentExpressionPath = (path) => {
let isComponent = false;
Expand Down Expand Up @@ -103,13 +109,12 @@ const isArrowComponentDeclarationPath = (path) => {

const isFunctionComponentDeclaration = (node) => isFunctionDeclaration(node)
&& isFunctionalComponentBody(node.body)
&& !containsNode(node.body, isArrowComponentExpression)
&& !containsNode(node.body, isFunctionComponentDeclaration);
&& !containsNode(node.body, isArrowComponentExpression, isJsxNode)
&& !containsNode(node.body, isFunctionComponentDeclaration, isJsxNode);

const isFunctionalComponentBody = (node) => containsNode(node, isJsxNode);

const isFunctionalComponentBody = (node) => containsNode(
node,
({ type }) => [ 'JSXElement', 'JSXFragment' ].includes(type)
);
const isJsxNode = ({ type }) => [ 'JSXElement', 'JSXFragment' ].includes(type);

const isMemberOfDeclaration = (node, objectName, name) => isMemberDeclaration(node, name)
&& node.expression.left.object.type === 'Identifier'
Expand All @@ -131,6 +136,8 @@ const isPropsDeclaration = (declaration) => declaration.type === 'VariableDeclar
const isPropTypesDeclaration = (node) => isMemberDeclaration(node, 'propTypes');

const createIsDefaultImport = (name) => (node) => node.type === 'ImportDeclaration'
&& node.specifiers
&& node.specifiers.length > 0
&& node.specifiers[0].type === 'ImportDefaultSpecifier'
&& node.specifiers[0].local.type === 'Identifier'
&& node.specifiers[0].local.name === name;
Expand Down

0 comments on commit 65d7744

Please sign in to comment.