<commit_before>import string from 'can-string';
import makeDebug from 'debug';

// TODO
//
export default function transformer(file, api) {
  const debug = makeDebug(`can-migrate:can-stache-define-element:${file.path}`);
  const j = api.jscodeshift;

  return j(file.source)
    .find(j.CallExpression, {
      callee: {
        type: 'MemberExpression',
        object: {
          name: 'Component'
        },
        property: {
          name: 'extend'
        }
      }
    })
    .forEach(path => {
      const props = {
        'view': {
          name: 'view',
          type: 'get',
          static: true,
          path: null
        },
        'ViewModel': {
          name: 'define',
          type: 'get',
          static: true,
          path: null
        },
        'events': {
          name: 'connected',
          type: 'method',
          static: false,
          path: null
        }
      };
      const methods = [];
      let tagName;
      let varDeclaration;

      // Loop over the properties to grab the ones we want to copy over
      path.value.arguments[0].properties
        .forEach(path => {
          if (props[path.key.name]) {
            props[path.key.name].path = path;
          } else if (path.key.name === 'tag') {
            tagName = path.value.value;
          }
        });

      // Replace variable declarations with class def
      if (path.parentPath && path.parentPath.value && path.parentPath.value.type === 'VariableDeclarator') {
        varDeclaration = path.parentPath.value.id.name;
        path = path.parentPath.parentPath.parentPath;
      }
      const className = string.pascalize(varDeclaration ||tagName);

      debug(`Replacing ${className} with StacheDefineElement class`);

      j(path).replaceWith(
        j.classDeclaration(
          j.identifier(className),
          j.classBody([]),
          j.identifier('StacheDefineElement')
        )
      );

      Object.keys(props).forEach(key => {
        const prop = props[key];

        if (prop.path !== null) {
          const propPath = prop.path;
          let blockStatement;

          if (prop.type === 'get') {
            blockStatement = [
              j.returnStatement(propPath.value)
            ];
          } else if (prop.type === 'method') {
            blockStatement = [
              j.variableDeclaration('const', [
                j.variableDeclarator(j.identifier(propPath.key.name), propPath.value)
              ])
            ];<commit_msg>Remove empty comments and add className by config<commit_after>import string from 'can-string';
import makeDebug from 'debug';
import getConfig from '../../../utils/getConfig';

export default function transformer(file, api, options) {
  const debug = makeDebug(`can-migrate:can-stache-define-element:${file.path}`);
  const j = api.jscodeshift;
  const config = getConfig(options.config);
  const extendedClassName = config.moduleToName['can-stache-define-element'];

  return j(file.source)
    .find(j.CallExpression, {
      callee: {
        type: 'MemberExpression',
        object: {
          name: 'Component'
        },
        property: {
          name: 'extend'
        }
      }
    })
    .forEach(path => {
      const props = {
        'view': {
          name: 'view',
          type: 'get',
          static: true,
          path: null
        },
        'ViewModel': {
          name: 'define',
          type: 'get',
          static: true,
          path: null
        },
        'events': {
          name: 'connected',
          type: 'method',
          static: false,
          path: null
        }
      };
      const methods = [];
      let tagName;
      let varDeclaration;

      // Loop over the properties to grab the ones we want to copy over
      path.value.arguments[0].properties
        .forEach(path => {
          if (props[path.key.name]) {
            props[path.key.name].path = path;
          } else if (path.key.name === 'tag') {
            tagName = path.value.value;
          }
        });

      // Replace variable declarations with class def
      if (path.parentPath && path.parentPath.value && path.parentPath.value.type === 'VariableDeclarator') {
        varDeclaration = path.parentPath.value.id.name;
        path = path.parentPath.parentPath.parentPath;
      }
      const className = string.pascalize(varDeclaration ||tagName);

      debug(`Replacing ${className} with ${extendedClassName} class`);

      j(path).replaceWith(
        j.classDeclaration(
          j.identifier(className),
          j.classBody([]),
          j.identifier(extendedClassName)
        )
      );

      Object.keys(props).forEach(key => {
        const prop = props[key];

        if (prop.path !== null) {
          const propPath = prop.path;
          let blockStatement;

          if (prop.type === 'get') {
            blockStatement = [
              j.returnStatement(propPath.value)
            ];
          } else if (prop.type === 'method') {
            blockStatement = [
              j.variableDeclaration('const', [
                j.variableDeclarator(j.identifier(propPath.key.name), propPath.value)
              ])
            ];
{'fraction_non_alphanumeric': 0.12834842936982077, 'fraction_numerical': 0.0003854307188282906, 'mean_word_length': 1.7445795875198309, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '982622', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1420, 'n_words': 311}