<commit_before>import annotateConstructor from '../helpers/annotateConstructor';
import createTypeAlias from '../helpers/createTypeAlias';
import findIndex from '../helpers/findIndex';
import findParentBody from '../helpers/findParentBody';
import transformProperties from '../helpers/transformProperties';
import ReactUtils from '../helpers/ReactUtils';

const isStaticPropType = (p) => {
  return p.type === 'ClassProperty' &&
    p.static &&
    p.key.type === 'Identifier' &&
    p.key.name === 'propTypes';
};

/**
 * Transforms es2016 components
 * @return true if any components were transformed.
 */
export default function transformEs6Classes(ast, j) {
  const reactUtils = ReactUtils(j);

  const classNamesWithPropsOutside = [];

  // NOTE: reactUtils.findReactES6ClassDeclaration misses extends for local
  // imported components.
  const reactClassPaths = reactUtils.findReactES6ClassDeclaration(ast);

  // find classes with propType static class property
  const modifications = reactClassPaths.forEach(p => {
    const className = reactUtils.getComponentName(p);
    const propIdentifier = reactClassPaths.length === 1 ? 'Props' : `${className}Props`;
    let properties;

    const classBody = p.value.body && p.value.body.body;
    if (classBody) {
      annotateConstructor(j, classBody, propIdentifier);
      const index = findIndex(classBody, isStaticPropType);
      if (typeof index !== 'undefined') {
        const classProperty = classBody.splice(index, 1).pop();
        properties = classProperty.value.properties;
      } else {
        // look for propTypes defined elsewhere
        classNamesWithPropsOutside.push(className);

        ast.find(j.AssignmentExpression, {
          left: {
            type: 'MemberExpression',
            object: {
              name: className,
            },
            property: {
              name: 'propTypes',
            },
          },<commit_msg>Fix sub classes not being transformed<commit_after>import annotateConstructor from '../helpers/annotateConstructor';
import createTypeAlias from '../helpers/createTypeAlias';
import findIndex from '../helpers/findIndex';
import findParentBody from '../helpers/findParentBody';
import transformProperties from '../helpers/transformProperties';
import ReactUtils from '../helpers/ReactUtils';

const isStaticPropType = (p) => {
  return p.type === 'ClassProperty' &&
    p.static &&
    p.key.type === 'Identifier' &&
    p.key.name === 'propTypes';
};

/**
 * Transforms es2016 components
 * @return true if any components were transformed.
 */
export default function transformEs6Classes(ast, j) {
  const reactUtils = ReactUtils(j);

  const classNamesWithPropsOutside = [];

  // NOTE: reactUtils.findReactES6ClassDeclaration(ast) is missing extends
  // for local imported components... If finding all classes is too greety,
  // we might combine findReactES6ClassDeclaration with classes that have a
  // render method.
  const reactClassPaths = ast.find(j.ClassDeclaration);

  // find classes with propType static class property
  const modifications = reactClassPaths.forEach(p => {
    const className = reactUtils.getComponentName(p);
    const propIdentifier = reactClassPaths.length === 1 ? 'Props' : `${className}Props`;
    let properties;

    const classBody = p.value.body && p.value.body.body;
    if (classBody) {
      annotateConstructor(j, classBody, propIdentifier);
      const index = findIndex(classBody, isStaticPropType);
      if (typeof index !== 'undefined') {
        const classProperty = classBody.splice(index, 1).pop();
        properties = classProperty.value.properties;
      } else {
        // look for propTypes defined elsewhere
        classNamesWithPropsOutside.push(className);

        ast.find(j.AssignmentExpression, {
          left: {
            type: 'MemberExpression',
            object: {
              name: className,
            },
            property: {
              name: 'propTypes',
            },
          },
{'fraction_non_alphanumeric': 0.1133350012509382, 'fraction_numerical': 0.004503377533149862, 'mean_word_length': 3.1430051813471502, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2465822', 'n_tokens_mistral': 1042, 'n_tokens_neox': 1034, 'n_words': 278}